Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: class UsersController < ApplicationController Chris@909: layout 'admin' Chris@909: Chris@909: before_filter :require_admin, :except => :show Chris@909: before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] Chris@909: accept_api_auth :index, :show, :create, :update, :destroy Chris@909: Chris@909: helper :sort Chris@909: include SortHelper Chris@909: helper :custom_fields Chris@909: include CustomFieldsHelper Chris@909: Chris@909: def index Chris@909: sort_init 'login', 'asc' Chris@909: sort_update %w(login firstname lastname mail admin created_on last_login_on) Chris@909: Chris@909: case params[:format] Chris@909: when 'xml', 'json' Chris@909: @offset, @limit = api_offset_and_limit Chris@909: else Chris@909: @limit = per_page_option Chris@909: end Chris@909: Chris@909: scope = User Chris@909: scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present? Chris@909: Chris@909: @status = params[:status] ? params[:status].to_i : 1 Chris@909: c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) Chris@909: Chris@909: unless params[:name].blank? Chris@909: name = "%#{params[:name].strip.downcase}%" Chris@909: c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name] Chris@909: end Chris@909: Chris@909: @user_count = scope.count(:conditions => c.conditions) Chris@909: @user_pages = Paginator.new self, @user_count, @limit, params['page'] Chris@909: @offset ||= @user_pages.current.offset Chris@909: @users = scope.find :all, Chris@909: :order => sort_clause, Chris@909: :conditions => c.conditions, Chris@909: :limit => @limit, Chris@909: :offset => @offset Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { Chris@909: @groups = Group.all.sort Chris@909: render :layout => !request.xhr? Chris@909: } Chris@909: format.api Chris@909: end Chris@909: end Chris@909: Chris@909: def show Chris@909: # show projects based on current user visibility Chris@909: @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) Chris@909: Chris@909: events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) Chris@909: @events_by_day = events.group_by(&:event_date) Chris@909: Chris@909: unless User.current.admin? Chris@909: if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) Chris@909: render_404 Chris@909: return Chris@909: end Chris@909: end Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { render :layout => 'base' } Chris@909: format.api Chris@909: end Chris@909: end Chris@909: Chris@909: def new Chris@909: @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) Chris@909: @auth_sources = AuthSource.find(:all) Chris@909: end Chris@909: Chris@909: verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } Chris@909: def create Chris@909: @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) Chris@909: @user.safe_attributes = params[:user] Chris@909: @user.admin = params[:user][:admin] || false Chris@909: @user.login = params[:user][:login] Chris@909: @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id Chris@909: Chris@909: # TODO: Similar to My#account Chris@909: @user.pref.attributes = params[:pref] Chris@909: @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') Chris@909: Chris@909: if @user.save Chris@909: @user.pref.save Chris@909: @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) Chris@909: Chris@909: Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information] Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { Chris@909: flash[:notice] = l(:notice_successful_create) Chris@909: redirect_to(params[:continue] ? Chris@909: {:controller => 'users', :action => 'new'} : Chris@909: {:controller => 'users', :action => 'edit', :id => @user} Chris@909: ) Chris@909: } Chris@909: format.api { render :action => 'show', :status => :created, :location => user_url(@user) } Chris@909: end Chris@909: else Chris@909: @auth_sources = AuthSource.find(:all) Chris@909: # Clear password input Chris@909: @user.password = @user.password_confirmation = nil Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { render :action => 'new' } Chris@909: format.api { render_validation_errors(@user) } Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def edit Chris@909: @auth_sources = AuthSource.find(:all) Chris@909: @membership ||= Member.new Chris@909: end Chris@909: Chris@909: verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } Chris@909: def update Chris@909: @user.admin = params[:user][:admin] if params[:user][:admin] Chris@909: @user.login = params[:user][:login] if params[:user][:login] Chris@909: if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) Chris@909: @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] Chris@909: end Chris@909: @user.safe_attributes = params[:user] Chris@909: # Was the account actived ? (do it before User#save clears the change) Chris@909: was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) Chris@909: # TODO: Similar to My#account Chris@909: @user.pref.attributes = params[:pref] Chris@909: @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') Chris@909: Chris@909: if @user.save Chris@909: @user.pref.save Chris@909: @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) Chris@909: Chris@909: if was_activated Chris@909: Mailer.deliver_account_activated(@user) Chris@909: elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? Chris@909: Mailer.deliver_account_information(@user, params[:user][:password]) Chris@909: end Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { Chris@909: flash[:notice] = l(:notice_successful_update) Chris@909: redirect_to :back Chris@909: } Chris@909: format.api { head :ok } Chris@909: end Chris@909: else Chris@909: @auth_sources = AuthSource.find(:all) Chris@909: @membership ||= Member.new Chris@909: # Clear password input Chris@909: @user.password = @user.password_confirmation = nil Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { render :action => :edit } Chris@909: format.api { render_validation_errors(@user) } Chris@909: end Chris@909: end Chris@909: rescue ::ActionController::RedirectBackError Chris@909: redirect_to :controller => 'users', :action => 'edit', :id => @user Chris@909: end Chris@909: Chris@909: verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } Chris@909: def destroy Chris@909: @user.destroy Chris@909: respond_to do |format| Chris@909: format.html { redirect_to(users_url) } Chris@909: format.api { head :ok } Chris@909: end Chris@909: end Chris@909: Chris@909: def edit_membership Chris@909: @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) Chris@909: @membership.save if request.post? Chris@909: respond_to do |format| Chris@909: if @membership.valid? Chris@909: format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } Chris@909: format.js { Chris@909: render(:update) {|page| Chris@909: page.replace_html "tab-content-memberships", :partial => 'users/memberships' Chris@909: page.visual_effect(:highlight, "member-#{@membership.id}") Chris@909: } Chris@909: } Chris@909: else Chris@909: format.js { Chris@909: render(:update) {|page| Chris@909: page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) Chris@909: } Chris@909: } Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def destroy_membership Chris@909: @membership = Member.find(params[:membership_id]) Chris@909: if request.post? && @membership.deletable? Chris@909: @membership.destroy Chris@909: end Chris@909: respond_to do |format| Chris@909: format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } Chris@909: format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } Chris@909: end Chris@909: end Chris@909: Chris@909: private Chris@909: Chris@909: def find_user Chris@909: if params[:id] == 'current' Chris@909: require_login || return Chris@909: @user = User.current Chris@909: else Chris@909: @user = User.find(params[:id]) Chris@909: end Chris@909: rescue ActiveRecord::RecordNotFound Chris@909: render_404 Chris@909: end Chris@909: end