Show More
Commit Description:
added problem_name to TestRequest and moved name_of to protected...
Commit Description:
added problem_name to TestRequest and moved name_of to protected
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@88 6386c4cd-e34a-4fa8-8920-d93eb39b512e
References:
File last commit:
Show/Diff file:
Action:
app/models/user.rb
| 76 lines
| 1.5 KiB
| text/x-ruby
| RubyLexer
|
|
r0 | require 'digest/sha1' | ||
class User < ActiveRecord::Base | ||||
has_and_belongs_to_many :roles | ||||
|
r36 | has_many :test_requests, :order => "problem_id" | ||
|
r0 | validates_presence_of :login | ||
validates_presence_of :full_name | ||||
|
r23 | validates_length_of :full_name, :minimum => 1 | ||
|
r0 | |||
validates_presence_of :password, :if => :password_required? | ||||
validates_length_of :password, :within => 4..20, :if => :password_required? | ||||
validates_confirmation_of :password, :if => :password_required? | ||||
attr_accessor :password | ||||
before_save :encrypt_new_password | ||||
def self.authenticate(login, password) | ||||
user = find_by_login(login) | ||||
return user if user && user.authenticated?(password) | ||||
end | ||||
def authenticated?(password) | ||||
hashed_password == encrypt(password,salt) | ||||
end | ||||
def admin? | ||||
self.roles.detect {|r| r.name == 'admin' } | ||||
end | ||||
|
r18 | def email_for_editing | ||
|
r23 | if self.email==nil | ||
"(unknown)" | ||||
elsif self.email=='' | ||||
"(blank)" | ||||
else | ||||
|
r18 | self.email | ||
end | ||||
end | ||||
def email_for_editing=(e) | ||||
self.email=e | ||||
end | ||||
def alias_for_editing | ||||
|
r23 | if self.alias==nil | ||
"(unknown)" | ||||
elsif self.alias=='' | ||||
"(blank)" | ||||
else | ||||
|
r18 | self.alias | ||
end | ||||
end | ||||
def alias_for_editing=(e) | ||||
self.alias=e | ||||
end | ||||
protected | ||||
|
r0 | def encrypt_new_password | ||
return if password.blank? | ||||
self.salt = (10+rand(90)).to_s | ||||
self.hashed_password = encrypt(password,salt) | ||||
end | ||||
def password_required? | ||||
hashed_password.blank? || !password.blank? | ||||
end | ||||
def encrypt(string,salt) | ||||
Digest::SHA1.hexdigest(salt + string) | ||||
end | ||||
end | ||||