Skip to content

Commit 110f5fd

Browse files
authored
Merge pull request #124 from javierav/sudoable
Remove dependency on redis / kredis for sudoable
2 parents c934cfc + cf425db commit 110f5fd

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## New version
22

3+
* Remove dependency on redis / kredis for sudoable
4+
35
## Authentication Zero 4.0.1 ##
46

57
* Remove rate limit from api generator

lib/generators/authentication/authentication_generator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def add_gems
2828

2929
if redis?
3030
gem "redis", "~> 4.0", comment: "Use Redis adapter to run additional authentication features"
31-
gem "kredis", comment: "Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]"
3231
end
3332

3433
if options.pwned?
@@ -259,7 +258,7 @@ def sudoable?
259258
end
260259

261260
def redis?
262-
options.ratelimit? || sudoable?
261+
options.ratelimit?
263262
end
264263

265264
def importmaps?

lib/generators/authentication/templates/controllers/html/sessions/sudos_controller.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Sessions::SudosController < ApplicationController
66
session_record = Current.session
77

88
if session_record.user.authenticate(params[:password])
9-
session_record.sudo.mark; redirect_to(params[:proceed_to_url])
9+
session_record.touch(:sudo_at); redirect_to(params[:proceed_to_url])
1010
else
1111
redirect_to new_sessions_sudo_path(proceed_to_url: params[:proceed_to_url]), alert: "The password you entered is incorrect"
1212
end

lib/generators/authentication/templates/migrations/create_sessions_migration.rb.tt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Mi
44
t.references :user, null: false, foreign_key: true
55
t.string :user_agent
66
t.string :ip_address
7+
<%- if sudoable? %>
8+
t.datetime :sudo_at, null: false
9+
<%- end -%>
710

811
t.timestamps
912
end
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
class Session < ApplicationRecord
22
belongs_to :user
3-
<%- if sudoable? %>
4-
kredis_flag :sudo, expires_in: 30.minutes
5-
<%- end -%>
63

74
before_create do
85
self.user_agent = Current.user_agent
96
self.ip_address = Current.ip_address
7+
<%- if sudoable? %>
8+
self.sudo_at = Time.current
9+
<%- end -%>
1010
end
11-
<%- if sudoable? %>
12-
after_create { sudo.mark }
13-
<%- end -%>
1411
<%- if options.trackable? %>
1512
after_create { user.events.create! action: "signed_in" }
1613
after_destroy { user.events.create! action: "signed_out" }
1714
<%- end -%>
15+
<%- if sudoable? %>
16+
17+
def sudo?
18+
sudo_at > 30.minutes.ago
19+
end
20+
<%- end -%>
1821
end

0 commit comments

Comments
 (0)