added user test, basic user methods
This commit is contained in:
47
app/models/user.rb
Normal file
47
app/models/user.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require 'digest/sha1'
|
||||
|
||||
class User < ActiveRecord::Base
|
||||
attr_accessor :password
|
||||
|
||||
validates_length_of :name, :within => 2..20, :on => :create
|
||||
validates_format_of :name, :with => /\A[^\s;,]+\Z/, :on => :create, :message => "cannot have whitespace, commas, or semicolons"
|
||||
validates_uniqueness_of :name, :case_sensitive => false, :on => :create
|
||||
validates_uniqueness_of :email, :case_sensitive => false, :on => :create, :if => lambda {|rec| !rec.email.blank?}
|
||||
validates_confirmation_of :password
|
||||
|
||||
before_save :encrypt_password
|
||||
after_save {|rec| Cache.put("user_name:#{rec.id}", rec.name)}
|
||||
|
||||
scope :named, lambda {|name| where(["lower(name) = ?", name])}
|
||||
|
||||
def self.authenticate(name, pass)
|
||||
authenticate_hash(name, sha1(pass))
|
||||
end
|
||||
|
||||
def self.authenticate_hash(name, pass)
|
||||
where(["lower(name) = ? AND password_hash = ?", name.downcase, pass]).first != nil
|
||||
end
|
||||
|
||||
def self.sha1(pass)
|
||||
Digest::SHA1.hexdigest("#{Danbooru.config.password_salt}--#{pass}--")
|
||||
end
|
||||
|
||||
def self.find_name(user_id)
|
||||
Cache.get("user_name:#{user_id}") do
|
||||
select_value_sql("SELECT name FROM users WHERE id = ?", user_id) || Danbooru.config.default_guest_name
|
||||
end
|
||||
end
|
||||
|
||||
def can_update?(object, foreign_key = :user_id)
|
||||
is_moderator? || is_admin? || object.__send__(foreign_key) == id
|
||||
end
|
||||
|
||||
def pretty_name
|
||||
name.tr("_", " ")
|
||||
end
|
||||
|
||||
def encrypt_password
|
||||
self.password_hash = self.class.sha1(password) if password
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user