@@ -1,147 +1,163 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006 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 | named_scope :givable, { :conditions => "builtin = 0", :order => 'position' } |
|
23 | named_scope :givable, { :conditions => "builtin = 0", :order => 'position' } | |
24 | named_scope :builtin, lambda { |*args| |
|
24 | named_scope :builtin, lambda { |*args| | |
25 | compare = 'not' if args.first == true |
|
25 | compare = 'not' if args.first == true | |
26 | { :conditions => "#{compare} builtin = 0" } |
|
26 | { :conditions => "#{compare} builtin = 0" } | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | before_destroy :check_deletable |
|
29 | before_destroy :check_deletable | |
30 | has_many :workflows, :dependent => :delete_all do |
|
30 | has_many :workflows, :dependent => :delete_all do | |
31 | def copy(source_role) |
|
31 | def copy(source_role) | |
32 | Workflow.copy(nil, source_role, nil, proxy_owner) |
|
32 | Workflow.copy(nil, source_role, nil, proxy_owner) | |
33 | end |
|
33 | end | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | has_many :member_roles, :dependent => :destroy |
|
36 | has_many :member_roles, :dependent => :destroy | |
37 | has_many :members, :through => :member_roles |
|
37 | has_many :members, :through => :member_roles | |
38 | acts_as_list |
|
38 | acts_as_list | |
39 |
|
39 | |||
40 | serialize :permissions, Array |
|
40 | serialize :permissions, Array | |
41 | attr_protected :builtin |
|
41 | attr_protected :builtin | |
42 |
|
42 | |||
43 | validates_presence_of :name |
|
43 | validates_presence_of :name | |
44 | validates_uniqueness_of :name |
|
44 | validates_uniqueness_of :name | |
45 | validates_length_of :name, :maximum => 30 |
|
45 | validates_length_of :name, :maximum => 30 | |
46 | validates_format_of :name, :with => /^[\w\s\'\-]*$/i |
|
46 | validates_format_of :name, :with => /^[\w\s\'\-]*$/i | |
47 |
|
47 | |||
48 | def permissions |
|
48 | def permissions | |
49 | read_attribute(:permissions) || [] |
|
49 | read_attribute(:permissions) || [] | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | def permissions=(perms) |
|
52 | def permissions=(perms) | |
53 | perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms |
|
53 | perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms | |
54 | write_attribute(:permissions, perms) |
|
54 | write_attribute(:permissions, perms) | |
55 | end |
|
55 | end | |
56 |
|
56 | |||
57 | def add_permission!(*perms) |
|
57 | def add_permission!(*perms) | |
58 | self.permissions = [] unless permissions.is_a?(Array) |
|
58 | self.permissions = [] unless permissions.is_a?(Array) | |
59 |
|
59 | |||
60 | permissions_will_change! |
|
60 | permissions_will_change! | |
61 | perms.each do |p| |
|
61 | perms.each do |p| | |
62 | p = p.to_sym |
|
62 | p = p.to_sym | |
63 | permissions << p unless permissions.include?(p) |
|
63 | permissions << p unless permissions.include?(p) | |
64 | end |
|
64 | end | |
65 | save! |
|
65 | save! | |
66 | end |
|
66 | end | |
67 |
|
67 | |||
68 | def remove_permission!(*perms) |
|
68 | def remove_permission!(*perms) | |
69 | return unless permissions.is_a?(Array) |
|
69 | return unless permissions.is_a?(Array) | |
70 | permissions_will_change! |
|
70 | permissions_will_change! | |
71 | perms.each { |p| permissions.delete(p.to_sym) } |
|
71 | perms.each { |p| permissions.delete(p.to_sym) } | |
72 | save! |
|
72 | save! | |
73 | end |
|
73 | end | |
74 |
|
74 | |||
75 | # Returns true if the role has the given permission |
|
75 | # Returns true if the role has the given permission | |
76 | def has_permission?(perm) |
|
76 | def has_permission?(perm) | |
77 | !permissions.nil? && permissions.include?(perm.to_sym) |
|
77 | !permissions.nil? && permissions.include?(perm.to_sym) | |
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | def <=>(role) |
|
80 | def <=>(role) | |
81 | role ? position <=> role.position : -1 |
|
81 | role ? position <=> role.position : -1 | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def to_s |
|
84 | def to_s | |
85 | name |
|
85 | name | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | # Return true if the role is a builtin role |
|
88 | # Return true if the role is a builtin role | |
89 | def builtin? |
|
89 | def builtin? | |
90 | self.builtin != 0 |
|
90 | self.builtin != 0 | |
91 | end |
|
91 | end | |
92 |
|
92 | |||
93 | # Return true if the role is a project member role |
|
93 | # Return true if the role is a project member role | |
94 | def member? |
|
94 | def member? | |
95 | !self.builtin? |
|
95 | !self.builtin? | |
96 | end |
|
96 | end | |
97 |
|
97 | |||
98 | # Return true if role is allowed to do the specified action |
|
98 | # Return true if role is allowed to do the specified action | |
99 | # action can be: |
|
99 | # action can be: | |
100 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
100 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') | |
101 | # * a permission Symbol (eg. :edit_project) |
|
101 | # * a permission Symbol (eg. :edit_project) | |
102 | def allowed_to?(action) |
|
102 | def allowed_to?(action) | |
103 | if action.is_a? Hash |
|
103 | if action.is_a? Hash | |
104 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
104 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" | |
105 | else |
|
105 | else | |
106 | allowed_permissions.include? action |
|
106 | allowed_permissions.include? action | |
107 | end |
|
107 | end | |
108 | end |
|
108 | end | |
109 |
|
109 | |||
110 | # Return all the permissions that can be given to the role |
|
110 | # Return all the permissions that can be given to the role | |
111 | def setable_permissions |
|
111 | def setable_permissions | |
112 | setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions |
|
112 | setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions | |
113 | setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER |
|
113 | setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER | |
114 | setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS |
|
114 | setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS | |
115 | setable_permissions |
|
115 | setable_permissions | |
116 | end |
|
116 | end | |
117 |
|
117 | |||
118 | # Find all the roles that can be given to a project member |
|
118 | # Find all the roles that can be given to a project member | |
119 | def self.find_all_givable |
|
119 | def self.find_all_givable | |
120 | find(:all, :conditions => {:builtin => 0}, :order => 'position') |
|
120 | find(:all, :conditions => {:builtin => 0}, :order => 'position') | |
121 | end |
|
121 | end | |
122 |
|
122 | |||
123 | # Return the builtin 'non member' role |
|
123 | # Return the builtin 'non member' role. If the role doesn't exist, | |
|
124 | # it will be created on the fly. | |||
124 | def self.non_member |
|
125 | def self.non_member | |
125 |
find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) |
|
126 | non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) | |
|
127 | if non_member_role.nil? | |||
|
128 | non_member_role = create(:name => 'Non member', :position => 0) do |role| | |||
|
129 | role.builtin = BUILTIN_NON_MEMBER | |||
|
130 | end | |||
|
131 | raise 'Unable to create the non-member role.' if non_member_role.new_record? | |||
|
132 | end | |||
|
133 | non_member_role | |||
126 | end |
|
134 | end | |
127 |
|
135 | |||
128 | # Return the builtin 'anonymous' role |
|
136 | # Return the builtin 'anonymous' role. If the role doesn't exist, | |
|
137 | # it will be created on the fly. | |||
129 | def self.anonymous |
|
138 | def self.anonymous | |
130 |
find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) |
|
139 | anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) | |
|
140 | if anonymous_role.nil? | |||
|
141 | anonymous_role = create(:name => 'Anonymous', :position => 0) do |role| | |||
|
142 | role.builtin = BUILTIN_ANONYMOUS | |||
|
143 | end | |||
|
144 | raise 'Unable to create the anonymous role.' if anonymous_role.new_record? | |||
|
145 | end | |||
|
146 | anonymous_role | |||
131 | end |
|
147 | end | |
132 |
|
148 | |||
133 |
|
149 | |||
134 | private |
|
150 | private | |
135 | def allowed_permissions |
|
151 | def allowed_permissions | |
136 | @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name} |
|
152 | @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name} | |
137 | end |
|
153 | end | |
138 |
|
154 | |||
139 | def allowed_actions |
|
155 | def allowed_actions | |
140 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
156 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten | |
141 | end |
|
157 | end | |
142 |
|
158 | |||
143 | def check_deletable |
|
159 | def check_deletable | |
144 | raise "Can't delete role" if members.any? |
|
160 | raise "Can't delete role" if members.any? | |
145 | raise "Can't delete builtin role" if builtin? |
|
161 | raise "Can't delete builtin role" if builtin? | |
146 | end |
|
162 | end | |
147 | end |
|
163 | end |
@@ -1,53 +1,104 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2008 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2008 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 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 |
|
19 | |||
20 | class RoleTest < ActiveSupport::TestCase |
|
20 | class RoleTest < ActiveSupport::TestCase | |
21 | fixtures :roles, :workflows |
|
21 | fixtures :roles, :workflows | |
22 |
|
22 | |||
23 | def test_copy_workflows |
|
23 | def test_copy_workflows | |
24 | source = Role.find(1) |
|
24 | source = Role.find(1) | |
25 | assert_equal 90, source.workflows.size |
|
25 | assert_equal 90, source.workflows.size | |
26 |
|
26 | |||
27 | target = Role.new(:name => 'Target') |
|
27 | target = Role.new(:name => 'Target') | |
28 | assert target.save |
|
28 | assert target.save | |
29 | target.workflows.copy(source) |
|
29 | target.workflows.copy(source) | |
30 | target.reload |
|
30 | target.reload | |
31 | assert_equal 90, target.workflows.size |
|
31 | assert_equal 90, target.workflows.size | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | def test_add_permission |
|
34 | def test_add_permission | |
35 | role = Role.find(1) |
|
35 | role = Role.find(1) | |
36 | size = role.permissions.size |
|
36 | size = role.permissions.size | |
37 | role.add_permission!("apermission", "anotherpermission") |
|
37 | role.add_permission!("apermission", "anotherpermission") | |
38 | role.reload |
|
38 | role.reload | |
39 | assert role.permissions.include?(:anotherpermission) |
|
39 | assert role.permissions.include?(:anotherpermission) | |
40 | assert_equal size + 2, role.permissions.size |
|
40 | assert_equal size + 2, role.permissions.size | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | def test_remove_permission |
|
43 | def test_remove_permission | |
44 | role = Role.find(1) |
|
44 | role = Role.find(1) | |
45 | size = role.permissions.size |
|
45 | size = role.permissions.size | |
46 | perm = role.permissions[0..1] |
|
46 | perm = role.permissions[0..1] | |
47 | role.remove_permission!(*perm) |
|
47 | role.remove_permission!(*perm) | |
48 | role.reload |
|
48 | role.reload | |
49 | assert ! role.permissions.include?(perm[0]) |
|
49 | assert ! role.permissions.include?(perm[0]) | |
50 | assert_equal size - 2, role.permissions.size |
|
50 | assert_equal size - 2, role.permissions.size | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
|
53 | context "#anonymous" do | |||
|
54 | should "return the anonymous role" do | |||
|
55 | role = Role.anonymous | |||
|
56 | assert role.builtin? | |||
|
57 | assert_equal Role::BUILTIN_ANONYMOUS, role.builtin | |||
|
58 | end | |||
|
59 | ||||
|
60 | context "with a missing anonymous role" do | |||
|
61 | setup do | |||
|
62 | Role.delete_all("builtin = #{Role::BUILTIN_ANONYMOUS}") | |||
|
63 | end | |||
|
64 | ||||
|
65 | should "create a new anonymous role" do | |||
|
66 | assert_difference('Role.count') do | |||
|
67 | Role.anonymous | |||
|
68 | end | |||
|
69 | end | |||
|
70 | ||||
|
71 | should "return the anonymous role" do | |||
|
72 | role = Role.anonymous | |||
|
73 | assert role.builtin? | |||
|
74 | assert_equal Role::BUILTIN_ANONYMOUS, role.builtin | |||
|
75 | end | |||
|
76 | end | |||
|
77 | end | |||
|
78 | ||||
|
79 | context "#non_member" do | |||
|
80 | should "return the non-member role" do | |||
|
81 | role = Role.non_member | |||
|
82 | assert role.builtin? | |||
|
83 | assert_equal Role::BUILTIN_NON_MEMBER, role.builtin | |||
|
84 | end | |||
|
85 | ||||
|
86 | context "with a missing non-member role" do | |||
|
87 | setup do | |||
|
88 | Role.delete_all("builtin = #{Role::BUILTIN_NON_MEMBER}") | |||
|
89 | end | |||
|
90 | ||||
|
91 | should "create a new non-member role" do | |||
|
92 | assert_difference('Role.count') do | |||
|
93 | Role.non_member | |||
|
94 | end | |||
|
95 | end | |||
|
96 | ||||
|
97 | should "return the non-member role" do | |||
|
98 | role = Role.non_member | |||
|
99 | assert role.builtin? | |||
|
100 | assert_equal Role::BUILTIN_NON_MEMBER, role.builtin | |||
|
101 | end | |||
|
102 | end | |||
|
103 | end | |||
53 | end |
|
104 | end |
General Comments 0
You need to be logged in to leave comments.
Login now