To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / my_controller.rb @ 1534:b31caaed9d4d

History | View | Annotate | Download (7.61 KB)

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

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

    
23
  helper :issues
24
  helper :users
25
  helper :custom_fields
26
  helper :projects
27
  helper :activities
28

    
29
  BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
30
             'issuesreportedbyme' => :label_reported_issues,
31
             'issueswatched' => :label_watched_issues,
32
             'activitymyprojects' => :label_activity_my_recent,
33
             'news' => :label_news_latest,
34
             'tipoftheday' => :label_tipoftheday,
35
             'calendar' => :label_calendar,
36
             'documents' => :label_document_plural,
37
             'timelog' => :label_spent_time,
38
             'myprojects' => :label_my_projects,
39
             'colleagues' => :label_my_colleagues
40
           }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
41

    
42
  DEFAULT_LAYOUT = {  'left' => ['myprojects', 'activitymyprojects'], 
43
                      'right' => ['colleagues', 'tipoftheday', 'issueswatched'] 
44
                   }.freeze
45

    
46
  def index
47
    page
48
    render :action => 'page'
49
  end
50

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

    
57
  # Edit user's account
58
  def account
59
    @user = User.current
60
    @pref = @user.pref
61
    @ssamr_user_details = @user.ssamr_user_detail
62
    
63
    
64
    if @user.ssamr_user_detail == nil
65
       @selected_institution_id = nil
66
     else
67
       @selected_institution_id = @ssamr_user_details.institution_id.to_i
68
     end    
69
    
70
    if request.post?
71
      @user.safe_attributes = params[:user]
72
      @user.pref.attributes = params[:pref]
73

    
74
      @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
75

    
76
      if @user.ssamr_user_detail == nil
77
        @ssamr_user_details = SsamrUserDetail.new()
78
        @user.ssamr_user_detail = @ssamr_user_details
79
      else
80
        @ssamr_user_details = @user.ssamr_user_detail
81
      end
82

    
83
      if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty?
84
        @ssamr_user_details.description = @user.ssamr_user_detail.description
85
        @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id
86
        @institution_type = @ssamr_user_details.institution_type
87
        @other_institution = @ssamr_user_details.other_institution
88
      else
89
        @ssamr_user_details.description = params[:ssamr_user_details][:description]
90
        @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id]
91
        @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type]
92
        @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution]
93
      end
94
                  
95
      if @user.save
96
        @user.pref.save
97
        set_language_if_valid @user.language
98
        flash[:notice] = l(:notice_account_updated)
99
        redirect_to my_account_path
100
        return
101
      end
102
    end
103
  end
104

    
105
  # Destroys user's account
106
  def destroy
107
    @user = User.current
108
    unless @user.own_account_deletable?
109
      redirect_to my_account_path
110
      return
111
    end
112

    
113
    if request.post? && params[:confirm]
114
      @user.destroy
115
      if @user.destroyed?
116
        logout_user
117
        flash[:notice] = l(:notice_account_deleted)
118
      end
119
      redirect_to home_path
120
    end
121
  end
122

    
123
  # Manage user's password
124
  def password
125
    @user = User.current
126
    unless @user.change_password_allowed?
127
      flash[:error] = l(:notice_can_t_change_password)
128
      redirect_to my_account_path
129
      return
130
    end
131
    if request.post?
132
      if !@user.check_password?(params[:password])
133
        flash.now[:error] = l(:notice_account_wrong_password)
134
      elsif params[:password] == params[:new_password]
135
        flash.now[:error] = l(:notice_new_password_must_be_different)
136
      else
137
        @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
138
        @user.must_change_passwd = false
139
        if @user.save
140
          flash[:notice] = l(:notice_account_password_updated)
141
          redirect_to my_account_path
142
        end
143
      end
144
    end
145
  end
146

    
147
  # Create a new feeds key
148
  def reset_rss_key
149
    if request.post?
150
      if User.current.rss_token
151
        User.current.rss_token.destroy
152
        User.current.reload
153
      end
154
      User.current.rss_key
155
      flash[:notice] = l(:notice_feeds_access_key_reseted)
156
    end
157
    redirect_to my_account_path
158
  end
159

    
160
  # Create a new API key
161
  def reset_api_key
162
    if request.post?
163
      if User.current.api_token
164
        User.current.api_token.destroy
165
        User.current.reload
166
      end
167
      User.current.api_key
168
      flash[:notice] = l(:notice_api_access_key_reseted)
169
    end
170
    redirect_to my_account_path
171
  end
172

    
173
  # User's page layout configuration
174
  def page_layout
175
    @user = User.current
176
    @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
177
    @block_options = []
178
    BLOCKS.each do |k, v|
179
      unless @blocks.values.flatten.include?(k)
180
        @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
181
      end
182
    end
183
  end
184

    
185
  # Add a block to user's page
186
  # The block is added on top of the page
187
  # params[:block] : id of the block to add
188
  def add_block
189
    block = params[:block].to_s.underscore
190
    if block.present? && BLOCKS.key?(block)
191
      @user = User.current
192
      layout = @user.pref[:my_page_layout] || {}
193
      # remove if already present in a group
194
      %w(top left right).each {|f| (layout[f] ||= []).delete block }
195
      # add it on top
196
      layout['top'].unshift block
197
      @user.pref[:my_page_layout] = layout
198
      @user.pref.save
199
    end
200
    redirect_to my_page_layout_path
201
  end
202

    
203
  # Remove a block to user's page
204
  # params[:block] : id of the block to remove
205
  def remove_block
206
    block = params[:block].to_s.underscore
207
    @user = User.current
208
    # remove block in all groups
209
    layout = @user.pref[:my_page_layout] || {}
210
    %w(top left right).each {|f| (layout[f] ||= []).delete block }
211
    @user.pref[:my_page_layout] = layout
212
    @user.pref.save
213
    redirect_to my_page_layout_path
214
  end
215

    
216
  # Change blocks order on user's page
217
  # params[:group] : group to order (top, left or right)
218
  # params[:list-(top|left|right)] : array of block ids of the group
219
  def order_blocks
220
    group = params[:group]
221
    @user = User.current
222
    if group.is_a?(String)
223
      group_items = (params["blocks"] || []).collect(&:underscore)
224
      group_items.each {|s| s.sub!(/^block_/, '')}
225
      if group_items and group_items.is_a? Array
226
        layout = @user.pref[:my_page_layout] || {}
227
        # remove group blocks if they are presents in other groups
228
        %w(top left right).each {|f|
229
          layout[f] = (layout[f] || []) - group_items
230
        }
231
        layout[group] = group_items
232
        @user.pref[:my_page_layout] = layout
233
        @user.pref.save
234
      end
235
    end
236
    render :nothing => true
237
  end
238
end