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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class UsersController < ApplicationController
  19. layout 'admin'
  20. self.main_menu = false
  21. before_action :require_admin, :except => :show
  22. before_action lambda {find_user(false)}, :only => :show
  23. before_action :find_user, :only => [:edit, :update, :destroy]
  24. accept_api_auth :index, :show, :create, :update, :destroy
  25. helper :sort
  26. include SortHelper
  27. helper :custom_fields
  28. include CustomFieldsHelper
  29. include UsersHelper
  30. helper :principal_memberships
  31. helper :activities
  32. include ActivitiesHelper
  33. require_sudo_mode :create, :update, :destroy
  34. def index
  35. sort_init 'login', 'asc'
  36. sort_update %w(login firstname lastname admin created_on last_login_on)
  37. case params[:format]
  38. when 'xml', 'json'
  39. @offset, @limit = api_offset_and_limit
  40. else
  41. @limit = per_page_option
  42. end
  43. @status = params[:status] || 1
  44. scope = User.logged.status(@status).preload(:email_address)
  45. scope = scope.like(params[:name]) if params[:name].present?
  46. scope = scope.in_group(params[:group_id]) if params[:group_id].present?
  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).to_a
  51. respond_to do |format|
  52. format.html do
  53. @groups = Group.givable.sort
  54. render :layout => !request.xhr?
  55. end
  56. format.csv do
  57. send_data(users_to_csv(scope.order(sort_clause)), :type => 'text/csv; header=present', :filename => 'users.csv')
  58. end
  59. format.api
  60. end
  61. end
  62. def show
  63. unless @user.visible?
  64. render_404
  65. return
  66. end
  67. # show projects based on current user visibility
  68. @memberships = @user.memberships.preload(:roles, :project).where(Project.visible_condition(User.current)).to_a
  69. @issue_counts = {}
  70. @issue_counts[:assigned] = {
  71. :total => Issue.visible.assigned_to(@user).count,
  72. :open => Issue.visible.open.assigned_to(@user).count
  73. }
  74. @issue_counts[:reported] = {
  75. :total => Issue.visible.where(:author_id => @user.id).count,
  76. :open => Issue.visible.open.where(:author_id => @user.id).count
  77. }
  78. respond_to do |format|
  79. format.html do
  80. events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
  81. @events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)}
  82. render :layout => 'base'
  83. end
  84. format.api
  85. end
  86. end
  87. def new
  88. @user = User.new(:language => Setting.default_language,
  89. :mail_notification => Setting.default_notification_option)
  90. @user.safe_attributes = params[:user]
  91. @auth_sources = AuthSource.all
  92. end
  93. def create
  94. @user = User.new(:language => Setting.default_language,
  95. :mail_notification => Setting.default_notification_option,
  96. :admin => false)
  97. @user.safe_attributes = params[:user]
  98. unless @user.auth_source_id
  99. @user.password = params[:user][:password]
  100. @user.password_confirmation = params[:user][:password_confirmation]
  101. end
  102. @user.pref.safe_attributes = params[:pref]
  103. if @user.save
  104. Mailer.deliver_account_information(@user, @user.password) if params[:send_information]
  105. respond_to do |format|
  106. format.html do
  107. flash[:notice] =
  108. l(:notice_user_successful_create,
  109. :id => view_context.link_to(@user.login, user_path(@user)))
  110. if params[:continue]
  111. attrs = {:generate_password => @user.generate_password}
  112. redirect_to new_user_path(:user => attrs)
  113. else
  114. redirect_to edit_user_path(@user)
  115. end
  116. end
  117. format.api {render :action => 'show', :status => :created, :location => user_url(@user)}
  118. end
  119. else
  120. @auth_sources = AuthSource.all
  121. # Clear password input
  122. @user.password = @user.password_confirmation = nil
  123. respond_to do |format|
  124. format.html {render :action => 'new'}
  125. format.api {render_validation_errors(@user)}
  126. end
  127. end
  128. end
  129. def edit
  130. @auth_sources = AuthSource.all
  131. @membership ||= Member.new
  132. end
  133. def update
  134. is_updating_password = params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
  135. if is_updating_password
  136. @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
  137. end
  138. @user.safe_attributes = params[:user]
  139. # Was the account actived ? (do it before User#save clears the change)
  140. was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
  141. # TODO: Similar to My#account
  142. @user.pref.safe_attributes = params[:pref]
  143. if @user.save
  144. @user.pref.save
  145. Mailer.deliver_password_updated(@user, User.current) if is_updating_password
  146. if was_activated
  147. Mailer.deliver_account_activated(@user)
  148. elsif @user.active? && params[:send_information] && @user != User.current
  149. Mailer.deliver_account_information(@user, @user.password)
  150. end
  151. respond_to do |format|
  152. format.html do
  153. flash[:notice] = l(:notice_successful_update)
  154. redirect_to_referer_or edit_user_path(@user)
  155. end
  156. format.api {render_api_ok}
  157. end
  158. else
  159. @auth_sources = AuthSource.all
  160. @membership ||= Member.new
  161. # Clear password input
  162. @user.password = @user.password_confirmation = nil
  163. respond_to do |format|
  164. format.html {render :action => :edit}
  165. format.api {render_validation_errors(@user)}
  166. end
  167. end
  168. end
  169. def destroy
  170. return render_error status: 422 if @user == User.current && !@user.own_account_deletable?
  171. if api_request? || params[:lock] || params[:confirm] == @user.login
  172. if params[:lock]
  173. @user.update_attribute :status, User::STATUS_LOCKED
  174. else
  175. @user.destroy
  176. end
  177. respond_to do |format|
  178. format.html {redirect_back_or_default(users_path)}
  179. format.api {render_api_ok}
  180. end
  181. end
  182. end
  183. private
  184. def find_user(logged = true)
  185. if params[:id] == 'current'
  186. require_login || return
  187. @user = User.current
  188. elsif logged
  189. @user = User.logged.find(params[:id])
  190. else
  191. @user = User.find(params[:id])
  192. end
  193. rescue ActiveRecord::RecordNotFound
  194. render_404
  195. end
  196. end