OSDN Git Service

Moves @layout 'base'@ to ApplicationController.
[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     conditions = "status <> 0"
37     conditions = ["status=?", @status] unless @status == 0
38     
39     @user_count = User.count(:conditions => conditions)
40     @user_pages = Paginator.new self, @user_count,
41                                                                 per_page_option,
42                                                                 params['page']                                                          
43     @users =  User.find :all,:order => sort_clause,
44                         :conditions => conditions,
45                                                 :limit  =>  @user_pages.items_per_page,
46                                                 :offset =>  @user_pages.current.offset
47
48     render :action => "list", :layout => false if request.xhr?  
49   end
50
51   def add
52     if request.get?
53       @user = User.new(:language => Setting.default_language)
54     else
55       @user = User.new(params[:user])
56       @user.admin = params[:user][:admin] || false
57       @user.login = params[:user][:login]
58       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
59       if @user.save
60         Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
61         flash[:notice] = l(:notice_successful_create)
62         redirect_to :action => 'list'
63       end
64     end
65     @auth_sources = AuthSource.find(:all)
66   end
67
68   def edit
69     @user = User.find(params[:id])
70     if request.post?
71       @user.admin = params[:user][:admin] if params[:user][:admin]
72       @user.login = params[:user][:login] if params[:user][:login]
73       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id
74       if @user.update_attributes(params[:user])
75         flash[:notice] = l(:notice_successful_update)
76         # Give a string to redirect_to otherwise it would use status param as the response code
77         redirect_to(url_for(:action => 'list', :status => params[:status], :page => params[:page]))
78       end
79     end
80     @auth_sources = AuthSource.find(:all)
81     @roles = Role.find_all_givable
82     @projects = Project.find(:all, :order => 'name', :conditions => "status=#{Project::STATUS_ACTIVE}") - @user.projects
83     @membership ||= Member.new
84     @memberships = @user.memberships
85   end
86   
87   def edit_membership
88     @user = User.find(params[:id])
89     @membership = params[:membership_id] ? Member.find(params[:membership_id]) : Member.new(:user => @user)
90     @membership.attributes = params[:membership]
91     @membership.save if request.post?
92     redirect_to :action => 'edit', :id => @user, :tab => 'memberships'
93   end
94   
95   def destroy_membership
96     @user = User.find(params[:id])
97     Member.find(params[:membership_id]).destroy if request.post?
98     redirect_to :action => 'edit', :id => @user, :tab => 'memberships'
99   end
100 end