~matteo-collina/+junk/gipieffe

« back to all changes in this revision

Viewing changes to app/models/associate.rb

  • Committer: Matteo Collina
  • Date: 2008-11-02 09:46:31 UTC
  • Revision ID: matteo.collina@gmail.com-20081102094631-h9w18le32hew28xp
 * Generated login system by the restful-authentication-i18n plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'digest/sha1'
 
2
 
 
3
class Associate < ActiveRecord::Base
 
4
  include Authentication
 
5
  include Authentication::ByPassword
 
6
  include Authentication::ByCookieToken
 
7
 
 
8
  validates_presence_of     :login
 
9
  validates_length_of       :login,    :within => 3..40
 
10
  validates_uniqueness_of   :login
 
11
  validates_format_of       :login,    :with => Authentication.login_regex, :message => Authentication.bad_login_message
 
12
 
 
13
  validates_format_of       :name,     :with => Authentication.name_regex,  :message => Authentication.bad_name_message, :allow_nil => true
 
14
  validates_length_of       :name,     :maximum => 100
 
15
 
 
16
  validates_presence_of     :email
 
17
  validates_length_of       :email,    :within => 6..100 #r@a.wk
 
18
  validates_uniqueness_of   :email
 
19
  validates_format_of       :email,    :with => Authentication.email_regex, :message => Authentication.bad_email_message
 
20
 
 
21
  
 
22
 
 
23
  # HACK HACK HACK -- how to do attr_accessible from here?
 
24
  # prevents a user from submitting a crafted form that bypasses activation
 
25
  # anything else you want your user to change should be added here.
 
26
  attr_accessible :login, :email, :name, :password, :password_confirmation
 
27
 
 
28
 
 
29
 
 
30
  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
 
31
  #
 
32
  # uff.  this is really an authorization, not authentication routine.  
 
33
  # We really need a Dispatch Chain here or something.
 
34
  # This will also let us return a human error message.
 
35
  #
 
36
  def self.authenticate(login, password)
 
37
    return nil if login.blank? || password.blank?
 
38
    u = find_by_login(login) # need to get the salt
 
39
    u && u.authenticated?(password) ? u : nil
 
40
  end
 
41
 
 
42
  def login=(value)
 
43
    write_attribute :login, (value ? value.downcase : nil)
 
44
  end
 
45
 
 
46
  def email=(value)
 
47
    write_attribute :email, (value ? value.downcase : nil)
 
48
  end
 
49
 
 
50
  protected
 
51
    
 
52
 
 
53
 
 
54
end