##// END OF EJS Templates
Merged r14140 (#19161)....
Jean-Philippe Lang -
r13767:5eb1b2626de8
parent child
Show More
@@ -1,188 +1,188
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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 UsersController < ApplicationController
18 class UsersController < ApplicationController
19 layout 'admin'
19 layout 'admin'
20
20
21 before_filter :require_admin, :except => :show
21 before_filter :require_admin, :except => :show
22 before_filter :find_user, :only => [:show, :edit, :update, :destroy]
22 before_filter :find_user, :only => [:show, :edit, :update, :destroy]
23 accept_api_auth :index, :show, :create, :update, :destroy
23 accept_api_auth :index, :show, :create, :update, :destroy
24
24
25 helper :sort
25 helper :sort
26 include SortHelper
26 include SortHelper
27 helper :custom_fields
27 helper :custom_fields
28 include CustomFieldsHelper
28 include CustomFieldsHelper
29 helper :principal_memberships
29 helper :principal_memberships
30
30
31 def index
31 def index
32 sort_init 'login', 'asc'
32 sort_init 'login', 'asc'
33 sort_update %w(login firstname lastname mail admin created_on last_login_on)
33 sort_update %w(login firstname lastname admin created_on last_login_on)
34
34
35 case params[:format]
35 case params[:format]
36 when 'xml', 'json'
36 when 'xml', 'json'
37 @offset, @limit = api_offset_and_limit
37 @offset, @limit = api_offset_and_limit
38 else
38 else
39 @limit = per_page_option
39 @limit = per_page_option
40 end
40 end
41
41
42 @status = params[:status] || 1
42 @status = params[:status] || 1
43
43
44 scope = User.logged.status(@status).preload(:email_address)
44 scope = User.logged.status(@status).preload(:email_address)
45 scope = scope.like(params[:name]) if params[:name].present?
45 scope = scope.like(params[:name]) if params[:name].present?
46 scope = scope.in_group(params[:group_id]) if params[:group_id].present?
46 scope = scope.in_group(params[:group_id]) if params[:group_id].present?
47
47
48 @user_count = scope.count
48 @user_count = scope.count
49 @user_pages = Paginator.new @user_count, @limit, params['page']
49 @user_pages = Paginator.new @user_count, @limit, params['page']
50 @offset ||= @user_pages.offset
50 @offset ||= @user_pages.offset
51 @users = scope.order(sort_clause).limit(@limit).offset(@offset).to_a
51 @users = scope.order(sort_clause).limit(@limit).offset(@offset).to_a
52
52
53 respond_to do |format|
53 respond_to do |format|
54 format.html {
54 format.html {
55 @groups = Group.all.sort
55 @groups = Group.all.sort
56 render :layout => !request.xhr?
56 render :layout => !request.xhr?
57 }
57 }
58 format.api
58 format.api
59 end
59 end
60 end
60 end
61
61
62 def show
62 def show
63 unless @user.visible?
63 unless @user.visible?
64 render_404
64 render_404
65 return
65 return
66 end
66 end
67
67
68 # show projects based on current user visibility
68 # show projects based on current user visibility
69 @memberships = @user.memberships.where(Project.visible_condition(User.current)).to_a
69 @memberships = @user.memberships.where(Project.visible_condition(User.current)).to_a
70
70
71 respond_to do |format|
71 respond_to do |format|
72 format.html {
72 format.html {
73 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
73 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
74 @events_by_day = events.group_by(&:event_date)
74 @events_by_day = events.group_by(&:event_date)
75 render :layout => 'base'
75 render :layout => 'base'
76 }
76 }
77 format.api
77 format.api
78 end
78 end
79 end
79 end
80
80
81 def new
81 def new
82 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
82 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
83 @user.safe_attributes = params[:user]
83 @user.safe_attributes = params[:user]
84 @auth_sources = AuthSource.all
84 @auth_sources = AuthSource.all
85 end
85 end
86
86
87 def create
87 def create
88 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
88 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
89 @user.safe_attributes = params[:user]
89 @user.safe_attributes = params[:user]
90 @user.admin = params[:user][:admin] || false
90 @user.admin = params[:user][:admin] || false
91 @user.login = params[:user][:login]
91 @user.login = params[:user][:login]
92 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
92 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
93 @user.pref.attributes = params[:pref] if params[:pref]
93 @user.pref.attributes = params[:pref] if params[:pref]
94
94
95 if @user.save
95 if @user.save
96 Mailer.account_information(@user, @user.password).deliver if params[:send_information]
96 Mailer.account_information(@user, @user.password).deliver if params[:send_information]
97
97
98 respond_to do |format|
98 respond_to do |format|
99 format.html {
99 format.html {
100 flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
100 flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
101 if params[:continue]
101 if params[:continue]
102 attrs = params[:user].slice(:generate_password)
102 attrs = params[:user].slice(:generate_password)
103 redirect_to new_user_path(:user => attrs)
103 redirect_to new_user_path(:user => attrs)
104 else
104 else
105 redirect_to edit_user_path(@user)
105 redirect_to edit_user_path(@user)
106 end
106 end
107 }
107 }
108 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
108 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
109 end
109 end
110 else
110 else
111 @auth_sources = AuthSource.all
111 @auth_sources = AuthSource.all
112 # Clear password input
112 # Clear password input
113 @user.password = @user.password_confirmation = nil
113 @user.password = @user.password_confirmation = nil
114
114
115 respond_to do |format|
115 respond_to do |format|
116 format.html { render :action => 'new' }
116 format.html { render :action => 'new' }
117 format.api { render_validation_errors(@user) }
117 format.api { render_validation_errors(@user) }
118 end
118 end
119 end
119 end
120 end
120 end
121
121
122 def edit
122 def edit
123 @auth_sources = AuthSource.all
123 @auth_sources = AuthSource.all
124 @membership ||= Member.new
124 @membership ||= Member.new
125 end
125 end
126
126
127 def update
127 def update
128 @user.admin = params[:user][:admin] if params[:user][:admin]
128 @user.admin = params[:user][:admin] if params[:user][:admin]
129 @user.login = params[:user][:login] if params[:user][:login]
129 @user.login = params[:user][:login] if params[:user][:login]
130 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
130 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
131 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
131 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
132 end
132 end
133 @user.safe_attributes = params[:user]
133 @user.safe_attributes = params[:user]
134 # Was the account actived ? (do it before User#save clears the change)
134 # Was the account actived ? (do it before User#save clears the change)
135 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
135 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
136 # TODO: Similar to My#account
136 # TODO: Similar to My#account
137 @user.pref.attributes = params[:pref] if params[:pref]
137 @user.pref.attributes = params[:pref] if params[:pref]
138
138
139 if @user.save
139 if @user.save
140 @user.pref.save
140 @user.pref.save
141
141
142 if was_activated
142 if was_activated
143 Mailer.account_activated(@user).deliver
143 Mailer.account_activated(@user).deliver
144 elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil?
144 elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil?
145 Mailer.account_information(@user, @user.password).deliver
145 Mailer.account_information(@user, @user.password).deliver
146 end
146 end
147
147
148 respond_to do |format|
148 respond_to do |format|
149 format.html {
149 format.html {
150 flash[:notice] = l(:notice_successful_update)
150 flash[:notice] = l(:notice_successful_update)
151 redirect_to_referer_or edit_user_path(@user)
151 redirect_to_referer_or edit_user_path(@user)
152 }
152 }
153 format.api { render_api_ok }
153 format.api { render_api_ok }
154 end
154 end
155 else
155 else
156 @auth_sources = AuthSource.all
156 @auth_sources = AuthSource.all
157 @membership ||= Member.new
157 @membership ||= Member.new
158 # Clear password input
158 # Clear password input
159 @user.password = @user.password_confirmation = nil
159 @user.password = @user.password_confirmation = nil
160
160
161 respond_to do |format|
161 respond_to do |format|
162 format.html { render :action => :edit }
162 format.html { render :action => :edit }
163 format.api { render_validation_errors(@user) }
163 format.api { render_validation_errors(@user) }
164 end
164 end
165 end
165 end
166 end
166 end
167
167
168 def destroy
168 def destroy
169 @user.destroy
169 @user.destroy
170 respond_to do |format|
170 respond_to do |format|
171 format.html { redirect_back_or_default(users_path) }
171 format.html { redirect_back_or_default(users_path) }
172 format.api { render_api_ok }
172 format.api { render_api_ok }
173 end
173 end
174 end
174 end
175
175
176 private
176 private
177
177
178 def find_user
178 def find_user
179 if params[:id] == 'current'
179 if params[:id] == 'current'
180 require_login || return
180 require_login || return
181 @user = User.current
181 @user = User.current
182 else
182 else
183 @user = User.find(params[:id])
183 @user = User.find(params[:id])
184 end
184 end
185 rescue ActiveRecord::RecordNotFound
185 rescue ActiveRecord::RecordNotFound
186 render_404
186 render_404
187 end
187 end
188 end
188 end
@@ -1,58 +1,58
1 <div class="contextual">
1 <div class="contextual">
2 <%= link_to l(:label_user_new), new_user_path, :class => 'icon icon-add' %>
2 <%= link_to l(:label_user_new), new_user_path, :class => 'icon icon-add' %>
3 </div>
3 </div>
4
4
5 <h2><%=l(:label_user_plural)%></h2>
5 <h2><%=l(:label_user_plural)%></h2>
6
6
7 <%= form_tag(users_path, :method => :get) do %>
7 <%= form_tag(users_path, :method => :get) do %>
8 <fieldset><legend><%= l(:label_filter_plural) %></legend>
8 <fieldset><legend><%= l(:label_filter_plural) %></legend>
9 <label for='status'><%= l(:field_status) %>:</label>
9 <label for='status'><%= l(:field_status) %>:</label>
10 <%= select_tag 'status', users_status_options_for_select(@status), :class => "small", :onchange => "this.form.submit(); return false;" %>
10 <%= select_tag 'status', users_status_options_for_select(@status), :class => "small", :onchange => "this.form.submit(); return false;" %>
11
11
12 <% if @groups.present? %>
12 <% if @groups.present? %>
13 <label for='group_id'><%= l(:label_group) %>:</label>
13 <label for='group_id'><%= l(:label_group) %>:</label>
14 <%= select_tag 'group_id', content_tag('option') + options_from_collection_for_select(@groups, :id, :name, params[:group_id].to_i), :onchange => "this.form.submit(); return false;" %>
14 <%= select_tag 'group_id', content_tag('option') + options_from_collection_for_select(@groups, :id, :name, params[:group_id].to_i), :onchange => "this.form.submit(); return false;" %>
15 <% end %>
15 <% end %>
16
16
17 <label for='name'><%= l(:label_user) %>:</label>
17 <label for='name'><%= l(:label_user) %>:</label>
18 <%= text_field_tag 'name', params[:name], :size => 30 %>
18 <%= text_field_tag 'name', params[:name], :size => 30 %>
19 <%= submit_tag l(:button_apply), :class => "small", :name => nil %>
19 <%= submit_tag l(:button_apply), :class => "small", :name => nil %>
20 <%= link_to l(:button_clear), users_path, :class => 'icon icon-reload' %>
20 <%= link_to l(:button_clear), users_path, :class => 'icon icon-reload' %>
21 </fieldset>
21 </fieldset>
22 <% end %>
22 <% end %>
23 &nbsp;
23 &nbsp;
24
24
25 <div class="autoscroll">
25 <div class="autoscroll">
26 <table class="list">
26 <table class="list">
27 <thead><tr>
27 <thead><tr>
28 <%= sort_header_tag('login', :caption => l(:field_login)) %>
28 <%= sort_header_tag('login', :caption => l(:field_login)) %>
29 <%= sort_header_tag('firstname', :caption => l(:field_firstname)) %>
29 <%= sort_header_tag('firstname', :caption => l(:field_firstname)) %>
30 <%= sort_header_tag('lastname', :caption => l(:field_lastname)) %>
30 <%= sort_header_tag('lastname', :caption => l(:field_lastname)) %>
31 <%= sort_header_tag('mail', :caption => l(:field_mail)) %>
31 <th><%= l(:field_mail) %></th>
32 <%= sort_header_tag('admin', :caption => l(:field_admin), :default_order => 'desc') %>
32 <%= sort_header_tag('admin', :caption => l(:field_admin), :default_order => 'desc') %>
33 <%= sort_header_tag('created_on', :caption => l(:field_created_on), :default_order => 'desc') %>
33 <%= sort_header_tag('created_on', :caption => l(:field_created_on), :default_order => 'desc') %>
34 <%= sort_header_tag('last_login_on', :caption => l(:field_last_login_on), :default_order => 'desc') %>
34 <%= sort_header_tag('last_login_on', :caption => l(:field_last_login_on), :default_order => 'desc') %>
35 <th></th>
35 <th></th>
36 </tr></thead>
36 </tr></thead>
37 <tbody>
37 <tbody>
38 <% for user in @users -%>
38 <% for user in @users -%>
39 <tr class="<%= user.css_classes %> <%= cycle("odd", "even") %>">
39 <tr class="<%= user.css_classes %> <%= cycle("odd", "even") %>">
40 <td class="username"><%= avatar(user, :size => "14") %><%= link_to h(user.login), edit_user_path(user) %></td>
40 <td class="username"><%= avatar(user, :size => "14") %><%= link_to h(user.login), edit_user_path(user) %></td>
41 <td class="firstname"><%= h(user.firstname) %></td>
41 <td class="firstname"><%= h(user.firstname) %></td>
42 <td class="lastname"><%= h(user.lastname) %></td>
42 <td class="lastname"><%= h(user.lastname) %></td>
43 <td class="email"><%= mail_to(h(user.mail)) %></td>
43 <td class="email"><%= mail_to(h(user.mail)) %></td>
44 <td class="tick"><%= checked_image user.admin? %></td>
44 <td class="tick"><%= checked_image user.admin? %></td>
45 <td class="created_on"><%= format_time(user.created_on) %></td>
45 <td class="created_on"><%= format_time(user.created_on) %></td>
46 <td class="last_login_on"><%= format_time(user.last_login_on) unless user.last_login_on.nil? %></td>
46 <td class="last_login_on"><%= format_time(user.last_login_on) unless user.last_login_on.nil? %></td>
47 <td class="buttons">
47 <td class="buttons">
48 <%= change_status_link(user) %>
48 <%= change_status_link(user) %>
49 <%= delete_link user_path(user, :back_url => request.original_fullpath) unless User.current == user %>
49 <%= delete_link user_path(user, :back_url => request.original_fullpath) unless User.current == user %>
50 </td>
50 </td>
51 </tr>
51 </tr>
52 <% end -%>
52 <% end -%>
53 </tbody>
53 </tbody>
54 </table>
54 </table>
55 </div>
55 </div>
56 <p class="pagination"><%= pagination_links_full @user_pages, @user_count %></p>
56 <p class="pagination"><%= pagination_links_full @user_pages, @user_count %></p>
57
57
58 <% html_title(l(:label_user_plural)) -%>
58 <% html_title(l(:label_user_plural)) -%>
General Comments 0
You need to be logged in to leave comments. Login now