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