2011-10-29 16:19:11 +04:00
|
|
|
#-- encoding: UTF-8
|
2011-05-30 00:11:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# Copyright (C) 2010-2011 the ChiliProject Team
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# 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.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
class UsersController < ApplicationController
|
2009-12-17 21:21:02 +03:00
|
|
|
layout 'admin'
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-10-29 21:37:00 +03:00
|
|
|
before_filter :require_admin, :except => :show
|
2011-08-02 04:04:47 +04:00
|
|
|
before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership]
|
|
|
|
accept_key_auth :index, :show, :create, :update, :destroy
|
2007-03-12 20:59:02 +03:00
|
|
|
|
|
|
|
include SortHelper
|
2011-05-30 22:52:25 +04:00
|
|
|
include CustomFieldsHelper
|
2006-07-09 20:30:01 +04:00
|
|
|
|
|
|
|
def index
|
2007-03-12 20:59:02 +03:00
|
|
|
sort_init 'login', 'asc'
|
2008-12-24 13:03:13 +03:00
|
|
|
sort_update %w(login firstname lastname mail admin created_on last_login_on)
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-12-11 16:13:49 +03:00
|
|
|
case params[:format]
|
|
|
|
when 'xml', 'json'
|
|
|
|
@offset, @limit = api_offset_and_limit
|
|
|
|
else
|
|
|
|
@limit = per_page_option
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2011-03-16 21:20:08 +03:00
|
|
|
scope = User
|
|
|
|
scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present?
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-02-03 22:53:52 +03:00
|
|
|
@status = params[:status] ? params[:status].to_i : 1
|
2008-10-24 20:59:15 +04:00
|
|
|
c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
|
|
|
|
|
|
|
|
unless params[:name].blank?
|
|
|
|
name = "%#{params[:name].strip.downcase}%"
|
2009-10-24 16:21:09 +04:00
|
|
|
c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
|
2008-10-24 20:59:15 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2011-03-16 21:20:08 +03:00
|
|
|
@user_count = scope.count(:conditions => c.conditions)
|
2010-12-11 16:13:49 +03:00
|
|
|
@user_pages = Paginator.new self, @user_count, @limit, params['page']
|
|
|
|
@offset ||= @user_pages.current.offset
|
2011-03-16 21:20:08 +03:00
|
|
|
@users = scope.find :all,
|
2010-12-11 16:13:49 +03:00
|
|
|
:order => sort_clause,
|
2008-10-24 20:59:15 +04:00
|
|
|
:conditions => c.conditions,
|
2010-12-11 16:13:49 +03:00
|
|
|
:limit => @limit,
|
|
|
|
:offset => @offset
|
2007-03-12 20:59:02 +03:00
|
|
|
|
2011-03-16 22:36:53 +03:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2011-03-16 21:20:08 +03:00
|
|
|
@groups = Group.all.sort
|
|
|
|
render :layout => !request.xhr?
|
|
|
|
}
|
2010-12-04 20:43:39 +03:00
|
|
|
format.api
|
2011-05-30 22:52:25 +04:00
|
|
|
end
|
2006-07-09 20:30:01 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-10-29 21:37:00 +03:00
|
|
|
def show
|
2010-08-11 03:07:44 +04:00
|
|
|
# show projects based on current user visibility
|
|
|
|
@memberships = @user.memberships.all(:conditions => Project.visible_by(User.current))
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-10-29 21:37:00 +03:00
|
|
|
events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
|
|
|
|
@events_by_day = events.group_by(&:event_date)
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-02-26 18:50:16 +03:00
|
|
|
unless User.current.admin?
|
|
|
|
if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
|
|
|
|
render_404
|
|
|
|
return
|
|
|
|
end
|
2009-10-29 21:37:00 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-12-03 12:39:56 +03:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :layout => 'base' }
|
2010-12-04 20:43:39 +03:00
|
|
|
format.api
|
2010-12-03 12:39:56 +03:00
|
|
|
end
|
2009-10-29 21:37:00 +03:00
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
|
2010-09-29 20:00:45 +04:00
|
|
|
def new
|
2010-12-12 17:19:24 +03:00
|
|
|
@user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
|
2010-09-28 19:28:50 +04:00
|
|
|
@auth_sources = AuthSource.find(:all)
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-09-28 19:28:50 +04:00
|
|
|
verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
|
|
|
|
def create
|
2010-12-12 17:19:24 +03:00
|
|
|
@user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
|
2010-12-12 16:19:07 +03:00
|
|
|
@user.safe_attributes = params[:user]
|
2010-09-28 19:28:50 +04:00
|
|
|
@user.admin = params[:user][:admin] || false
|
|
|
|
@user.login = params[:user][:login]
|
2011-03-10 15:46:48 +03:00
|
|
|
@user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] if @user.change_password_allowed?
|
2010-09-29 02:13:06 +04:00
|
|
|
|
|
|
|
# TODO: Similar to My#account
|
|
|
|
@user.pref.attributes = params[:pref]
|
|
|
|
@user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
|
|
|
|
|
2010-09-28 19:28:50 +04:00
|
|
|
if @user.save
|
2010-09-29 02:13:06 +04:00
|
|
|
@user.pref.save
|
2010-12-12 17:19:24 +03:00
|
|
|
@user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
|
2010-09-29 02:13:06 +04:00
|
|
|
|
2010-12-12 18:26:55 +03:00
|
|
|
Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information]
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-12-03 12:39:56 +03:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:notice] = l(:notice_successful_create)
|
2011-05-30 22:52:25 +04:00
|
|
|
redirect_to(params[:continue] ?
|
|
|
|
{:controller => 'users', :action => 'new'} :
|
2010-12-03 12:39:56 +03:00
|
|
|
{:controller => 'users', :action => 'edit', :id => @user}
|
|
|
|
)
|
|
|
|
}
|
2010-12-04 20:43:39 +03:00
|
|
|
format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
|
2010-12-03 12:39:56 +03:00
|
|
|
end
|
2006-07-09 20:30:01 +04:00
|
|
|
else
|
2010-09-28 19:28:50 +04:00
|
|
|
@auth_sources = AuthSource.find(:all)
|
2010-12-12 16:39:55 +03:00
|
|
|
# Clear password input
|
|
|
|
@user.password = @user.password_confirmation = nil
|
2010-09-29 02:13:06 +04:00
|
|
|
|
2010-12-03 12:39:56 +03:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => 'new' }
|
2010-12-03 14:51:06 +03:00
|
|
|
format.api { render_validation_errors(@user) }
|
2010-12-03 12:39:56 +03:00
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
2006-07-09 20:30:01 +04:00
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
|
2006-07-09 20:30:01 +04:00
|
|
|
def edit
|
2007-03-12 20:59:02 +03:00
|
|
|
@auth_sources = AuthSource.find(:all)
|
|
|
|
@membership ||= Member.new
|
2010-09-30 22:22:46 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-09-30 22:22:46 +04:00
|
|
|
verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
|
|
|
|
def update
|
|
|
|
@user.admin = params[:user][:admin] if params[:user][:admin]
|
|
|
|
@user.login = params[:user][:login] if params[:user][:login]
|
2011-03-10 15:46:48 +03:00
|
|
|
@user.safe_attributes = params[:user]
|
|
|
|
if params[:user][:password].present? && @user.change_password_allowed?
|
2010-12-12 16:39:55 +03:00
|
|
|
@user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
|
2010-09-30 22:22:46 +04:00
|
|
|
end
|
|
|
|
# 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
|
2010-12-12 17:19:24 +03:00
|
|
|
@user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
|
2010-09-30 22:22:46 +04:00
|
|
|
|
|
|
|
if was_activated
|
|
|
|
Mailer.deliver_account_activated(@user)
|
2011-03-10 15:46:48 +03:00
|
|
|
elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.change_password_allowed?
|
2010-12-12 16:39:55 +03:00
|
|
|
Mailer.deliver_account_information(@user, params[:user][:password])
|
2010-09-30 22:22:46 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-12-03 12:39:56 +03:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:notice] = l(:notice_successful_update)
|
|
|
|
redirect_to :back
|
|
|
|
}
|
2010-12-03 14:45:55 +03:00
|
|
|
format.api { head :ok }
|
2010-12-03 12:39:56 +03:00
|
|
|
end
|
2010-09-30 22:22:46 +04:00
|
|
|
else
|
|
|
|
@auth_sources = AuthSource.find(:all)
|
|
|
|
@membership ||= Member.new
|
2010-12-12 16:39:55 +03:00
|
|
|
# Clear password input
|
|
|
|
@user.password = @user.password_confirmation = nil
|
2010-09-30 22:22:46 +04:00
|
|
|
|
2010-12-03 12:39:56 +03:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => :edit }
|
2010-12-03 14:51:06 +03:00
|
|
|
format.api { render_validation_errors(@user) }
|
2010-12-03 12:39:56 +03:00
|
|
|
end
|
2010-09-30 22:22:46 +04:00
|
|
|
end
|
2009-09-12 12:36:46 +04:00
|
|
|
rescue ::ActionController::RedirectBackError
|
|
|
|
redirect_to :controller => 'users', :action => 'edit', :id => @user
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
2010-09-30 22:22:46 +04:00
|
|
|
|
2011-08-02 04:04:47 +04:00
|
|
|
verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
|
|
|
|
def destroy
|
|
|
|
# Only allow to delete users with STATUS_REGISTERED for now
|
|
|
|
# It is assumed that these users are not yet references in any way
|
|
|
|
# from other objects.
|
|
|
|
return render_403 unless @user.deletable?
|
|
|
|
|
|
|
|
@user.destroy
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:notice] = l(:notice_successful_delete)
|
|
|
|
redirect_back_or_default(:action => 'index')
|
|
|
|
}
|
|
|
|
format.api { head :ok }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
def edit_membership
|
2010-03-18 18:49:11 +03:00
|
|
|
@membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
|
2008-05-02 19:21:21 +04:00
|
|
|
@membership.save if request.post?
|
2009-05-10 14:54:31 +04:00
|
|
|
respond_to do |format|
|
2010-08-11 02:37:00 +04:00
|
|
|
if @membership.valid?
|
|
|
|
format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
|
|
|
|
format.js {
|
|
|
|
render(:update) {|page|
|
|
|
|
page.replace_html "tab-content-memberships", :partial => 'users/memberships'
|
|
|
|
page.visual_effect(:highlight, "member-#{@membership.id}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
format.js {
|
|
|
|
render(:update) {|page|
|
|
|
|
page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
def destroy_membership
|
2009-09-12 12:36:46 +04:00
|
|
|
@membership = Member.find(params[:membership_id])
|
|
|
|
if request.post? && @membership.deletable?
|
|
|
|
@membership.destroy
|
|
|
|
end
|
2009-05-10 14:54:31 +04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
|
|
|
|
format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
|
|
|
|
end
|
2006-07-09 20:30:01 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-12-20 20:45:09 +03:00
|
|
|
private
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-12-20 20:45:09 +03:00
|
|
|
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
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|