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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. helper :queries
  34. include QueriesHelper
  35. helper :user_queries
  36. include UserQueriesHelper
  37. require_sudo_mode :create, :update, :destroy
  38. def index
  39. use_session = !request.format.csv?
  40. retrieve_query(UserQuery, use_session)
  41. if @query.valid?
  42. scope = @query.results_scope
  43. @user_count = scope.count
  44. respond_to do |format|
  45. format.html do
  46. @limit = per_page_option
  47. @user_pages = Paginator.new @user_count, @limit, params['page']
  48. @offset ||= @user_pages.offset
  49. @users = scope.limit(@limit).offset(@offset).to_a
  50. render :layout => !request.xhr?
  51. end
  52. format.csv do
  53. # Export all entries
  54. @entries = scope.to_a
  55. send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'users.csv')
  56. end
  57. format.api do
  58. @offset, @limit = api_offset_and_limit
  59. @users = scope.limit(@limit).offset(@offset).to_a
  60. end
  61. end
  62. else
  63. respond_to do |format|
  64. format.html {render :layout => !request.xhr?}
  65. format.csv {head :unprocessable_entity}
  66. format.api {render_validation_errors(@query)}
  67. end
  68. end
  69. end
  70. def show
  71. unless @user.visible?
  72. render_404
  73. return
  74. end
  75. # show projects based on current user visibility
  76. @memberships = @user.memberships.preload(:roles, :project).where(Project.visible_condition(User.current)).to_a
  77. @issue_counts = {}
  78. @issue_counts[:assigned] = {
  79. :total => Issue.visible.assigned_to(@user).count,
  80. :open => Issue.visible.open.assigned_to(@user).count
  81. }
  82. @issue_counts[:reported] = {
  83. :total => Issue.visible.where(:author_id => @user.id).count,
  84. :open => Issue.visible.open.where(:author_id => @user.id).count
  85. }
  86. respond_to do |format|
  87. format.html do
  88. events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
  89. @events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)}
  90. render :layout => 'base'
  91. end
  92. format.api
  93. end
  94. end
  95. def new
  96. @user = User.new(:language => Setting.default_language,
  97. :mail_notification => Setting.default_notification_option)
  98. @user.safe_attributes = params[:user]
  99. @auth_sources = AuthSource.all
  100. end
  101. def create
  102. @user = User.new(:language => Setting.default_language,
  103. :mail_notification => Setting.default_notification_option,
  104. :admin => false)
  105. @user.safe_attributes = params[:user]
  106. unless @user.auth_source_id
  107. @user.password = params[:user][:password]
  108. @user.password_confirmation = params[:user][:password_confirmation]
  109. end
  110. @user.pref.safe_attributes = params[:pref]
  111. if @user.save
  112. Mailer.deliver_account_information(@user, @user.password) if params[:send_information]
  113. respond_to do |format|
  114. format.html do
  115. flash[:notice] =
  116. l(:notice_user_successful_create,
  117. :id => view_context.link_to(@user.login, user_path(@user)))
  118. if params[:continue]
  119. attrs = {:generate_password => @user.generate_password}
  120. redirect_to new_user_path(:user => attrs)
  121. else
  122. redirect_to edit_user_path(@user)
  123. end
  124. end
  125. format.api {render :action => 'show', :status => :created, :location => user_url(@user)}
  126. end
  127. else
  128. @auth_sources = AuthSource.all
  129. # Clear password input
  130. @user.password = @user.password_confirmation = nil
  131. respond_to do |format|
  132. format.html {render :action => 'new'}
  133. format.api {render_validation_errors(@user)}
  134. end
  135. end
  136. end
  137. def edit
  138. @auth_sources = AuthSource.all
  139. @membership ||= Member.new
  140. end
  141. def update
  142. is_updating_password = params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
  143. if is_updating_password
  144. @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
  145. end
  146. @user.safe_attributes = params[:user]
  147. # Was the account actived ? (do it before User#save clears the change)
  148. was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
  149. # TODO: Similar to My#account
  150. @user.pref.safe_attributes = params[:pref]
  151. if @user.save
  152. @user.pref.save
  153. Mailer.deliver_password_updated(@user, User.current) if is_updating_password
  154. if was_activated
  155. Mailer.deliver_account_activated(@user)
  156. elsif @user.active? && params[:send_information] && @user != User.current
  157. Mailer.deliver_account_information(@user, @user.password)
  158. end
  159. respond_to do |format|
  160. format.html do
  161. flash[:notice] = l(:notice_successful_update)
  162. redirect_to_referer_or edit_user_path(@user)
  163. end
  164. format.api {render_api_ok}
  165. end
  166. else
  167. @auth_sources = AuthSource.all
  168. @membership ||= Member.new
  169. # Clear password input
  170. @user.password = @user.password_confirmation = nil
  171. respond_to do |format|
  172. format.html {render :action => :edit}
  173. format.api {render_validation_errors(@user)}
  174. end
  175. end
  176. end
  177. def destroy
  178. return render_error status: 422 if @user == User.current && !@user.own_account_deletable?
  179. if api_request? || params[:lock] || params[:confirm] == @user.login
  180. if params[:lock]
  181. @user.update_attribute :status, User::STATUS_LOCKED
  182. else
  183. @user.destroy
  184. end
  185. respond_to do |format|
  186. format.html {redirect_back_or_default(users_path)}
  187. format.api {render_api_ok}
  188. end
  189. end
  190. end
  191. def bulk_destroy
  192. @users = User.logged.where(id: params[:ids]).where.not(id: User.current)
  193. (render_404; return) unless @users.any?
  194. if params[:lock]
  195. @users.update_all status: User::STATUS_LOCKED
  196. flash[:notice] = l(:notice_successful_update)
  197. redirect_to users_path
  198. elsif params[:confirm] == I18n.t(:general_text_Yes)
  199. @users.destroy_all
  200. flash[:notice] = l(:notice_successful_delete)
  201. redirect_to users_path
  202. end
  203. end
  204. private
  205. def find_user(logged = true)
  206. if params[:id] == 'current'
  207. require_login || return
  208. @user = User.current
  209. elsif logged
  210. @user = User.logged.find(params[:id])
  211. else
  212. @user = User.find(params[:id])
  213. end
  214. rescue ActiveRecord::RecordNotFound
  215. render_404
  216. end
  217. end