annotate app/controllers/my_controller.rb @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents e248c7af89ec
children c86dacc2ef0a dffacf8a6908
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class MyController < ApplicationController
Chris@0 19 before_filter :require_login
Chris@1464 20 # let user change user's password when user has to
Chris@1464 21 skip_before_filter :check_password_change, :only => :password
Chris@0 22
Chris@0 23 helper :issues
Chris@119 24 helper :users
Chris@0 25 helper :custom_fields
Chris@0 26
Chris@0 27 BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
Chris@0 28 'issuesreportedbyme' => :label_reported_issues,
Chris@0 29 'issueswatched' => :label_watched_issues,
Chris@0 30 'news' => :label_news_latest,
Chris@0 31 'calendar' => :label_calendar,
Chris@0 32 'documents' => :label_document_plural,
Chris@0 33 'timelog' => :label_spent_time
Chris@0 34 }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
Chris@0 35
Chris@909 36 DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
Chris@909 37 'right' => ['issuesreportedbyme']
Chris@0 38 }.freeze
Chris@0 39
Chris@0 40 def index
Chris@0 41 page
Chris@0 42 render :action => 'page'
Chris@0 43 end
Chris@0 44
Chris@0 45 # Show user's page
Chris@0 46 def page
Chris@0 47 @user = User.current
Chris@0 48 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
Chris@0 49 end
Chris@0 50
Chris@0 51 # Edit user's account
Chris@0 52 def account
Chris@0 53 @user = User.current
Chris@0 54 @pref = @user.pref
Chris@0 55 if request.post?
Chris@119 56 @user.safe_attributes = params[:user]
Chris@0 57 @user.pref.attributes = params[:pref]
Chris@0 58 if @user.save
Chris@0 59 @user.pref.save
Chris@0 60 set_language_if_valid @user.language
Chris@0 61 flash[:notice] = l(:notice_account_updated)
Chris@1464 62 redirect_to my_account_path
Chris@0 63 return
Chris@0 64 end
Chris@0 65 end
Chris@0 66 end
Chris@0 67
Chris@1115 68 # Destroys user's account
Chris@1115 69 def destroy
Chris@1115 70 @user = User.current
Chris@1115 71 unless @user.own_account_deletable?
Chris@1464 72 redirect_to my_account_path
Chris@1115 73 return
Chris@1115 74 end
Chris@1115 75
Chris@1115 76 if request.post? && params[:confirm]
Chris@1115 77 @user.destroy
Chris@1115 78 if @user.destroyed?
Chris@1115 79 logout_user
Chris@1115 80 flash[:notice] = l(:notice_account_deleted)
Chris@1115 81 end
Chris@1115 82 redirect_to home_path
Chris@1115 83 end
Chris@1115 84 end
Chris@1115 85
Chris@0 86 # Manage user's password
Chris@0 87 def password
Chris@0 88 @user = User.current
Chris@0 89 unless @user.change_password_allowed?
Chris@0 90 flash[:error] = l(:notice_can_t_change_password)
Chris@1464 91 redirect_to my_account_path
Chris@0 92 return
Chris@0 93 end
Chris@0 94 if request.post?
Chris@1464 95 if !@user.check_password?(params[:password])
Chris@1464 96 flash.now[:error] = l(:notice_account_wrong_password)
Chris@1464 97 elsif params[:password] == params[:new_password]
Chris@1464 98 flash.now[:error] = l(:notice_new_password_must_be_different)
Chris@1464 99 else
Chris@0 100 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
Chris@1464 101 @user.must_change_passwd = false
Chris@0 102 if @user.save
Chris@0 103 flash[:notice] = l(:notice_account_password_updated)
Chris@1464 104 redirect_to my_account_path
Chris@0 105 end
Chris@0 106 end
Chris@0 107 end
Chris@0 108 end
Chris@909 109
Chris@0 110 # Create a new feeds key
Chris@0 111 def reset_rss_key
Chris@0 112 if request.post?
Chris@0 113 if User.current.rss_token
Chris@0 114 User.current.rss_token.destroy
Chris@0 115 User.current.reload
Chris@0 116 end
Chris@0 117 User.current.rss_key
Chris@0 118 flash[:notice] = l(:notice_feeds_access_key_reseted)
Chris@0 119 end
Chris@1464 120 redirect_to my_account_path
Chris@0 121 end
Chris@0 122
Chris@0 123 # Create a new API key
Chris@0 124 def reset_api_key
Chris@0 125 if request.post?
Chris@0 126 if User.current.api_token
Chris@0 127 User.current.api_token.destroy
Chris@0 128 User.current.reload
Chris@0 129 end
Chris@0 130 User.current.api_key
Chris@0 131 flash[:notice] = l(:notice_api_access_key_reseted)
Chris@0 132 end
Chris@1464 133 redirect_to my_account_path
Chris@0 134 end
Chris@0 135
Chris@0 136 # User's page layout configuration
Chris@0 137 def page_layout
Chris@0 138 @user = User.current
Chris@0 139 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
Chris@0 140 @block_options = []
Chris@1115 141 BLOCKS.each do |k, v|
Chris@1115 142 unless %w(top left right).detect {|f| (@blocks[f] ||= []).include?(k)}
Chris@1115 143 @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
Chris@1115 144 end
Chris@1115 145 end
Chris@0 146 end
Chris@909 147
Chris@0 148 # Add a block to user's page
Chris@0 149 # The block is added on top of the page
Chris@0 150 # params[:block] : id of the block to add
Chris@0 151 def add_block
Chris@0 152 block = params[:block].to_s.underscore
Chris@1294 153 if block.present? && BLOCKS.key?(block)
Chris@1294 154 @user = User.current
Chris@1294 155 layout = @user.pref[:my_page_layout] || {}
Chris@1294 156 # remove if already present in a group
Chris@1294 157 %w(top left right).each {|f| (layout[f] ||= []).delete block }
Chris@1294 158 # add it on top
Chris@1294 159 layout['top'].unshift block
Chris@1294 160 @user.pref[:my_page_layout] = layout
Chris@1294 161 @user.pref.save
Chris@1294 162 end
Chris@1464 163 redirect_to my_page_layout_path
Chris@0 164 end
Chris@909 165
Chris@0 166 # Remove a block to user's page
Chris@0 167 # params[:block] : id of the block to remove
Chris@0 168 def remove_block
Chris@0 169 block = params[:block].to_s.underscore
Chris@0 170 @user = User.current
Chris@0 171 # remove block in all groups
Chris@0 172 layout = @user.pref[:my_page_layout] || {}
Chris@0 173 %w(top left right).each {|f| (layout[f] ||= []).delete block }
Chris@0 174 @user.pref[:my_page_layout] = layout
Chris@909 175 @user.pref.save
Chris@1464 176 redirect_to my_page_layout_path
Chris@0 177 end
Chris@0 178
Chris@0 179 # Change blocks order on user's page
Chris@0 180 # params[:group] : group to order (top, left or right)
Chris@0 181 # params[:list-(top|left|right)] : array of block ids of the group
Chris@0 182 def order_blocks
Chris@0 183 group = params[:group]
Chris@0 184 @user = User.current
Chris@0 185 if group.is_a?(String)
Chris@1115 186 group_items = (params["blocks"] || []).collect(&:underscore)
Chris@1115 187 group_items.each {|s| s.sub!(/^block_/, '')}
Chris@0 188 if group_items and group_items.is_a? Array
Chris@0 189 layout = @user.pref[:my_page_layout] || {}
Chris@0 190 # remove group blocks if they are presents in other groups
Chris@0 191 %w(top left right).each {|f|
Chris@0 192 layout[f] = (layout[f] || []) - group_items
Chris@0 193 }
Chris@0 194 layout[group] = group_items
Chris@0 195 @user.pref[:my_page_layout] = layout
Chris@909 196 @user.pref.save
Chris@0 197 end
Chris@0 198 end
Chris@0 199 render :nothing => true
Chris@0 200 end
Chris@0 201 end