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