Sha256: 841f549bf6b5baf465e4767c2c03e5bfdc8282b44e7341be7f150f92a593ec49

Contents?: true

Size: 1.58 KB

Versions: 42

Compression:

Stored size: 1.58 KB

Contents

class User < ActiveRecord::Base # :nodoc:
  require 'dynamic_methods'
  include DynamicMethods
  
  attr_accessible :first_name, :last_name
  attr_reader :password # :nodoc:
  
  has_and_belongs_to_many :groups, :class_name => 'UserGroup', :join_table => 'user_group_memberships'
  
  validates_presence_of [ :username, :password, :first_name, :last_name ], :message => 'is required'
  validates_length_of :password, :minimum => 4
  validates_uniqueness_of :username, :message => 'already in use'
  validates_confirmation_of :password
  
  before_validation :fake_password_confirmation, :on => :update
  
  def name ; [ first_name, last_name ].compact.join(' ') ; end
  
  SaltLength = 16 unless defined?(SaltLength) # :nodoc:
  
  def password=(val) # :nodoc:
    @password = val
    self.password_hash = User.hash_password(val) if (val ||= "") != ""
  end
  
  def self.hash_password(val, salt = '') # :nodoc:
    require 'digest/sha1'
    
    # create the salt if we need to
    if salt.length != SaltLength
      salt = ''
      allowed_chars = (('a'..'f').to_a).concat(('0'..'9').to_a)
      SaltLength.times do
        salt << allowed_chars[rand(allowed_chars.length)]
      end
    end
    
    # now, let the hashing begin
    digest = Digest::SHA1.new
    digest << salt << val
    salt << digest.hexdigest
  end
  
  def fake_password_confirmation # :nodoc:
    # if password is blank, user is not trying to change it.
    # just appease the validator by setting something valid
    if ((@password ||= "") == "")
      @password = "imapassword" 
      @password_confirmation = "imapassword" 
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
imagine_cms-3.0.33 app/models/user.rb
imagine_cms-3.0.32 app/models/user.rb
imagine_cms-3.0.31 app/models/user.rb
imagine_cms-3.0.30 app/models/user.rb
imagine_cms-3.0.29 app/models/user.rb
imagine_cms-3.0.28 app/models/user.rb
imagine_cms-3.0.27 app/models/user.rb
imagine_cms-3.0.26 app/models/user.rb
imagine_cms-3.0.25 app/models/user.rb
imagine_cms-3.0.24 app/models/user.rb
imagine_cms-3.0.23.1 app/models/user.rb
imagine_cms-3.0.23 app/models/user.rb
imagine_cms-3.0.22 app/models/user.rb
imagine_cms-3.0.21 app/models/user.rb
imagine_cms-3.0.20 app/models/user.rb
imagine_cms-3.0.19 app/models/user.rb
imagine_cms-3.0.18 app/models/user.rb
imagine_cms-3.0.17 app/models/user.rb
imagine_cms-3.0.16 app/models/user.rb
imagine_cms-3.0.15 app/models/user.rb