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