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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 ->{ 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 {
  53. @groups = Group.givable.sort
  54. render :layout => !request.xhr?
  55. }
  56. format.csv {
  57. send_data(users_to_csv(scope.order(sort_clause)), :type => 'text/csv; header=present', :filename => 'users.csv')
  58. }
  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 {
  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. }
  84. format.api
  85. end
  86. end
  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. end
  92. def create
  93. @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option, :admin => false)
  94. @user.safe_attributes = params[:user]
  95. @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
  96. @user.pref.safe_attributes = params[:pref]
  97. if @user.save
  98. Mailer.deliver_account_information(@user, @user.password) if params[:send_information]
  99. respond_to do |format|
  100. format.html {
  101. flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
  102. if params[:continue]
  103. attrs = {:generate_password => @user.generate_password }
  104. redirect_to new_user_path(:user => attrs)
  105. else
  106. redirect_to edit_user_path(@user)
  107. end
  108. }
  109. format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
  110. end
  111. else
  112. @auth_sources = AuthSource.all
  113. # Clear password input
  114. @user.password = @user.password_confirmation = nil
  115. respond_to do |format|
  116. format.html { render :action => 'new' }
  117. format.api { render_validation_errors(@user) }
  118. end
  119. end
  120. end
  121. def edit
  122. @auth_sources = AuthSource.all
  123. @membership ||= Member.new
  124. end
  125. def update
  126. if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
  127. @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
  128. end
  129. @user.safe_attributes = params[:user]
  130. # Was the account actived ? (do it before User#save clears the change)
  131. was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
  132. # TODO: Similar to My#account
  133. @user.pref.safe_attributes = params[:pref]
  134. if @user.save
  135. @user.pref.save
  136. if was_activated
  137. Mailer.deliver_account_activated(@user)
  138. elsif @user.active? && params[:send_information] && @user != User.current
  139. Mailer.deliver_account_information(@user, @user.password)
  140. end
  141. respond_to do |format|
  142. format.html {
  143. flash[:notice] = l(:notice_successful_update)
  144. redirect_to_referer_or edit_user_path(@user)
  145. }
  146. format.api { render_api_ok }
  147. end
  148. else
  149. @auth_sources = AuthSource.all
  150. @membership ||= Member.new
  151. # Clear password input
  152. @user.password = @user.password_confirmation = nil
  153. respond_to do |format|
  154. format.html { render :action => :edit }
  155. format.api { render_validation_errors(@user) }
  156. end
  157. end
  158. end
  159. def destroy
  160. @user.destroy
  161. respond_to do |format|
  162. format.html { redirect_back_or_default(users_path) }
  163. format.api { render_api_ok }
  164. end
  165. end
  166. private
  167. def find_user(logged = true)
  168. if params[:id] == 'current'
  169. require_login || return
  170. @user = User.current
  171. elsif logged
  172. @user = User.logged.find(params[:id])
  173. else
  174. @user = User.find(params[:id])
  175. end
  176. rescue ActiveRecord::RecordNotFound
  177. render_404
  178. end
  179. end