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 @ 1358:b8f94812d737

History | View | Annotate | Download (8.84 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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
    @status = params[:status] || 1
42

    
43
    scope = User.logged.status(@status)
44
    scope = scope.like(params[:name]) if params[:name].present?
45
    scope = scope.in_group(params[:group_id]) if params[:group_id].present?
46

    
47
    @user_count = scope.count
48
    @user_pages = Paginator.new self, @user_count, @limit, params['page']
49
    @offset ||= @user_pages.current.offset
50
    @users =  scope.find :all,
51
                        :order => sort_clause,
52
                        :limit  =>  @limit,
53
                        :offset =>  @offset
54

    
55
    respond_to do |format|
56
      format.html {
57
        @groups = Group.all.sort
58
        render :layout => !request.xhr?
59
      }
60
      format.api
61
    end        
62
  end
63

    
64
  def show
65

    
66
    if @user.ssamr_user_detail != nil
67
      @description = @user.ssamr_user_detail.description
68
      @institution_name = @user.ssamr_user_detail.institution_name
69
    end
70
    
71
    # show projects based on current user visibility
72
    @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
73

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

    
77
    unless User.current.admin?
78
      if !@user.active? || (@user != User.current  && @memberships.empty? && events.empty?)
79
        render_404
80
        return
81
      end
82
    end
83

    
84
    respond_to do |format|
85
      format.html { render :layout => 'base' }
86
      format.api
87
    end
88
  end
89

    
90
  def new      
91
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
92
    @auth_sources = AuthSource.find(:all)
93

    
94
    @ssamr_user_details = SsamrUserDetail.new
95
  end
96

    
97
  def create
98
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
99
    @user.safe_attributes = params[:user]
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[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
104

    
105
    # TODO: Similar to My#account
106
    @user.pref.attributes = params[:pref]
107
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
108

    
109
    @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details])
110

    
111
    # associates the 2 objects
112
    @user.ssamr_user_detail = @ssamr_user_details
113

    
114
    if @user.save
115
      @user.pref.attributes = params[:pref]
116
      @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
117
      @user.pref.save
118
                 
119
      @ssamr_user_details.save!
120
          
121

    
122
      Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information]
123

    
124
      respond_to do |format|
125
        format.html {
126
          flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
127
          redirect_to(params[:continue] ?
128
            {:controller => 'users', :action => 'new'} :
129
            {:controller => 'users', :action => 'edit', :id => @user}
130
          )
131
        }
132
        format.api  { render :action => 'show', :status => :created, :location => user_url(@user) }
133
      end
134
    else
135
      @auth_sources = AuthSource.find(:all)
136
      # Clear password input
137
      @user.password = @user.password_confirmation = nil
138

    
139
      respond_to do |format|
140
        format.html { render :action => 'new' }
141
        format.api  { render_validation_errors(@user) }
142
      end
143
    end
144
  end
145

    
146
  def edit
147
    
148
    @ssamr_user_details = @user.ssamr_user_detail
149
    
150
    if @user.ssamr_user_detail == nil
151
      @selected_institution_id = nil
152
    else
153
      @selected_institution_id = @user.ssamr_user_detail.institution_id.to_i    
154
    end
155
    
156
    @auth_sources = AuthSource.find(:all)
157
    @membership ||= Member.new
158
  end
159

    
160
  def update
161
    @user.admin = params[:user][:admin] if params[:user][:admin]
162
    @user.login = params[:user][:login] if params[:user][:login]
163
    if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
164
      @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
165
    end
166
    @user.safe_attributes = params[:user]
167
    # Was the account actived ? (do it before User#save clears the change)
168
    was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
169
    # TODO: Similar to My#account
170
    @user.pref.attributes = params[:pref]
171
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
172

    
173
    if @user.ssamr_user_detail == nil
174
      @ssamr_user_details = SsamrUserDetail.new()
175
      @user.ssamr_user_detail = @ssamr_user_details
176
    else
177
      @ssamr_user_details = @user.ssamr_user_detail
178
    end
179
    
180
    if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty?
181
      @ssamr_user_details.description = @user.ssamr_user_detail.description
182
      @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id
183
      @ssamr_user_details.other_institution = @user.ssamr_user_detail.other_institution
184
      @ssamr_user_details.institution_type = @user.ssamr_user_detail.institution_type
185

    
186
    else
187
      @ssamr_user_details.description = params[:ssamr_user_details][:description]
188
      @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id]
189
      @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution]
190
      @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type]
191
    end
192

    
193
    if @user.save
194
      @user.pref.save
195
      @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
196

    
197
      if was_activated
198
        Mailer.account_activated(@user).deliver
199
      elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
200
        Mailer.account_information(@user, params[:user][:password]).deliver
201
      end
202

    
203
      respond_to do |format|
204
        format.html {
205
          flash[:notice] = l(:notice_successful_update)
206
          redirect_to_referer_or edit_user_path(@user)
207
        }
208
        format.api  { render_api_ok }
209
      end
210
    else
211
      @auth_sources = AuthSource.find(:all)
212
      @membership ||= Member.new
213
      # Clear password input
214
      @user.password = @user.password_confirmation = nil
215

    
216
      respond_to do |format|
217
        format.html { render :action => :edit }
218
        format.api  { render_validation_errors(@user) }
219
      end
220
    end
221
  end
222

    
223
  def destroy
224
    @user.destroy
225
    respond_to do |format|
226
      format.html { redirect_back_or_default(users_url) }
227
      format.api  { render_api_ok }
228
    end
229
  end
230

    
231
  def edit_membership
232
    @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
233
    @membership.save
234
    respond_to do |format|
235
      format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
236
      format.js
237
    end
238
  end
239

    
240
  def destroy_membership
241
    @membership = Member.find(params[:membership_id])
242
    if @membership.deletable?
243
      @membership.destroy
244
    end
245
    respond_to do |format|
246
      format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
247
      format.js
248
    end
249
  end
250

    
251
  private
252

    
253
  def find_user
254
    if params[:id] == 'current'
255
      require_login || return
256
      @user = User.current
257
    else
258
      @user = User.find(params[:id])
259
    end
260
  rescue ActiveRecord::RecordNotFound
261
    render_404
262
  end
263
end