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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 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. def index
  28. page
  29. render :action => 'page'
  30. end
  31. # Show user's page
  32. def page
  33. @user = User.current
  34. @blocks = @user.pref.my_page_layout
  35. end
  36. # Edit user's account
  37. def account
  38. @user = User.current
  39. @pref = @user.pref
  40. if request.post?
  41. @user.safe_attributes = params[:user]
  42. @user.pref.safe_attributes = params[:pref]
  43. if @user.save
  44. @user.pref.save
  45. set_language_if_valid @user.language
  46. flash[:notice] = l(:notice_account_updated)
  47. redirect_to my_account_path
  48. return
  49. end
  50. end
  51. end
  52. # Destroys user's account
  53. def destroy
  54. @user = User.current
  55. unless @user.own_account_deletable?
  56. redirect_to my_account_path
  57. return
  58. end
  59. if request.post? && params[:confirm]
  60. @user.destroy
  61. if @user.destroyed?
  62. logout_user
  63. flash[:notice] = l(:notice_account_deleted)
  64. end
  65. redirect_to home_path
  66. end
  67. end
  68. # Manage user's password
  69. def password
  70. @user = User.current
  71. unless @user.change_password_allowed?
  72. flash[:error] = l(:notice_can_t_change_password)
  73. redirect_to my_account_path
  74. return
  75. end
  76. if request.post?
  77. if !@user.check_password?(params[:password])
  78. flash.now[:error] = l(:notice_account_wrong_password)
  79. elsif params[:password] == params[:new_password]
  80. flash.now[:error] = l(:notice_new_password_must_be_different)
  81. else
  82. @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
  83. @user.must_change_passwd = false
  84. if @user.save
  85. # The session token was destroyed by the password change, generate a new one
  86. session[:tk] = @user.generate_session_token
  87. Mailer.password_updated(@user)
  88. flash[:notice] = l(:notice_account_password_updated)
  89. redirect_to my_account_path
  90. end
  91. end
  92. end
  93. end
  94. # Create a new feeds key
  95. def reset_rss_key
  96. if request.post?
  97. if User.current.rss_token
  98. User.current.rss_token.destroy
  99. User.current.reload
  100. end
  101. User.current.rss_key
  102. flash[:notice] = l(:notice_feeds_access_key_reseted)
  103. end
  104. redirect_to my_account_path
  105. end
  106. def show_api_key
  107. @user = User.current
  108. end
  109. # Create a new API key
  110. def reset_api_key
  111. if request.post?
  112. if User.current.api_token
  113. User.current.api_token.destroy
  114. User.current.reload
  115. end
  116. User.current.api_key
  117. flash[:notice] = l(:notice_api_access_key_reseted)
  118. end
  119. redirect_to my_account_path
  120. end
  121. def update_page
  122. @user = User.current
  123. block_settings = params[:settings] || {}
  124. block_settings.each do |block, settings|
  125. @user.pref.update_block_settings(block, settings)
  126. end
  127. @user.pref.save
  128. @updated_blocks = block_settings.keys
  129. end
  130. # User's page layout configuration
  131. def page_layout
  132. @user = User.current
  133. @blocks = @user.pref.my_page_layout
  134. end
  135. # Add a block to user's page
  136. # The block is added on top of the page
  137. # params[:block] : id of the block to add
  138. def add_block
  139. @user = User.current
  140. @user.pref.add_block params[:block]
  141. @user.pref.save
  142. redirect_to my_page_layout_path
  143. end
  144. # Remove a block to user's page
  145. # params[:block] : id of the block to remove
  146. def remove_block
  147. @user = User.current
  148. @user.pref.remove_block params[:block]
  149. @user.pref.save
  150. redirect_to my_page_layout_path
  151. end
  152. # Change blocks order on user's page
  153. # params[:group] : group to order (top, left or right)
  154. # params[:blocks] : array of block ids of the group
  155. def order_blocks
  156. group = params[:group]
  157. @user = User.current
  158. if group.is_a?(String)
  159. group_items = (params["blocks"] || []).collect(&:underscore)
  160. group_items.each {|s| s.sub!(/^block_/, '')}
  161. # remove group blocks if they are presents in other groups
  162. group_items.each {|s| @user.pref.remove_block(s)}
  163. @user.pref.my_page_layout[group] = group_items
  164. @user.pref.save
  165. end
  166. head 200
  167. end
  168. end