Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 2625 Bytes
1 | require 'digest/sha1' |
2 | |
3 | class User < ActiveRecord::Base |
4 | |
5 | # Default Order |
6 | default_scope :order => 'name' |
7 | |
8 | # Associations |
9 | belongs_to :created_by, :class_name => 'User' |
10 | belongs_to :updated_by, :class_name => 'User' |
11 | |
12 | # Validations |
13 | validates_uniqueness_of :login |
14 | |
15 | validates_confirmation_of :password, :if => :confirm_password? |
16 | |
17 | validates_presence_of :name, :login |
18 | validates_presence_of :password, :password_confirmation, :if => :new_record? |
19 | |
20 | validates_format_of :email, :allow_nil => true, :with => /^$|^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i |
21 | |
22 | validates_length_of :name, :maximum => 100, :allow_nil => true |
23 | validates_length_of :login, :within => 3..40, :allow_nil => true |
24 | validates_length_of :password, :within => 5..40, :allow_nil => true, :if => :validate_length_of_password? |
25 | validates_length_of :email, :maximum => 255, :allow_nil => true |
26 | |
27 | validates_numericality_of :id, :only_integer => true, :allow_nil => true |
28 | |
29 | attr_writer :confirm_password |
30 | class << self |
31 | def unprotected_attributes |
32 | @unprotected_attributes ||= [:name, :email, :login, :password, :password_confirmation, :locale] |
33 | end |
34 | |
35 | def unprotected_attributes=(array) |
36 | @unprotected_attributes = array.map{|att| att.to_sym } |
37 | end |
38 | end |
39 | |
40 | def has_role?(role) |
41 | respond_to?("#{role}?") && send("#{role}?") |
42 | end |
43 | |
44 | def sha1(phrase) |
45 | Digest::SHA1.hexdigest("--#{salt}--#{phrase}--") |
46 | end |
47 | |
48 | def self.authenticate(login_or_email, password) |
49 | user = find(:first, :conditions => ["login = ? OR email = ?", login_or_email, login_or_email]) |
50 | user if user && user.authenticated?(password) |
51 | end |
52 | |
53 | def authenticated?(password) |
54 | self.password == sha1(password) |
55 | end |
56 | |
57 | def after_initialize |
58 | @confirm_password = true |
59 | end |
60 | |
61 | def confirm_password? |
62 | @confirm_password |
63 | end |
64 | |
65 | def remember_me |
66 | update_attribute(:session_token, sha1(Time.now + Radiant::Config['session_timeout'].to_i)) unless self.session_token? |
67 | end |
68 | |
69 | def forget_me |
70 | update_attribute(:session_token, nil) |
71 | end |
72 | |
73 | private |
74 | |
75 | def validate_length_of_password? |
76 | new_record? or not password.to_s.empty? |
77 | end |
78 | |
79 | before_create :encrypt_password |
80 | def encrypt_password |
81 | self.salt = Digest::SHA1.hexdigest("--#{Time.now}--#{login}--sweet harmonious biscuits--") |
82 | self.password = sha1(password) |
83 | end |
84 | |
85 | before_update :encrypt_password_unless_empty_or_unchanged |
86 | def encrypt_password_unless_empty_or_unchanged |
87 | user = self.class.find(self.id) |
88 | case password |
89 | when '' |
90 | self.password = user.password |
91 | when user.password |
92 | else |
93 | encrypt_password |
94 | end |
95 | end |
96 | |
97 | end |