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 @ 1358:b8f94812d737

History | View | Annotate | Download (7.51 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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

    
21
  helper :issues
22
  helper :users
23
  helper :custom_fields
24
  helper :projects
25
  helper :activities
26

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

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

    
44
  def index
45
    page
46
    render :action => 'page'
47
  end
48

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

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

    
73
      if @user.ssamr_user_detail == nil
74
        @ssamr_user_details = SsamrUserDetail.new()
75
        @user.ssamr_user_detail = @ssamr_user_details
76
      else
77
        @ssamr_user_details = @user.ssamr_user_detail
78
      end
79

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

    
103
  # Destroys user's account
104
  def destroy
105
    @user = User.current
106
    unless @user.own_account_deletable?
107
      redirect_to :action => 'account'
108
      return
109
    end
110

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

    
121
  # Manage user's password
122
  def password
123
    @user = User.current
124
    unless @user.change_password_allowed?
125
      flash[:error] = l(:notice_can_t_change_password)
126
      redirect_to :action => 'account'
127
      return
128
    end
129
    if request.post?
130
      if @user.check_password?(params[:password])
131
        @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
132
        if @user.save
133
          flash[:notice] = l(:notice_account_password_updated)
134
          redirect_to :action => 'account'
135
        end
136
      else
137
        flash[:error] = l(:notice_account_wrong_password)
138
      end
139
    end
140
  end
141

    
142
  # Create a new feeds key
143
  def reset_rss_key
144
    if request.post?
145
      if User.current.rss_token
146
        User.current.rss_token.destroy
147
        User.current.reload
148
      end
149
      User.current.rss_key
150
      flash[:notice] = l(:notice_feeds_access_key_reseted)
151
    end
152
    redirect_to :action => 'account'
153
  end
154

    
155
  # Create a new API key
156
  def reset_api_key
157
    if request.post?
158
      if User.current.api_token
159
        User.current.api_token.destroy
160
        User.current.reload
161
      end
162
      User.current.api_key
163
      flash[:notice] = l(:notice_api_access_key_reseted)
164
    end
165
    redirect_to :action => 'account'
166
  end
167

    
168
  # User's page layout configuration
169
  def page_layout
170
    @user = User.current
171
    @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
172
    @block_options = []
173
    BLOCKS.each do |k, v|
174
      unless %w(top left right).detect {|f| (@blocks[f] ||= []).include?(k)}
175
        @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
176
      end
177
    end
178
  end
179

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

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

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