##// END OF EJS Templates
Adds #add_block and #remove_block methods....
Jean-Philippe Lang -
r15550:55be5e82b849
parent child
Show More
@@ -1,192 +1,174
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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_action :require_login
19 before_action :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_action :check_password_change, :only => :password
21 skip_before_action :check_password_change, :only => :password
22
22
23 require_sudo_mode :account, only: :post
23 require_sudo_mode :account, only: :post
24 require_sudo_mode :reset_rss_key, :reset_api_key, :show_api_key, :destroy
24 require_sudo_mode :reset_rss_key, :reset_api_key, :show_api_key, :destroy
25
25
26 helper :issues
26 helper :issues
27 helper :users
27 helper :users
28 helper :custom_fields
28 helper :custom_fields
29
29
30 def index
30 def index
31 page
31 page
32 render :action => 'page'
32 render :action => 'page'
33 end
33 end
34
34
35 # Show user's page
35 # Show user's page
36 def page
36 def page
37 @user = User.current
37 @user = User.current
38 @blocks = @user.pref.my_page_layout
38 @blocks = @user.pref.my_page_layout
39 end
39 end
40
40
41 # Edit user's account
41 # Edit user's account
42 def account
42 def account
43 @user = User.current
43 @user = User.current
44 @pref = @user.pref
44 @pref = @user.pref
45 if request.post?
45 if request.post?
46 @user.safe_attributes = params[:user]
46 @user.safe_attributes = params[:user]
47 @user.pref.safe_attributes = params[:pref]
47 @user.pref.safe_attributes = params[:pref]
48 if @user.save
48 if @user.save
49 @user.pref.save
49 @user.pref.save
50 set_language_if_valid @user.language
50 set_language_if_valid @user.language
51 flash[:notice] = l(:notice_account_updated)
51 flash[:notice] = l(:notice_account_updated)
52 redirect_to my_account_path
52 redirect_to my_account_path
53 return
53 return
54 end
54 end
55 end
55 end
56 end
56 end
57
57
58 # Destroys user's account
58 # Destroys user's account
59 def destroy
59 def destroy
60 @user = User.current
60 @user = User.current
61 unless @user.own_account_deletable?
61 unless @user.own_account_deletable?
62 redirect_to my_account_path
62 redirect_to my_account_path
63 return
63 return
64 end
64 end
65
65
66 if request.post? && params[:confirm]
66 if request.post? && params[:confirm]
67 @user.destroy
67 @user.destroy
68 if @user.destroyed?
68 if @user.destroyed?
69 logout_user
69 logout_user
70 flash[:notice] = l(:notice_account_deleted)
70 flash[:notice] = l(:notice_account_deleted)
71 end
71 end
72 redirect_to home_path
72 redirect_to home_path
73 end
73 end
74 end
74 end
75
75
76 # Manage user's password
76 # Manage user's password
77 def password
77 def password
78 @user = User.current
78 @user = User.current
79 unless @user.change_password_allowed?
79 unless @user.change_password_allowed?
80 flash[:error] = l(:notice_can_t_change_password)
80 flash[:error] = l(:notice_can_t_change_password)
81 redirect_to my_account_path
81 redirect_to my_account_path
82 return
82 return
83 end
83 end
84 if request.post?
84 if request.post?
85 if !@user.check_password?(params[:password])
85 if !@user.check_password?(params[:password])
86 flash.now[:error] = l(:notice_account_wrong_password)
86 flash.now[:error] = l(:notice_account_wrong_password)
87 elsif params[:password] == params[:new_password]
87 elsif params[:password] == params[:new_password]
88 flash.now[:error] = l(:notice_new_password_must_be_different)
88 flash.now[:error] = l(:notice_new_password_must_be_different)
89 else
89 else
90 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
90 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
91 @user.must_change_passwd = false
91 @user.must_change_passwd = false
92 if @user.save
92 if @user.save
93 # The session token was destroyed by the password change, generate a new one
93 # The session token was destroyed by the password change, generate a new one
94 session[:tk] = @user.generate_session_token
94 session[:tk] = @user.generate_session_token
95 Mailer.password_updated(@user)
95 Mailer.password_updated(@user)
96 flash[:notice] = l(:notice_account_password_updated)
96 flash[:notice] = l(:notice_account_password_updated)
97 redirect_to my_account_path
97 redirect_to my_account_path
98 end
98 end
99 end
99 end
100 end
100 end
101 end
101 end
102
102
103 # Create a new feeds key
103 # Create a new feeds key
104 def reset_rss_key
104 def reset_rss_key
105 if request.post?
105 if request.post?
106 if User.current.rss_token
106 if User.current.rss_token
107 User.current.rss_token.destroy
107 User.current.rss_token.destroy
108 User.current.reload
108 User.current.reload
109 end
109 end
110 User.current.rss_key
110 User.current.rss_key
111 flash[:notice] = l(:notice_feeds_access_key_reseted)
111 flash[:notice] = l(:notice_feeds_access_key_reseted)
112 end
112 end
113 redirect_to my_account_path
113 redirect_to my_account_path
114 end
114 end
115
115
116 def show_api_key
116 def show_api_key
117 @user = User.current
117 @user = User.current
118 end
118 end
119
119
120 # Create a new API key
120 # Create a new API key
121 def reset_api_key
121 def reset_api_key
122 if request.post?
122 if request.post?
123 if User.current.api_token
123 if User.current.api_token
124 User.current.api_token.destroy
124 User.current.api_token.destroy
125 User.current.reload
125 User.current.reload
126 end
126 end
127 User.current.api_key
127 User.current.api_key
128 flash[:notice] = l(:notice_api_access_key_reseted)
128 flash[:notice] = l(:notice_api_access_key_reseted)
129 end
129 end
130 redirect_to my_account_path
130 redirect_to my_account_path
131 end
131 end
132
132
133 # User's page layout configuration
133 # User's page layout configuration
134 def page_layout
134 def page_layout
135 @user = User.current
135 @user = User.current
136 @blocks = @user.pref.my_page_layout
136 @blocks = @user.pref.my_page_layout
137 end
137 end
138
138
139 # Add a block to user's page
139 # Add a block to user's page
140 # The block is added on top of the page
140 # The block is added on top of the page
141 # params[:block] : id of the block to add
141 # params[:block] : id of the block to add
142 def add_block
142 def add_block
143 block = params[:block].to_s.underscore
143 @user = User.current
144 if block.present? && Redmine::MyPage.blocks.key?(block)
144 @user.pref.add_block params[:block]
145 @user = User.current
145 @user.pref.save
146 layout = @user.pref.my_page_layout
147 # remove if already present in a group
148 %w(top left right).each {|f| (layout[f] ||= []).delete block }
149 # add it on top
150 layout['top'].unshift block
151 @user.pref.my_page_layout = layout
152 @user.pref.save
153 end
154 redirect_to my_page_layout_path
146 redirect_to my_page_layout_path
155 end
147 end
156
148
157 # Remove a block to user's page
149 # Remove a block to user's page
158 # params[:block] : id of the block to remove
150 # params[:block] : id of the block to remove
159 def remove_block
151 def remove_block
160 block = params[:block].to_s.underscore
161 @user = User.current
152 @user = User.current
162 # remove block in all groups
153 @user.pref.remove_block params[:block]
163 layout = @user.pref.my_page_layout
164 %w(top left right).each {|f| (layout[f] ||= []).delete block }
165 @user.pref.my_page_layout = layout
166 @user.pref.save
154 @user.pref.save
167 redirect_to my_page_layout_path
155 redirect_to my_page_layout_path
168 end
156 end
169
157
170 # Change blocks order on user's page
158 # Change blocks order on user's page
171 # params[:group] : group to order (top, left or right)
159 # params[:group] : group to order (top, left or right)
172 # params[:list-(top|left|right)] : array of block ids of the group
160 # params[:blocks] : array of block ids of the group
173 def order_blocks
161 def order_blocks
174 group = params[:group]
162 group = params[:group]
175 @user = User.current
163 @user = User.current
176 if group.is_a?(String)
164 if group.is_a?(String)
177 group_items = (params["blocks"] || []).collect(&:underscore)
165 group_items = (params["blocks"] || []).collect(&:underscore)
178 group_items.each {|s| s.sub!(/^block_/, '')}
166 group_items.each {|s| s.sub!(/^block_/, '')}
179 if group_items and group_items.is_a? Array
167 # remove group blocks if they are presents in other groups
180 layout = @user.pref.my_page_layout
168 group_items.each {|s| @user.pref.remove_block(s)}
181 # remove group blocks if they are presents in other groups
169 @user.pref.my_page_layout[group] = group_items
182 %w(top left right).each {|f|
170 @user.pref.save
183 layout[f] = (layout[f] || []) - group_items
184 }
185 layout[group] = group_items
186 @user.pref.my_page_layout = layout
187 @user.pref.save
188 end
189 end
171 end
190 head 200
172 head 200
191 end
173 end
192 end
174 end
@@ -1,93 +1,111
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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 UserPreference < ActiveRecord::Base
18 class UserPreference < ActiveRecord::Base
19 include Redmine::SafeAttributes
19 include Redmine::SafeAttributes
20
20
21 belongs_to :user
21 belongs_to :user
22 serialize :others
22 serialize :others
23
23
24 attr_protected :others, :user_id
24 attr_protected :others, :user_id
25
25
26 before_save :set_others_hash
26 before_save :set_others_hash
27
27
28 safe_attributes 'hide_mail',
28 safe_attributes 'hide_mail',
29 'time_zone',
29 'time_zone',
30 'comments_sorting',
30 'comments_sorting',
31 'warn_on_leaving_unsaved',
31 'warn_on_leaving_unsaved',
32 'no_self_notified',
32 'no_self_notified',
33 'textarea_font'
33 'textarea_font'
34
34
35 TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
35 TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
36
36
37 def initialize(attributes=nil, *args)
37 def initialize(attributes=nil, *args)
38 super
38 super
39 if new_record? && !(attributes && attributes.key?(:hide_mail))
39 if new_record? && !(attributes && attributes.key?(:hide_mail))
40 self.hide_mail = Setting.default_users_hide_mail?
40 self.hide_mail = Setting.default_users_hide_mail?
41 end
41 end
42 if new_record? && !(attributes && attributes.key?(:no_self_notified))
42 if new_record? && !(attributes && attributes.key?(:no_self_notified))
43 self.no_self_notified = true
43 self.no_self_notified = true
44 end
44 end
45 self.others ||= {}
45 self.others ||= {}
46 end
46 end
47
47
48 def set_others_hash
48 def set_others_hash
49 self.others ||= {}
49 self.others ||= {}
50 end
50 end
51
51
52 def [](attr_name)
52 def [](attr_name)
53 if has_attribute? attr_name
53 if has_attribute? attr_name
54 super
54 super
55 else
55 else
56 others ? others[attr_name] : nil
56 others ? others[attr_name] : nil
57 end
57 end
58 end
58 end
59
59
60 def []=(attr_name, value)
60 def []=(attr_name, value)
61 if has_attribute? attr_name
61 if has_attribute? attr_name
62 super
62 super
63 else
63 else
64 h = (read_attribute(:others) || {}).dup
64 h = (read_attribute(:others) || {}).dup
65 h.update(attr_name => value)
65 h.update(attr_name => value)
66 write_attribute(:others, h)
66 write_attribute(:others, h)
67 value
67 value
68 end
68 end
69 end
69 end
70
70
71 def comments_sorting; self[:comments_sorting] end
71 def comments_sorting; self[:comments_sorting] end
72 def comments_sorting=(order); self[:comments_sorting]=order end
72 def comments_sorting=(order); self[:comments_sorting]=order end
73
73
74 def warn_on_leaving_unsaved; self[:warn_on_leaving_unsaved] || '1'; end
74 def warn_on_leaving_unsaved; self[:warn_on_leaving_unsaved] || '1'; end
75 def warn_on_leaving_unsaved=(value); self[:warn_on_leaving_unsaved]=value; end
75 def warn_on_leaving_unsaved=(value); self[:warn_on_leaving_unsaved]=value; end
76
76
77 def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end
77 def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end
78 def no_self_notified=(value); self[:no_self_notified]=value; end
78 def no_self_notified=(value); self[:no_self_notified]=value; end
79
79
80 def activity_scope; Array(self[:activity_scope]) ; end
80 def activity_scope; Array(self[:activity_scope]) ; end
81 def activity_scope=(value); self[:activity_scope]=value ; end
81 def activity_scope=(value); self[:activity_scope]=value ; end
82
82
83 def textarea_font; self[:textarea_font] end
83 def textarea_font; self[:textarea_font] end
84 def textarea_font=(value); self[:textarea_font]=value; end
84 def textarea_font=(value); self[:textarea_font]=value; end
85
85
86 def my_page_layout
86 def my_page_layout
87 self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
87 self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
88 end
88 end
89
89
90 def my_page_layout=(arg)
90 def my_page_layout=(arg)
91 self[:my_page_layout] = arg
91 self[:my_page_layout] = arg
92 end
92 end
93
94 def remove_block(block)
95 block = block.to_s.underscore
96 %w(top left right).each do |f|
97 (my_page_layout[f] ||= []).delete(block)
98 end
99 my_page_layout
100 end
101
102 def add_block(block)
103 block = block.to_s.underscore
104 return unless Redmine::MyPage.blocks.key?(block)
105
106 remove_block(block)
107 # add it on top
108 my_page_layout['top'] ||= []
109 my_page_layout['top'].unshift(block)
110 end
93 end
111 end
General Comments 0
You need to be logged in to leave comments. Login now