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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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. helper :issues
  20. helper :users
  21. helper :custom_fields
  22. BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
  23. 'issuesreportedbyme' => :label_reported_issues,
  24. 'issueswatched' => :label_watched_issues,
  25. 'news' => :label_news_latest,
  26. 'calendar' => :label_calendar,
  27. 'documents' => :label_document_plural,
  28. 'timelog' => :label_spent_time
  29. }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
  30. DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
  31. 'right' => ['issuesreportedbyme']
  32. }.freeze
  33. def index
  34. page
  35. render :action => 'page'
  36. end
  37. # Show user's page
  38. def page
  39. @user = User.current
  40. @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
  41. end
  42. # Edit user's account
  43. def account
  44. @user = User.current
  45. @pref = @user.pref
  46. if request.post?
  47. @user.safe_attributes = params[:user]
  48. @user.pref.attributes = params[:pref]
  49. @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
  50. if @user.save
  51. @user.pref.save
  52. @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
  53. set_language_if_valid @user.language
  54. flash[:notice] = l(:notice_account_updated)
  55. redirect_to :action => 'account'
  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 :action => 'account'
  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 :action => 'account'
  82. return
  83. end
  84. if request.post?
  85. if @user.check_password?(params[:password])
  86. @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
  87. if @user.save
  88. flash[:notice] = l(:notice_account_password_updated)
  89. redirect_to :action => 'account'
  90. end
  91. else
  92. flash[:error] = l(:notice_account_wrong_password)
  93. end
  94. end
  95. end
  96. # Create a new feeds key
  97. def reset_rss_key
  98. if request.post?
  99. if User.current.rss_token
  100. User.current.rss_token.destroy
  101. User.current.reload
  102. end
  103. User.current.rss_key
  104. flash[:notice] = l(:notice_feeds_access_key_reseted)
  105. end
  106. redirect_to :action => 'account'
  107. end
  108. # Create a new API key
  109. def reset_api_key
  110. if request.post?
  111. if User.current.api_token
  112. User.current.api_token.destroy
  113. User.current.reload
  114. end
  115. User.current.api_key
  116. flash[:notice] = l(:notice_api_access_key_reseted)
  117. end
  118. redirect_to :action => 'account'
  119. end
  120. # User's page layout configuration
  121. def page_layout
  122. @user = User.current
  123. @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
  124. @block_options = []
  125. BLOCKS.each {|k, v| @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]}
  126. end
  127. # Add a block to user's page
  128. # The block is added on top of the page
  129. # params[:block] : id of the block to add
  130. def add_block
  131. block = params[:block].to_s.underscore
  132. (render :nothing => true; return) unless block && (BLOCKS.keys.include? block)
  133. @user = User.current
  134. layout = @user.pref[:my_page_layout] || {}
  135. # remove if already present in a group
  136. %w(top left right).each {|f| (layout[f] ||= []).delete block }
  137. # add it on top
  138. layout['top'].unshift block
  139. @user.pref[:my_page_layout] = layout
  140. @user.pref.save
  141. render :partial => "block", :locals => {:user => @user, :block_name => block}
  142. end
  143. # Remove a block to user's page
  144. # params[:block] : id of the block to remove
  145. def remove_block
  146. block = params[:block].to_s.underscore
  147. @user = User.current
  148. # remove block in all groups
  149. layout = @user.pref[:my_page_layout] || {}
  150. %w(top left right).each {|f| (layout[f] ||= []).delete block }
  151. @user.pref[:my_page_layout] = layout
  152. @user.pref.save
  153. render :nothing => true
  154. end
  155. # Change blocks order on user's page
  156. # params[:group] : group to order (top, left or right)
  157. # params[:list-(top|left|right)] : array of block ids of the group
  158. def order_blocks
  159. group = params[:group]
  160. @user = User.current
  161. if group.is_a?(String)
  162. group_items = (params["list-#{group}"] || []).collect(&:underscore)
  163. if group_items and group_items.is_a? Array
  164. layout = @user.pref[:my_page_layout] || {}
  165. # remove group blocks if they are presents in other groups
  166. %w(top left right).each {|f|
  167. layout[f] = (layout[f] || []) - group_items
  168. }
  169. layout[group] = group_items
  170. @user.pref[:my_page_layout] = layout
  171. @user.pref.save
  172. end
  173. end
  174. render :nothing => true
  175. end
  176. end