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