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 @ 1304:6137548ba453

History | View | Annotate | Download (9.95 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
      @institution_name = @user.ssamr_user_detail.institution_name
75
    end
76
    
77
    # show projects based on current user visibility
78
    @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
79

    
80
    events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
81
    @events_by_day = events.group_by(&:event_date)
82

    
83
    unless User.current.admin?
84
      if !@user.active? || (@user != User.current  && @memberships.empty? && events.empty?)
85
        render_404
86
        return
87
      end
88
    end
89

    
90
    respond_to do |format|
91
      format.html { render :layout => 'base' }
92
      format.api
93
    end
94
  end
95

    
96
  def new      
97
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
98
    @auth_sources = AuthSource.find(:all)
99

    
100
    @ssamr_user_details = SsamrUserDetail.new
101
  end
102

    
103
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
104
  def create
105
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
106
    @user.safe_attributes = params[:user]
107
    @user = User.new(params[:user])    
108
    @user.admin = params[:user][:admin] || false
109
    @user.login = params[:user][:login]
110
    @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
111

    
112
    # TODO: Similar to My#account
113
    @user.pref.attributes = params[:pref]
114
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
115

    
116
    @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details])
117

    
118
    # associates the 2 objects
119
    @user.ssamr_user_detail = @ssamr_user_details
120

    
121
    if @user.save
122
      @user.pref.save
123
                 
124
      @ssamr_user_details.save!
125
          
126

    
127
      Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information]
128

    
129
      respond_to do |format|
130
        format.html {
131
          flash[:notice] = l(:notice_successful_create)
132
          redirect_to(params[:continue] ?
133
            {:controller => 'users', :action => 'new'} :
134
            {:controller => 'users', :action => 'edit', :id => @user}
135
          )
136
        }
137
        format.api  { render :action => 'show', :status => :created, :location => user_url(@user) }
138
      end
139
    else
140
      @auth_sources = AuthSource.find(:all)
141
      # Clear password input
142
      @user.password = @user.password_confirmation = nil
143

    
144
      respond_to do |format|
145
        format.html { render :action => 'new' }
146
        format.api  { render_validation_errors(@user) }
147
      end
148
    end
149
  end
150

    
151
  def edit
152
    
153
    @ssamr_user_details = @user.ssamr_user_detail
154
    
155
    if @user.ssamr_user_detail == nil
156
      @selected_institution_id = nil
157
    else
158
      @selected_institution_id = @user.ssamr_user_detail.institution_id.to_i    
159
    end
160
    
161
    @auth_sources = AuthSource.find(:all)
162
    @membership ||= Member.new
163
  end
164

    
165
  verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
166
  def update
167
    @user.admin = params[:user][:admin] if params[:user][:admin]
168
    @user.login = params[:user][:login] if params[:user][:login]
169
    if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
170
      @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
171
    end
172
    @user.safe_attributes = params[:user]
173
    # Was the account actived ? (do it before User#save clears the change)
174
    was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
175
    # TODO: Similar to My#account
176
    @user.pref.attributes = params[:pref]
177
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
178

    
179
    if @user.ssamr_user_detail == nil
180
      @ssamr_user_details = SsamrUserDetail.new()
181
      @user.ssamr_user_detail = @ssamr_user_details
182
    else
183
      @ssamr_user_details = @user.ssamr_user_detail
184
    end
185
    
186
    if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty?
187
      @ssamr_user_details.description = @user.ssamr_user_detail.description
188
      @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id
189
      @ssamr_user_details.other_institution = @user.ssamr_user_detail.other_institution
190
      @ssamr_user_details.institution_type = @user.ssamr_user_detail.institution_type
191

    
192
    else
193
      @ssamr_user_details.description = params[:ssamr_user_details][:description]
194
      @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id]
195
      @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution]
196
      @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type]
197
    end
198

    
199
    if @user.save
200
      @user.pref.save
201
      @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
202

    
203
      if was_activated
204
        Mailer.deliver_account_activated(@user)
205
      elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
206
        Mailer.deliver_account_information(@user, params[:user][:password])
207
      end
208

    
209
      respond_to do |format|
210
        format.html {
211
          flash[:notice] = l(:notice_successful_update)
212
          redirect_to :back
213
        }
214
        format.api  { head :ok }
215
      end
216
    else
217
      @auth_sources = AuthSource.find(:all)
218
      @membership ||= Member.new
219
      # Clear password input
220
      @user.password = @user.password_confirmation = nil
221

    
222
      respond_to do |format|
223
        format.html { render :action => :edit }
224
        format.api  { render_validation_errors(@user) }
225
      end
226
    end
227
  rescue ::ActionController::RedirectBackError
228
    redirect_to :controller => 'users', :action => 'edit', :id => @user
229
  end
230

    
231
  verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
232
  def destroy
233
    @user.destroy
234
    respond_to do |format|
235
      format.html { redirect_to(users_url) }
236
      format.api  { head :ok }
237
    end
238
  end
239

    
240
  def edit_membership
241
    @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
242
    @membership.save if request.post?
243
    respond_to do |format|
244
      if @membership.valid?
245
        format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
246
        format.js {
247
          render(:update) {|page|
248
            page.replace_html "tab-content-memberships", :partial => 'users/memberships'
249
            page.visual_effect(:highlight, "member-#{@membership.id}")
250
          }
251
        }
252
      else
253
        format.js {
254
          render(:update) {|page|
255
            page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
256
          }
257
        }
258
      end
259
    end
260
  end
261

    
262
  def destroy_membership
263
    @membership = Member.find(params[:membership_id])
264
    if request.post? && @membership.deletable?
265
      @membership.destroy
266
    end
267
    respond_to do |format|
268
      format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
269
      format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
270
    end
271
  end
272

    
273
  private
274

    
275
  def find_user
276
    if params[:id] == 'current'
277
      require_login || return
278
      @user = User.current
279
    else
280
      @user = User.find(params[:id])
281
    end
282
  rescue ActiveRecord::RecordNotFound
283
    render_404
284
  end
285
end