1
1
class User < ActiveRecord ::Base
2
+ extend Enumerize
3
+
4
+ # Being pesimistic here and making the default waiting for approval for security reasons
5
+ enumerize :status , in : [ :active , :suspended , :waiting_approval ] , :default => :waiting_approval
2
6
3
7
has_many :activities
4
8
before_save :ensure_authentication_token
5
9
before_save :ensure_gravatar_hash
10
+ before_save :mark_status_depending_on_app_settings
6
11
12
+ after_create :ensure_at_least_one_admin
13
+ after_destroy :ensure_at_least_one_admin
14
+
7
15
validates :username , :presence => true , :uniqueness => true
16
+ validates :first_name , :presence => true
17
+ validates :last_name , :presence => true
8
18
9
19
10
20
# Kandan.devise_modules is defined in config/initializers/kandan.rb
@@ -13,17 +23,40 @@ class User < ActiveRecord::Base
13
23
# Setup accessible (or protected) attributes for your model
14
24
attr_accessible :id , :username , :email , :password , :password_confirmation , :remember_me , :first_name , :last_name , :locale , :gravatar_hash
15
25
26
+ def full_name
27
+ "#{ self . first_name . to_s } #{ self . last_name . to_s } " . titlecase
28
+ end
29
+
30
+ def full_name_or_username
31
+ self . full_name . blank? ? self . username : self . full_name
32
+ end
33
+
16
34
def cloudfuji_extra_attributes ( extra_attributes )
17
35
self . first_name = extra_attributes [ "first_name" ] . to_s
18
36
self . last_name = extra_attributes [ "last_name" ] . to_s
19
37
self . email = extra_attributes [ "email" ]
20
38
self . locale = extra_attributes [ "locale" ]
21
39
end
22
40
41
+ # Callback to mark the user status depending on the settings of the app
42
+ def mark_status_depending_on_app_settings
43
+ # If the site is public we will make the user active. Otherwise we will make the user as waiting_approval
44
+ self . status = Setting . my_settings . public_site? ? :active : :waiting_approval
45
+ end
46
+
23
47
def ensure_gravatar_hash
24
48
self . gravatar_hash = Digest ::MD5 . hexdigest self . email
25
49
end
26
50
51
+ # We never want an app without an admin so let's ensure there is at least one user
52
+ def ensure_at_least_one_admin
53
+ if User . count == 1
54
+ u = User . first
55
+ u . is_admin = true
56
+ u . save!
57
+ end
58
+ end
59
+
27
60
def active_for_authentication?
28
61
super && active?
29
62
end
0 commit comments