To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / users_controller.rb @ 832:cbd0b4560105

History | View | Annotate | Download (10.3 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  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
  before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership]
23
  accept_api_auth :index, :show, :create, :update, :destroy
24

    
25
  helper :sort
26
  include SortHelper
27
  helper :custom_fields
28
  include CustomFieldsHelper   
29

    
30
  def index
31
    sort_init 'login', 'asc'
32
    sort_update %w(login firstname lastname mail admin created_on last_login_on)
33
    
34
    case params[:format]
35
    when 'xml', 'json'
36
      @offset, @limit = api_offset_and_limit
37
    else
38
      @limit = per_page_option
39
    end
40
    
41
    scope = User
42
    scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present?
43
    
44
    @status = params[:status] ? params[:status].to_i : 1
45
    c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
46

    
47
    unless params[:name].blank?
48
      name = "%#{params[:name].strip.downcase}%"
49
      c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
50
    end
51
    
52
    @user_count = scope.count(:conditions => c.conditions)
53
    @user_pages = Paginator.new self, @user_count, @limit, params['page']
54
    @offset ||= @user_pages.current.offset
55
    @users =  scope.find :all,
56
                        :order => sort_clause,
57
                        :conditions => c.conditions,
58
                        :limit  =>  @limit,
59
                        :offset =>  @offset
60

    
61
    respond_to do |format|
62
      format.html {
63
        @groups = Group.all.sort
64
        render :layout => !request.xhr?
65
      }
66
      format.api
67
    end        
68
  end
69
  
70
  def show
71

    
72
    if @user.ssamr_user_detail != nil
73
      @description = @user.ssamr_user_detail.description
74
      
75
      if @user.ssamr_user_detail.institution_type != nil
76
        # institution_type is true for listed institutions
77
        if (@user.ssamr_user_detail.institution_type)
78
          @institution_name = Institution.find(@user.ssamr_user_detail.institution_id).name
79
        else
80
          @institution_name = @user.ssamr_user_detail.other_institution
81
        end
82
      end
83
    end
84
    
85
    # show projects based on current user visibility
86
    @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
87
    
88
    events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
89
    @events_by_day = events.group_by(&:event_date)
90
    
91
    unless User.current.admin?
92
      if !@user.active? || (@user != User.current  && @memberships.empty? && events.empty?)
93
        render_404
94
        return
95
      end
96
    end
97
    
98
    respond_to do |format|
99
      format.html { render :layout => 'base' }
100
      format.api
101
    end
102
  end
103

    
104
  def new      
105
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
106
    @auth_sources = AuthSource.find(:all)
107

    
108
    @ssamr_user_details = SsamrUserDetail.new
109
  end
110
  
111
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
112
  def create
113
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
114
    @user.safe_attributes = params[:user]
115
    @user = User.new(params[:user])    
116
    @user.admin = params[:user][:admin] || false
117
    @user.login = params[:user][:login]
118
    @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
119

    
120
    # TODO: Similar to My#account
121
    @user.pref.attributes = params[:pref]
122
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
123

    
124
    @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details])
125

    
126
    # associates the 2 objects
127
    @user.ssamr_user_detail = @ssamr_user_details
128

    
129
    if @user.save
130
      @user.pref.save
131
                 
132
      @ssamr_user_details.save!
133
          
134

    
135
      Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information]
136
      
137
      respond_to do |format|
138
        format.html {
139
          flash[:notice] = l(:notice_successful_create)
140
          redirect_to(params[:continue] ? 
141
            {:controller => 'users', :action => 'new'} : 
142
            {:controller => 'users', :action => 'edit', :id => @user}
143
          )
144
        }
145
        format.api  { render :action => 'show', :status => :created, :location => user_url(@user) }
146
      end
147
    else
148
      @auth_sources = AuthSource.find(:all)
149
      # Clear password input
150
      @user.password = @user.password_confirmation = nil
151

    
152
      respond_to do |format|
153
        format.html { render :action => 'new' }
154
        format.api  { render_validation_errors(@user) }
155
      end
156
    end
157
  end
158

    
159
  def edit
160
    
161
    @ssamr_user_details = @user.ssamr_user_detail
162
    
163
    if @user.ssamr_user_detail == nil
164
      @selected_institution_id = nil
165
    else
166
      @selected_institution_id = @user.ssamr_user_detail.institution_id.to_i    
167
    end
168
    
169
    @auth_sources = AuthSource.find(:all)
170
    @membership ||= Member.new
171
  end
172
  
173
  verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
174
  def update
175
    @user.admin = params[:user][:admin] if params[:user][:admin]
176
    @user.login = params[:user][:login] if params[:user][:login]
177
    if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
178
      @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
179
    end
180
    @user.safe_attributes = params[:user]
181
    # Was the account actived ? (do it before User#save clears the change)
182
    was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
183
    # TODO: Similar to My#account
184
    @user.pref.attributes = params[:pref]
185
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
186

    
187
    if @user.ssamr_user_detail == nil
188
      @ssamr_user_details = SsamrUserDetail.new()
189
      @user.ssamr_user_detail = @ssamr_user_details
190
    else
191
      @ssamr_user_details = @user.ssamr_user_detail
192
    end
193
    
194
    if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty?
195
      @ssamr_user_details.description = @user.ssamr_user_detail.description
196
      @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id
197
      @ssamr_user_details.other_institution = @user.ssamr_user_detail.other_institution
198
      @ssamr_user_details.institution_type = @user.ssamr_user_detail.institution_type
199

    
200
    else
201
      @ssamr_user_details.description = params[:ssamr_user_details][:description]
202
      @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id]
203
      @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution]
204
      @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type]
205
    end
206

    
207
    if @user.save
208
      @user.pref.save
209
      @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
210

    
211
      if was_activated
212
        Mailer.deliver_account_activated(@user)
213
      elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
214
        Mailer.deliver_account_information(@user, params[:user][:password])
215
      end
216
      
217
      respond_to do |format|
218
        format.html {
219
          flash[:notice] = l(:notice_successful_update)
220
          redirect_to :back
221
        }
222
        format.api  { head :ok }
223
      end
224
    else
225
      @auth_sources = AuthSource.find(:all)
226
      @membership ||= Member.new
227
      # Clear password input
228
      @user.password = @user.password_confirmation = nil
229

    
230
      respond_to do |format|
231
        format.html { render :action => :edit }
232
        format.api  { render_validation_errors(@user) }
233
      end
234
    end
235
  rescue ::ActionController::RedirectBackError
236
    redirect_to :controller => 'users', :action => 'edit', :id => @user
237
  end
238

    
239
  verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
240
  def destroy
241
    @user.destroy
242
    respond_to do |format|
243
      format.html { redirect_to(users_url) }
244
      format.api  { head :ok }
245
    end
246
  end
247

    
248
  def edit_membership
249
    @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
250
    @membership.save if request.post?
251
    respond_to do |format|
252
      if @membership.valid?
253
        format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
254
        format.js {
255
          render(:update) {|page|
256
            page.replace_html "tab-content-memberships", :partial => 'users/memberships'
257
            page.visual_effect(:highlight, "member-#{@membership.id}")
258
          }
259
        }
260
      else
261
        format.js {
262
          render(:update) {|page|
263
            page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
264
          }
265
        }
266
      end
267
    end
268
  end
269
  
270
  def destroy_membership
271
    @membership = Member.find(params[:membership_id])
272
    if request.post? && @membership.deletable?
273
      @membership.destroy
274
    end
275
    respond_to do |format|
276
      format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
277
      format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
278
    end
279
  end
280
  
281
  private
282
  
283
  def find_user
284
    if params[:id] == 'current'
285
      require_login || return
286
      @user = User.current
287
    else
288
      @user = User.find(params[:id])
289
    end
290
  rescue ActiveRecord::RecordNotFound
291
    render_404
292
  end
293
end