Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: class MyController < ApplicationController Chris@1494: before_filter :require_login Chris@1494: # let user change user's password when user has to Chris@1494: skip_before_filter :check_password_change, :only => :password Chris@1494: Chris@1494: helper :issues Chris@1494: helper :users Chris@1494: helper :custom_fields Chris@1494: Chris@1494: BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues, Chris@1494: 'issuesreportedbyme' => :label_reported_issues, Chris@1494: 'issueswatched' => :label_watched_issues, Chris@1494: 'news' => :label_news_latest, Chris@1494: 'calendar' => :label_calendar, Chris@1494: 'documents' => :label_document_plural, Chris@1494: 'timelog' => :label_spent_time Chris@1494: }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze Chris@1494: Chris@1494: DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'], Chris@1494: 'right' => ['issuesreportedbyme'] Chris@1494: }.freeze Chris@1494: Chris@1494: def index Chris@1494: page Chris@1494: render :action => 'page' Chris@1494: end Chris@1494: Chris@1494: # Show user's page Chris@1494: def page Chris@1494: @user = User.current Chris@1494: @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT Chris@1494: end Chris@1494: Chris@1494: # Edit user's account Chris@1494: def account Chris@1494: @user = User.current Chris@1494: @pref = @user.pref Chris@1494: if request.post? Chris@1494: @user.safe_attributes = params[:user] Chris@1494: @user.pref.attributes = params[:pref] Chris@1494: if @user.save Chris@1494: @user.pref.save Chris@1494: set_language_if_valid @user.language Chris@1494: flash[:notice] = l(:notice_account_updated) Chris@1494: redirect_to my_account_path Chris@1494: return Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # Destroys user's account Chris@1494: def destroy Chris@1494: @user = User.current Chris@1494: unless @user.own_account_deletable? Chris@1494: redirect_to my_account_path Chris@1494: return Chris@1494: end Chris@1494: Chris@1494: if request.post? && params[:confirm] Chris@1494: @user.destroy Chris@1494: if @user.destroyed? Chris@1494: logout_user Chris@1494: flash[:notice] = l(:notice_account_deleted) Chris@1494: end Chris@1494: redirect_to home_path Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # Manage user's password Chris@1494: def password Chris@1494: @user = User.current Chris@1494: unless @user.change_password_allowed? Chris@1494: flash[:error] = l(:notice_can_t_change_password) Chris@1494: redirect_to my_account_path Chris@1494: return Chris@1494: end Chris@1494: if request.post? Chris@1494: if !@user.check_password?(params[:password]) Chris@1494: flash.now[:error] = l(:notice_account_wrong_password) Chris@1494: elsif params[:password] == params[:new_password] Chris@1494: flash.now[:error] = l(:notice_new_password_must_be_different) Chris@1494: else Chris@1494: @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] Chris@1494: @user.must_change_passwd = false Chris@1494: if @user.save Chris@1494: flash[:notice] = l(:notice_account_password_updated) Chris@1494: redirect_to my_account_path Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # Create a new feeds key Chris@1494: def reset_rss_key Chris@1494: if request.post? Chris@1494: if User.current.rss_token Chris@1494: User.current.rss_token.destroy Chris@1494: User.current.reload Chris@1494: end Chris@1494: User.current.rss_key Chris@1494: flash[:notice] = l(:notice_feeds_access_key_reseted) Chris@1494: end Chris@1494: redirect_to my_account_path Chris@1494: end Chris@1494: Chris@1494: # Create a new API key Chris@1494: def reset_api_key Chris@1494: if request.post? Chris@1494: if User.current.api_token Chris@1494: User.current.api_token.destroy Chris@1494: User.current.reload Chris@1494: end Chris@1494: User.current.api_key Chris@1494: flash[:notice] = l(:notice_api_access_key_reseted) Chris@1494: end Chris@1494: redirect_to my_account_path Chris@1494: end Chris@1494: Chris@1494: # User's page layout configuration Chris@1494: def page_layout Chris@1494: @user = User.current Chris@1494: @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup Chris@1494: @block_options = [] Chris@1494: BLOCKS.each do |k, v| Chris@1494: unless %w(top left right).detect {|f| (@blocks[f] ||= []).include?(k)} Chris@1494: @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize] Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # Add a block to user's page Chris@1494: # The block is added on top of the page Chris@1494: # params[:block] : id of the block to add Chris@1494: def add_block Chris@1494: block = params[:block].to_s.underscore Chris@1494: if block.present? && BLOCKS.key?(block) Chris@1494: @user = User.current Chris@1494: layout = @user.pref[:my_page_layout] || {} Chris@1494: # remove if already present in a group Chris@1494: %w(top left right).each {|f| (layout[f] ||= []).delete block } Chris@1494: # add it on top Chris@1494: layout['top'].unshift block Chris@1494: @user.pref[:my_page_layout] = layout Chris@1494: @user.pref.save Chris@1494: end Chris@1494: redirect_to my_page_layout_path Chris@1494: end Chris@1494: Chris@1494: # Remove a block to user's page Chris@1494: # params[:block] : id of the block to remove Chris@1494: def remove_block Chris@1494: block = params[:block].to_s.underscore Chris@1494: @user = User.current Chris@1494: # remove block in all groups Chris@1494: layout = @user.pref[:my_page_layout] || {} Chris@1494: %w(top left right).each {|f| (layout[f] ||= []).delete block } Chris@1494: @user.pref[:my_page_layout] = layout Chris@1494: @user.pref.save Chris@1494: redirect_to my_page_layout_path Chris@1494: end Chris@1494: Chris@1494: # Change blocks order on user's page Chris@1494: # params[:group] : group to order (top, left or right) Chris@1494: # params[:list-(top|left|right)] : array of block ids of the group Chris@1494: def order_blocks Chris@1494: group = params[:group] Chris@1494: @user = User.current Chris@1494: if group.is_a?(String) Chris@1494: group_items = (params["blocks"] || []).collect(&:underscore) Chris@1494: group_items.each {|s| s.sub!(/^block_/, '')} Chris@1494: if group_items and group_items.is_a? Array Chris@1494: layout = @user.pref[:my_page_layout] || {} Chris@1494: # remove group blocks if they are presents in other groups Chris@1494: %w(top left right).each {|f| Chris@1494: layout[f] = (layout[f] || []) - group_items Chris@1494: } Chris@1494: layout[group] = group_items Chris@1494: @user.pref[:my_page_layout] = layout Chris@1494: @user.pref.save Chris@1494: end Chris@1494: end Chris@1494: render :nothing => true Chris@1494: end Chris@1494: end