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

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