@@ -1,179 +1,176 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class Role < ActiveRecord::Base |
|
18 | class Role < ActiveRecord::Base | |
19 | # Built-in roles |
|
19 | # Built-in roles | |
20 | BUILTIN_NON_MEMBER = 1 |
|
20 | BUILTIN_NON_MEMBER = 1 | |
21 | BUILTIN_ANONYMOUS = 2 |
|
21 | BUILTIN_ANONYMOUS = 2 | |
22 |
|
22 | |||
23 | ISSUES_VISIBILITY_OPTIONS = [ |
|
23 | ISSUES_VISIBILITY_OPTIONS = [ | |
24 | ['all', :label_issues_visibility_all], |
|
24 | ['all', :label_issues_visibility_all], | |
25 | ['default', :label_issues_visibility_public], |
|
25 | ['default', :label_issues_visibility_public], | |
26 | ['own', :label_issues_visibility_own] |
|
26 | ['own', :label_issues_visibility_own] | |
27 | ] |
|
27 | ] | |
28 |
|
28 | |||
29 | named_scope :givable, { :conditions => "builtin = 0", :order => 'position' } |
|
29 | named_scope :givable, { :conditions => "builtin = 0", :order => 'position' } | |
30 | named_scope :builtin, lambda { |*args| |
|
30 | named_scope :builtin, lambda { |*args| | |
31 | compare = 'not' if args.first == true |
|
31 | compare = 'not' if args.first == true | |
32 | { :conditions => "#{compare} builtin = 0" } |
|
32 | { :conditions => "#{compare} builtin = 0" } | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | before_destroy :check_deletable |
|
35 | before_destroy :check_deletable | |
36 | has_many :workflows, :dependent => :delete_all do |
|
36 | has_many :workflows, :dependent => :delete_all do | |
37 | def copy(source_role) |
|
37 | def copy(source_role) | |
38 | Workflow.copy(nil, source_role, nil, proxy_owner) |
|
38 | Workflow.copy(nil, source_role, nil, proxy_owner) | |
39 | end |
|
39 | end | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | has_many :member_roles, :dependent => :destroy |
|
42 | has_many :member_roles, :dependent => :destroy | |
43 | has_many :members, :through => :member_roles |
|
43 | has_many :members, :through => :member_roles | |
44 | acts_as_list |
|
44 | acts_as_list | |
45 |
|
45 | |||
46 | serialize :permissions, Array |
|
46 | serialize :permissions, Array | |
47 | attr_protected :builtin |
|
47 | attr_protected :builtin | |
48 |
|
48 | |||
49 | validates_presence_of :name |
|
49 | validates_presence_of :name | |
50 | validates_uniqueness_of :name |
|
50 | validates_uniqueness_of :name | |
51 | validates_length_of :name, :maximum => 30 |
|
51 | validates_length_of :name, :maximum => 30 | |
52 | validates_inclusion_of :issues_visibility, |
|
52 | validates_inclusion_of :issues_visibility, | |
53 | :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first), |
|
53 | :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first), | |
54 | :if => lambda {|role| role.respond_to?(:issues_visibility)} |
|
54 | :if => lambda {|role| role.respond_to?(:issues_visibility)} | |
55 |
|
55 | |||
56 | def permissions |
|
56 | def permissions | |
57 | read_attribute(:permissions) || [] |
|
57 | read_attribute(:permissions) || [] | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def permissions=(perms) |
|
60 | def permissions=(perms) | |
61 | perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms |
|
61 | perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms | |
62 | write_attribute(:permissions, perms) |
|
62 | write_attribute(:permissions, perms) | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | def add_permission!(*perms) |
|
65 | def add_permission!(*perms) | |
66 | self.permissions = [] unless permissions.is_a?(Array) |
|
66 | self.permissions = [] unless permissions.is_a?(Array) | |
67 |
|
67 | |||
68 | permissions_will_change! |
|
68 | permissions_will_change! | |
69 | perms.each do |p| |
|
69 | perms.each do |p| | |
70 | p = p.to_sym |
|
70 | p = p.to_sym | |
71 | permissions << p unless permissions.include?(p) |
|
71 | permissions << p unless permissions.include?(p) | |
72 | end |
|
72 | end | |
73 | save! |
|
73 | save! | |
74 | end |
|
74 | end | |
75 |
|
75 | |||
76 | def remove_permission!(*perms) |
|
76 | def remove_permission!(*perms) | |
77 | return unless permissions.is_a?(Array) |
|
77 | return unless permissions.is_a?(Array) | |
78 | permissions_will_change! |
|
78 | permissions_will_change! | |
79 | perms.each { |p| permissions.delete(p.to_sym) } |
|
79 | perms.each { |p| permissions.delete(p.to_sym) } | |
80 | save! |
|
80 | save! | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | # Returns true if the role has the given permission |
|
83 | # Returns true if the role has the given permission | |
84 | def has_permission?(perm) |
|
84 | def has_permission?(perm) | |
85 | !permissions.nil? && permissions.include?(perm.to_sym) |
|
85 | !permissions.nil? && permissions.include?(perm.to_sym) | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | def <=>(role) |
|
88 | def <=>(role) | |
89 | role ? position <=> role.position : -1 |
|
89 | role ? position <=> role.position : -1 | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | def to_s |
|
92 | def to_s | |
93 | name |
|
93 | name | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | def name |
|
96 | def name | |
97 | case builtin |
|
97 | case builtin | |
98 | when 1; l(:label_role_non_member, :default => read_attribute(:name)) |
|
98 | when 1; l(:label_role_non_member, :default => read_attribute(:name)) | |
99 | when 2; l(:label_role_anonymous, :default => read_attribute(:name)) |
|
99 | when 2; l(:label_role_anonymous, :default => read_attribute(:name)) | |
100 | else; read_attribute(:name) |
|
100 | else; read_attribute(:name) | |
101 | end |
|
101 | end | |
102 | end |
|
102 | end | |
103 |
|
103 | |||
104 | # Return true if the role is a builtin role |
|
104 | # Return true if the role is a builtin role | |
105 | def builtin? |
|
105 | def builtin? | |
106 | self.builtin != 0 |
|
106 | self.builtin != 0 | |
107 | end |
|
107 | end | |
108 |
|
108 | |||
109 | # Return true if the role is a project member role |
|
109 | # Return true if the role is a project member role | |
110 | def member? |
|
110 | def member? | |
111 | !self.builtin? |
|
111 | !self.builtin? | |
112 | end |
|
112 | end | |
113 |
|
113 | |||
114 | # Return true if role is allowed to do the specified action |
|
114 | # Return true if role is allowed to do the specified action | |
115 | # action can be: |
|
115 | # action can be: | |
116 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
116 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') | |
117 | # * a permission Symbol (eg. :edit_project) |
|
117 | # * a permission Symbol (eg. :edit_project) | |
118 | def allowed_to?(action) |
|
118 | def allowed_to?(action) | |
119 | if action.is_a? Hash |
|
119 | if action.is_a? Hash | |
120 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
120 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" | |
121 | else |
|
121 | else | |
122 | allowed_permissions.include? action |
|
122 | allowed_permissions.include? action | |
123 | end |
|
123 | end | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | # Return all the permissions that can be given to the role |
|
126 | # Return all the permissions that can be given to the role | |
127 | def setable_permissions |
|
127 | def setable_permissions | |
128 | setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions |
|
128 | setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions | |
129 | setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER |
|
129 | setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER | |
130 | setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS |
|
130 | setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS | |
131 | setable_permissions |
|
131 | setable_permissions | |
132 | end |
|
132 | end | |
133 |
|
133 | |||
134 | # Find all the roles that can be given to a project member |
|
134 | # Find all the roles that can be given to a project member | |
135 | def self.find_all_givable |
|
135 | def self.find_all_givable | |
136 | find(:all, :conditions => {:builtin => 0}, :order => 'position') |
|
136 | find(:all, :conditions => {:builtin => 0}, :order => 'position') | |
137 | end |
|
137 | end | |
138 |
|
138 | |||
139 | # Return the builtin 'non member' role. If the role doesn't exist, |
|
139 | # Return the builtin 'non member' role. If the role doesn't exist, | |
140 | # it will be created on the fly. |
|
140 | # it will be created on the fly. | |
141 | def self.non_member |
|
141 | def self.non_member | |
142 | non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) |
|
142 | find_or_create_system_role(BUILTIN_NON_MEMBER, 'Non member') | |
143 | if non_member_role.nil? |
|
|||
144 | non_member_role = create(:name => 'Non member', :position => 0) do |role| |
|
|||
145 | role.builtin = BUILTIN_NON_MEMBER |
|
|||
146 | end |
|
|||
147 | raise 'Unable to create the non-member role.' if non_member_role.new_record? |
|
|||
148 | end |
|
|||
149 | non_member_role |
|
|||
150 | end |
|
143 | end | |
151 |
|
144 | |||
152 | # Return the builtin 'anonymous' role. If the role doesn't exist, |
|
145 | # Return the builtin 'anonymous' role. If the role doesn't exist, | |
153 | # it will be created on the fly. |
|
146 | # it will be created on the fly. | |
154 | def self.anonymous |
|
147 | def self.anonymous | |
155 | anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) |
|
148 | find_or_create_system_role(BUILTIN_ANONYMOUS, 'Anonymous') | |
156 | if anonymous_role.nil? |
|
|||
157 | anonymous_role = create(:name => 'Anonymous', :position => 0) do |role| |
|
|||
158 | role.builtin = BUILTIN_ANONYMOUS |
|
|||
159 | end |
|
|||
160 | raise 'Unable to create the anonymous role.' if anonymous_role.new_record? |
|
|||
161 | end |
|
|||
162 | anonymous_role |
|
|||
163 | end |
|
149 | end | |
164 |
|
||||
165 |
|
150 | |||
166 | private |
|
151 | private | |
|
152 | ||||
167 | def allowed_permissions |
|
153 | def allowed_permissions | |
168 | @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name} |
|
154 | @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name} | |
169 | end |
|
155 | end | |
170 |
|
156 | |||
171 | def allowed_actions |
|
157 | def allowed_actions | |
172 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
158 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten | |
173 | end |
|
159 | end | |
174 |
|
160 | |||
175 | def check_deletable |
|
161 | def check_deletable | |
176 | raise "Can't delete role" if members.any? |
|
162 | raise "Can't delete role" if members.any? | |
177 | raise "Can't delete builtin role" if builtin? |
|
163 | raise "Can't delete builtin role" if builtin? | |
178 | end |
|
164 | end | |
|
165 | ||||
|
166 | def self.find_or_create_system_role(builtin, name) | |||
|
167 | role = first(:conditions => {:builtin => builtin}) | |||
|
168 | if role.nil? | |||
|
169 | role = create(:name => name, :position => 0) do |r| | |||
|
170 | r.builtin = builtin | |||
|
171 | end | |||
|
172 | raise "Unable to create the #{name} role." if role.new_record? | |||
|
173 | end | |||
|
174 | role | |||
|
175 | end | |||
179 | end |
|
176 | end |
General Comments 0
You need to be logged in to leave comments.
Login now