Class: AuthFx::UserProfile

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/fx-auth/user_profile.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sign_up(email, pass_phrase) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fx-auth/user_profile.rb', line 55

def self. email, pass_phrase
  user = UserProfile.first :email => email
  raise DuplicateUserError if user

  user = UserProfile.new :email => email, :pass_phrase => pass_phrase
  if user.valid?
    user.status = :online
    user.save
  end
  user
end

Instance Method Details

#authenticate?(token) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
# File 'lib/fx-auth/user_profile.rb', line 132

def authenticate? token
  authenticated = (self.status == :online and
      self.pass_key and
      self.pass_key.authenticate? token
  )
  self.pass_key.reset_timer if authenticated
  authenticated
end

#authorized?(*roles) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/fx-auth/user_profile.rb', line 142

def authorized? *roles
  roles.any? { |role| self.in_role? role }
end

#in_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/fx-auth/user_profile.rb', line 147

def in_role? role
  found = self.roles.first :name => role
  !found.nil?
end

#lock_expired?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/fx-auth/user_profile.rb', line 160

def lock_expired?
  Time.now > self.locked_until
end

#sign_offObject



97
98
99
100
# File 'lib/fx-auth/user_profile.rb', line 97

def sign_off
  self.status = :offline
  save
end

#sign_on(email, pass_phrase) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fx-auth/user_profile.rb', line 68

def sign_on email, pass_phrase
  self.status = :offline if self.pass_key and self.pass_key.expired?
  self.status = :offline if self.status == :locked and self.lock_expired?

  if self.status == :online
    self.pass_key

  elsif self.status == :offline
    if self.email == email and self.pass_phrase_crypt == pass_phrase
      self.status = :online
      save
      self.pass_key

    else
      self.sign_on_attempts += 1
      save
      raise InvalidUserError unless self.sign_on_attempts >= 3 # TODO make configurable

      self.status = :locked
      save
      raise LockedUserError.new self.locked_until
    end

  else # :locked
    raise LockedUserError.new self.locked_until
  end
end

#statusObject



123
124
125
126
127
128
129
# File 'lib/fx-auth/user_profile.rb', line 123

def status
  self.status = :offline if super == :online and
      self.pass_key and
      self.pass_key.expires_at and
      Time.now > self.pass_key.expires_at
  super
end

#status=(value) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fx-auth/user_profile.rb', line 103

def status=(value)
  if value == :online
    self.locked_until     = Time.now - 30 * 60 # Unlocked 30 minutes ago - TODO make configurable
    self.sign_on_attempts = 0
    self.pass_key         = PassKey.new

  elsif value == :offline
    self.locked_until     = Time.now - 30 * 60 # Unlocked 30 minutes ago - TODO make configurable
    self.sign_on_attempts = 0
    self.pass_key.destroy if self.pass_key

  elsif value == :locked
    self.locked_until = Time.now + 30 * 60 # Lock for 30 minutes - TODO make configurable
    self.pass_key.destroy if self.pass_key
  end

  super
end

#verify_email?(email, code) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
# File 'lib/fx-auth/user_profile.rb', line 153

def verify_email? email, code
  self.email_verified = (self.email == email and self.email_verification_code == code)
  save
  self.email_verified
end