##// END OF EJS Templates
Change Role#anonymous and #non_member so they generate the record as needed....
Eric Davis -
r3249:155083ec9722
parent child
Show More
@@ -1,147 +1,163
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Role < ActiveRecord::Base
19 19 # Built-in roles
20 20 BUILTIN_NON_MEMBER = 1
21 21 BUILTIN_ANONYMOUS = 2
22 22
23 23 named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
24 24 named_scope :builtin, lambda { |*args|
25 25 compare = 'not' if args.first == true
26 26 { :conditions => "#{compare} builtin = 0" }
27 27 }
28 28
29 29 before_destroy :check_deletable
30 30 has_many :workflows, :dependent => :delete_all do
31 31 def copy(source_role)
32 32 Workflow.copy(nil, source_role, nil, proxy_owner)
33 33 end
34 34 end
35 35
36 36 has_many :member_roles, :dependent => :destroy
37 37 has_many :members, :through => :member_roles
38 38 acts_as_list
39 39
40 40 serialize :permissions, Array
41 41 attr_protected :builtin
42 42
43 43 validates_presence_of :name
44 44 validates_uniqueness_of :name
45 45 validates_length_of :name, :maximum => 30
46 46 validates_format_of :name, :with => /^[\w\s\'\-]*$/i
47 47
48 48 def permissions
49 49 read_attribute(:permissions) || []
50 50 end
51 51
52 52 def permissions=(perms)
53 53 perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
54 54 write_attribute(:permissions, perms)
55 55 end
56 56
57 57 def add_permission!(*perms)
58 58 self.permissions = [] unless permissions.is_a?(Array)
59 59
60 60 permissions_will_change!
61 61 perms.each do |p|
62 62 p = p.to_sym
63 63 permissions << p unless permissions.include?(p)
64 64 end
65 65 save!
66 66 end
67 67
68 68 def remove_permission!(*perms)
69 69 return unless permissions.is_a?(Array)
70 70 permissions_will_change!
71 71 perms.each { |p| permissions.delete(p.to_sym) }
72 72 save!
73 73 end
74 74
75 75 # Returns true if the role has the given permission
76 76 def has_permission?(perm)
77 77 !permissions.nil? && permissions.include?(perm.to_sym)
78 78 end
79 79
80 80 def <=>(role)
81 81 role ? position <=> role.position : -1
82 82 end
83 83
84 84 def to_s
85 85 name
86 86 end
87 87
88 88 # Return true if the role is a builtin role
89 89 def builtin?
90 90 self.builtin != 0
91 91 end
92 92
93 93 # Return true if the role is a project member role
94 94 def member?
95 95 !self.builtin?
96 96 end
97 97
98 98 # Return true if role is allowed to do the specified action
99 99 # action can be:
100 100 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
101 101 # * a permission Symbol (eg. :edit_project)
102 102 def allowed_to?(action)
103 103 if action.is_a? Hash
104 104 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
105 105 else
106 106 allowed_permissions.include? action
107 107 end
108 108 end
109 109
110 110 # Return all the permissions that can be given to the role
111 111 def setable_permissions
112 112 setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
113 113 setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
114 114 setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
115 115 setable_permissions
116 116 end
117 117
118 118 # Find all the roles that can be given to a project member
119 119 def self.find_all_givable
120 120 find(:all, :conditions => {:builtin => 0}, :order => 'position')
121 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 125 def self.non_member
125 find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) || raise('Missing non-member builtin role.')
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 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 138 def self.anonymous
130 find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) || raise('Missing anonymous builtin role.')
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 147 end
132 148
133 149
134 150 private
135 151 def allowed_permissions
136 152 @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
137 153 end
138 154
139 155 def allowed_actions
140 156 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
141 157 end
142 158
143 159 def check_deletable
144 160 raise "Can't delete role" if members.any?
145 161 raise "Can't delete builtin role" if builtin?
146 162 end
147 163 end
@@ -1,53 +1,104
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class RoleTest < ActiveSupport::TestCase
21 21 fixtures :roles, :workflows
22 22
23 23 def test_copy_workflows
24 24 source = Role.find(1)
25 25 assert_equal 90, source.workflows.size
26 26
27 27 target = Role.new(:name => 'Target')
28 28 assert target.save
29 29 target.workflows.copy(source)
30 30 target.reload
31 31 assert_equal 90, target.workflows.size
32 32 end
33 33
34 34 def test_add_permission
35 35 role = Role.find(1)
36 36 size = role.permissions.size
37 37 role.add_permission!("apermission", "anotherpermission")
38 38 role.reload
39 39 assert role.permissions.include?(:anotherpermission)
40 40 assert_equal size + 2, role.permissions.size
41 41 end
42 42
43 43 def test_remove_permission
44 44 role = Role.find(1)
45 45 size = role.permissions.size
46 46 perm = role.permissions[0..1]
47 47 role.remove_permission!(*perm)
48 48 role.reload
49 49 assert ! role.permissions.include?(perm[0])
50 50 assert_equal size - 2, role.permissions.size
51 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 104 end
General Comments 0
You need to be logged in to leave comments. Login now