Sha256: e3e6a2c0d327e4ec449bf2db06791a45a83abcfaac17dc36fa0df37ea0b484e7

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

module Optimacms
  class CmsAdminUser < ActiveRecord::Base
    self.table_name = 'cms_users'

    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    #devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
    devise :database_authenticatable, :recoverable, :rememberable, :trackable,
           authentication_keys: [:login]
    #, :validatable

    attr_accessor :login

    # search
    searchable_by_simple_filter

    ### validate
    validates_confirmation_of :password

    validates :password, :presence     => true,
              :confirmation => true,
              :length       => { :minimum => 6 },
              :if           => :should_validate_password? # only validate if password changed!

    validates :username,
              presence: :true,
              uniqueness: { case_sensitive: false }

    # Only allow letter, number, underscore and punctuation.
    #validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, :multiline => true


    def should_validate_password?
      x = new_record? || !password.blank?
      #if: Proc.new{|obj| obj.new_record? || !obj.<attribute>.blank? }
    end

    ### callbacks

    before_destroy :can_destroy?


    def can_destroy?
      !self.is_superadmin
    end


    ## properties

    def is_superadmin?
      self.is_superadmin
    end




    ### for login auth

    def self.find_for_database_authentication(warden_conditions)
      conditions = warden_conditions.dup
      conditions[:email].downcase! if conditions[:email]

      if login = conditions.delete(:login)
        where(conditions.to_hash).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
      elsif conditions.has_key?(:username) || conditions.has_key?(:email)
        where(conditions.to_hash).first
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
optimacms-0.4.3 app/models/optimacms/cms_admin_user.rb
optimacms-0.4.2 app/models/optimacms/cms_admin_user.rb