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 @ 1592:72d9219f2f19

History | View | Annotate | Download (8.36 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2014  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 @user_count, @limit, params['page']
49
    @offset ||= @user_pages.offset
50
    @users =  scope.order(sort_clause).limit(@limit).offset(@offset).all
51

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

    
61
  def show
62

    
63
    if @user.ssamr_user_detail != nil
64
      @description = @user.ssamr_user_detail.description
65
      @institution_name = @user.ssamr_user_detail.institution_name
66
    end
67
    
68
    # show projects based on current user visibility
69
    @memberships = @user.memberships.where(Project.visible_condition(User.current)).all
70

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

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

    
81
    respond_to do |format|
82
      format.html { render :layout => 'base' }
83
      format.api
84
    end
85
  end
86

    
87
  def new      
88
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
89
    @user.safe_attributes = params[:user]
90
    @auth_sources = AuthSource.all
91
    @ssamr_user_details = SsamrUserDetail.new
92
  end
93

    
94
  def create
95
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
96
    @user.safe_attributes = params[:user]
97
    @user = User.new(params[:user])    
98
    @user.admin = params[:user][:admin] || false
99
    @user.login = params[:user][:login]
100
    @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
101
    # TODO: Similar to My#account
102
    @user.pref.attributes = params[:pref]
103
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
104

    
105
    @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details])
106

    
107
    # associates the 2 objects
108
    @user.ssamr_user_detail = @ssamr_user_details
109

    
110

    
111
    if @user.save
112
      @user.pref.save
113
      @ssamr_user_details.save!
114

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

    
117
      respond_to do |format|
118
        format.html {
119
          flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
120
          if params[:continue]
121
            attrs = params[:user].slice(:generate_password)
122
            redirect_to new_user_path(:user => attrs)
123
          else
124
            redirect_to edit_user_path(@user)
125
          end
126
        }
127
        format.api  { render :action => 'show', :status => :created, :location => user_url(@user) }
128
      end
129
    else
130
      @auth_sources = AuthSource.all
131
      # Clear password input
132
      @user.password = @user.password_confirmation = nil
133

    
134
      respond_to do |format|
135
        format.html { render :action => 'new' }
136
        format.api  { render_validation_errors(@user) }
137
      end
138
    end
139
  end
140

    
141
  def edit
142
    @auth_sources = AuthSource.all
143
    
144
    @ssamr_user_details = @user.ssamr_user_detail
145
    
146
    if @user.ssamr_user_detail == nil
147
      @selected_institution_id = nil
148
    else
149
      @selected_institution_id = @user.ssamr_user_detail.institution_id.to_i    
150
    end
151
    
152
    @membership ||= Member.new
153
  end
154

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

    
167
    if @user.ssamr_user_detail == nil
168
      @ssamr_user_details = SsamrUserDetail.new()
169
      @user.ssamr_user_detail = @ssamr_user_details
170
    else
171
      @ssamr_user_details = @user.ssamr_user_detail
172
    end
173
    
174
    if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty?
175
      @ssamr_user_details.description = @user.ssamr_user_detail.description
176
      @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id
177
      @ssamr_user_details.other_institution = @user.ssamr_user_detail.other_institution
178
      @ssamr_user_details.institution_type = @user.ssamr_user_detail.institution_type
179

    
180
    else
181
      @ssamr_user_details.description = params[:ssamr_user_details][:description]
182
      @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id]
183
      @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution]
184
      @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type]
185
    end
186

    
187
    if @user.save
188
      @user.pref.save
189

    
190
      if was_activated
191
        Mailer.account_activated(@user).deliver
192
      elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil?
193
        Mailer.account_information(@user, @user.password).deliver
194
      end
195

    
196
      respond_to do |format|
197
        format.html {
198
          flash[:notice] = l(:notice_successful_update)
199
          redirect_to_referer_or edit_user_path(@user)
200
        }
201
        format.api  { render_api_ok }
202
      end
203
    else
204
      @auth_sources = AuthSource.all
205
      @membership ||= Member.new
206
      # Clear password input
207
      @user.password = @user.password_confirmation = nil
208

    
209
      respond_to do |format|
210
        format.html { render :action => :edit }
211
        format.api  { render_validation_errors(@user) }
212
      end
213
    end
214
  end
215

    
216
  def destroy
217
    @user.destroy
218
    respond_to do |format|
219
      format.html { redirect_back_or_default(users_path) }
220
      format.api  { render_api_ok }
221
    end
222
  end
223

    
224
  def edit_membership
225
    @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
226
    @membership.save
227
    respond_to do |format|
228
      format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
229
      format.js
230
    end
231
  end
232

    
233
  def destroy_membership
234
    @membership = Member.find(params[:membership_id])
235
    if @membership.deletable?
236
      @membership.destroy
237
    end
238
    respond_to do |format|
239
      format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
240
      format.js
241
    end
242
  end
243

    
244
  private
245

    
246
  def find_user
247
    if params[:id] == 'current'
248
      require_login || return
249
      @user = User.current
250
    else
251
      @user = User.find(params[:id])
252
    end
253
  rescue ActiveRecord::RecordNotFound
254
    render_404
255
  end
256
end