@@ -1,190 +1,190 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 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 | require_sudo_mode :create, :update, :destroy |
|
31 | require_sudo_mode :create, :update, :destroy | |
32 |
|
32 | |||
33 | def index |
|
33 | def index | |
34 | sort_init 'login', 'asc' |
|
34 | sort_init 'login', 'asc' | |
35 | sort_update %w(login firstname lastname admin created_on last_login_on) |
|
35 | sort_update %w(login firstname lastname admin created_on last_login_on) | |
36 |
|
36 | |||
37 | case params[:format] |
|
37 | case params[:format] | |
38 | when 'xml', 'json' |
|
38 | when 'xml', 'json' | |
39 | @offset, @limit = api_offset_and_limit |
|
39 | @offset, @limit = api_offset_and_limit | |
40 | else |
|
40 | else | |
41 | @limit = per_page_option |
|
41 | @limit = per_page_option | |
42 | end |
|
42 | end | |
43 |
|
43 | |||
44 | @status = params[:status] || 1 |
|
44 | @status = params[:status] || 1 | |
45 |
|
45 | |||
46 | scope = User.logged.status(@status).preload(:email_address) |
|
46 | scope = User.logged.status(@status).preload(:email_address) | |
47 | scope = scope.like(params[:name]) if params[:name].present? |
|
47 | scope = scope.like(params[:name]) if params[:name].present? | |
48 | scope = scope.in_group(params[:group_id]) if params[:group_id].present? |
|
48 | scope = scope.in_group(params[:group_id]) if params[:group_id].present? | |
49 |
|
49 | |||
50 | @user_count = scope.count |
|
50 | @user_count = scope.count | |
51 | @user_pages = Paginator.new @user_count, @limit, params['page'] |
|
51 | @user_pages = Paginator.new @user_count, @limit, params['page'] | |
52 | @offset ||= @user_pages.offset |
|
52 | @offset ||= @user_pages.offset | |
53 | @users = scope.order(sort_clause).limit(@limit).offset(@offset).to_a |
|
53 | @users = scope.order(sort_clause).limit(@limit).offset(@offset).to_a | |
54 |
|
54 | |||
55 | respond_to do |format| |
|
55 | respond_to do |format| | |
56 | format.html { |
|
56 | format.html { | |
57 |
@groups = Group. |
|
57 | @groups = Group.givable.sort | |
58 | render :layout => !request.xhr? |
|
58 | render :layout => !request.xhr? | |
59 | } |
|
59 | } | |
60 | format.api |
|
60 | format.api | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def show |
|
64 | def show | |
65 | unless @user.visible? |
|
65 | unless @user.visible? | |
66 | render_404 |
|
66 | render_404 | |
67 | return |
|
67 | return | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | # show projects based on current user visibility |
|
70 | # show projects based on current user visibility | |
71 | @memberships = @user.memberships.where(Project.visible_condition(User.current)).to_a |
|
71 | @memberships = @user.memberships.where(Project.visible_condition(User.current)).to_a | |
72 |
|
72 | |||
73 | respond_to do |format| |
|
73 | respond_to do |format| | |
74 | format.html { |
|
74 | format.html { | |
75 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
75 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) | |
76 | @events_by_day = events.group_by(&:event_date) |
|
76 | @events_by_day = events.group_by(&:event_date) | |
77 | render :layout => 'base' |
|
77 | render :layout => 'base' | |
78 | } |
|
78 | } | |
79 | format.api |
|
79 | format.api | |
80 | end |
|
80 | end | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | def new |
|
83 | def new | |
84 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
84 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
85 | @user.safe_attributes = params[:user] |
|
85 | @user.safe_attributes = params[:user] | |
86 | @auth_sources = AuthSource.all |
|
86 | @auth_sources = AuthSource.all | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | def create |
|
89 | def create | |
90 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
90 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
91 | @user.safe_attributes = params[:user] |
|
91 | @user.safe_attributes = params[:user] | |
92 | @user.admin = params[:user][:admin] || false |
|
92 | @user.admin = params[:user][:admin] || false | |
93 | @user.login = params[:user][:login] |
|
93 | @user.login = params[:user][:login] | |
94 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id |
|
94 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id | |
95 | @user.pref.attributes = params[:pref] if params[:pref] |
|
95 | @user.pref.attributes = params[:pref] if params[:pref] | |
96 |
|
96 | |||
97 | if @user.save |
|
97 | if @user.save | |
98 | Mailer.account_information(@user, @user.password).deliver if params[:send_information] |
|
98 | Mailer.account_information(@user, @user.password).deliver if params[:send_information] | |
99 |
|
99 | |||
100 | respond_to do |format| |
|
100 | respond_to do |format| | |
101 | format.html { |
|
101 | format.html { | |
102 | flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user))) |
|
102 | flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user))) | |
103 | if params[:continue] |
|
103 | if params[:continue] | |
104 | attrs = params[:user].slice(:generate_password) |
|
104 | attrs = params[:user].slice(:generate_password) | |
105 | redirect_to new_user_path(:user => attrs) |
|
105 | redirect_to new_user_path(:user => attrs) | |
106 | else |
|
106 | else | |
107 | redirect_to edit_user_path(@user) |
|
107 | redirect_to edit_user_path(@user) | |
108 | end |
|
108 | end | |
109 | } |
|
109 | } | |
110 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } |
|
110 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } | |
111 | end |
|
111 | end | |
112 | else |
|
112 | else | |
113 | @auth_sources = AuthSource.all |
|
113 | @auth_sources = AuthSource.all | |
114 | # Clear password input |
|
114 | # Clear password input | |
115 | @user.password = @user.password_confirmation = nil |
|
115 | @user.password = @user.password_confirmation = nil | |
116 |
|
116 | |||
117 | respond_to do |format| |
|
117 | respond_to do |format| | |
118 | format.html { render :action => 'new' } |
|
118 | format.html { render :action => 'new' } | |
119 | format.api { render_validation_errors(@user) } |
|
119 | format.api { render_validation_errors(@user) } | |
120 | end |
|
120 | end | |
121 | end |
|
121 | end | |
122 | end |
|
122 | end | |
123 |
|
123 | |||
124 | def edit |
|
124 | def edit | |
125 | @auth_sources = AuthSource.all |
|
125 | @auth_sources = AuthSource.all | |
126 | @membership ||= Member.new |
|
126 | @membership ||= Member.new | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | def update |
|
129 | def update | |
130 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
130 | @user.admin = params[:user][:admin] if params[:user][:admin] | |
131 | @user.login = params[:user][:login] if params[:user][:login] |
|
131 | @user.login = params[:user][:login] if params[:user][:login] | |
132 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
|
132 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) | |
133 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
|
133 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] | |
134 | end |
|
134 | end | |
135 | @user.safe_attributes = params[:user] |
|
135 | @user.safe_attributes = params[:user] | |
136 | # Was the account actived ? (do it before User#save clears the change) |
|
136 | # Was the account actived ? (do it before User#save clears the change) | |
137 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
|
137 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) | |
138 | # TODO: Similar to My#account |
|
138 | # TODO: Similar to My#account | |
139 | @user.pref.attributes = params[:pref] if params[:pref] |
|
139 | @user.pref.attributes = params[:pref] if params[:pref] | |
140 |
|
140 | |||
141 | if @user.save |
|
141 | if @user.save | |
142 | @user.pref.save |
|
142 | @user.pref.save | |
143 |
|
143 | |||
144 | if was_activated |
|
144 | if was_activated | |
145 | Mailer.account_activated(@user).deliver |
|
145 | Mailer.account_activated(@user).deliver | |
146 | elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil? && @user != User.current |
|
146 | elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil? && @user != User.current | |
147 | Mailer.account_information(@user, @user.password).deliver |
|
147 | Mailer.account_information(@user, @user.password).deliver | |
148 | end |
|
148 | end | |
149 |
|
149 | |||
150 | respond_to do |format| |
|
150 | respond_to do |format| | |
151 | format.html { |
|
151 | format.html { | |
152 | flash[:notice] = l(:notice_successful_update) |
|
152 | flash[:notice] = l(:notice_successful_update) | |
153 | redirect_to_referer_or edit_user_path(@user) |
|
153 | redirect_to_referer_or edit_user_path(@user) | |
154 | } |
|
154 | } | |
155 | format.api { render_api_ok } |
|
155 | format.api { render_api_ok } | |
156 | end |
|
156 | end | |
157 | else |
|
157 | else | |
158 | @auth_sources = AuthSource.all |
|
158 | @auth_sources = AuthSource.all | |
159 | @membership ||= Member.new |
|
159 | @membership ||= Member.new | |
160 | # Clear password input |
|
160 | # Clear password input | |
161 | @user.password = @user.password_confirmation = nil |
|
161 | @user.password = @user.password_confirmation = nil | |
162 |
|
162 | |||
163 | respond_to do |format| |
|
163 | respond_to do |format| | |
164 | format.html { render :action => :edit } |
|
164 | format.html { render :action => :edit } | |
165 | format.api { render_validation_errors(@user) } |
|
165 | format.api { render_validation_errors(@user) } | |
166 | end |
|
166 | end | |
167 | end |
|
167 | end | |
168 | end |
|
168 | end | |
169 |
|
169 | |||
170 | def destroy |
|
170 | def destroy | |
171 | @user.destroy |
|
171 | @user.destroy | |
172 | respond_to do |format| |
|
172 | respond_to do |format| | |
173 | format.html { redirect_back_or_default(users_path) } |
|
173 | format.html { redirect_back_or_default(users_path) } | |
174 | format.api { render_api_ok } |
|
174 | format.api { render_api_ok } | |
175 | end |
|
175 | end | |
176 | end |
|
176 | end | |
177 |
|
177 | |||
178 | private |
|
178 | private | |
179 |
|
179 | |||
180 | def find_user |
|
180 | def find_user | |
181 | if params[:id] == 'current' |
|
181 | if params[:id] == 'current' | |
182 | require_login || return |
|
182 | require_login || return | |
183 | @user = User.current |
|
183 | @user = User.current | |
184 | else |
|
184 | else | |
185 | @user = User.find(params[:id]) |
|
185 | @user = User.find(params[:id]) | |
186 | end |
|
186 | end | |
187 | rescue ActiveRecord::RecordNotFound |
|
187 | rescue ActiveRecord::RecordNotFound | |
188 | render_404 |
|
188 | render_404 | |
189 | end |
|
189 | end | |
190 | end |
|
190 | end |
General Comments 0
You need to be logged in to leave comments.
Login now