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