Class: Role

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

Overview

redMine - project management software Copyright (C) 2006 Jean-Philippe Lang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Constant Summary collapse

BUILTIN_DEFAULT_USER =

Built-in roles

1
BUILTIN_ANONYMOUS =
2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Role


46
47
48
49
# File 'app/models/role.rb', line 46

def initialize *args
  super *args
  self.builtin = 0
end

Class Method Details

.anonymousObject

Return the builtin 'anonymous' role. If the role doesn't exist, it will be created on the fly.


132
133
134
135
136
137
138
139
140
141
# File 'app/models/role.rb', line 132

def self.anonymous
  anonymous_role = first(:conditions => {:builtin => BUILTIN_ANONYMOUS})
  if anonymous_role.nil?
    anonymous_role = create!(:name => 'Anonymous') do |role|
      role.builtin = BUILTIN_ANONYMOUS
    end
    raise ::Foreman::Exception.new(N_("Unable to create the anonymous role.")) if anonymous_role.new_record?
  end
  anonymous_role
end

.default_userObject

Return the builtin 'default user' role. If the role doesn't exist, it will be created on the fly.


119
120
121
122
123
124
125
126
127
128
# File 'app/models/role.rb', line 119

def self.default_user
  default_user_role = first(:conditions => {:builtin => BUILTIN_DEFAULT_USER})
  if default_user_role.nil?
    default_user_role = create!(:name => 'Default user') do |role|
      role.builtin = BUILTIN_DEFAULT_USER
    end
    raise ::Foreman::Exception.new(N_('Unable to create the default user role.')) if default_user_role.new_record?
  end
  default_user_role
end

.find_all_givableObject

Find all the roles that can be given to a user


113
114
115
# File 'app/models/role.rb', line 113

def self.find_all_givable
  all(:conditions => {:builtin => 0}, :order => 'name')
end

Instance Method Details

#add_permission!(*perms) ⇒ Object


60
61
62
63
64
65
66
67
68
69
# File 'app/models/role.rb', line 60

def add_permission!(*perms)
  self.permissions = [] unless permissions.is_a?(Array)

  permissions_will_change!
  perms.each do |p|
    p = p.to_sym
    permissions << p unless permissions.include?(p)
  end
  save!
end

#allowed_to?(action) ⇒ Boolean

Return true if role is allowed to do the specified action action can be:

  • a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')

  • a permission Symbol (eg. :edit_project)


97
98
99
100
101
102
103
# File 'app/models/role.rb', line 97

def allowed_to?(action)
  if action.is_a? Hash
    allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
  else
    allowed_permissions.include? action
  end
end

#builtin?Boolean

Return true if the role is a builtin role


84
85
86
# File 'app/models/role.rb', line 84

def builtin?
  self.builtin != 0
end

#has_permission?(perm) ⇒ Boolean

Returns true if the role has the given permission


79
80
81
# File 'app/models/role.rb', line 79

def has_permission?(perm)
  !permissions.nil? && permissions.include?(perm.to_sym)
end

#permissionsObject


51
52
53
# File 'app/models/role.rb', line 51

def permissions
  read_attribute(:permissions) || []
end

#permissions=(perms) ⇒ Object


55
56
57
58
# File 'app/models/role.rb', line 55

def permissions=(perms)
  perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
  write_attribute(:permissions, perms)
end

#remove_permission!(*perms) ⇒ Object


71
72
73
74
75
76
# File 'app/models/role.rb', line 71

def remove_permission!(*perms)
  return unless permissions.is_a?(Array)
  permissions_will_change!
  perms.each { |p| permissions.delete(p.to_sym) }
  save!
end

#setable_permissionsObject

Return all the permissions that can be given to the role


106
107
108
109
110
# File 'app/models/role.rb', line 106

def setable_permissions
  setable_permissions  = Foreman::AccessControl.permissions - Foreman::AccessControl.public_permissions
  setable_permissions -= Foreman::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
  setable_permissions
end

#user?Boolean

Return true if the role is a user role


89
90
91
# File 'app/models/role.rb', line 89

def user?
  !self.builtin?
end