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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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. before_filter :require_login
  19. # let user change user's password when user has to
  20. skip_before_filter :check_password_change, :only => :password
  21. helper :issues
  22. helper :users
  23. helper :custom_fields
  24. BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
  25. 'issuesreportedbyme' => :label_reported_issues,
  26. 'issueswatched' => :label_watched_issues,
  27. 'news' => :label_news_latest,
  28. 'calendar' => :label_calendar,
  29. 'documents' => :label_document_plural,
  30. 'timelog' => :label_spent_time
  31. }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
  32. DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
  33. 'right' => ['issuesreportedbyme']
  34. }.freeze
  35. def index
  36. page
  37. render :action => 'page'
  38. end
  39. # Show user's page
  40. def page
  41. @user = User.current
  42. @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
  43. end
  44. # Edit user's account
  45. def account
  46. @user = User.current
  47. @pref = @user.pref
  48. if request.post?
  49. @user.safe_attributes = params[:user]
  50. @user.pref.attributes = params[:pref]
  51. if @user.save
  52. @user.pref.save
  53. set_language_if_valid @user.language
  54. flash[:notice] = l(:notice_account_updated)
  55. redirect_to my_account_path
  56. return
  57. end
  58. end
  59. end
  60. # Destroys user's account
  61. def destroy
  62. @user = User.current
  63. unless @user.own_account_deletable?
  64. redirect_to my_account_path
  65. return
  66. end
  67. if request.post? && params[:confirm]
  68. @user.destroy
  69. if @user.destroyed?
  70. logout_user
  71. flash[:notice] = l(:notice_account_deleted)
  72. end
  73. redirect_to home_path
  74. end
  75. end
  76. # Manage user's password
  77. def password
  78. @user = User.current
  79. unless @user.change_password_allowed?
  80. flash[:error] = l(:notice_can_t_change_password)
  81. redirect_to my_account_path
  82. return
  83. end
  84. if request.post?
  85. if !@user.check_password?(params[:password])
  86. flash.now[:error] = l(:notice_account_wrong_password)
  87. elsif params[:password] == params[:new_password]
  88. flash.now[:error] = l(:notice_new_password_must_be_different)
  89. else
  90. @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
  91. @user.must_change_passwd = false
  92. if @user.save
  93. flash[:notice] = l(:notice_account_password_updated)
  94. redirect_to my_account_path
  95. end
  96. end
  97. end
  98. end
  99. # Create a new feeds key
  100. def reset_rss_key
  101. if request.post?
  102. if User.current.rss_token
  103. User.current.rss_token.destroy
  104. User.current.reload
  105. end
  106. User.current.rss_key
  107. flash[:notice] = l(:notice_feeds_access_key_reseted)
  108. end
  109. redirect_to my_account_path
  110. end
  111. # Create a new API key
  112. def reset_api_key
  113. if request.post?
  114. if User.current.api_token
  115. User.current.api_token.destroy
  116. User.current.reload
  117. end
  118. User.current.api_key
  119. flash[:notice] = l(:notice_api_access_key_reseted)
  120. end
  121. redirect_to my_account_path
  122. end
  123. # User's page layout configuration
  124. def page_layout
  125. @user = User.current
  126. @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
  127. @block_options = []
  128. BLOCKS.each do |k, v|
  129. unless @blocks.values.flatten.include?(k)
  130. @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
  131. end
  132. end
  133. end
  134. # Add a block to user's page
  135. # The block is added on top of the page
  136. # params[:block] : id of the block to add
  137. def add_block
  138. block = params[:block].to_s.underscore
  139. if block.present? && BLOCKS.key?(block)
  140. @user = User.current
  141. layout = @user.pref[:my_page_layout] || {}
  142. # remove if already present in a group
  143. %w(top left right).each {|f| (layout[f] ||= []).delete block }
  144. # add it on top
  145. layout['top'].unshift block
  146. @user.pref[:my_page_layout] = layout
  147. @user.pref.save
  148. end
  149. redirect_to my_page_layout_path
  150. end
  151. # Remove a block to user's page
  152. # params[:block] : id of the block to remove
  153. def remove_block
  154. block = params[:block].to_s.underscore
  155. @user = User.current
  156. # remove block in all groups
  157. layout = @user.pref[:my_page_layout] || {}
  158. %w(top left right).each {|f| (layout[f] ||= []).delete block }
  159. @user.pref[:my_page_layout] = layout
  160. @user.pref.save
  161. redirect_to my_page_layout_path
  162. end
  163. # Change blocks order on user's page
  164. # params[:group] : group to order (top, left or right)
  165. # params[:list-(top|left|right)] : array of block ids of the group
  166. def order_blocks
  167. group = params[:group]
  168. @user = User.current
  169. if group.is_a?(String)
  170. group_items = (params["blocks"] || []).collect(&:underscore)
  171. group_items.each {|s| s.sub!(/^block_/, '')}
  172. if group_items and group_items.is_a? Array
  173. layout = @user.pref[:my_page_layout] || {}
  174. # remove group blocks if they are presents in other groups
  175. %w(top left right).each {|f|
  176. layout[f] = (layout[f] || []) - group_items
  177. }
  178. layout[group] = group_items
  179. @user.pref[:my_page_layout] = layout
  180. @user.pref.save
  181. end
  182. end
  183. render :nothing => true
  184. end
  185. end