Class: Caboose::User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::User
- Defined in:
- app/models/caboose/user.rb
Constant Summary collapse
- ADMIN_USER_ID =
1
- LOGGED_OUT_USER_ID =
2
Class Method Summary collapse
- .logged_out_user(site_id) ⇒ Object
- .logged_out_user_id(site_id) ⇒ Object
- .user_for_reset_id(reset_id) ⇒ Object
- .validate_token(token) ⇒ Object
Instance Method Summary collapse
- #add_to_role(role_id) ⇒ Object
- #add_to_role_with_name(role_name) ⇒ Object
- #is_allowed(resource, action) ⇒ Object
- #is_logged_out_user? ⇒ Boolean
- #is_member?(role_id) ⇒ Boolean
Class Method Details
.logged_out_user(site_id) ⇒ Object
27 28 29 30 |
# File 'app/models/caboose/user.rb', line 27 def self.logged_out_user(site_id) return self.where(:site_id => site_id, :username => 'elo').first #return self.where(:id => self::LOGGED_OUT_USER_ID).first end |
.logged_out_user_id(site_id) ⇒ Object
32 33 34 35 |
# File 'app/models/caboose/user.rb', line 32 def self.logged_out_user_id(site_id) return self.where(:site_id => site_id, :username => 'elo').limit(1).pluck(:id)[0] #return self::LOGGED_OUT_USER_ID end |
.user_for_reset_id(reset_id) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'app/models/caboose/user.rb', line 90 def self.user_for_reset_id(reset_id) return nil if reset_id.nil? d = DateTime.now - 3.days if self.where("password_reset_id = ? and password_reset_sent > ?", reset_id, d).exists? return self.where("password_reset_id = ? and password_reset_sent > ?", reset_id, d).first end return nil end |
.validate_token(token) ⇒ Object
61 62 63 64 |
# File 'app/models/caboose/user.rb', line 61 def self.validate_token(token) user = self.where('token' => token).first return user end |
Instance Method Details
#add_to_role(role_id) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/caboose/user.rb', line 72 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 |
#add_to_role_with_name(role_name) ⇒ Object
66 67 68 69 70 |
# File 'app/models/caboose/user.rb', line 66 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 |
#is_allowed(resource, action) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/caboose/user.rb', line 41 def is_allowed(resource, action) elo = Caboose::Role.logged_out_role(self.site_id) elo_is_allowed = elo.is_allowed(resource, action) return true if elo_is_allowed return false if !elo_is_allowed && self.is_logged_out_user? eli = Caboose::Role.logged_in_role(self.site_id) return true if self.id != elo.id && eli.is_allowed(resource, action) for role in roles #Caboose.log("Checking permissions for #{role.name} role") if role.is_allowed(resource, action) #Caboose.log("Role #{role.name} is allowed to view page") return true else #Caboose.log("Role #{role.name} is not allowed to view page") end #return true if role.is_allowed(resource, action) end return false; end |
#is_logged_out_user? ⇒ Boolean
37 38 39 |
# File 'app/models/caboose/user.rb', line 37 def is_logged_out_user? return self.id == Caboose::User.logged_out_user_id(self.site_id) end |
#is_member?(role_id) ⇒ Boolean
83 84 85 86 87 88 |
# File 'app/models/caboose/user.rb', line 83 def is_member?(role_id) roles.each do |r| return true if (r.id == role_id) end return false end |