Sha256: 72851e87fa2a26d210cd66132417f166d9e03e8e839142151efafff825a264a4

Contents?: true

Size: 1.24 KB

Versions: 13

Compression:

Stored size: 1.24 KB

Contents

class Caboose::User < ActiveRecord::Base
  self.table_name = "users"  
  #has_and_belongs_to_many :roles
  has_many :role_memberships
  has_many :roles, :through => :role_memberships  
  attr_accessible :email, :first_name, :last_name, :username, :token, :password

  before_save do
    self.email = self.email.downcase if self.email
  end
  
  def self.logged_out_user
    return self.where('username' => 'elo').first
  end
  
  def self.logged_out_user_id
    return self.where('username' => 'elo').limit(1).pluck(:id)[0]
  end
  
  def is_allowed(resource, action)
    for role in roles
      if role.is_allowed(resource, action)
        return true
      end
    end
    return false;
  end
  
  def self.validate_token(token)
    user = self.where('token' => token).first
    return user 
  end
  
  def add_to_role_with_name(role_name)
    r = Caboose::Role.where(:name => role_name).first
    return false if r.nil?
    return add_to_role(r.id)
  end
  
  def add_to_role(role_id)
    r = Caboose::Role.find(role_id)
    return false if r.nil?
    
    if (!is_member?(r.id))
      roles.push r
      save
    end
    return true
  end
  
  def is_member?(role_id)
    roles.each do |r|
      return true if (r.id == role_id)
    end
    return false
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
caboose-cms-0.2.104 app/models/caboose/user.rb
caboose-cms-0.2.103 app/models/caboose/user.rb
caboose-cms-0.2.102 app/models/caboose/user.rb
caboose-cms-0.2.100 app/models/caboose/user.rb
caboose-cms-0.2.99 app/models/caboose/user.rb
caboose-cms-0.2.98 app/models/caboose/user.rb
caboose-cms-0.2.97 app/models/caboose/user.rb
caboose-cms-0.2.96 app/models/caboose/user.rb
caboose-cms-0.2.95 app/models/caboose/user.rb
caboose-cms-0.2.94 app/models/caboose/user.rb
caboose-cms-0.2.93 app/models/caboose/user.rb
caboose-cms-0.2.92 app/models/caboose/user.rb
caboose-cms-0.2.91 app/models/caboose/user.rb