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

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