OSDN Git Service

Restores object count and adds offset/limit attributes to API responses for paginated...
[redminele/redmine.git] / app / controllers / users_controller.rb
1 # Redmine - project management software
2 # Copyright (C) 2006-2010  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   accept_key_auth :index, :show, :create, :update
23
24   helper :sort
25   include SortHelper
26   helper :custom_fields
27   include CustomFieldsHelper   
28
29   def index
30     sort_init 'login', 'asc'
31     sort_update %w(login firstname lastname mail admin created_on last_login_on)
32     
33     case params[:format]
34     when 'xml', 'json'
35       @offset, @limit = api_offset_and_limit
36     else
37       @limit = per_page_option
38     end
39     
40     @status = params[:status] ? params[:status].to_i : 1
41     c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
42
43     unless params[:name].blank?
44       name = "%#{params[:name].strip.downcase}%"
45       c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
46     end
47     
48     @user_count = User.count(:conditions => c.conditions)
49     @user_pages = Paginator.new self, @user_count, @limit, params['page']
50     @offset ||= @user_pages.current.offset
51     @users =  User.find :all,
52                         :order => sort_clause,
53                         :conditions => c.conditions,
54                         :limit  =>  @limit,
55                         :offset =>  @offset
56
57                 respond_to do |format|
58                   format.html { render :layout => !request.xhr? }
59       format.api
60                 end     
61   end
62   
63   def show
64     @user = User.find(params[:id])
65     
66     # show projects based on current user visibility
67     @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current))
68     
69     events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
70     @events_by_day = events.group_by(&:event_date)
71     
72     unless User.current.admin?
73       if !@user.active? || (@user != User.current  && @memberships.empty? && events.empty?)
74         render_404
75         return
76       end
77     end
78     
79     respond_to do |format|
80       format.html { render :layout => 'base' }
81       format.api
82     end
83   rescue ActiveRecord::RecordNotFound
84     render_404
85   end
86
87   def new
88     @notification_options = User::MAIL_NOTIFICATION_OPTIONS
89     @notification_option = Setting.default_notification_option
90
91     @user = User.new(:language => Setting.default_language)
92     @auth_sources = AuthSource.find(:all)
93   end
94   
95   verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
96   def create
97     @notification_options = User::MAIL_NOTIFICATION_OPTIONS
98     @notification_option = Setting.default_notification_option
99
100     @user = User.new(params[:user])
101     @user.admin = params[:user][:admin] || false
102     @user.login = params[:user][:login]
103     @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
104
105     # TODO: Similar to My#account
106     @user.mail_notification = params[:notification_option] || 'only_my_events'
107     @user.pref.attributes = params[:pref]
108     @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
109
110     if @user.save
111       @user.pref.save
112       @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
113
114       Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
115       
116       respond_to do |format|
117         format.html {
118           flash[:notice] = l(:notice_successful_create)
119           redirect_to(params[:continue] ? 
120             {:controller => 'users', :action => 'new'} : 
121             {:controller => 'users', :action => 'edit', :id => @user}
122           )
123         }
124         format.api  { render :action => 'show', :status => :created, :location => user_url(@user) }
125       end
126     else
127       @auth_sources = AuthSource.find(:all)
128       @notification_option = @user.mail_notification
129
130       respond_to do |format|
131         format.html { render :action => 'new' }
132         format.api  { render_validation_errors(@user) }
133       end
134     end
135   end
136
137   def edit
138     @user = User.find(params[:id])
139     @notification_options = @user.valid_notification_options
140     @notification_option = @user.mail_notification
141
142     @auth_sources = AuthSource.find(:all)
143     @membership ||= Member.new
144   end
145   
146   verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
147   def update
148     @user = User.find(params[:id])
149     @notification_options = @user.valid_notification_options
150     @notification_option = @user.mail_notification
151
152     @user.admin = params[:user][:admin] if params[:user][:admin]
153     @user.login = params[:user][:login] if params[:user][:login]
154     if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
155       @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
156     end
157     @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
158     @user.attributes = params[:user]
159     # Was the account actived ? (do it before User#save clears the change)
160     was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
161     # TODO: Similar to My#account
162     @user.mail_notification = params[:notification_option] || 'only_my_events'
163     @user.pref.attributes = params[:pref]
164     @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
165
166     if @user.save
167       @user.pref.save
168       @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
169
170       if was_activated
171         Mailer.deliver_account_activated(@user)
172       elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
173         Mailer.deliver_account_information(@user, params[:password])
174       end
175       
176       respond_to do |format|
177         format.html {
178           flash[:notice] = l(:notice_successful_update)
179           redirect_to :back
180         }
181         format.api  { head :ok }
182       end
183     else
184       @auth_sources = AuthSource.find(:all)
185       @membership ||= Member.new
186
187       respond_to do |format|
188         format.html { render :action => :edit }
189         format.api  { render_validation_errors(@user) }
190       end
191     end
192   rescue ::ActionController::RedirectBackError
193     redirect_to :controller => 'users', :action => 'edit', :id => @user
194   end
195
196   def edit_membership
197     @user = User.find(params[:id])
198     @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
199     @membership.save if request.post?
200     respond_to do |format|
201       if @membership.valid?
202         format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
203         format.js {
204           render(:update) {|page|
205             page.replace_html "tab-content-memberships", :partial => 'users/memberships'
206             page.visual_effect(:highlight, "member-#{@membership.id}")
207           }
208         }
209       else
210         format.js {
211           render(:update) {|page|
212             page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
213           }
214         }
215       end
216     end
217   end
218   
219   def destroy_membership
220     @user = User.find(params[:id])
221     @membership = Member.find(params[:membership_id])
222     if request.post? && @membership.deletable?
223       @membership.destroy
224     end
225     respond_to do |format|
226       format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
227       format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
228     end
229   end
230 end