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