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.

my_controller.rb 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 MyController < ApplicationController
  19. self.main_menu = false
  20. before_action :require_login
  21. # let user change user's password when user has to
  22. skip_before_action :check_password_change, :check_twofa_activation, :only => :password
  23. accept_api_auth :account
  24. require_sudo_mode :account, only: :put
  25. require_sudo_mode :reset_atom_key, :reset_api_key, :show_api_key, :destroy
  26. helper :issues
  27. helper :users
  28. helper :custom_fields
  29. helper :queries
  30. helper :activities
  31. def index
  32. page
  33. render :action => 'page'
  34. end
  35. # Show user's page
  36. def page
  37. @user = User.current
  38. @groups = @user.pref.my_page_groups
  39. @blocks = @user.pref.my_page_layout
  40. end
  41. # Edit user's account
  42. def account
  43. @user = User.current
  44. @pref = @user.pref
  45. if request.put?
  46. @user.safe_attributes = params[:user]
  47. @user.pref.safe_attributes = params[:pref]
  48. if @user.save
  49. @user.pref.save
  50. respond_to do |format|
  51. format.html do
  52. flash[:notice] = l(:notice_account_updated)
  53. redirect_to my_account_path
  54. end
  55. format.api {render_api_ok}
  56. end
  57. return
  58. else
  59. respond_to do |format|
  60. format.html {render :action => :account}
  61. format.api {render_validation_errors(@user)}
  62. end
  63. end
  64. end
  65. end
  66. # Destroys user's account
  67. def destroy
  68. @user = User.current
  69. unless @user.own_account_deletable?
  70. redirect_to my_account_path
  71. return
  72. end
  73. if request.post? && params[:confirm]
  74. @user.destroy
  75. if @user.destroyed?
  76. logout_user
  77. flash[:notice] = l(:notice_account_deleted)
  78. end
  79. redirect_to home_path
  80. end
  81. end
  82. # Manage user's password
  83. def password
  84. @user = User.current
  85. unless @user.change_password_allowed?
  86. flash[:error] = l(:notice_can_t_change_password)
  87. redirect_to my_account_path
  88. return
  89. end
  90. if request.post?
  91. if !@user.check_password?(params[:password])
  92. flash.now[:error] = l(:notice_account_wrong_password)
  93. elsif params[:password] == params[:new_password]
  94. flash.now[:error] = l(:notice_new_password_must_be_different)
  95. else
  96. @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
  97. @user.must_change_passwd = false
  98. if @user.save
  99. # The session token was destroyed by the password change, generate a new one
  100. session[:tk] = @user.generate_session_token
  101. Mailer.deliver_password_updated(@user, User.current)
  102. flash[:notice] = l(:notice_account_password_updated)
  103. redirect_to my_account_path
  104. end
  105. end
  106. end
  107. end
  108. # Create a new feeds key
  109. def reset_atom_key
  110. if request.post?
  111. if User.current.atom_token
  112. User.current.atom_token.destroy
  113. User.current.reload
  114. end
  115. User.current.atom_key
  116. flash[:notice] = l(:notice_feeds_access_key_reseted)
  117. end
  118. redirect_to my_account_path
  119. end
  120. def show_api_key
  121. @user = User.current
  122. end
  123. # Create a new API key
  124. def reset_api_key
  125. if request.post?
  126. if User.current.api_token
  127. User.current.api_token.destroy
  128. User.current.reload
  129. end
  130. User.current.api_key
  131. flash[:notice] = l(:notice_api_access_key_reseted)
  132. end
  133. redirect_to my_account_path
  134. end
  135. def update_page
  136. @user = User.current
  137. block_settings = params[:settings] || {}
  138. block_settings.each do |block, settings|
  139. @user.pref.update_block_settings(block, settings.to_unsafe_hash)
  140. end
  141. @user.pref.save
  142. @updated_blocks = block_settings.keys
  143. end
  144. # Add a block to user's page
  145. # The block is added on top of the page
  146. # params[:block] : id of the block to add
  147. def add_block
  148. @user = User.current
  149. @block = params[:block]
  150. if @user.pref.add_block @block
  151. @user.pref.save
  152. respond_to do |format|
  153. format.html {redirect_to my_page_path}
  154. format.js
  155. end
  156. else
  157. render_error :status => 422
  158. end
  159. end
  160. # Remove a block to user's page
  161. # params[:block] : id of the block to remove
  162. def remove_block
  163. @user = User.current
  164. @block = params[:block]
  165. @user.pref.remove_block @block
  166. @user.pref.save
  167. respond_to do |format|
  168. format.html {redirect_to my_page_path}
  169. format.js
  170. end
  171. end
  172. # Change blocks order on user's page
  173. # params[:group] : group to order (top, left or right)
  174. # params[:blocks] : array of block ids of the group
  175. def order_blocks
  176. @user = User.current
  177. @user.pref.order_blocks params[:group], params[:blocks]
  178. @user.pref.save
  179. head 200
  180. end
  181. end