Class: Role
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Role
- Defined in:
- app/models/role.rb
Overview
redMine - project management software Copyright © 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_NON_MEMBER =
Built-in roles
1
- BUILTIN_ANONYMOUS =
2
Class Method Summary collapse
-
.anonymous ⇒ Object
Return the builtin ‘anonymous’ role.
-
.find_all_givable ⇒ Object
Find all the roles that can be given to a project member.
-
.non_member ⇒ Object
Return the builtin ‘non member’ role.
Instance Method Summary collapse
- #<=>(role) ⇒ Object
- #add_permission!(*perms) ⇒ Object
-
#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).
-
#builtin? ⇒ Boolean
Return true if the role is a builtin role.
-
#has_permission?(perm) ⇒ Boolean
Returns true if the role has the given permission.
-
#member? ⇒ Boolean
Return true if the role is a project member role.
- #permissions ⇒ Object
- #permissions=(perms) ⇒ Object
- #remove_permission!(*perms) ⇒ Object
-
#setable_permissions ⇒ Object
Return all the permissions that can be given to the role.
- #to_s ⇒ Object
Class Method Details
.anonymous ⇒ Object
Return the builtin ‘anonymous’ role. If the role doesn’t exist, it will be created on the fly.
137 138 139 140 141 142 143 144 145 146 |
# File 'app/models/role.rb', line 137 def self.anonymous anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) if anonymous_role.nil? anonymous_role = create(:name => 'Anonymous', :position => 0) do |role| role.builtin = BUILTIN_ANONYMOUS end raise 'Unable to create the anonymous role.' if anonymous_role.new_record? end anonymous_role end |
.find_all_givable ⇒ Object
Find all the roles that can be given to a project member
118 119 120 |
# File 'app/models/role.rb', line 118 def self.find_all_givable find(:all, :conditions => {:builtin => 0}, :order => 'position') end |
.non_member ⇒ Object
Return the builtin ‘non member’ role. If the role doesn’t exist, it will be created on the fly.
124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/role.rb', line 124 def self.non_member non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) if non_member_role.nil? non_member_role = create(:name => 'Non member', :position => 0) do |role| role.builtin = BUILTIN_NON_MEMBER end raise 'Unable to create the non-member role.' if non_member_role.new_record? end non_member_role end |
Instance Method Details
#<=>(role) ⇒ Object
79 80 81 |
# File 'app/models/role.rb', line 79 def <=>(role) role ? position <=> role.position : -1 end |
#add_permission!(*perms) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/role.rb', line 56 def (*perms) self. = [] unless .is_a?(Array) perms.each do |p| p = p.to_sym << p unless .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)
101 102 103 104 105 106 107 |
# File 'app/models/role.rb', line 101 def allowed_to?(action) if action.is_a? Hash allowed_actions.include? "#{action[:controller]}/#{action[:action]}" else .include? action end end |
#builtin? ⇒ Boolean
Return true if the role is a builtin role
88 89 90 |
# File 'app/models/role.rb', line 88 def builtin? self.builtin != 0 end |
#has_permission?(perm) ⇒ Boolean
Returns true if the role has the given permission
75 76 77 |
# File 'app/models/role.rb', line 75 def (perm) !.nil? && .include?(perm.to_sym) end |
#member? ⇒ Boolean
Return true if the role is a project member role
93 94 95 |
# File 'app/models/role.rb', line 93 def member? !self.builtin? end |
#permissions ⇒ Object
47 48 49 |
# File 'app/models/role.rb', line 47 def read_attribute(:permissions) || [] end |
#permissions=(perms) ⇒ Object
51 52 53 54 |
# File 'app/models/role.rb', line 51 def (perms) perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms write_attribute(:permissions, perms) end |
#remove_permission!(*perms) ⇒ Object
67 68 69 70 71 72 |
# File 'app/models/role.rb', line 67 def (*perms) return unless .is_a?(Array) perms.each { |p| .delete(p.to_sym) } save! end |
#setable_permissions ⇒ Object
Return all the permissions that can be given to the role
110 111 112 113 114 115 |
# File 'app/models/role.rb', line 110 def = Redmine::AccessControl. - Redmine::AccessControl. -= Redmine::AccessControl. if self.builtin == BUILTIN_NON_MEMBER -= Redmine::AccessControl. if self.builtin == BUILTIN_ANONYMOUS end |
#to_s ⇒ Object
83 84 85 |
# File 'app/models/role.rb', line 83 def to_s name end |