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 @ 1298:4f746d8966dd
History | View | Annotate | Download (8.6 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 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 | 1115:433d4f72a19b | Chris | @status = params[:status] || 1 |
| 42 | 909:cbb26bc654de | Chris | |
| 43 | 1115:433d4f72a19b | Chris | 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 | 0:513646585e45 | Chris | |
| 47 | 1115:433d4f72a19b | Chris | @user_count = scope.count
|
| 48 | 1295:622f24f53b42 | Chris | @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 | 0:513646585e45 | Chris | |
| 52 | 441:cbce1fd3b1b7 | Chris | respond_to do |format|
|
| 53 | format.html {
|
||
| 54 | @groups = Group.all.sort |
||
| 55 | render :layout => !request.xhr?
|
||
| 56 | } |
||
| 57 | 117:af80e5618e9b | Chris | format.api |
| 58 | 1295:622f24f53b42 | Chris | end
|
| 59 | 0:513646585e45 | Chris | end
|
| 60 | 909:cbb26bc654de | Chris | |
| 61 | 0:513646585e45 | Chris | def show |
| 62 | 92:e408a3f7089f | luisf | |
| 63 | if @user.ssamr_user_detail != nil |
||
| 64 | @description = @user.ssamr_user_detail.description |
||
| 65 | 1217:875b5b4c574d | chris | @institution_name = @user.ssamr_user_detail.institution_name |
| 66 | 92:e408a3f7089f | luisf | end
|
| 67 | 37:94944d00e43c | chris | |
| 68 | 14:1d32c0a0efbf | Chris | # show projects based on current user visibility
|
| 69 | 441:cbce1fd3b1b7 | Chris | @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) |
| 70 | 909:cbb26bc654de | Chris | |
| 71 | 0:513646585e45 | Chris | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
| 72 | @events_by_day = events.group_by(&:event_date) |
||
| 73 | 909:cbb26bc654de | Chris | |
| 74 | 0:513646585e45 | Chris | unless User.current.admin? |
| 75 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
||
| 76 | render_404 |
||
| 77 | return
|
||
| 78 | end
|
||
| 79 | end
|
||
| 80 | 909:cbb26bc654de | Chris | |
| 81 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 82 | format.html { render :layout => 'base' }
|
||
| 83 | format.api |
||
| 84 | end
|
||
| 85 | 0:513646585e45 | Chris | end
|
| 86 | |||
| 87 | 61:aedfe093ec32 | luisf | def new |
| 88 | 117:af80e5618e9b | Chris | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
| 89 | 1295:622f24f53b42 | Chris | @auth_sources = AuthSource.all |
| 90 | 60:cf39b52d24b4 | luisf | |
| 91 | @ssamr_user_details = SsamrUserDetail.new |
||
| 92 | 37:94944d00e43c | chris | end
|
| 93 | 909:cbb26bc654de | Chris | |
| 94 | 37:94944d00e43c | chris | def create |
| 95 | 117:af80e5618e9b | Chris | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
| 96 | @user.safe_attributes = params[:user] |
||
| 97 | 60:cf39b52d24b4 | luisf | @user = User.new(params[:user]) |
| 98 | 37:94944d00e43c | chris | @user.admin = params[:user][:admin] || false |
| 99 | @user.login = params[:user][:login] |
||
| 100 | 117:af80e5618e9b | Chris | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id |
| 101 | 37:94944d00e43c | chris | |
| 102 | 909:cbb26bc654de | Chris | # TODO: Similar to My#account
|
| 103 | @user.pref.attributes = params[:pref] |
||
| 104 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
||
| 105 | |||
| 106 | 61:aedfe093ec32 | luisf | @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details]) |
| 107 | |||
| 108 | # associates the 2 objects
|
||
| 109 | @user.ssamr_user_detail = @ssamr_user_details |
||
| 110 | |||
| 111 | 37:94944d00e43c | chris | if @user.save |
| 112 | 1115:433d4f72a19b | Chris | @user.pref.attributes = params[:pref] |
| 113 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
||
| 114 | 37:94944d00e43c | chris | @user.pref.save
|
| 115 | 61:aedfe093ec32 | luisf | |
| 116 | 60:cf39b52d24b4 | luisf | @ssamr_user_details.save!
|
| 117 | |||
| 118 | 37:94944d00e43c | chris | |
| 119 | 1115:433d4f72a19b | Chris | Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information] |
| 120 | 909:cbb26bc654de | Chris | |
| 121 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 122 | format.html {
|
||
| 123 | 1115:433d4f72a19b | Chris | flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user))) |
| 124 | 1295:622f24f53b42 | Chris | if params[:continue] |
| 125 | redirect_to new_user_path |
||
| 126 | else
|
||
| 127 | redirect_to edit_user_path(@user)
|
||
| 128 | end
|
||
| 129 | 117:af80e5618e9b | Chris | } |
| 130 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
|
||
| 131 | end
|
||
| 132 | 0:513646585e45 | Chris | else
|
| 133 | 1295:622f24f53b42 | Chris | @auth_sources = AuthSource.all |
| 134 | 117:af80e5618e9b | Chris | # Clear password input
|
| 135 | @user.password = @user.password_confirmation = nil |
||
| 136 | 37:94944d00e43c | chris | |
| 137 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 138 | format.html { render :action => 'new' }
|
||
| 139 | format.api { render_validation_errors(@user) }
|
||
| 140 | end
|
||
| 141 | 0:513646585e45 | Chris | end
|
| 142 | end
|
||
| 143 | |||
| 144 | def edit |
||
| 145 | 1295:622f24f53b42 | Chris | @auth_sources = AuthSource.all |
| 146 | 60:cf39b52d24b4 | luisf | @ssamr_user_details = @user.ssamr_user_detail |
| 147 | 161:bad82329a115 | luisf | |
| 148 | 185:594ed6aef7bd | luisf | if @user.ssamr_user_detail == nil |
| 149 | @selected_institution_id = nil |
||
| 150 | else
|
||
| 151 | @selected_institution_id = @user.ssamr_user_detail.institution_id.to_i |
||
| 152 | end
|
||
| 153 | 60:cf39b52d24b4 | luisf | |
| 154 | 0:513646585e45 | Chris | @membership ||= Member.new |
| 155 | 37:94944d00e43c | chris | end
|
| 156 | 909:cbb26bc654de | Chris | |
| 157 | 37:94944d00e43c | chris | def update |
| 158 | @user.admin = params[:user][:admin] if params[:user][:admin] |
||
| 159 | @user.login = params[:user][:login] if params[:user][:login] |
||
| 160 | 117:af80e5618e9b | Chris | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
| 161 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
||
| 162 | 37:94944d00e43c | chris | end
|
| 163 | 117:af80e5618e9b | Chris | @user.safe_attributes = params[:user] |
| 164 | 37:94944d00e43c | chris | # Was the account actived ? (do it before User#save clears the change)
|
| 165 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
||
| 166 | # TODO: Similar to My#account
|
||
| 167 | @user.pref.attributes = params[:pref] |
||
| 168 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
||
| 169 | |||
| 170 | 92:e408a3f7089f | luisf | if @user.ssamr_user_detail == nil |
| 171 | @ssamr_user_details = SsamrUserDetail.new() |
||
| 172 | @user.ssamr_user_detail = @ssamr_user_details |
||
| 173 | else
|
||
| 174 | @ssamr_user_details = @user.ssamr_user_detail |
||
| 175 | end
|
||
| 176 | 108:d70a0b926135 | luisf | |
| 177 | 63:b5bd39e27658 | luisf | if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty? |
| 178 | @ssamr_user_details.description = @user.ssamr_user_detail.description |
||
| 179 | 108:d70a0b926135 | luisf | @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id |
| 180 | 158:e71a969c151a | luisf | @ssamr_user_details.other_institution = @user.ssamr_user_detail.other_institution |
| 181 | @ssamr_user_details.institution_type = @user.ssamr_user_detail.institution_type |
||
| 182 | |||
| 183 | 63:b5bd39e27658 | luisf | else
|
| 184 | @ssamr_user_details.description = params[:ssamr_user_details][:description] |
||
| 185 | 108:d70a0b926135 | luisf | @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id] |
| 186 | 158:e71a969c151a | luisf | @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution] |
| 187 | @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type] |
||
| 188 | 63:b5bd39e27658 | luisf | end
|
| 189 | |||
| 190 | 37:94944d00e43c | chris | if @user.save |
| 191 | @user.pref.save
|
||
| 192 | 117:af80e5618e9b | Chris | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
| 193 | 37:94944d00e43c | chris | |
| 194 | if was_activated
|
||
| 195 | 1115:433d4f72a19b | Chris | Mailer.account_activated(@user).deliver |
| 196 | 117:af80e5618e9b | Chris | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? |
| 197 | 1115:433d4f72a19b | Chris | Mailer.account_information(@user, params[:user][:password]).deliver |
| 198 | 37:94944d00e43c | chris | end
|
| 199 | 909:cbb26bc654de | Chris | |
| 200 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 201 | format.html {
|
||
| 202 | flash[:notice] = l(:notice_successful_update) |
||
| 203 | 1115:433d4f72a19b | Chris | redirect_to_referer_or edit_user_path(@user)
|
| 204 | 117:af80e5618e9b | Chris | } |
| 205 | 1115:433d4f72a19b | Chris | format.api { render_api_ok }
|
| 206 | 117:af80e5618e9b | Chris | end
|
| 207 | 37:94944d00e43c | chris | else
|
| 208 | 1295:622f24f53b42 | Chris | @auth_sources = AuthSource.all |
| 209 | 37:94944d00e43c | chris | @membership ||= Member.new |
| 210 | 117:af80e5618e9b | Chris | # Clear password input
|
| 211 | @user.password = @user.password_confirmation = nil |
||
| 212 | 37:94944d00e43c | chris | |
| 213 | 117:af80e5618e9b | Chris | respond_to do |format|
|
| 214 | format.html { render :action => :edit }
|
||
| 215 | format.api { render_validation_errors(@user) }
|
||
| 216 | end
|
||
| 217 | 37:94944d00e43c | chris | end
|
| 218 | 0:513646585e45 | Chris | end
|
| 219 | 37:94944d00e43c | chris | |
| 220 | 128:07fa8a8b56a8 | Chris | def destroy |
| 221 | @user.destroy
|
||
| 222 | respond_to do |format|
|
||
| 223 | 1295:622f24f53b42 | Chris | format.html { redirect_back_or_default(users_path) }
|
| 224 | 1115:433d4f72a19b | Chris | format.api { render_api_ok }
|
| 225 | 128:07fa8a8b56a8 | Chris | end
|
| 226 | end
|
||
| 227 | |||
| 228 | 0:513646585e45 | Chris | def edit_membership |
| 229 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
||
| 230 | 1115:433d4f72a19b | Chris | @membership.save
|
| 231 | 0:513646585e45 | Chris | respond_to do |format|
|
| 232 | 1295:622f24f53b42 | Chris | format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
|
| 233 | 1115:433d4f72a19b | Chris | format.js |
| 234 | 14:1d32c0a0efbf | Chris | end
|
| 235 | 0:513646585e45 | Chris | end
|
| 236 | 909:cbb26bc654de | Chris | |
| 237 | 0:513646585e45 | Chris | def destroy_membership |
| 238 | @membership = Member.find(params[:membership_id]) |
||
| 239 | 1115:433d4f72a19b | Chris | if @membership.deletable? |
| 240 | 0:513646585e45 | Chris | @membership.destroy
|
| 241 | end
|
||
| 242 | respond_to do |format|
|
||
| 243 | 1295:622f24f53b42 | Chris | format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
|
| 244 | 1115:433d4f72a19b | Chris | format.js |
| 245 | 0:513646585e45 | Chris | end
|
| 246 | end
|
||
| 247 | 909:cbb26bc654de | Chris | |
| 248 | 117:af80e5618e9b | Chris | private |
| 249 | 909:cbb26bc654de | Chris | |
| 250 | 117:af80e5618e9b | Chris | def find_user |
| 251 | if params[:id] == 'current' |
||
| 252 | require_login || return
|
||
| 253 | @user = User.current |
||
| 254 | else
|
||
| 255 | @user = User.find(params[:id]) |
||
| 256 | end
|
||
| 257 | rescue ActiveRecord::RecordNotFound |
||
| 258 | render_404 |
||
| 259 | end
|
||
| 260 | 0:513646585e45 | Chris | end |