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

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