Class: Caboose::Role
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Role
- Defined in:
- app/models/caboose/role.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
Class Method Summary collapse
- .admin_role(site_id) ⇒ Object
- .admin_role_id(site_id) ⇒ Object
- .flat_tree(site_id, prefix = '-') ⇒ Object
- .flat_tree_helper(role, prefix, str) ⇒ Object
- .logged_in_role(site_id) ⇒ Object
- .logged_in_role_id(site_id) ⇒ Object
- .logged_out_role(site_id) ⇒ Object
- .logged_out_role_id(site_id) ⇒ Object
-
.roles_with_user(user_id) ⇒ Object
—————————————————————————– Class methods —————————————————————————–.
- .tree(site_id) ⇒ Object
Instance Method Summary collapse
- #is_allowed(resource, action) ⇒ Object
- #is_ancestor_of?(role) ⇒ Boolean
- #is_child_of?(role) ⇒ Boolean
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
18 19 20 |
# File 'app/models/caboose/role.rb', line 18 def children @children end |
Class Method Details
.admin_role(site_id) ⇒ Object
20 21 22 |
# File 'app/models/caboose/role.rb', line 20 def self.admin_role(site_id) return self.where(:site_id => site_id, :name => 'Admin').first end |
.admin_role_id(site_id) ⇒ Object
24 25 26 |
# File 'app/models/caboose/role.rb', line 24 def self.admin_role_id(site_id) return self.where(:site_id => site_id, :name => 'Admin').limit(1).pluck(:id)[0] end |
.flat_tree(site_id, prefix = '-') ⇒ Object
78 79 80 81 82 83 84 |
# File 'app/models/caboose/role.rb', line 78 def self.flat_tree(site_id, prefix = '-') arr = [] self.tree(site_id).each do |r| arr += self.flat_tree_helper(r, prefix, '') end return arr end |
.flat_tree_helper(role, prefix, str) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'app/models/caboose/role.rb', line 86 def self.flat_tree_helper(role, prefix, str) role.name = "#{str}#{role.name}" arr = [role] role.children.each do |r| arr += self.flat_tree_helper(r, prefix, "#{str}#{prefix}") end return arr end |
.logged_in_role(site_id) ⇒ Object
36 37 38 |
# File 'app/models/caboose/role.rb', line 36 def self.logged_in_role(site_id) return self.where(:site_id => site_id, :name => 'Everyone Logged In').first end |
.logged_in_role_id(site_id) ⇒ Object
40 41 42 |
# File 'app/models/caboose/role.rb', line 40 def self.logged_in_role_id(site_id) return self.where(:site_id => site_id, :name => 'Everyone Logged In').limit(1).pluck(:id)[0] end |
.logged_out_role(site_id) ⇒ Object
28 29 30 |
# File 'app/models/caboose/role.rb', line 28 def self.logged_out_role(site_id) return self.where(:site_id => site_id, :name => 'Everyone Logged Out').first end |
.logged_out_role_id(site_id) ⇒ Object
32 33 34 |
# File 'app/models/caboose/role.rb', line 32 def self.logged_out_role_id(site_id) return self.where(:site_id => site_id, :name => 'Everyone Logged Out').limit(1).pluck(:id)[0] end |
.roles_with_user(user_id) ⇒ Object
Class methods
70 71 72 |
# File 'app/models/caboose/role.rb', line 70 def self.roles_with_user(user_id) return self.where("users.id" => user_id).all(:include => :users) end |
.tree(site_id) ⇒ Object
74 75 76 |
# File 'app/models/caboose/role.rb', line 74 def self.tree(site_id) return self.where(:parent_id => -1, :site_id => site_id).reorder("name").all end |
Instance Method Details
#is_allowed(resource, action) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/caboose/role.rb', line 44 def is_allowed(resource, action) # Check for the admin permission for perm in return true if (perm.resource == "all" && perm.action == "all") end if (resource.is_a?(Caboose::Page)) for perm in return true if (perm.page_id == resource.id && perm.action == action) end elsif for perm in return true if (perm.resource == resource && perm.action == action) end end return false end |
#is_ancestor_of?(role) ⇒ Boolean
95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/caboose/role.rb', line 95 def is_ancestor_of?(role) if (role.is_a?(Integer) || role.is_a?(String)) role_id = role.to_i return false if role_id == -1 role = Caboose::Role.find(role) end return false if role.parent_id == -1 return false if role.parent.nil? return true if role.parent.id == id return is_ancestor_of?(role.parent) end |
#is_child_of?(role) ⇒ Boolean
107 108 109 110 |
# File 'app/models/caboose/role.rb', line 107 def is_child_of?(role) role = Role.find(role) if role.is_a?(Integer) return role.is_ancestor_of?(self) end |