Class: StaticAuth::Session
- Inherits:
-
Object
- Object
- StaticAuth::Session
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion
- Defined in:
- lib/static_auth/session.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#role ⇒ Object
Returns the value of attribute role.
-
#session ⇒ Object
Returns the value of attribute session.
Class Method Summary collapse
- .encrypt(value) ⇒ Object
- .password_for(role, password) ⇒ Object
- .roles(*args) ⇒ Object
- .set_encryption_method(method) ⇒ Object
Instance Method Summary collapse
- #attributes=(attrs) ⇒ Object
- #authorized?(role = nil) ⇒ Boolean
-
#initialize(session) ⇒ Session
constructor
A new instance of Session.
- #logout(role) ⇒ Object
- #logout_all ⇒ Object
- #persisted? ⇒ Boolean
- #save ⇒ Object
Constructor Details
#initialize(session) ⇒ Session
Returns a new instance of Session.
48 49 50 |
# File 'lib/static_auth/session.rb', line 48 def initialize(session) self.session = session end |
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
25 26 27 |
# File 'lib/static_auth/session.rb', line 25 def password @password end |
#role ⇒ Object
Returns the value of attribute role.
25 26 27 |
# File 'lib/static_auth/session.rb', line 25 def role @role end |
#session ⇒ Object
Returns the value of attribute session.
47 48 49 |
# File 'lib/static_auth/session.rb', line 47 def session @session end |
Class Method Details
.encrypt(value) ⇒ Object
37 38 39 |
# File 'lib/static_auth/session.rb', line 37 def encrypt(value) encryption_methods[encryption_method].call(value.to_s) end |
.password_for(role, password) ⇒ Object
32 33 34 35 |
# File 'lib/static_auth/session.rb', line 32 def password_for(role, password) check_role(role) self.defined_passwords[role] = password end |
.roles(*args) ⇒ Object
28 29 30 |
# File 'lib/static_auth/session.rb', line 28 def roles(*args) self.defined_roles = self.defined_roles.concat(args).uniq end |
.set_encryption_method(method) ⇒ Object
41 42 43 44 |
# File 'lib/static_auth/session.rb', line 41 def set_encryption_method(method) raise ArgumentError, "Unknown encryption method #{method.to_s} (#{self.encryption_methods.keys.join(', ')}). You can add method through class inheritable accessor #encryption_methods." unless self.encryption_methods.keys.include?(method) self.encryption_method = method end |
Instance Method Details
#attributes=(attrs) ⇒ Object
78 79 80 |
# File 'lib/static_auth/session.rb', line 78 def attributes=(attrs) attrs.each { |key, value| send(:"#{key}=", value) } end |
#authorized?(role = nil) ⇒ Boolean
61 62 63 64 65 66 67 |
# File 'lib/static_auth/session.rb', line 61 def (role = nil) unless role.nil? self.session[session_key_for(role)] == password_for(role) else self.class.roles.any? { |r| self.session[session_key_for(r)] == password_for(r) } end end |
#logout(role) ⇒ Object
69 70 71 72 |
# File 'lib/static_auth/session.rb', line 69 def logout(role) check_role(role) self.session[session_key_for(role)] = nil end |
#logout_all ⇒ Object
74 75 76 |
# File 'lib/static_auth/session.rb', line 74 def logout_all self.class.defined_roles.each { |r| logout(r) } end |
#persisted? ⇒ Boolean
16 |
# File 'lib/static_auth/session.rb', line 16 def persisted?; false; end |
#save ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/static_auth/session.rb', line 52 def save if !self.role.blank? && self.class.defined_roles.include?(self.role.to_sym) self.session[session_key_for(role)] = self.class.encrypt(self.password) true else false end end |