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

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