annotate app/controllers/.svn/text-base/users_controller.rb.svn-base @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 1d32c0a0efbf
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2009 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class UsersController < ApplicationController
Chris@0 19 layout 'admin'
Chris@0 20
Chris@0 21 before_filter :require_admin, :except => :show
Chris@0 22
Chris@0 23 helper :sort
Chris@0 24 include SortHelper
Chris@0 25 helper :custom_fields
Chris@0 26 include CustomFieldsHelper
Chris@0 27
Chris@0 28 def index
Chris@0 29 sort_init 'login', 'asc'
Chris@0 30 sort_update %w(login firstname lastname mail admin created_on last_login_on)
Chris@0 31
Chris@0 32 @status = params[:status] ? params[:status].to_i : 1
Chris@0 33 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
Chris@0 34
Chris@0 35 unless params[:name].blank?
Chris@0 36 name = "%#{params[:name].strip.downcase}%"
Chris@0 37 c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
Chris@0 38 end
Chris@0 39
Chris@0 40 @user_count = User.count(:conditions => c.conditions)
Chris@0 41 @user_pages = Paginator.new self, @user_count,
Chris@0 42 per_page_option,
Chris@0 43 params['page']
Chris@0 44 @users = User.find :all,:order => sort_clause,
Chris@0 45 :conditions => c.conditions,
Chris@0 46 :limit => @user_pages.items_per_page,
Chris@0 47 :offset => @user_pages.current.offset
Chris@0 48
Chris@0 49 render :layout => !request.xhr?
Chris@0 50 end
Chris@0 51
Chris@0 52 def show
Chris@0 53 @user = User.find(params[:id])
Chris@0 54 @custom_values = @user.custom_values
Chris@0 55
Chris@0 56 # show only public projects and private projects that the logged in user is also a member of
Chris@0 57 @memberships = @user.memberships.select do |membership|
Chris@0 58 membership.project.is_public? || (User.current.member_of?(membership.project))
Chris@0 59 end
Chris@0 60
Chris@0 61 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
Chris@0 62 @events_by_day = events.group_by(&:event_date)
Chris@0 63
Chris@0 64 unless User.current.admin?
Chris@0 65 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
Chris@0 66 render_404
Chris@0 67 return
Chris@0 68 end
Chris@0 69 end
Chris@0 70 render :layout => 'base'
Chris@0 71
Chris@0 72 rescue ActiveRecord::RecordNotFound
Chris@0 73 render_404
Chris@0 74 end
Chris@0 75
Chris@0 76 def add
Chris@0 77 if request.get?
Chris@0 78 @user = User.new(:language => Setting.default_language)
Chris@0 79 else
Chris@0 80 @user = User.new(params[:user])
Chris@0 81 @user.admin = params[:user][:admin] || false
Chris@0 82 @user.login = params[:user][:login]
Chris@0 83 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
Chris@0 84 if @user.save
Chris@0 85 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
Chris@0 86 flash[:notice] = l(:notice_successful_create)
Chris@0 87 redirect_to(params[:continue] ? {:controller => 'users', :action => 'add'} :
Chris@0 88 {:controller => 'users', :action => 'edit', :id => @user})
Chris@0 89 return
Chris@0 90 end
Chris@0 91 end
Chris@0 92 @auth_sources = AuthSource.find(:all)
Chris@0 93 end
Chris@0 94
Chris@0 95 def edit
Chris@0 96 @user = User.find(params[:id])
Chris@0 97 if request.post?
Chris@0 98 @user.admin = params[:user][:admin] if params[:user][:admin]
Chris@0 99 @user.login = params[:user][:login] if params[:user][:login]
Chris@0 100 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id
Chris@0 101 @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
Chris@0 102 @user.attributes = params[:user]
Chris@0 103 # Was the account actived ? (do it before User#save clears the change)
Chris@0 104 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
Chris@0 105 if @user.save
Chris@0 106 if was_activated
Chris@0 107 Mailer.deliver_account_activated(@user)
Chris@0 108 elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
Chris@0 109 Mailer.deliver_account_information(@user, params[:password])
Chris@0 110 end
Chris@0 111 flash[:notice] = l(:notice_successful_update)
Chris@0 112 redirect_to :back
Chris@0 113 end
Chris@0 114 end
Chris@0 115 @auth_sources = AuthSource.find(:all)
Chris@0 116 @membership ||= Member.new
Chris@0 117 rescue ::ActionController::RedirectBackError
Chris@0 118 redirect_to :controller => 'users', :action => 'edit', :id => @user
Chris@0 119 end
Chris@0 120
Chris@0 121 def edit_membership
Chris@0 122 @user = User.find(params[:id])
Chris@0 123 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
Chris@0 124 @membership.save if request.post?
Chris@0 125 respond_to do |format|
Chris@0 126 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
Chris@0 127 format.js {
Chris@0 128 render(:update) {|page|
Chris@0 129 page.replace_html "tab-content-memberships", :partial => 'users/memberships'
Chris@0 130 page.visual_effect(:highlight, "member-#{@membership.id}")
Chris@0 131 }
Chris@0 132 }
Chris@0 133 end
Chris@0 134 end
Chris@0 135
Chris@0 136 def destroy_membership
Chris@0 137 @user = User.find(params[:id])
Chris@0 138 @membership = Member.find(params[:membership_id])
Chris@0 139 if request.post? && @membership.deletable?
Chris@0 140 @membership.destroy
Chris@0 141 end
Chris@0 142 respond_to do |format|
Chris@0 143 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
Chris@0 144 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
Chris@0 145 end
Chris@0 146 end
Chris@0 147 end