OSDN Git Service

Adds the ability to search for a user on the administration users list.
[redminele/redmine.git] / app / controllers / users_controller.rb
1 # redMine - project management software
2 # Copyright (C) 2006-2007  Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 class UsersController < ApplicationController
19   before_filter :require_admin
20
21   helper :sort
22   include SortHelper
23   helper :custom_fields
24   include CustomFieldsHelper   
25
26   def index
27     list
28     render :action => 'list' unless request.xhr?
29   end
30
31   def list
32     sort_init 'login', 'asc'
33     sort_update
34     
35     @status = params[:status] ? params[:status].to_i : 1
36     c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
37
38     unless params[:name].blank?
39       name = "%#{params[:name].strip.downcase}%"
40       c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ?", name, name, name]
41     end
42     
43     @user_count = User.count(:conditions => c.conditions)
44     @user_pages = Paginator.new self, @user_count,
45                                                                 per_page_option,
46                                                                 params['page']                                                          
47     @users =  User.find :all,:order => sort_clause,
48                         :conditions => c.conditions,
49                                                 :limit  =>  @user_pages.items_per_page,
50                                                 :offset =>  @user_pages.current.offset
51
52     render :action => "list", :layout => false if request.xhr?  
53   end
54
55   def add
56     if request.get?
57       @user = User.new(:language => Setting.default_language)
58     else
59       @user = User.new(params[:user])
60       @user.admin = params[:user][:admin] || false
61       @user.login = params[:user][:login]
62       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
63       if @user.save
64         Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
65         flash[:notice] = l(:notice_successful_create)
66         redirect_to :action => 'list'
67       end
68     end
69     @auth_sources = AuthSource.find(:all)
70   end
71
72   def edit
73     @user = User.find(params[:id])
74     if request.post?
75       @user.admin = params[:user][:admin] if params[:user][:admin]
76       @user.login = params[:user][:login] if params[:user][:login]
77       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id
78       if @user.update_attributes(params[:user])
79         flash[:notice] = l(:notice_successful_update)
80         # Give a string to redirect_to otherwise it would use status param as the response code
81         redirect_to(url_for(:action => 'list', :status => params[:status], :page => params[:page]))
82       end
83     end
84     @auth_sources = AuthSource.find(:all)
85     @roles = Role.find_all_givable
86     @projects = Project.find(:all, :order => 'name', :conditions => "status=#{Project::STATUS_ACTIVE}") - @user.projects
87     @membership ||= Member.new
88     @memberships = @user.memberships
89   end
90   
91   def edit_membership
92     @user = User.find(params[:id])
93     @membership = params[:membership_id] ? Member.find(params[:membership_id]) : Member.new(:user => @user)
94     @membership.attributes = params[:membership]
95     @membership.save if request.post?
96     redirect_to :action => 'edit', :id => @user, :tab => 'memberships'
97   end
98   
99   def destroy_membership
100     @user = User.find(params[:id])
101     Member.find(params[:membership_id]).destroy if request.post?
102     redirect_to :action => 'edit', :id => @user, :tab => 'memberships'
103   end
104 end