| Class | User |
| In: |
app/models/user.rb
|
| Parent: | ActiveRecord::Base |
ユーザーのモデル this model expects a certain database layout and its based on the name/login pattern.
| PasswordDigestClass | = | Digest::SHA512 |
| CHANGEABLE_FIELDS | = | [] |
| mail_address | -> |
| password | [RW] | |
| password_confirmation | [RW] | |
| password_needs_confirmation | [RW] |
現在のユーザーがシステム管理者かどうかを判定する。
# File app/models/user.rb, line 199
199: def self.admin?
200: if current
201: return current.admin?
202: else
203: return false
204: end
205: end
login と pass による認証を行う。 成功した場合は nil でない値、失敗した場合は nil を返す。
# File app/models/user.rb, line 85
85: def self.authenticate(login, pass, update_failed_login_count=false)
86: User.transaction do
87: u = find( :first, :conditions => ["login = ? AND verified = ? AND deleted = ?", login, true, false])
88: return nil if u.nil?
89: return nil if u.lockout?
90: ret = find( :first, :conditions => ["login = ? AND salted_password = ? AND verified = ?", login, salted_password(u.salt, hashed(pass)), true])
91: if update_failed_login_count
92: if ret
93: if u.failed_login_count != 0
94: u.failed_login_count = 0
95: u.save!
96: end
97: else
98: u.failed_login_count += 1
99: u.save!
100: end
101: end
102: return ret
103: end
104: end
id と token による認証を行う。 成功した場合は nil でない値、失敗した場合は nil を返す。
# File app/models/user.rb, line 108
108: def self.authenticate_by_token(id, token)
109: # Allow logins for deleted accounts, but only via this method (and
110: # not the regular authenticate call)
111: logger.info "Attempting authorization of #{id} with #{token}"
112: u = find( :first, :conditions => ["id = ? AND security_token = ?", id, token])
113: if u
114: logger.info "Authenticated by token: #{u.inspect}"
115: else
116: logger.info "Not authenticated" if u.nil?
117: end
118: return nil if (u.nil? or u.token_expired?)
119: u.update_attributes :verified => true, :token_expiry => Clock.now
120: return u
121: end
現在のユーザーを返す。
# File app/models/user.rb, line 180
180: def self.current
181: CacheEachRequest.current[:user]
182: end
現在のユーザーを代入する。
# File app/models/user.rb, line 185
185: def self.current=(u)
186: CacheEachRequest.current[:user] = u
187: end
現在のユーザーIDを返す。
# File app/models/user.rb, line 190
190: def self.current_id
191: if current
192: return current.id
193: else
194: return nil
195: end
196: end
現在のページごとの個数の既定値を返す。
# File app/models/user.rb, line 208
208: def self.list_default_per_page
209: return current.list_default_per_page
210: rescue
211: return columns_hash["list_default_per_page"].default
212: end
現在の行ごとの個数の既定値を返す。
# File app/models/user.rb, line 215
215: def self.list_header_per_line
216: return current.list_header_per_line
217: rescue
218: return columns_hash["list_header_per_line"].default
219: end
現在のメニュー履歴の最大値を返す。
# File app/models/user.rb, line 222
222: def self.menu_history_max
223: return current.menu_history_max
224: rescue
225: return columns_hash["menu_history_max"].default
226: end
# File app/models/user.rb, line 73
73: def initialize(attributes = nil)
74: super
75: @password_needs_confirmation = false
76: end
email からユーザーを探す。
# File app/models/user.rb, line 279
279: def self.find_by_email(email)
280: person = Person.find_by_mail_address(email, :include => [:user])
281: if person
282: return person.user
283: else
284: return nil
285: end
286: end
str をハッシュ化する。
# File app/models/user.rb, line 251
251: def self.hashed(str)
252: return PasswordDigestClass.hexdigest("#{UserSystem::CONFIG[:password_hash_prefix]}--#{str}--")[0,128]
253: end
salt と hashed_password をハッシュ化する。
# File app/models/user.rb, line 274
274: def self.salted_password(salt, hashed_password)
275: hashed(salt + hashed_password)
276: end
パスワードを pass に変更する。
# File app/models/user.rb, line 139
139: def change_password(pass, confirm = nil)
140: self.password = pass
141: self.password_confirmation = confirm.nil? ? pass : confirm
142: @password_needs_confirmation = true
143: end
token を生成する。
# File app/models/user.rb, line 129
129: def generate_security_token
130: if self.security_token.nil? or self.token_expiry.nil? or (Clock.now.to_i + token_lifetime / 2) >= self.token_expiry.to_i
131: token = new_security_token
132: return token
133: else
134: return self.security_token
135: end
136: end
ユーザの言語のコード。
# File app/models/user.rb, line 229
229: def lang
230: code = GetText.locale.to_s[0,2]
231: code ||= person.last_language
232: code ||= Language.default_code
233: return code
234: end
ログイン資格が失効していれば true、さもなくば false を返す。
# File app/models/user.rb, line 174
174: def lockout?
175: return false unless config_password.enable_lockout?
176: return failed_login_count >= config_password.lockout_threshold
177: end
パスワードの有効期限が切れていれば true、さもなくば false を返す。
# File app/models/user.rb, line 163
163: def password_expire?
164: unless config_password
165: raise ArgumentError, s_("ArgumentError|invalid domain")
166: end
167: if config_password.enable_max_age?
168: return Date.today >= password_updated_on + config_password.max_age
169: end
170: return false
171: end
パスワードが更新された時刻を返す。
# File app/models/user.rb, line 151
151: def password_updated_on
152: str = read_attribute("password_updated_on")
153: if /\A(\d{4})(\d\d)(\d\d)\z/ =~ str
154: return Date.new(*$~.captures.map(&:to_i))
155: elsif str.nil?
156: return nil
157: else
158: raise ArgumentError, "invalid password_updated_on #{str.inspect}"
159: end
160: end
token が期限切れかどうかを判定する。
# File app/models/user.rb, line 124
124: def token_expired?
125: self.security_token and self.token_expiry and (Clock.now >= self.token_expiry)
126: end
token の生存時間を秒単位で返す。
# File app/models/user.rb, line 146
146: def token_lifetime
147: UserSystem::CONFIG[:security_token_life_hours] * 60 * 60
148: end
ユーザーの言語の翻訳のクラス。
# File app/models/user.rb, line 237
237: def translation_class
238: "PoTranslation#{lang.camelize}".constantize
239: end
パスワードを暗号化する。
# File app/models/user.rb, line 256
256: def crypt_password
257: if @password_needs_confirmation
258: write_attribute("salt", self.class.hashed("salt-#{Clock.now}.#{Clock.now.usec}"))
259: write_attribute("salted_password", self.class.salted_password(salt, self.class.hashed(@password)))
260: write_attribute("password_updated_on", Time.now.utc.strftime("%Y%m%d"))
261: end
262: end
新しい token を返す。
# File app/models/user.rb, line 265
265: def new_security_token
266: expiry = Time.at(Clock.now.to_i + token_lifetime)
267: write_attribute('security_token', self.class.hashed(self.salted_password + Clock.now.to_i.to_s + rand.to_s))
268: write_attribute('token_expiry', expiry)
269: update_without_callbacks
270: return self.security_token
271: end