##// END OF EJS Templates
Code cleanup (#14766)....
Jean-Philippe Lang -
r12383:b9f45c80c655
parent child
Show More
@@ -1,201 +1,201
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class MyController < ApplicationController
18 class MyController < ApplicationController
19 before_filter :require_login
19 before_filter :require_login
20 # let user change user's password when user has to
20 # let user change user's password when user has to
21 skip_before_filter :check_password_change, :only => :password
21 skip_before_filter :check_password_change, :only => :password
22
22
23 helper :issues
23 helper :issues
24 helper :users
24 helper :users
25 helper :custom_fields
25 helper :custom_fields
26
26
27 BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
27 BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
28 'issuesreportedbyme' => :label_reported_issues,
28 'issuesreportedbyme' => :label_reported_issues,
29 'issueswatched' => :label_watched_issues,
29 'issueswatched' => :label_watched_issues,
30 'news' => :label_news_latest,
30 'news' => :label_news_latest,
31 'calendar' => :label_calendar,
31 'calendar' => :label_calendar,
32 'documents' => :label_document_plural,
32 'documents' => :label_document_plural,
33 'timelog' => :label_spent_time
33 'timelog' => :label_spent_time
34 }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
34 }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
35
35
36 DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
36 DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
37 'right' => ['issuesreportedbyme']
37 'right' => ['issuesreportedbyme']
38 }.freeze
38 }.freeze
39
39
40 def index
40 def index
41 page
41 page
42 render :action => 'page'
42 render :action => 'page'
43 end
43 end
44
44
45 # Show user's page
45 # Show user's page
46 def page
46 def page
47 @user = User.current
47 @user = User.current
48 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
48 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
49 end
49 end
50
50
51 # Edit user's account
51 # Edit user's account
52 def account
52 def account
53 @user = User.current
53 @user = User.current
54 @pref = @user.pref
54 @pref = @user.pref
55 if request.post?
55 if request.post?
56 @user.safe_attributes = params[:user]
56 @user.safe_attributes = params[:user]
57 @user.pref.attributes = params[:pref]
57 @user.pref.attributes = params[:pref]
58 if @user.save
58 if @user.save
59 @user.pref.save
59 @user.pref.save
60 set_language_if_valid @user.language
60 set_language_if_valid @user.language
61 flash[:notice] = l(:notice_account_updated)
61 flash[:notice] = l(:notice_account_updated)
62 redirect_to my_account_path
62 redirect_to my_account_path
63 return
63 return
64 end
64 end
65 end
65 end
66 end
66 end
67
67
68 # Destroys user's account
68 # Destroys user's account
69 def destroy
69 def destroy
70 @user = User.current
70 @user = User.current
71 unless @user.own_account_deletable?
71 unless @user.own_account_deletable?
72 redirect_to my_account_path
72 redirect_to my_account_path
73 return
73 return
74 end
74 end
75
75
76 if request.post? && params[:confirm]
76 if request.post? && params[:confirm]
77 @user.destroy
77 @user.destroy
78 if @user.destroyed?
78 if @user.destroyed?
79 logout_user
79 logout_user
80 flash[:notice] = l(:notice_account_deleted)
80 flash[:notice] = l(:notice_account_deleted)
81 end
81 end
82 redirect_to home_path
82 redirect_to home_path
83 end
83 end
84 end
84 end
85
85
86 # Manage user's password
86 # Manage user's password
87 def password
87 def password
88 @user = User.current
88 @user = User.current
89 unless @user.change_password_allowed?
89 unless @user.change_password_allowed?
90 flash[:error] = l(:notice_can_t_change_password)
90 flash[:error] = l(:notice_can_t_change_password)
91 redirect_to my_account_path
91 redirect_to my_account_path
92 return
92 return
93 end
93 end
94 if request.post?
94 if request.post?
95 if !@user.check_password?(params[:password])
95 if !@user.check_password?(params[:password])
96 flash.now[:error] = l(:notice_account_wrong_password)
96 flash.now[:error] = l(:notice_account_wrong_password)
97 elsif params[:password] == params[:new_password]
97 elsif params[:password] == params[:new_password]
98 flash.now[:error] = l(:notice_new_password_must_be_different)
98 flash.now[:error] = l(:notice_new_password_must_be_different)
99 else
99 else
100 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
100 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
101 @user.must_change_passwd = false
101 @user.must_change_passwd = false
102 if @user.save
102 if @user.save
103 flash[:notice] = l(:notice_account_password_updated)
103 flash[:notice] = l(:notice_account_password_updated)
104 redirect_to my_account_path
104 redirect_to my_account_path
105 end
105 end
106 end
106 end
107 end
107 end
108 end
108 end
109
109
110 # Create a new feeds key
110 # Create a new feeds key
111 def reset_rss_key
111 def reset_rss_key
112 if request.post?
112 if request.post?
113 if User.current.rss_token
113 if User.current.rss_token
114 User.current.rss_token.destroy
114 User.current.rss_token.destroy
115 User.current.reload
115 User.current.reload
116 end
116 end
117 User.current.rss_key
117 User.current.rss_key
118 flash[:notice] = l(:notice_feeds_access_key_reseted)
118 flash[:notice] = l(:notice_feeds_access_key_reseted)
119 end
119 end
120 redirect_to my_account_path
120 redirect_to my_account_path
121 end
121 end
122
122
123 # Create a new API key
123 # Create a new API key
124 def reset_api_key
124 def reset_api_key
125 if request.post?
125 if request.post?
126 if User.current.api_token
126 if User.current.api_token
127 User.current.api_token.destroy
127 User.current.api_token.destroy
128 User.current.reload
128 User.current.reload
129 end
129 end
130 User.current.api_key
130 User.current.api_key
131 flash[:notice] = l(:notice_api_access_key_reseted)
131 flash[:notice] = l(:notice_api_access_key_reseted)
132 end
132 end
133 redirect_to my_account_path
133 redirect_to my_account_path
134 end
134 end
135
135
136 # User's page layout configuration
136 # User's page layout configuration
137 def page_layout
137 def page_layout
138 @user = User.current
138 @user = User.current
139 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
139 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
140 @block_options = []
140 @block_options = []
141 BLOCKS.each do |k, v|
141 BLOCKS.each do |k, v|
142 unless %w(top left right).detect {|f| (@blocks[f] ||= []).include?(k)}
142 unless @blocks.values.flatten.include?(k)
143 @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
143 @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
144 end
144 end
145 end
145 end
146 end
146 end
147
147
148 # Add a block to user's page
148 # Add a block to user's page
149 # The block is added on top of the page
149 # The block is added on top of the page
150 # params[:block] : id of the block to add
150 # params[:block] : id of the block to add
151 def add_block
151 def add_block
152 block = params[:block].to_s.underscore
152 block = params[:block].to_s.underscore
153 if block.present? && BLOCKS.key?(block)
153 if block.present? && BLOCKS.key?(block)
154 @user = User.current
154 @user = User.current
155 layout = @user.pref[:my_page_layout] || {}
155 layout = @user.pref[:my_page_layout] || {}
156 # remove if already present in a group
156 # remove if already present in a group
157 %w(top left right).each {|f| (layout[f] ||= []).delete block }
157 %w(top left right).each {|f| (layout[f] ||= []).delete block }
158 # add it on top
158 # add it on top
159 layout['top'].unshift block
159 layout['top'].unshift block
160 @user.pref[:my_page_layout] = layout
160 @user.pref[:my_page_layout] = layout
161 @user.pref.save
161 @user.pref.save
162 end
162 end
163 redirect_to my_page_layout_path
163 redirect_to my_page_layout_path
164 end
164 end
165
165
166 # Remove a block to user's page
166 # Remove a block to user's page
167 # params[:block] : id of the block to remove
167 # params[:block] : id of the block to remove
168 def remove_block
168 def remove_block
169 block = params[:block].to_s.underscore
169 block = params[:block].to_s.underscore
170 @user = User.current
170 @user = User.current
171 # remove block in all groups
171 # remove block in all groups
172 layout = @user.pref[:my_page_layout] || {}
172 layout = @user.pref[:my_page_layout] || {}
173 %w(top left right).each {|f| (layout[f] ||= []).delete block }
173 %w(top left right).each {|f| (layout[f] ||= []).delete block }
174 @user.pref[:my_page_layout] = layout
174 @user.pref[:my_page_layout] = layout
175 @user.pref.save
175 @user.pref.save
176 redirect_to my_page_layout_path
176 redirect_to my_page_layout_path
177 end
177 end
178
178
179 # Change blocks order on user's page
179 # Change blocks order on user's page
180 # params[:group] : group to order (top, left or right)
180 # params[:group] : group to order (top, left or right)
181 # params[:list-(top|left|right)] : array of block ids of the group
181 # params[:list-(top|left|right)] : array of block ids of the group
182 def order_blocks
182 def order_blocks
183 group = params[:group]
183 group = params[:group]
184 @user = User.current
184 @user = User.current
185 if group.is_a?(String)
185 if group.is_a?(String)
186 group_items = (params["blocks"] || []).collect(&:underscore)
186 group_items = (params["blocks"] || []).collect(&:underscore)
187 group_items.each {|s| s.sub!(/^block_/, '')}
187 group_items.each {|s| s.sub!(/^block_/, '')}
188 if group_items and group_items.is_a? Array
188 if group_items and group_items.is_a? Array
189 layout = @user.pref[:my_page_layout] || {}
189 layout = @user.pref[:my_page_layout] || {}
190 # remove group blocks if they are presents in other groups
190 # remove group blocks if they are presents in other groups
191 %w(top left right).each {|f|
191 %w(top left right).each {|f|
192 layout[f] = (layout[f] || []) - group_items
192 layout[f] = (layout[f] || []) - group_items
193 }
193 }
194 layout[group] = group_items
194 layout[group] = group_items
195 @user.pref[:my_page_layout] = layout
195 @user.pref[:my_page_layout] = layout
196 @user.pref.save
196 @user.pref.save
197 end
197 end
198 end
198 end
199 render :nothing => true
199 render :nothing => true
200 end
200 end
201 end
201 end
General Comments 0
You need to be logged in to leave comments. Login now