Sha256: cee2956220daa97dc396afdbd4796d6d61ef793390ea09dd136a9b9e0f8151c8

Contents?: true

Size: 1.56 KB

Versions: 7

Compression:

Stored size: 1.56 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 = self.class.hash_password(val) unless val.blank?
  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.blank?
      @password = "imapassword" 
      @password_confirmation = "imapassword" 
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
imagine_cms-5.2.6 app/models/user.rb
imagine_cms-5.2.5 app/models/user.rb
imagine_cms-5.2.4 app/models/user.rb
imagine_cms-5.2.3 app/models/user.rb
imagine_cms-5.2.2 app/models/user.rb
imagine_cms-5.2.1 app/models/user.rb
imagine_cms-5.2.0 app/models/user.rb