@@ -1,103 +1,103 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 MembersController < ApplicationController |
|
19 | 19 | model_object Member |
|
20 | 20 | before_filter :find_model_object, :except => [:new, :autocomplete_for_member] |
|
21 | 21 | before_filter :find_project_from_association, :except => [:new, :autocomplete_for_member] |
|
22 | 22 | before_filter :find_project, :only => [:new, :autocomplete_for_member] |
|
23 | 23 | before_filter :authorize |
|
24 | 24 | |
|
25 | 25 | def new |
|
26 | 26 | members = [] |
|
27 | 27 | if params[:member] && request.post? |
|
28 | 28 | attrs = params[:member].dup |
|
29 | 29 | if (user_ids = attrs.delete(:user_ids)) |
|
30 | 30 | user_ids.each do |user_id| |
|
31 | 31 | members << Member.new(:role_ids => params[:member][:role_ids], :user_id => user_id) |
|
32 | 32 | end |
|
33 | 33 | else |
|
34 | 34 | members << Member.new(:role_ids => params[:member][:role_ids], :user_id => params[:member][:user_id]) |
|
35 | 35 | end |
|
36 | 36 | @project.members << members |
|
37 | 37 | end |
|
38 | 38 | respond_to do |format| |
|
39 | 39 | if members.present? && members.all? {|m| m.valid? } |
|
40 | 40 | |
|
41 | 41 | format.html { redirect_to :controller => 'projects', :action => 'settings', :tab => 'members', :id => @project } |
|
42 | 42 | |
|
43 | 43 | format.js { |
|
44 | 44 | render(:update) {|page| |
|
45 | 45 | page.replace_html "tab-content-members", :partial => 'projects/settings/members' |
|
46 | 46 | page << 'hideOnLoad()' |
|
47 | 47 | members.each {|member| page.visual_effect(:highlight, "member-#{member.id}") } |
|
48 | 48 | } |
|
49 | 49 | } |
|
50 | 50 | else |
|
51 | 51 | |
|
52 | 52 | format.js { |
|
53 | 53 | render(:update) {|page| |
|
54 | 54 | errors = members.collect {|m| |
|
55 | 55 | m.errors.full_messages |
|
56 | 56 | }.flatten.uniq |
|
57 | 57 | |
|
58 | 58 | page.alert(l(:notice_failed_to_save_members, :errors => errors.join(', '))) |
|
59 | 59 | } |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | end |
|
63 | 63 | end |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def edit |
|
67 | 67 | if params[:member] |
|
68 | 68 | @member.role_ids = params[:member][:role_ids] |
|
69 | 69 | end |
|
70 | 70 | if request.post? and @member.save |
|
71 | 71 | respond_to do |format| |
|
72 | 72 | format.html { redirect_to :controller => 'projects', :action => 'settings', :tab => 'members', :id => @project } |
|
73 | 73 | format.js { |
|
74 | 74 | render(:update) {|page| |
|
75 | 75 | page.replace_html "tab-content-members", :partial => 'projects/settings/members' |
|
76 | 76 | page << 'hideOnLoad()' |
|
77 | 77 | page.visual_effect(:highlight, "member-#{@member.id}") |
|
78 | 78 | } |
|
79 | 79 | } |
|
80 | 80 | end |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def destroy |
|
85 | 85 | if request.post? && @member.deletable? |
|
86 | 86 | @member.destroy |
|
87 | 87 | end |
|
88 | 88 | respond_to do |format| |
|
89 | 89 | format.html { redirect_to :controller => 'projects', :action => 'settings', :tab => 'members', :id => @project } |
|
90 | 90 | format.js { render(:update) {|page| |
|
91 | 91 | page.replace_html "tab-content-members", :partial => 'projects/settings/members' |
|
92 | 92 | page << 'hideOnLoad()' |
|
93 | 93 | } |
|
94 | 94 | } |
|
95 | 95 | end |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | def autocomplete_for_member |
|
99 |
@principals = Principal.active.like(params[:q]). |
|
|
99 | @principals = Principal.active.not_member_of(@project).like(params[:q]).all(:limit => 100) | |
|
100 | 100 | render :layout => false |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | end |
@@ -1,64 +1,74 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 Principal < ActiveRecord::Base |
|
19 | 19 | set_table_name "#{table_name_prefix}users#{table_name_suffix}" |
|
20 | 20 | |
|
21 | 21 | has_many :members, :foreign_key => 'user_id', :dependent => :destroy |
|
22 | 22 | has_many :memberships, :class_name => 'Member', :foreign_key => 'user_id', :include => [ :project, :roles ], :conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}", :order => "#{Project.table_name}.name" |
|
23 | 23 | has_many :projects, :through => :memberships |
|
24 | 24 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify |
|
25 | 25 | |
|
26 | 26 | # Groups and active users |
|
27 | 27 | named_scope :active, :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status = 1)" |
|
28 | 28 | |
|
29 | 29 | named_scope :like, lambda {|q| |
|
30 | 30 | s = "%#{q.to_s.strip.downcase}%" |
|
31 | 31 | {:conditions => ["LOWER(login) LIKE :s OR LOWER(firstname) LIKE :s OR LOWER(lastname) LIKE :s OR LOWER(mail) LIKE :s", {:s => s}], |
|
32 | 32 | :order => 'type, login, lastname, firstname, mail' |
|
33 | 33 | } |
|
34 | 34 | } |
|
35 | # Principals that are not members of projects | |
|
36 | named_scope :not_member_of, lambda {|projects| | |
|
37 | projects = [projects] unless projects.is_a?(Array) | |
|
38 | if projects.empty? | |
|
39 | {:conditions => "1=0"} | |
|
40 | else | |
|
41 | ids = projects.map(&:id) | |
|
42 | {:conditions => ["#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]} | |
|
43 | end | |
|
44 | } | |
|
35 | 45 | |
|
36 | 46 | before_create :set_default_empty_values |
|
37 | 47 | |
|
38 | 48 | def name(formatter = nil) |
|
39 | 49 | to_s |
|
40 | 50 | end |
|
41 | 51 | |
|
42 | 52 | def <=>(principal) |
|
43 | 53 | if principal.nil? |
|
44 | 54 | -1 |
|
45 | 55 | elsif self.class.name == principal.class.name |
|
46 | 56 | self.to_s.downcase <=> principal.to_s.downcase |
|
47 | 57 | else |
|
48 | 58 | # groups after users |
|
49 | 59 | principal.class.name <=> self.class.name |
|
50 | 60 | end |
|
51 | 61 | end |
|
52 | 62 | |
|
53 | 63 | protected |
|
54 | 64 | |
|
55 | 65 | # Make sure we don't try to insert NULL values (see #4632) |
|
56 | 66 | def set_default_empty_values |
|
57 | 67 | self.login ||= '' |
|
58 | 68 | self.hashed_password ||= '' |
|
59 | 69 | self.firstname ||= '' |
|
60 | 70 | self.lastname ||= '' |
|
61 | 71 | self.mail ||= '' |
|
62 | 72 | true |
|
63 | 73 | end |
|
64 | 74 | end |
@@ -1,83 +1,83 | |||
|
1 | 1 | <%= error_messages_for 'member' %> |
|
2 | 2 | <% roles = Role.find_all_givable |
|
3 | 3 | members = @project.member_principals.find(:all, :include => [:roles, :principal]).sort %> |
|
4 | 4 | |
|
5 | 5 | <div class="splitcontentleft"> |
|
6 | 6 | <% if members.any? %> |
|
7 | 7 | <table class="list members"> |
|
8 | 8 | <thead><tr> |
|
9 | 9 | <th><%= l(:label_user) %> / <%= l(:label_group) %></th> |
|
10 | 10 | <th><%= l(:label_role_plural) %></th> |
|
11 | 11 | <th style="width:15%"></th> |
|
12 | 12 | <%= call_hook(:view_projects_settings_members_table_header, :project => @project) %> |
|
13 | 13 | </tr></thead> |
|
14 | 14 | <tbody> |
|
15 | 15 | <% members.each do |member| %> |
|
16 | 16 | <% next if member.new_record? %> |
|
17 | 17 | <tr id="member-<%= member.id %>" class="<%= cycle 'odd', 'even' %> member"> |
|
18 | 18 | <td class="<%= member.principal.class.name.downcase %>"><%= link_to_user member.principal %></td> |
|
19 | 19 | <td class="roles"> |
|
20 | 20 | <span id="member-<%= member.id %>-roles"><%=h member.roles.sort.collect(&:to_s).join(', ') %></span> |
|
21 | 21 | <% if authorize_for('members', 'edit') %> |
|
22 | 22 | <% remote_form_for(:member, member, :url => {:controller => 'members', :action => 'edit', :id => member}, |
|
23 | 23 | :method => :post, |
|
24 | 24 | :html => { :id => "member-#{member.id}-roles-form", :class => 'hol' }) do |f| %> |
|
25 | 25 | <p><% roles.each do |role| %> |
|
26 | 26 | <label><%= check_box_tag 'member[role_ids][]', role.id, member.roles.include?(role), |
|
27 | 27 | :disabled => member.member_roles.detect {|mr| mr.role_id == role.id && !mr.inherited_from.nil?} %> <%=h role %></label><br /> |
|
28 | 28 | <% end %></p> |
|
29 | 29 | <%= hidden_field_tag 'member[role_ids][]', '' %> |
|
30 | 30 | <p><%= submit_tag l(:button_change), :class => "small" %> |
|
31 | 31 | <%= link_to_function l(:button_cancel), "$('member-#{member.id}-roles').show(); $('member-#{member.id}-roles-form').hide(); return false;" %></p> |
|
32 | 32 | <% end %> |
|
33 | 33 | <% end %> |
|
34 | 34 | </td> |
|
35 | 35 | <td class="buttons"> |
|
36 | 36 | <%= link_to_function l(:button_edit), "$('member-#{member.id}-roles').hide(); $('member-#{member.id}-roles-form').show(); return false;", :class => 'icon icon-edit' %> |
|
37 | 37 | <%= link_to_remote(l(:button_delete), { :url => {:controller => 'members', :action => 'destroy', :id => member}, |
|
38 | 38 | :method => :post, |
|
39 | 39 | :confirm => (!User.current.admin? && member.include?(User.current) ? l(:text_own_membership_delete_confirmation) : nil) |
|
40 | 40 | }, :title => l(:button_delete), |
|
41 | 41 | :class => 'icon icon-del') if member.deletable? %> |
|
42 | 42 | </td> |
|
43 | 43 | <%= call_hook(:view_projects_settings_members_table_row, { :project => @project, :member => member}) %> |
|
44 | 44 | </tr> |
|
45 | 45 | <% end; reset_cycle %> |
|
46 | 46 | </tbody> |
|
47 | 47 | </table> |
|
48 | 48 | <% else %> |
|
49 | 49 | <p class="nodata"><%= l(:label_no_data) %></p> |
|
50 | 50 | <% end %> |
|
51 | 51 | </div> |
|
52 | 52 | |
|
53 |
<% principals = Principal.active. |
|
|
53 | <% principals = Principal.active.not_member_of(@project).all(:limit => 100, :order => 'type, login, lastname ASC') %> | |
|
54 | 54 | |
|
55 | 55 | <div class="splitcontentright"> |
|
56 | 56 | <% if roles.any? && principals.any? %> |
|
57 | 57 | <% remote_form_for(:member, @member, :url => {:controller => 'members', :action => 'new', :id => @project}, :method => :post, |
|
58 | 58 | :loading => '$(\'member-add-submit\').disable();', |
|
59 | 59 | :complete => 'if($(\'member-add-submit\')) $(\'member-add-submit\').enable();') do |f| %> |
|
60 | 60 | <fieldset><legend><%=l(:label_member_new)%></legend> |
|
61 | 61 | |
|
62 | 62 | <p><%= label_tag "principal_search", l(:label_principal_search) %><%= text_field_tag 'principal_search', nil %></p> |
|
63 | 63 | <%= observe_field(:principal_search, |
|
64 | 64 | :frequency => 0.5, |
|
65 | 65 | :update => :principals, |
|
66 | 66 | :url => { :controller => 'members', :action => 'autocomplete_for_member', :id => @project }, |
|
67 | 67 | :with => 'q') |
|
68 | 68 | %> |
|
69 | 69 | |
|
70 | 70 | <div id="principals"> |
|
71 | 71 | <%= principals_check_box_tags 'member[user_ids][]', principals %> |
|
72 | 72 | </div> |
|
73 | 73 | |
|
74 | 74 | <p><%= l(:label_role_plural) %>: |
|
75 | 75 | <% roles.each do |role| %> |
|
76 | 76 | <label><%= check_box_tag 'member[role_ids][]', role.id %> <%=h role %></label> |
|
77 | 77 | <% end %></p> |
|
78 | 78 | |
|
79 | 79 | <p><%= submit_tag l(:button_add), :id => 'member-add-submit' %></p> |
|
80 | 80 | </fieldset> |
|
81 | 81 | <% end %> |
|
82 | 82 | <% end %> |
|
83 | 83 | </div> |
@@ -1,66 +1,73 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 PrincipalTest < ActiveSupport::TestCase |
|
21 | fixtures :users, :projects, :members, :member_roles | |
|
22 | ||
|
23 | def test_not_member_of_scope_should_return_users_that_have_no_memberships | |
|
24 | projects = Project.find_all_by_id(1, 2) | |
|
25 | expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort | |
|
26 | assert_equal expected, Principal.not_member_of(projects).sort | |
|
27 | end | |
|
21 | 28 | |
|
22 | 29 | context "#like" do |
|
23 | 30 | setup do |
|
24 | 31 | Principal.generate!(:login => 'login') |
|
25 | 32 | Principal.generate!(:login => 'login2') |
|
26 | 33 | |
|
27 | 34 | Principal.generate!(:firstname => 'firstname') |
|
28 | 35 | Principal.generate!(:firstname => 'firstname2') |
|
29 | 36 | |
|
30 | 37 | Principal.generate!(:lastname => 'lastname') |
|
31 | 38 | Principal.generate!(:lastname => 'lastname2') |
|
32 | 39 | |
|
33 | 40 | Principal.generate!(:mail => 'mail@example.com') |
|
34 | 41 | Principal.generate!(:mail => 'mail2@example.com') |
|
35 | 42 | end |
|
36 | 43 | |
|
37 | 44 | should "search login" do |
|
38 | 45 | results = Principal.like('login') |
|
39 | 46 | |
|
40 | 47 | assert_equal 2, results.count |
|
41 | 48 | assert results.all? {|u| u.login.match(/login/) } |
|
42 | 49 | end |
|
43 | 50 | |
|
44 | 51 | should "search firstname" do |
|
45 | 52 | results = Principal.like('firstname') |
|
46 | 53 | |
|
47 | 54 | assert_equal 2, results.count |
|
48 | 55 | assert results.all? {|u| u.firstname.match(/firstname/) } |
|
49 | 56 | end |
|
50 | 57 | |
|
51 | 58 | should "search lastname" do |
|
52 | 59 | results = Principal.like('lastname') |
|
53 | 60 | |
|
54 | 61 | assert_equal 2, results.count |
|
55 | 62 | assert results.all? {|u| u.lastname.match(/lastname/) } |
|
56 | 63 | end |
|
57 | 64 | |
|
58 | 65 | should "search mail" do |
|
59 | 66 | results = Principal.like('mail') |
|
60 | 67 | |
|
61 | 68 | assert_equal 2, results.count |
|
62 | 69 | assert results.all? {|u| u.mail.match(/mail/) } |
|
63 | 70 | end |
|
64 | 71 | end |
|
65 | 72 | |
|
66 | 73 | end |
General Comments 0
You need to be logged in to leave comments.
Login now