To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / controllers / users_controller.rb @ 912:5e80956cc792
History | View | Annotate | Download (10.3 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 441:cbce1fd3b1b7 | Chris | # Copyright (C) 2006-2011 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 21 | 0:513646585e45 | Chris | before_filter :require_admin, :except => :show |
| 22 | 128:07fa8a8b56a8 | Chris | before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] |
| 23 | 507:0c939c159af4 | Chris | accept_api_auth :index, :show, :create, :update, :destroy |
| 24 | 0:513646585e45 | Chris | |
| 25 | helper :sort
|
||
| 26 | include SortHelper
|
||
| 27 | helper :custom_fields
|
||
| 28 | 909:cbb26bc654de | Chris | include CustomFieldsHelper
|
| 29 | 0:513646585e45 | Chris | |
| 30 | def index |
||
| 31 | sort_init 'login', 'asc' |
||
| 32 | sort_update %w(login firstname lastname mail admin created_on last_login_on)
|
||
| 33 | 909:cbb26bc654de | Chris | |
| 34 | 117:af80e5618e9b | Chris | case params[:format] |
| 35 | when 'xml', 'json' |
||
| 36 | @offset, @limit = api_offset_and_limit |
||
| 37 | else
|
||
| 38 | @limit = per_page_option
|
||
| 39 | end
|
||
| 40 | 909:cbb26bc654de | Chris | |
| 41 | 441:cbce1fd3b1b7 | Chris | scope = User
|
| 42 | scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present? |
||
| 43 | 909:cbb26bc654de | Chris | |
| 44 | 0:513646585e45 | Chris | @status = params[:status] ? params[:status].to_i : 1 |
| 45 | c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) |
||
| 46 | |||
| 47 | unless params[:name].blank? |
||
| 48 | name = "%#{params[:name].strip.downcase}%"
|
||
| 49 | c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
|
||
| 50 | end
|
||
| 51 | 909:cbb26bc654de | Chris | |
| 52 | 441:cbce1fd3b1b7 | Chris | @user_count = scope.count(:conditions => c.conditions) |
| 53 | 117:af80e5618e9b | Chris | @user_pages = Paginator.new self, @user_count, @limit, params['page'] |
| 54 | @offset ||= @user_pages.current.offset |
||
| 55 | 441:cbce1fd3b1b7 | Chris | @users = scope.find :all, |
| 56 | 117:af80e5618e9b | Chris | :order => sort_clause,
|
| 57 | 0:513646585e45 | Chris | :conditions => c.conditions,
|
| 58 | 117:af80e5618e9b | Chris | :limit => @limit, |
| 59 | :offset => @offset |
||
| 60 | 0:513646585e45 | Chris | |
| 61 | 441:cbce1fd3b1b7 | Chris | respond_to do |format|
|
| 62 | format.html {
|
||
| 63 | @groups = Group.all.sort |
||
| 64 | render :layout => !request.xhr?
|
||
| 65 | } |
||
| 66 | 117:af80e5618e9b | Chris | format.api |
| 67 | 441:cbce1fd3b1b7 | Chris | end
|
| 68 | 0:513646585e45 | Chris | end
|
| 69 | 909:cbb26bc654de | Chris | |
| 70 | 0:513646585e45 | Chris | def show |
| 71 | 92:e408a3f7089f | luisf | |
| 72 | if @user.ssamr_user_detail != nil |
||
| 73 | @description = @user.ssamr_user_detail.description |
||
| 74 | 160:7eb2194ee428 | luisf | |
| 75 | if @user.ssamr_user_detail.institution_type != nil |
||
| 76 | # institution_type is true for listed institutions
|
||
| 77 | if (@user.ssamr_user_detail.institution_type) |
||
| 78 | @institution_name = Institution.find(@user.ssamr_user_detail.institution_id).name |
||
| 79 | else
|
||
| 80 | @institution_name = @user.ssamr_user_detail.other_institution |
||
| 81 | end
|
||
| 82 | 104:0511601cda6b | luisf | end
|
| 83 | 92:e408a3f7089f | luisf | end
|
| 84 | 37:94944d00e43c | chris | |
| 85 | 14:1d32c0a0efbf | Chris | # show projects based on current user visibility
|
| 86 | 441:cbce1fd3b1b7 | Chris | @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) |
| 87 | 909:cbb26bc654de | Chris | |
| 88 | 0:513646585e45 | Chris | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
| 89 | @events_by_day = events.group_by(&:event_date) |
||
| 90 | 909:cbb26bc654de | Chris | |
| 91 | 0:513646585e45 | Chris | unless User.current.admin? |
| 92 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
||
| 93 | render_404 |
||
| 94 | return
|
||
| 95 | end
|
||
| 96 | end
|
||
| 97 | 909:cbb26bc654de | Chris | |
| 98 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 99 | format.html { render :layout => 'base' }
|
||
| 100 | format.api |
||
| 101 | end
|
||
| 102 | 0:513646585e45 | Chris | end
|
| 103 | |||
| 104 | 61:aedfe093ec32 | luisf | def new |
| 105 | 117:af80e5618e9b | Chris | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
| 106 | 37:94944d00e43c | chris | @auth_sources = AuthSource.find(:all) |
| 107 | 60:cf39b52d24b4 | luisf | |
| 108 | @ssamr_user_details = SsamrUserDetail.new |
||
| 109 | 37:94944d00e43c | chris | end
|
| 110 | 909:cbb26bc654de | Chris | |
| 111 | 37:94944d00e43c | chris | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
| 112 | def create |
||
| 113 | 117:af80e5618e9b | Chris | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
| 114 | @user.safe_attributes = params[:user] |
||
| 115 | 60:cf39b52d24b4 | luisf | @user = User.new(params[:user]) |
| 116 | 37:94944d00e43c | chris | @user.admin = params[:user][:admin] || false |
| 117 | @user.login = params[:user][:login] |
||
| 118 | 117:af80e5618e9b | Chris | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id |
| 119 | 37:94944d00e43c | chris | |
| 120 | # TODO: Similar to My#account
|
||
| 121 | @user.pref.attributes = params[:pref] |
||
| 122 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
||
| 123 | |||
| 124 | 61:aedfe093ec32 | luisf | @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details]) |
| 125 | |||
| 126 | # associates the 2 objects
|
||
| 127 | @user.ssamr_user_detail = @ssamr_user_details |
||
| 128 | |||
| 129 | 37:94944d00e43c | chris | if @user.save |
| 130 | @user.pref.save
|
||
| 131 | 61:aedfe093ec32 | luisf | |
| 132 | 60:cf39b52d24b4 | luisf | @ssamr_user_details.save!
|
| 133 | |||
| 134 | 37:94944d00e43c | chris | |
| 135 | 117:af80e5618e9b | Chris | Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information] |
| 136 | 909:cbb26bc654de | Chris | |
| 137 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 138 | format.html {
|
||
| 139 | flash[:notice] = l(:notice_successful_create) |
||
| 140 | 909:cbb26bc654de | Chris | redirect_to(params[:continue] ?
|
| 141 | {:controller => 'users', :action => 'new'} :
|
||
| 142 | 117:af80e5618e9b | Chris | {:controller => 'users', :action => 'edit', :id => @user}
|
| 143 | ) |
||
| 144 | } |
||
| 145 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
|
||
| 146 | end
|
||
| 147 | 0:513646585e45 | Chris | else
|
| 148 | 37:94944d00e43c | chris | @auth_sources = AuthSource.find(:all) |
| 149 | 117:af80e5618e9b | Chris | # Clear password input
|
| 150 | @user.password = @user.password_confirmation = nil |
||
| 151 | 37:94944d00e43c | chris | |
| 152 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 153 | format.html { render :action => 'new' }
|
||
| 154 | format.api { render_validation_errors(@user) }
|
||
| 155 | end
|
||
| 156 | 0:513646585e45 | Chris | end
|
| 157 | end
|
||
| 158 | |||
| 159 | def edit |
||
| 160 | 55:bbb139d5ca95 | luisf | |
| 161 | 60:cf39b52d24b4 | luisf | @ssamr_user_details = @user.ssamr_user_detail |
| 162 | 161:bad82329a115 | luisf | |
| 163 | 185:594ed6aef7bd | luisf | if @user.ssamr_user_detail == nil |
| 164 | @selected_institution_id = nil |
||
| 165 | else
|
||
| 166 | @selected_institution_id = @user.ssamr_user_detail.institution_id.to_i |
||
| 167 | end
|
||
| 168 | 60:cf39b52d24b4 | luisf | |
| 169 | 0:513646585e45 | Chris | @auth_sources = AuthSource.find(:all) |
| 170 | @membership ||= Member.new |
||
| 171 | 37:94944d00e43c | chris | end
|
| 172 | 909:cbb26bc654de | Chris | |
| 173 | 37:94944d00e43c | chris | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
| 174 | def update |
||
| 175 | @user.admin = params[:user][:admin] if params[:user][:admin] |
||
| 176 | @user.login = params[:user][:login] if params[:user][:login] |
||
| 177 | 117:af80e5618e9b | Chris | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
| 178 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
||
| 179 | 37:94944d00e43c | chris | end
|
| 180 | 117:af80e5618e9b | Chris | @user.safe_attributes = params[:user] |
| 181 | 37:94944d00e43c | chris | # Was the account actived ? (do it before User#save clears the change)
|
| 182 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
||
| 183 | # TODO: Similar to My#account
|
||
| 184 | @user.pref.attributes = params[:pref] |
||
| 185 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
||
| 186 | |||
| 187 | 92:e408a3f7089f | luisf | if @user.ssamr_user_detail == nil |
| 188 | @ssamr_user_details = SsamrUserDetail.new() |
||
| 189 | @user.ssamr_user_detail = @ssamr_user_details |
||
| 190 | else
|
||
| 191 | @ssamr_user_details = @user.ssamr_user_detail |
||
| 192 | end
|
||
| 193 | 108:d70a0b926135 | luisf | |
| 194 | 63:b5bd39e27658 | luisf | if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty? |
| 195 | @ssamr_user_details.description = @user.ssamr_user_detail.description |
||
| 196 | 108:d70a0b926135 | luisf | @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id |
| 197 | 158:e71a969c151a | luisf | @ssamr_user_details.other_institution = @user.ssamr_user_detail.other_institution |
| 198 | @ssamr_user_details.institution_type = @user.ssamr_user_detail.institution_type |
||
| 199 | |||
| 200 | 63:b5bd39e27658 | luisf | else
|
| 201 | @ssamr_user_details.description = params[:ssamr_user_details][:description] |
||
| 202 | 108:d70a0b926135 | luisf | @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id] |
| 203 | 158:e71a969c151a | luisf | @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution] |
| 204 | @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type] |
||
| 205 | 63:b5bd39e27658 | luisf | end
|
| 206 | |||
| 207 | 37:94944d00e43c | chris | if @user.save |
| 208 | @user.pref.save
|
||
| 209 | 117:af80e5618e9b | Chris | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
| 210 | 37:94944d00e43c | chris | |
| 211 | if was_activated
|
||
| 212 | Mailer.deliver_account_activated(@user) |
||
| 213 | 117:af80e5618e9b | Chris | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? |
| 214 | Mailer.deliver_account_information(@user, params[:user][:password]) |
||
| 215 | 37:94944d00e43c | chris | end
|
| 216 | 909:cbb26bc654de | Chris | |
| 217 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 218 | format.html {
|
||
| 219 | flash[:notice] = l(:notice_successful_update) |
||
| 220 | redirect_to :back
|
||
| 221 | } |
||
| 222 | format.api { head :ok }
|
||
| 223 | end
|
||
| 224 | 37:94944d00e43c | chris | else
|
| 225 | @auth_sources = AuthSource.find(:all) |
||
| 226 | @membership ||= Member.new |
||
| 227 | 117:af80e5618e9b | Chris | # Clear password input
|
| 228 | @user.password = @user.password_confirmation = nil |
||
| 229 | 37:94944d00e43c | chris | |
| 230 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 231 | format.html { render :action => :edit }
|
||
| 232 | format.api { render_validation_errors(@user) }
|
||
| 233 | end
|
||
| 234 | 37:94944d00e43c | chris | end
|
| 235 | 0:513646585e45 | Chris | rescue ::ActionController::RedirectBackError |
| 236 | redirect_to :controller => 'users', :action => 'edit', :id => @user |
||
| 237 | end
|
||
| 238 | 37:94944d00e43c | chris | |
| 239 | 128:07fa8a8b56a8 | Chris | verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } |
| 240 | def destroy |
||
| 241 | @user.destroy
|
||
| 242 | respond_to do |format|
|
||
| 243 | format.html { redirect_to(users_url) }
|
||
| 244 | format.api { head :ok }
|
||
| 245 | end
|
||
| 246 | end
|
||
| 247 | |||
| 248 | 0:513646585e45 | Chris | def edit_membership |
| 249 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
||
| 250 | @membership.save if request.post? |
||
| 251 | respond_to do |format|
|
||
| 252 | 14:1d32c0a0efbf | Chris | if @membership.valid? |
| 253 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
|
||
| 254 | format.js {
|
||
| 255 | render(:update) {|page|
|
||
| 256 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' |
||
| 257 | page.visual_effect(:highlight, "member-#{@membership.id}") |
||
| 258 | } |
||
| 259 | } |
||
| 260 | else
|
||
| 261 | format.js {
|
||
| 262 | render(:update) {|page|
|
||
| 263 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) |
||
| 264 | } |
||
| 265 | } |
||
| 266 | end
|
||
| 267 | end
|
||
| 268 | 0:513646585e45 | Chris | end
|
| 269 | 909:cbb26bc654de | Chris | |
| 270 | 0:513646585e45 | Chris | def destroy_membership |
| 271 | @membership = Member.find(params[:membership_id]) |
||
| 272 | if request.post? && @membership.deletable? |
||
| 273 | @membership.destroy
|
||
| 274 | end
|
||
| 275 | respond_to do |format|
|
||
| 276 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
|
||
| 277 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
|
||
| 278 | end
|
||
| 279 | end
|
||
| 280 | 909:cbb26bc654de | Chris | |
| 281 | 117:af80e5618e9b | Chris | private |
| 282 | 909:cbb26bc654de | Chris | |
| 283 | 117:af80e5618e9b | Chris | def find_user |
| 284 | if params[:id] == 'current' |
||
| 285 | require_login || return
|
||
| 286 | @user = User.current |
||
| 287 | else
|
||
| 288 | @user = User.find(params[:id]) |
||
| 289 | end
|
||
| 290 | rescue ActiveRecord::RecordNotFound |
||
| 291 | render_404 |
||
| 292 | end
|
||
| 293 | 0:513646585e45 | Chris | end |