Class: Caboose::Role

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/role.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



17
18
19
# File 'app/models/caboose/role.rb', line 17

def children
  @children
end

Class Method Details

.admin_roleObject



19
20
21
# File 'app/models/caboose/role.rb', line 19

def self.admin_role
  return self.where('name' => 'Admin').first
end

.admin_role_idObject



23
24
25
# File 'app/models/caboose/role.rb', line 23

def self.admin_role_id
  return self.where('name' => 'Admin').limit(1).pluck(:id)[0]
end

.flat_tree(prefix = '-') ⇒ Object



78
79
80
81
82
83
84
# File 'app/models/caboose/role.rb', line 78

def self.flat_tree(prefix = '-')
  arr = []
  self.tree.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_roleObject



35
36
37
# File 'app/models/caboose/role.rb', line 35

def self.logged_in_role
  return self.where('name' => 'Everyone Logged In').first
end

.logged_in_role_idObject



39
40
41
# File 'app/models/caboose/role.rb', line 39

def self.logged_in_role_id
  return self.where('name' => 'Everyone Logged In').limit(1).pluck(:id)[0]
end

.logged_out_roleObject



27
28
29
# File 'app/models/caboose/role.rb', line 27

def self.logged_out_role
  return self.where('name' => 'Everyone Logged Out').first
end

.logged_out_role_idObject



31
32
33
# File 'app/models/caboose/role.rb', line 31

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




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

.treeObject



74
75
76
# File 'app/models/caboose/role.rb', line 74

def self.tree
  return self.where(:parent_id => -1).reorder("name").all
end

Instance Method Details

#is_allowed(resource, action) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/caboose/role.rb', line 43

def is_allowed(resource, action)
  
  # Check for the admin permission
  for perm in permissions
    return true if (perm.resource == "all" && perm.action == "all")
  end
      
  if (resource.is_a?(Caboose::Page))
    for perm in page_permissions
      return true if (perm.page_id == resource.id && perm.action == action)
    end        
  elsif
    for perm in permissions
      return true if (perm.resource == resource && perm.action == action)
    end
  end
  return false
end

#is_ancestor_of?(role) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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