my_controller.rb 6.42 KB
Newer Older
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2016  Jean-Philippe Lang
jplang's avatar
jplang committed
3 4 5 6 7
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
jplang's avatar
jplang committed
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
jplang's avatar
jplang committed
14 15 16 17 18
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class MyController < ApplicationController
19
  before_action :require_login
20
  # let user change user's password when user has to
21
  skip_before_filter :check_password_change, :only => :password
jplang's avatar
jplang committed
22

23 24 25
  require_sudo_mode :account, only: :post
  require_sudo_mode :reset_rss_key, :reset_api_key, :show_api_key, :destroy

26
  helper :issues
jplang's avatar
jplang committed
27
  helper :users
28
  helper :custom_fields
29

30 31
  BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
             'issuesreportedbyme' => :label_reported_issues,
32
             'issueswatched' => :label_watched_issues,
33
             'news' => :label_news_latest,
jplang's avatar
jplang committed
34
             'calendar' => :label_calendar,
35 36
             'documents' => :label_document_plural,
             'timelog' => :label_spent_time
37
           }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
jplang's avatar
jplang committed
38

39 40
  DEFAULT_LAYOUT = {  'left' => ['issuesassignedtome'],
                      'right' => ['issuesreportedbyme']
41 42
                   }.freeze

jplang's avatar
jplang committed
43 44 45 46 47 48 49
  def index
    page
    render :action => 'page'
  end

  # Show user's page
  def page
50
    @user = User.current
51
    @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
jplang's avatar
jplang committed
52 53 54 55
  end

  # Edit user's account
  def account
56
    @user = User.current
57
    @pref = @user.pref
58
    if request.post?
jplang's avatar
jplang committed
59 60
      @user.safe_attributes = params[:user] if params[:user]
      @user.pref.attributes = params[:pref] if params[:pref]
61 62 63 64
      if @user.save
        @user.pref.save
        set_language_if_valid @user.language
        flash[:notice] = l(:notice_account_updated)
jplang's avatar
jplang committed
65
        redirect_to my_account_path
66 67
        return
      end
jplang's avatar
jplang committed
68 69 70
    end
  end

71 72 73 74
  # Destroys user's account
  def destroy
    @user = User.current
    unless @user.own_account_deletable?
jplang's avatar
jplang committed
75
      redirect_to my_account_path
76 77 78 79 80 81 82 83 84 85 86 87 88
      return
    end

    if request.post? && params[:confirm]
      @user.destroy
      if @user.destroyed?
        logout_user
        flash[:notice] = l(:notice_account_deleted)
      end
      redirect_to home_path
    end
  end

89 90
  # Manage user's password
  def password
91
    @user = User.current
92
    unless @user.change_password_allowed?
93
      flash[:error] = l(:notice_can_t_change_password)
jplang's avatar
jplang committed
94
      redirect_to my_account_path
95 96
      return
    end
97
    if request.post?
98 99 100
      if !@user.check_password?(params[:password])
        flash.now[:error] = l(:notice_account_wrong_password)
      elsif params[:password] == params[:new_password]
jplang's avatar
jplang committed
101
        flash.now[:error] = l(:notice_new_password_must_be_different)
102
      else
103
        @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
104
        @user.must_change_passwd = false
105
        if @user.save
106 107
          # The session token was destroyed by the password change, generate a new one
          session[:tk] = @user.generate_session_token
108
          Mailer.password_updated(@user)
109
          flash[:notice] = l(:notice_account_password_updated)
jplang's avatar
jplang committed
110
          redirect_to my_account_path
111
        end
jplang's avatar
jplang committed
112 113
      end
    end
114
  end
115

116 117
  # Create a new feeds key
  def reset_rss_key
118 119 120 121 122 123
    if request.post?
      if User.current.rss_token
        User.current.rss_token.destroy
        User.current.reload
      end
      User.current.rss_key
124 125
      flash[:notice] = l(:notice_feeds_access_key_reseted)
    end
jplang's avatar
jplang committed
126
    redirect_to my_account_path
jplang's avatar
jplang committed
127 128
  end

129 130 131 132
  def show_api_key
    @user = User.current
  end

133 134 135 136 137 138 139 140 141 142
  # Create a new API key
  def reset_api_key
    if request.post?
      if User.current.api_token
        User.current.api_token.destroy
        User.current.reload
      end
      User.current.api_key
      flash[:notice] = l(:notice_api_access_key_reseted)
    end
jplang's avatar
jplang committed
143
    redirect_to my_account_path
144 145
  end

jplang's avatar
jplang committed
146 147
  # User's page layout configuration
  def page_layout
148
    @user = User.current
149
    @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
jplang's avatar
jplang committed
150
    @block_options = []
151
    BLOCKS.each do |k, v|
jplang's avatar
jplang committed
152
      unless @blocks.values.flatten.include?(k)
153 154 155
        @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
      end
    end
jplang's avatar
jplang committed
156
  end
157

jplang's avatar
jplang committed
158 159 160 161
  # Add a block to user's page
  # The block is added on top of the page
  # params[:block] : id of the block to add
  def add_block
162
    block = params[:block].to_s.underscore
163 164 165 166 167 168 169 170 171 172
    if block.present? && BLOCKS.key?(block)
      @user = User.current
      layout = @user.pref[:my_page_layout] || {}
      # remove if already present in a group
      %w(top left right).each {|f| (layout[f] ||= []).delete block }
      # add it on top
      layout['top'].unshift block
      @user.pref[:my_page_layout] = layout
      @user.pref.save
    end
jplang's avatar
jplang committed
173
    redirect_to my_page_layout_path
jplang's avatar
jplang committed
174
  end
175

jplang's avatar
jplang committed
176 177 178
  # Remove a block to user's page
  # params[:block] : id of the block to remove
  def remove_block
179
    block = params[:block].to_s.underscore
180
    @user = User.current
jplang's avatar
jplang committed
181
    # remove block in all groups
182 183 184
    layout = @user.pref[:my_page_layout] || {}
    %w(top left right).each {|f| (layout[f] ||= []).delete block }
    @user.pref[:my_page_layout] = layout
185
    @user.pref.save
jplang's avatar
jplang committed
186
    redirect_to my_page_layout_path
jplang's avatar
jplang committed
187 188 189 190 191 192 193
  end

  # Change blocks order on user's page
  # params[:group] : group to order (top, left or right)
  # params[:list-(top|left|right)] : array of block ids of the group
  def order_blocks
    group = params[:group]
194
    @user = User.current
195
    if group.is_a?(String)
196 197
      group_items = (params["blocks"] || []).collect(&:underscore)
      group_items.each {|s| s.sub!(/^block_/, '')}
198
      if group_items and group_items.is_a? Array
199
        layout = @user.pref[:my_page_layout] || {}
200 201
        # remove group blocks if they are presents in other groups
        %w(top left right).each {|f|
202
          layout[f] = (layout[f] || []) - group_items
203
        }
204 205
        layout[group] = group_items
        @user.pref[:my_page_layout] = layout
206
        @user.pref.save
207
      end
jplang's avatar
jplang committed
208 209 210 211
    end
    render :nothing => true
  end
end