/
terrys_user_functions.rb
131 lines (114 loc) · 2.74 KB
/
terrys_user_functions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module Terrys_user_functions
attr_accessor :new_password
attr_accessor :confirm_new_password
def full_name
firstname+' '+lastname
end
def is_admin?
if is_terry?
return true
end
unless roles.empty?
unless roles.select{|r| r.admin or r.god}.empty?
return true
end
end
false
end
def is_authorized?(controller,action)
right=Right.find_by_controller_and_action(controller,action)
if right and right.fundamental
return true
end
#result=self.roles.detect{|role| role.rights.detect{|right| (right.action == action) && (right.controller == controller)}}
if rights.include?(right)
result=true
else
result=nil
end
if not result and is_admin?
unless right
right=Right.new(:controller=>controller, :action=>action)
right.save
end
self.rights << right
save
result=true
end
result
end
def is_god?
if is_terry?
return true
end
unless roles.empty?
unless roles.select{|r| r.god}.empty?
return true
end
end
false
end
def is_terry?
if email and ['itsterry@gmail.com','terry@shuttleworths.net','terry@tttinternational.com'].include?(email)
true
else
false
end
end
def login!
self.last_login=current_login||Time.now
self.current_login=Time.now
end
def password=(pass=nil)
set_password(pass)
end
def refresh_password(size = 8)
chars = (('a'..'z').collect{|n| n} + ('0'..'9').collect{|n| n}) - %w(i o 0 1 l 0)
password=(1..size).collect{|a| chars[rand(chars.size)] }.join
set_password!(password)
return password
end
def rights
if roles.empty?
[]
else
roles.collect{|r| r.rights}.flatten.uniq
end
end
def set_password(pass=nil)
if pass.nil? or pass.blank? or pass.length<1
return nil
end
pass=pass.downcase
salt = [Array.new(6){rand(256).chr}.join].pack("m" ).chomp
self.password_salt, self.password_hash =
salt, Digest::SHA256.hexdigest(pass + salt)
return true
end
def set_password!(pass=nil)
set_password(pass)
#self.last_password_reset=Time.now
save
end
def title
firstname+' '+lastname
end
def validate_new_password
unless new_password.blank? and confirm_new_password.blank?
if new_password==confirm_new_password
self.password=new_password
else
errors.add(:new_password,'password does not match confirmation')
errors.add(:confirm_new_password,'password does not match confirmation')
return false
end
end
end
def viewable_roles
if roles.empty?
[]
else
roles.collect{|ro| ro.self_and_descendants}.flatten.uniq.sort_by{|ro| ro.title}
end
end
end