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 ⇒ Object
- .admin_role_id ⇒ Object
- .flat_tree(site_id, prefix = '-') ⇒ Object
- .flat_tree_helper(role, prefix, str) ⇒ Object
- .logged_in_role ⇒ Object
- .logged_in_role_id ⇒ Object
- .logged_out_role ⇒ Object
- .logged_out_role_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 ⇒ Object
20 21 22 |
# File 'app/models/caboose/role.rb', line 20 def self.admin_role return self.where('name' => 'Admin').first end |
.admin_role_id ⇒ Object
24 25 26 |
# File 'app/models/caboose/role.rb', line 24 def self.admin_role_id return self.where('name' => 'Admin').limit(1).pluck(:id)[0] end |
.flat_tree(site_id, prefix = '-') ⇒ Object
79 80 81 82 83 84 85 |
# File 'app/models/caboose/role.rb', line 79 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
87 88 89 90 91 92 93 94 |
# File 'app/models/caboose/role.rb', line 87 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 ⇒ Object
36 37 38 |
# File 'app/models/caboose/role.rb', line 36 def self.logged_in_role return self.where('name' => 'Everyone Logged In').first end |
.logged_in_role_id ⇒ Object
40 41 42 |
# File 'app/models/caboose/role.rb', line 40 def self.logged_in_role_id return self.where('name' => 'Everyone Logged In').limit(1).pluck(:id)[0] end |
.logged_out_role ⇒ Object
28 29 30 |
# File 'app/models/caboose/role.rb', line 28 def self.logged_out_role return self.where('name' => 'Everyone Logged Out').first end |
.logged_out_role_id ⇒ Object
32 33 34 |
# File 'app/models/caboose/role.rb', line 32 def self.logged_out_role_id return self.where('name' => 'Everyone Logged Out').limit(1).pluck(:id)[0] end |
.roles_with_user(user_id) ⇒ Object
Class methods
71 72 73 |
# File 'app/models/caboose/role.rb', line 71 def self.roles_with_user(user_id) return self.where("users.id" => user_id).all(:include => :users) end |
.tree(site_id) ⇒ Object
75 76 77 |
# File 'app/models/caboose/role.rb', line 75 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 61 |
# 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
96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/caboose/role.rb', line 96 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
108 109 110 111 |
# File 'app/models/caboose/role.rb', line 108 def is_child_of?(role) role = Role.find(role) if role.is_a?(Integer) return role.is_ancestor_of?(self) end |