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

BUILTIN_NON_MEMBER =

Built-in roles

1
BUILTIN_ANONYMOUS =
2

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) anonymous

Return the builtin 'anonymous' role



129
130
131
# File 'app/models/role.rb', line 129

def self.anonymous
  find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) || raise('Missing anonymous builtin role.')
end

+ (Object) find_all_givable

Find all the roles that can be given to a project member



119
120
121
# File 'app/models/role.rb', line 119

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

+ (Object) non_member

Return the builtin 'non member' role



124
125
126
# File 'app/models/role.rb', line 124

def self.non_member
  find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) || raise('Missing non-member builtin role.')
end

Instance Method Details

- (Object) <=>(role)



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

def <=>(role)
  position <=> role.position
end

- (Object) add_permission!(*perms)



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

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

- (Boolean) allowed_to?(action)

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)



102
103
104
105
106
107
108
# File 'app/models/role.rb', line 102

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

- (Boolean) builtin?

Return true if the role is a builtin role



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

def builtin?
  self.builtin != 0
end

- (Boolean) has_permission?(perm)

Returns true if the role has the given permission



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

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

- (Boolean) member?

Return true if the role is a project member role



94
95
96
# File 'app/models/role.rb', line 94

def member?
  !self.builtin?
end

- (Object) permissions



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

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

- (Object) permissions=(perms)



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

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

- (Object) remove_permission!(*perms)



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

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

- (Object) setable_permissions

Return all the permissions that can be given to the role



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

def setable_permissions
  setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
  setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
  setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
  setable_permissions
end