annotate .svn/pristine/f3/f30cc0647876cb77a493a4e5745be36df8d841df.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 class UsersController < ApplicationController
Chris@909 19 layout 'admin'
Chris@909 20
Chris@909 21 before_filter :require_admin, :except => :show
Chris@909 22 before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership]
Chris@909 23 accept_api_auth :index, :show, :create, :update, :destroy
Chris@909 24
Chris@909 25 helper :sort
Chris@909 26 include SortHelper
Chris@909 27 helper :custom_fields
Chris@909 28 include CustomFieldsHelper
Chris@909 29
Chris@909 30 def index
Chris@909 31 sort_init 'login', 'asc'
Chris@909 32 sort_update %w(login firstname lastname mail admin created_on last_login_on)
Chris@909 33
Chris@909 34 case params[:format]
Chris@909 35 when 'xml', 'json'
Chris@909 36 @offset, @limit = api_offset_and_limit
Chris@909 37 else
Chris@909 38 @limit = per_page_option
Chris@909 39 end
Chris@909 40
Chris@909 41 scope = User
Chris@909 42 scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present?
Chris@909 43
Chris@909 44 @status = params[:status] ? params[:status].to_i : 1
Chris@909 45 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
Chris@909 46
Chris@909 47 unless params[:name].blank?
Chris@909 48 name = "%#{params[:name].strip.downcase}%"
Chris@909 49 c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
Chris@909 50 end
Chris@909 51
Chris@909 52 @user_count = scope.count(:conditions => c.conditions)
Chris@909 53 @user_pages = Paginator.new self, @user_count, @limit, params['page']
Chris@909 54 @offset ||= @user_pages.current.offset
Chris@909 55 @users = scope.find :all,
Chris@909 56 :order => sort_clause,
Chris@909 57 :conditions => c.conditions,
Chris@909 58 :limit => @limit,
Chris@909 59 :offset => @offset
Chris@909 60
Chris@909 61 respond_to do |format|
Chris@909 62 format.html {
Chris@909 63 @groups = Group.all.sort
Chris@909 64 render :layout => !request.xhr?
Chris@909 65 }
Chris@909 66 format.api
Chris@909 67 end
Chris@909 68 end
Chris@909 69
Chris@909 70 def show
Chris@909 71 # show projects based on current user visibility
Chris@909 72 @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
Chris@909 73
Chris@909 74 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
Chris@909 75 @events_by_day = events.group_by(&:event_date)
Chris@909 76
Chris@909 77 unless User.current.admin?
Chris@909 78 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
Chris@909 79 render_404
Chris@909 80 return
Chris@909 81 end
Chris@909 82 end
Chris@909 83
Chris@909 84 respond_to do |format|
Chris@909 85 format.html { render :layout => 'base' }
Chris@909 86 format.api
Chris@909 87 end
Chris@909 88 end
Chris@909 89
Chris@909 90 def new
Chris@909 91 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
Chris@909 92 @auth_sources = AuthSource.find(:all)
Chris@909 93 end
Chris@909 94
Chris@909 95 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
Chris@909 96 def create
Chris@909 97 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
Chris@909 98 @user.safe_attributes = params[:user]
Chris@909 99 @user.admin = params[:user][:admin] || false
Chris@909 100 @user.login = params[:user][:login]
Chris@909 101 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
Chris@909 102
Chris@909 103 # TODO: Similar to My#account
Chris@909 104 @user.pref.attributes = params[:pref]
Chris@909 105 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
Chris@909 106
Chris@909 107 if @user.save
Chris@909 108 @user.pref.save
Chris@909 109 @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
Chris@909 110
Chris@909 111 Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information]
Chris@909 112
Chris@909 113 respond_to do |format|
Chris@909 114 format.html {
Chris@909 115 flash[:notice] = l(:notice_successful_create)
Chris@909 116 redirect_to(params[:continue] ?
Chris@909 117 {:controller => 'users', :action => 'new'} :
Chris@909 118 {:controller => 'users', :action => 'edit', :id => @user}
Chris@909 119 )
Chris@909 120 }
Chris@909 121 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
Chris@909 122 end
Chris@909 123 else
Chris@909 124 @auth_sources = AuthSource.find(:all)
Chris@909 125 # Clear password input
Chris@909 126 @user.password = @user.password_confirmation = nil
Chris@909 127
Chris@909 128 respond_to do |format|
Chris@909 129 format.html { render :action => 'new' }
Chris@909 130 format.api { render_validation_errors(@user) }
Chris@909 131 end
Chris@909 132 end
Chris@909 133 end
Chris@909 134
Chris@909 135 def edit
Chris@909 136 @auth_sources = AuthSource.find(:all)
Chris@909 137 @membership ||= Member.new
Chris@909 138 end
Chris@909 139
Chris@909 140 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
Chris@909 141 def update
Chris@909 142 @user.admin = params[:user][:admin] if params[:user][:admin]
Chris@909 143 @user.login = params[:user][:login] if params[:user][:login]
Chris@909 144 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
Chris@909 145 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
Chris@909 146 end
Chris@909 147 @user.safe_attributes = params[:user]
Chris@909 148 # Was the account actived ? (do it before User#save clears the change)
Chris@909 149 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
Chris@909 150 # TODO: Similar to My#account
Chris@909 151 @user.pref.attributes = params[:pref]
Chris@909 152 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
Chris@909 153
Chris@909 154 if @user.save
Chris@909 155 @user.pref.save
Chris@909 156 @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
Chris@909 157
Chris@909 158 if was_activated
Chris@909 159 Mailer.deliver_account_activated(@user)
Chris@909 160 elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
Chris@909 161 Mailer.deliver_account_information(@user, params[:user][:password])
Chris@909 162 end
Chris@909 163
Chris@909 164 respond_to do |format|
Chris@909 165 format.html {
Chris@909 166 flash[:notice] = l(:notice_successful_update)
Chris@909 167 redirect_to :back
Chris@909 168 }
Chris@909 169 format.api { head :ok }
Chris@909 170 end
Chris@909 171 else
Chris@909 172 @auth_sources = AuthSource.find(:all)
Chris@909 173 @membership ||= Member.new
Chris@909 174 # Clear password input
Chris@909 175 @user.password = @user.password_confirmation = nil
Chris@909 176
Chris@909 177 respond_to do |format|
Chris@909 178 format.html { render :action => :edit }
Chris@909 179 format.api { render_validation_errors(@user) }
Chris@909 180 end
Chris@909 181 end
Chris@909 182 rescue ::ActionController::RedirectBackError
Chris@909 183 redirect_to :controller => 'users', :action => 'edit', :id => @user
Chris@909 184 end
Chris@909 185
Chris@909 186 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
Chris@909 187 def destroy
Chris@909 188 @user.destroy
Chris@909 189 respond_to do |format|
Chris@909 190 format.html { redirect_to(users_url) }
Chris@909 191 format.api { head :ok }
Chris@909 192 end
Chris@909 193 end
Chris@909 194
Chris@909 195 def edit_membership
Chris@909 196 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
Chris@909 197 @membership.save if request.post?
Chris@909 198 respond_to do |format|
Chris@909 199 if @membership.valid?
Chris@909 200 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
Chris@909 201 format.js {
Chris@909 202 render(:update) {|page|
Chris@909 203 page.replace_html "tab-content-memberships", :partial => 'users/memberships'
Chris@909 204 page.visual_effect(:highlight, "member-#{@membership.id}")
Chris@909 205 }
Chris@909 206 }
Chris@909 207 else
Chris@909 208 format.js {
Chris@909 209 render(:update) {|page|
Chris@909 210 page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
Chris@909 211 }
Chris@909 212 }
Chris@909 213 end
Chris@909 214 end
Chris@909 215 end
Chris@909 216
Chris@909 217 def destroy_membership
Chris@909 218 @membership = Member.find(params[:membership_id])
Chris@909 219 if request.post? && @membership.deletable?
Chris@909 220 @membership.destroy
Chris@909 221 end
Chris@909 222 respond_to do |format|
Chris@909 223 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
Chris@909 224 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
Chris@909 225 end
Chris@909 226 end
Chris@909 227
Chris@909 228 private
Chris@909 229
Chris@909 230 def find_user
Chris@909 231 if params[:id] == 'current'
Chris@909 232 require_login || return
Chris@909 233 @user = User.current
Chris@909 234 else
Chris@909 235 @user = User.find(params[:id])
Chris@909 236 end
Chris@909 237 rescue ActiveRecord::RecordNotFound
Chris@909 238 render_404
Chris@909 239 end
Chris@909 240 end