Class: StaticAuth::Session

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion
Defined in:
lib/static_auth/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#passwordObject

Returns the value of attribute password.



25
26
27
# File 'lib/static_auth/session.rb', line 25

def password
  @password
end

#roleObject

Returns the value of attribute role.



25
26
27
# File 'lib/static_auth/session.rb', line 25

def role
  @role
end

#sessionObject

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

Raises:

  • (ArgumentError)


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

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'lib/static_auth/session.rb', line 61

def authorized?(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_allObject



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

Returns:

  • (Boolean)


16
# File 'lib/static_auth/session.rb', line 16

def persisted?; false; end

#saveObject



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