Mercurial > hg > soundsoftware-site
comparison .svn/pristine/ae/ae59ba764b863f990d0884c0c55bada9eb65b729.svn-base @ 1464:261b3d9a4903 redmine-2.4
Update to Redmine 2.4 branch rev 12663
author | Chris Cannam |
---|---|
date | Tue, 14 Jan 2014 14:37:42 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1296:038ba2d95de8 | 1464:261b3d9a4903 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2013 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
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 | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 class UsersController < ApplicationController | |
19 layout 'admin' | |
20 | |
21 before_filter :require_admin, :except => :show | |
22 before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] | |
23 accept_api_auth :index, :show, :create, :update, :destroy | |
24 | |
25 helper :sort | |
26 include SortHelper | |
27 helper :custom_fields | |
28 include CustomFieldsHelper | |
29 | |
30 def index | |
31 sort_init 'login', 'asc' | |
32 sort_update %w(login firstname lastname mail admin created_on last_login_on) | |
33 | |
34 case params[:format] | |
35 when 'xml', 'json' | |
36 @offset, @limit = api_offset_and_limit | |
37 else | |
38 @limit = per_page_option | |
39 end | |
40 | |
41 @status = params[:status] || 1 | |
42 | |
43 scope = User.logged.status(@status) | |
44 scope = scope.like(params[:name]) if params[:name].present? | |
45 scope = scope.in_group(params[:group_id]) if params[:group_id].present? | |
46 | |
47 @user_count = scope.count | |
48 @user_pages = Paginator.new @user_count, @limit, params['page'] | |
49 @offset ||= @user_pages.offset | |
50 @users = scope.order(sort_clause).limit(@limit).offset(@offset).all | |
51 | |
52 respond_to do |format| | |
53 format.html { | |
54 @groups = Group.all.sort | |
55 render :layout => !request.xhr? | |
56 } | |
57 format.api | |
58 end | |
59 end | |
60 | |
61 def show | |
62 # show projects based on current user visibility | |
63 @memberships = @user.memberships.where(Project.visible_condition(User.current)).all | |
64 | |
65 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) | |
66 @events_by_day = events.group_by(&:event_date) | |
67 | |
68 unless User.current.admin? | |
69 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) | |
70 render_404 | |
71 return | |
72 end | |
73 end | |
74 | |
75 respond_to do |format| | |
76 format.html { render :layout => 'base' } | |
77 format.api | |
78 end | |
79 end | |
80 | |
81 def new | |
82 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
83 @user.safe_attributes = params[:user] | |
84 @auth_sources = AuthSource.all | |
85 end | |
86 | |
87 def create | |
88 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
89 @user.safe_attributes = params[:user] | |
90 @user.admin = params[:user][:admin] || false | |
91 @user.login = params[:user][:login] | |
92 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id | |
93 | |
94 if @user.save | |
95 @user.pref.attributes = params[:pref] | |
96 @user.pref.save | |
97 | |
98 Mailer.account_information(@user, @user.password).deliver if params[:send_information] | |
99 | |
100 respond_to do |format| | |
101 format.html { | |
102 flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user))) | |
103 if params[:continue] | |
104 attrs = params[:user].slice(:generate_password) | |
105 redirect_to new_user_path(:user => attrs) | |
106 else | |
107 redirect_to edit_user_path(@user) | |
108 end | |
109 } | |
110 format.api { render :action => 'show', :status => :created, :location => user_url(@user) } | |
111 end | |
112 else | |
113 @auth_sources = AuthSource.all | |
114 # Clear password input | |
115 @user.password = @user.password_confirmation = nil | |
116 | |
117 respond_to do |format| | |
118 format.html { render :action => 'new' } | |
119 format.api { render_validation_errors(@user) } | |
120 end | |
121 end | |
122 end | |
123 | |
124 def edit | |
125 @auth_sources = AuthSource.all | |
126 @membership ||= Member.new | |
127 end | |
128 | |
129 def update | |
130 @user.admin = params[:user][:admin] if params[:user][:admin] | |
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?) | |
133 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] | |
134 end | |
135 @user.safe_attributes = params[:user] | |
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]) | |
138 # TODO: Similar to My#account | |
139 @user.pref.attributes = params[:pref] | |
140 | |
141 if @user.save | |
142 @user.pref.save | |
143 | |
144 if was_activated | |
145 Mailer.account_activated(@user).deliver | |
146 elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil? | |
147 Mailer.account_information(@user, @user.password).deliver | |
148 end | |
149 | |
150 respond_to do |format| | |
151 format.html { | |
152 flash[:notice] = l(:notice_successful_update) | |
153 redirect_to_referer_or edit_user_path(@user) | |
154 } | |
155 format.api { render_api_ok } | |
156 end | |
157 else | |
158 @auth_sources = AuthSource.all | |
159 @membership ||= Member.new | |
160 # Clear password input | |
161 @user.password = @user.password_confirmation = nil | |
162 | |
163 respond_to do |format| | |
164 format.html { render :action => :edit } | |
165 format.api { render_validation_errors(@user) } | |
166 end | |
167 end | |
168 end | |
169 | |
170 def destroy | |
171 @user.destroy | |
172 respond_to do |format| | |
173 format.html { redirect_back_or_default(users_path) } | |
174 format.api { render_api_ok } | |
175 end | |
176 end | |
177 | |
178 def edit_membership | |
179 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) | |
180 @membership.save | |
181 respond_to do |format| | |
182 format.html { redirect_to edit_user_path(@user, :tab => 'memberships') } | |
183 format.js | |
184 end | |
185 end | |
186 | |
187 def destroy_membership | |
188 @membership = Member.find(params[:membership_id]) | |
189 if @membership.deletable? | |
190 @membership.destroy | |
191 end | |
192 respond_to do |format| | |
193 format.html { redirect_to edit_user_path(@user, :tab => 'memberships') } | |
194 format.js | |
195 end | |
196 end | |
197 | |
198 private | |
199 | |
200 def find_user | |
201 if params[:id] == 'current' | |
202 require_login || return | |
203 @user = User.current | |
204 else | |
205 @user = User.find(params[:id]) | |
206 end | |
207 rescue ActiveRecord::RecordNotFound | |
208 render_404 | |
209 end | |
210 end |