OSDN Git Service

Refactor: Extract method to the Member model
[redminele/redmine.git] / app / controllers / users_controller.rb
1 # Redmine - project management software
2 # Copyright (C) 2006-2009  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   layout 'admin'
20   
21   before_filter :require_admin, :except => :show
22
23   helper :sort
24   include SortHelper
25   helper :custom_fields
26   include CustomFieldsHelper   
27
28   def index
29     sort_init 'login', 'asc'
30     sort_update %w(login firstname lastname mail admin created_on last_login_on)
31     
32     @status = params[:status] ? params[:status].to_i : 1
33     c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
34
35     unless params[:name].blank?
36       name = "%#{params[:name].strip.downcase}%"
37       c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
38     end
39     
40     @user_count = User.count(:conditions => c.conditions)
41     @user_pages = Paginator.new self, @user_count,
42                                                                 per_page_option,
43                                                                 params['page']                                                          
44     @users =  User.find :all,:order => sort_clause,
45                         :conditions => c.conditions,
46                                                 :limit  =>  @user_pages.items_per_page,
47                                                 :offset =>  @user_pages.current.offset
48
49     render :layout => !request.xhr?     
50   end
51   
52   def show
53     @user = User.find(params[:id])
54     @custom_values = @user.custom_values
55     
56     # show only public projects and private projects that the logged in user is also a member of
57     @memberships = @user.memberships.select do |membership|
58       membership.project.is_public? || (User.current.member_of?(membership.project))
59     end
60     
61     events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
62     @events_by_day = events.group_by(&:event_date)
63     
64     unless User.current.admin?
65       if !@user.active? || (@user != User.current  && @memberships.empty? && events.empty?)
66         render_404
67         return
68       end
69     end
70     render :layout => 'base'
71
72   rescue ActiveRecord::RecordNotFound
73     render_404
74   end
75
76   def add
77     if request.get?
78       @user = User.new(:language => Setting.default_language)
79     else
80       @user = User.new(params[:user])
81       @user.admin = params[:user][:admin] || false
82       @user.login = params[:user][:login]
83       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
84       if @user.save
85         Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
86         flash[:notice] = l(:notice_successful_create)
87         redirect_to(params[:continue] ? {:controller => 'users', :action => 'add'} : 
88                                         {:controller => 'users', :action => 'edit', :id => @user})
89         return
90       end
91     end
92     @auth_sources = AuthSource.find(:all)
93   end
94
95   def edit
96     @user = User.find(params[:id])
97     if request.post?
98       @user.admin = params[:user][:admin] if params[:user][:admin]
99       @user.login = params[:user][:login] if params[:user][:login]
100       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id
101       @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
102       @user.attributes = params[:user]
103       # Was the account actived ? (do it before User#save clears the change)
104       was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
105       if @user.save
106         if was_activated
107           Mailer.deliver_account_activated(@user)
108         elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
109           Mailer.deliver_account_information(@user, params[:password])
110         end
111         flash[:notice] = l(:notice_successful_update)
112         redirect_to :back
113       end
114     end
115     @auth_sources = AuthSource.find(:all)
116     @membership ||= Member.new
117   rescue ::ActionController::RedirectBackError
118     redirect_to :controller => 'users', :action => 'edit', :id => @user
119   end
120   
121   def edit_membership
122     @user = User.find(params[:id])
123     @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
124     @membership.save if request.post?
125     respond_to do |format|
126        format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
127        format.js { 
128          render(:update) {|page| 
129            page.replace_html "tab-content-memberships", :partial => 'users/memberships'
130            page.visual_effect(:highlight, "member-#{@membership.id}")
131          }
132        }
133      end
134   end
135   
136   def destroy_membership
137     @user = User.find(params[:id])
138     @membership = Member.find(params[:membership_id])
139     if request.post? && @membership.deletable?
140       @membership.destroy
141     end
142     respond_to do |format|
143       format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
144       format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
145     end
146   end
147 end