@@ -1,117 +1,117 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 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 Group < Principal |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | |
|
21 | 21 | has_and_belongs_to_many :users, |
|
22 | 22 | :join_table => "#{table_name_prefix}groups_users#{table_name_suffix}", |
|
23 | 23 | :after_add => :user_added, |
|
24 | 24 | :after_remove => :user_removed |
|
25 | 25 | |
|
26 | 26 | acts_as_customizable |
|
27 | 27 | |
|
28 | 28 | validates_presence_of :lastname |
|
29 | 29 | validates_uniqueness_of :lastname, :case_sensitive => false |
|
30 | 30 | validates_length_of :lastname, :maximum => 255 |
|
31 | 31 | attr_protected :id |
|
32 | 32 | |
|
33 | 33 | before_destroy :remove_references_before_destroy |
|
34 | 34 | |
|
35 |
scope :sorted, lambda { order(:type => :asc, :lastname => : |
|
|
35 | scope :sorted, lambda { order(:type => :asc, :lastname => :asc) } | |
|
36 | 36 | scope :named, lambda {|arg| where("LOWER(#{table_name}.lastname) = LOWER(?)", arg.to_s.strip)} |
|
37 | 37 | scope :givable, lambda {where(:type => 'Group')} |
|
38 | 38 | |
|
39 | 39 | safe_attributes 'name', |
|
40 | 40 | 'user_ids', |
|
41 | 41 | 'custom_field_values', |
|
42 | 42 | 'custom_fields', |
|
43 | 43 | :if => lambda {|group, user| user.admin? && !group.builtin?} |
|
44 | 44 | |
|
45 | 45 | def to_s |
|
46 | 46 | name.to_s |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def name |
|
50 | 50 | lastname |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def name=(arg) |
|
54 | 54 | self.lastname = arg |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | def builtin_type |
|
58 | 58 | nil |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | # Return true if the group is a builtin group |
|
62 | 62 | def builtin? |
|
63 | 63 | false |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | # Returns true if the group can be given to a user |
|
67 | 67 | def givable? |
|
68 | 68 | !builtin? |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def user_added(user) |
|
72 | 72 | members.each do |member| |
|
73 | 73 | next if member.project.nil? |
|
74 | 74 | user_member = Member.find_by_project_id_and_user_id(member.project_id, user.id) || Member.new(:project_id => member.project_id, :user_id => user.id) |
|
75 | 75 | member.member_roles.each do |member_role| |
|
76 | 76 | user_member.member_roles << MemberRole.new(:role => member_role.role, :inherited_from => member_role.id) |
|
77 | 77 | end |
|
78 | 78 | user_member.save! |
|
79 | 79 | end |
|
80 | 80 | end |
|
81 | 81 | |
|
82 | 82 | def user_removed(user) |
|
83 | 83 | members.each do |member| |
|
84 | 84 | MemberRole. |
|
85 | 85 | joins(:member). |
|
86 | 86 | where("#{Member.table_name}.user_id = ? AND #{MemberRole.table_name}.inherited_from IN (?)", user.id, member.member_role_ids). |
|
87 | 87 | each(&:destroy) |
|
88 | 88 | end |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | def self.human_attribute_name(attribute_key_name, *args) |
|
92 | 92 | attr_name = attribute_key_name.to_s |
|
93 | 93 | if attr_name == 'lastname' |
|
94 | 94 | attr_name = "name" |
|
95 | 95 | end |
|
96 | 96 | super(attr_name, *args) |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def self.anonymous |
|
100 | 100 | GroupAnonymous.load_instance |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def self.non_member |
|
104 | 104 | GroupNonMember.load_instance |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | private |
|
108 | 108 | |
|
109 | 109 | # Removes references that are not handled by associations |
|
110 | 110 | def remove_references_before_destroy |
|
111 | 111 | return if self.id.nil? |
|
112 | 112 | |
|
113 | 113 | Issue.where(['assigned_to_id = ?', id]).update_all('assigned_to_id = NULL') |
|
114 | 114 | end |
|
115 | 115 | end |
|
116 | 116 | |
|
117 | 117 | require_dependency "group_builtin" |
@@ -1,161 +1,169 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 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.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class GroupTest < ActiveSupport::TestCase |
|
21 | 21 | fixtures :projects, :trackers, :issue_statuses, :issues, |
|
22 | 22 | :enumerations, :users, |
|
23 | 23 | :projects_trackers, |
|
24 | 24 | :roles, |
|
25 | 25 | :member_roles, |
|
26 | 26 | :members, |
|
27 | 27 | :groups_users |
|
28 | 28 | |
|
29 | 29 | include Redmine::I18n |
|
30 | 30 | |
|
31 | 31 | def test_create |
|
32 | 32 | g = Group.new(:name => 'New group') |
|
33 | 33 | assert g.save |
|
34 | 34 | g.reload |
|
35 | 35 | assert_equal 'New group', g.name |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def test_name_should_accept_255_characters |
|
39 | 39 | name = 'a' * 255 |
|
40 | 40 | g = Group.new(:name => name) |
|
41 | 41 | assert g.save |
|
42 | 42 | g.reload |
|
43 | 43 | assert_equal name, g.name |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def test_blank_name_error_message |
|
47 | 47 | set_language_if_valid 'en' |
|
48 | 48 | g = Group.new |
|
49 | 49 | assert !g.save |
|
50 | 50 | assert_include "Name cannot be blank", g.errors.full_messages |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def test_blank_name_error_message_fr |
|
54 | 54 | set_language_if_valid 'fr' |
|
55 | 55 | str = "Nom doit \xc3\xaatre renseign\xc3\xa9(e)".force_encoding('UTF-8') |
|
56 | 56 | g = Group.new |
|
57 | 57 | assert !g.save |
|
58 | 58 | assert_include str, g.errors.full_messages |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | def test_group_roles_should_be_given_to_added_user |
|
62 | 62 | group = Group.find(11) |
|
63 | 63 | user = User.find(9) |
|
64 | 64 | project = Project.first |
|
65 | 65 | |
|
66 | 66 | Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) |
|
67 | 67 | group.users << user |
|
68 | 68 | assert user.member_of?(project) |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def test_new_roles_should_be_given_to_existing_user |
|
72 | 72 | group = Group.find(11) |
|
73 | 73 | user = User.find(9) |
|
74 | 74 | project = Project.first |
|
75 | 75 | |
|
76 | 76 | group.users << user |
|
77 | 77 | m = Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) |
|
78 | 78 | assert user.member_of?(project) |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def test_user_roles_should_updated_when_updating_user_ids |
|
82 | 82 | group = Group.find(11) |
|
83 | 83 | user = User.find(9) |
|
84 | 84 | project = Project.first |
|
85 | 85 | |
|
86 | 86 | Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) |
|
87 | 87 | group.user_ids = [user.id] |
|
88 | 88 | group.save! |
|
89 | 89 | assert User.find(9).member_of?(project) |
|
90 | 90 | |
|
91 | 91 | group.user_ids = [1] |
|
92 | 92 | group.save! |
|
93 | 93 | assert !User.find(9).member_of?(project) |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def test_user_roles_should_updated_when_updating_group_roles |
|
97 | 97 | group = Group.find(11) |
|
98 | 98 | user = User.find(9) |
|
99 | 99 | project = Project.first |
|
100 | 100 | group.users << user |
|
101 | 101 | m = Member.create!(:principal => group, :project => project, :role_ids => [1]) |
|
102 | 102 | assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort |
|
103 | 103 | |
|
104 | 104 | m.role_ids = [1, 2] |
|
105 | 105 | assert_equal [1, 2], user.reload.roles_for_project(project).collect(&:id).sort |
|
106 | 106 | |
|
107 | 107 | m.role_ids = [2] |
|
108 | 108 | assert_equal [2], user.reload.roles_for_project(project).collect(&:id).sort |
|
109 | 109 | |
|
110 | 110 | m.role_ids = [1] |
|
111 | 111 | assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def test_user_memberships_should_be_removed_when_removing_group_membership |
|
115 | 115 | assert User.find(8).member_of?(Project.find(5)) |
|
116 | 116 | Member.find_by_project_id_and_user_id(5, 10).destroy |
|
117 | 117 | assert !User.find(8).member_of?(Project.find(5)) |
|
118 | 118 | end |
|
119 | 119 | |
|
120 | 120 | def test_user_roles_should_be_removed_when_removing_user_from_group |
|
121 | 121 | assert User.find(8).member_of?(Project.find(5)) |
|
122 | 122 | User.find(8).groups = [] |
|
123 | 123 | assert !User.find(8).member_of?(Project.find(5)) |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def test_destroy_should_unassign_issues |
|
127 | 127 | group = Group.find(10) |
|
128 | 128 | Issue.where(:id => 1).update_all(["assigned_to_id = ?", group.id]) |
|
129 | 129 | |
|
130 | 130 | assert group.destroy |
|
131 | 131 | assert group.destroyed? |
|
132 | 132 | |
|
133 | 133 | assert_equal nil, Issue.find(1).assigned_to_id |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | def test_builtin_groups_should_be_created_if_missing |
|
137 | 137 | Group.delete_all |
|
138 | 138 | |
|
139 | 139 | assert_difference 'Group.count', 2 do |
|
140 | 140 | group = Group.anonymous |
|
141 | 141 | assert_equal GroupAnonymous, group.class |
|
142 | 142 | |
|
143 | 143 | group = Group.non_member |
|
144 | 144 | assert_equal GroupNonMember, group.class |
|
145 | 145 | end |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def test_builtin_in_group_should_be_uniq |
|
149 | 149 | group = GroupAnonymous.new |
|
150 | 150 | group.name = 'Foo' |
|
151 | 151 | assert !group.save |
|
152 | 152 | end |
|
153 | 153 | |
|
154 | 154 | def test_builtin_in_group_should_not_accept_users |
|
155 | 155 | group = Group.anonymous |
|
156 | 156 | assert_raise RuntimeError do |
|
157 | 157 | group.users << User.find(1) |
|
158 | 158 | end |
|
159 | 159 | assert_equal 0, group.reload.users.count |
|
160 | 160 | end |
|
161 | ||
|
162 | def test_sorted_scope_should_sort_groups_alphabetically | |
|
163 | Group.delete_all | |
|
164 | b = Group.generate!(:name => 'B') | |
|
165 | a = Group.generate!(:name => 'A') | |
|
166 | ||
|
167 | assert_equal %w(A B), Group.sorted.to_a.map(&:name) | |
|
168 | end | |
|
161 | 169 | end |
General Comments 0
You need to be logged in to leave comments.
Login now