You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users_controller.rb 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. class UsersController < ApplicationController
  18. layout 'admin'
  19. self.main_menu = false
  20. before_action :require_admin, :except => :show
  21. before_action ->{ find_user(false) }, :only => :show
  22. before_action :find_user, :only => [:edit, :update, :destroy]
  23. accept_api_auth :index, :show, :create, :update, :destroy
  24. helper :sort
  25. include SortHelper
  26. helper :custom_fields
  27. include CustomFieldsHelper
  28. include UsersHelper
  29. helper :principal_memberships
  30. helper :activities
  31. include ActivitiesHelper
  32. require_sudo_mode :create, :update, :destroy
  33. def index
  34. sort_init 'login', 'asc'
  35. sort_update %w(login firstname lastname admin created_on last_login_on)
  36. case params[:format]
  37. when 'xml', 'json'
  38. @offset, @limit = api_offset_and_limit
  39. else
  40. @limit = per_page_option
  41. end
  42. @status = params[:status] || 1
  43. scope = User.logged.status(@status).preload(:email_address)
  44. scope = scope.like(params[:name]) if params[:name].present?
  45. scope = scope.in_group(params[:group_id]) if params[:group_id].present?
  46. @user_count = scope.count
  47. @user_pages = Paginator.new @user_count, @limit, params['page']
  48. @offset ||= @user_pages.offset
  49. @users = scope.order(sort_clause).limit(@limit).offset(@offset).to_a
  50. respond_to do |format|
  51. format.html {
  52. @groups = Group.givable.sort
  53. render :layout => !request.xhr?
  54. }
  55. format.csv {
  56. send_data(users_to_csv(scope.order(sort_clause)), :type => 'text/csv; header=present', :filename => 'users.csv')
  57. }
  58. format.api
  59. end
  60. end
  61. def show
  62. unless @user.visible?
  63. render_404
  64. return
  65. end
  66. # show projects based on current user visibility
  67. @memberships = @user.memberships.preload(:roles, :project).where(Project.visible_condition(User.current)).to_a
  68. respond_to do |format|
  69. format.html {
  70. events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
  71. @events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)}
  72. render :layout => 'base'
  73. }
  74. format.api
  75. end
  76. end
  77. def new
  78. @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
  79. @user.safe_attributes = params[:user]
  80. @auth_sources = AuthSource.all
  81. end
  82. def create
  83. @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option, :admin => false)
  84. @user.safe_attributes = params[:user]
  85. @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
  86. @user.pref.safe_attributes = params[:pref]
  87. if @user.save
  88. Mailer.deliver_account_information(@user, @user.password) if params[:send_information]
  89. respond_to do |format|
  90. format.html {
  91. flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
  92. if params[:continue]
  93. attrs = {:generate_password => @user.generate_password }
  94. redirect_to new_user_path(:user => attrs)
  95. else
  96. redirect_to edit_user_path(@user)
  97. end
  98. }
  99. format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
  100. end
  101. else
  102. @auth_sources = AuthSource.all
  103. # Clear password input
  104. @user.password = @user.password_confirmation = nil
  105. respond_to do |format|
  106. format.html { render :action => 'new' }
  107. format.api { render_validation_errors(@user) }
  108. end
  109. end
  110. end
  111. def edit
  112. @auth_sources = AuthSource.all
  113. @membership ||= Member.new
  114. end
  115. def update
  116. if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
  117. @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
  118. end
  119. @user.safe_attributes = params[:user]
  120. # Was the account actived ? (do it before User#save clears the change)
  121. was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
  122. # TODO: Similar to My#account
  123. @user.pref.safe_attributes = params[:pref]
  124. if @user.save
  125. @user.pref.save
  126. if was_activated
  127. Mailer.deliver_account_activated(@user)
  128. elsif @user.active? && params[:send_information] && @user != User.current
  129. Mailer.deliver_account_information(@user, @user.password)
  130. end
  131. respond_to do |format|
  132. format.html {
  133. flash[:notice] = l(:notice_successful_update)
  134. redirect_to_referer_or edit_user_path(@user)
  135. }
  136. format.api { render_api_ok }
  137. end
  138. else
  139. @auth_sources = AuthSource.all
  140. @membership ||= Member.new
  141. # Clear password input
  142. @user.password = @user.password_confirmation = nil
  143. respond_to do |format|
  144. format.html { render :action => :edit }
  145. format.api { render_validation_errors(@user) }
  146. end
  147. end
  148. end
  149. def destroy
  150. @user.destroy
  151. respond_to do |format|
  152. format.html { redirect_back_or_default(users_path) }
  153. format.api { render_api_ok }
  154. end
  155. end
  156. private
  157. def find_user(logged = true)
  158. if params[:id] == 'current'
  159. require_login || return
  160. @user = User.current
  161. elsif logged
  162. @user = User.logged.find(params[:id])
  163. else
  164. @user = User.find(params[:id])
  165. end
  166. rescue ActiveRecord::RecordNotFound
  167. render_404
  168. end
  169. end