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


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

#duplicate(site_id) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/caboose/role.rb', line 112

def duplicate(site_id)
  r = Caboose::Role.where(:site_id => site_id, :name => self.name).first
  return if r

  # If we're at the top of the role hierarchy
  if self.parent_id == -1
    r = Caboose::Role.create(
      :site_id => site_id,
      :parent_id => -1, 
      :name => r.name,
      :description => r.description
    )
    self.role_permissions.each{ |rp| Caboose::RolePermission.create(:permission_id => rp.permission_id, :role_id => r.id) }
    self.children.each{ |r2| r2.duplicate(site_id) }
    return
  end
  
  # Otherwise, there is a parent, try to find it
  new_parent = Caboose::Role.where(:site_id => site_id, :name => self.parent.name).first
  if new_parent
    r = Caboose::Role.create(
      :site_id => site_id,
      :parent_id => new_parent.id, 
      :name => r.name,
      :description => r.description
    )
    self.role_permissions.each{ |rp| Caboose::RolePermission.create(:permission_id => rp.permission_id, :role_id => r.id) }
    self.children.each{ |r2| r2.duplicate(site_id) }
    return
  end
  
  # Since we can't find the parent, recursively duplicate the current role's parent into the new site
  self.parent.duplicate(site_id)
end

#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 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