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

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