@@ -1,192 +1,174 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class MyController < ApplicationController |
|
19 | 19 | before_action :require_login |
|
20 | 20 | # let user change user's password when user has to |
|
21 | 21 | skip_before_action :check_password_change, :only => :password |
|
22 | 22 | |
|
23 | 23 | require_sudo_mode :account, only: :post |
|
24 | 24 | require_sudo_mode :reset_rss_key, :reset_api_key, :show_api_key, :destroy |
|
25 | 25 | |
|
26 | 26 | helper :issues |
|
27 | 27 | helper :users |
|
28 | 28 | helper :custom_fields |
|
29 | 29 | |
|
30 | 30 | def index |
|
31 | 31 | page |
|
32 | 32 | render :action => 'page' |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | # Show user's page |
|
36 | 36 | def page |
|
37 | 37 | @user = User.current |
|
38 | 38 | @blocks = @user.pref.my_page_layout |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | # Edit user's account |
|
42 | 42 | def account |
|
43 | 43 | @user = User.current |
|
44 | 44 | @pref = @user.pref |
|
45 | 45 | if request.post? |
|
46 | 46 | @user.safe_attributes = params[:user] |
|
47 | 47 | @user.pref.safe_attributes = params[:pref] |
|
48 | 48 | if @user.save |
|
49 | 49 | @user.pref.save |
|
50 | 50 | set_language_if_valid @user.language |
|
51 | 51 | flash[:notice] = l(:notice_account_updated) |
|
52 | 52 | redirect_to my_account_path |
|
53 | 53 | return |
|
54 | 54 | end |
|
55 | 55 | end |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | # Destroys user's account |
|
59 | 59 | def destroy |
|
60 | 60 | @user = User.current |
|
61 | 61 | unless @user.own_account_deletable? |
|
62 | 62 | redirect_to my_account_path |
|
63 | 63 | return |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | if request.post? && params[:confirm] |
|
67 | 67 | @user.destroy |
|
68 | 68 | if @user.destroyed? |
|
69 | 69 | logout_user |
|
70 | 70 | flash[:notice] = l(:notice_account_deleted) |
|
71 | 71 | end |
|
72 | 72 | redirect_to home_path |
|
73 | 73 | end |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | # Manage user's password |
|
77 | 77 | def password |
|
78 | 78 | @user = User.current |
|
79 | 79 | unless @user.change_password_allowed? |
|
80 | 80 | flash[:error] = l(:notice_can_t_change_password) |
|
81 | 81 | redirect_to my_account_path |
|
82 | 82 | return |
|
83 | 83 | end |
|
84 | 84 | if request.post? |
|
85 | 85 | if !@user.check_password?(params[:password]) |
|
86 | 86 | flash.now[:error] = l(:notice_account_wrong_password) |
|
87 | 87 | elsif params[:password] == params[:new_password] |
|
88 | 88 | flash.now[:error] = l(:notice_new_password_must_be_different) |
|
89 | 89 | else |
|
90 | 90 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
91 | 91 | @user.must_change_passwd = false |
|
92 | 92 | if @user.save |
|
93 | 93 | # The session token was destroyed by the password change, generate a new one |
|
94 | 94 | session[:tk] = @user.generate_session_token |
|
95 | 95 | Mailer.password_updated(@user) |
|
96 | 96 | flash[:notice] = l(:notice_account_password_updated) |
|
97 | 97 | redirect_to my_account_path |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | end |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | # Create a new feeds key |
|
104 | 104 | def reset_rss_key |
|
105 | 105 | if request.post? |
|
106 | 106 | if User.current.rss_token |
|
107 | 107 | User.current.rss_token.destroy |
|
108 | 108 | User.current.reload |
|
109 | 109 | end |
|
110 | 110 | User.current.rss_key |
|
111 | 111 | flash[:notice] = l(:notice_feeds_access_key_reseted) |
|
112 | 112 | end |
|
113 | 113 | redirect_to my_account_path |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | 116 | def show_api_key |
|
117 | 117 | @user = User.current |
|
118 | 118 | end |
|
119 | 119 | |
|
120 | 120 | # Create a new API key |
|
121 | 121 | def reset_api_key |
|
122 | 122 | if request.post? |
|
123 | 123 | if User.current.api_token |
|
124 | 124 | User.current.api_token.destroy |
|
125 | 125 | User.current.reload |
|
126 | 126 | end |
|
127 | 127 | User.current.api_key |
|
128 | 128 | flash[:notice] = l(:notice_api_access_key_reseted) |
|
129 | 129 | end |
|
130 | 130 | redirect_to my_account_path |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | # User's page layout configuration |
|
134 | 134 | def page_layout |
|
135 | 135 | @user = User.current |
|
136 | 136 | @blocks = @user.pref.my_page_layout |
|
137 | 137 | end |
|
138 | 138 | |
|
139 | 139 | # Add a block to user's page |
|
140 | 140 | # The block is added on top of the page |
|
141 | 141 | # params[:block] : id of the block to add |
|
142 | 142 | def add_block |
|
143 | block = params[:block].to_s.underscore | |
|
144 | if block.present? && Redmine::MyPage.blocks.key?(block) | |
|
145 | 143 |
|
|
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 | |
|
144 | @user.pref.add_block params[:block] | |
|
152 | 145 |
|
|
153 | end | |
|
154 | 146 | redirect_to my_page_layout_path |
|
155 | 147 | end |
|
156 | 148 | |
|
157 | 149 | # Remove a block to user's page |
|
158 | 150 | # params[:block] : id of the block to remove |
|
159 | 151 | def remove_block |
|
160 | block = params[:block].to_s.underscore | |
|
161 | 152 | @user = User.current |
|
162 | # remove block in all groups | |
|
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 | |
|
153 | @user.pref.remove_block params[:block] | |
|
166 | 154 | @user.pref.save |
|
167 | 155 | redirect_to my_page_layout_path |
|
168 | 156 | end |
|
169 | 157 | |
|
170 | 158 | # Change blocks order on user's page |
|
171 | 159 | # params[:group] : group to order (top, left or right) |
|
172 |
# params[: |
|
|
160 | # params[:blocks] : array of block ids of the group | |
|
173 | 161 | def order_blocks |
|
174 | 162 | group = params[:group] |
|
175 | 163 | @user = User.current |
|
176 | 164 | if group.is_a?(String) |
|
177 | 165 | group_items = (params["blocks"] || []).collect(&:underscore) |
|
178 | 166 | group_items.each {|s| s.sub!(/^block_/, '')} |
|
179 | if group_items and group_items.is_a? Array | |
|
180 | layout = @user.pref.my_page_layout | |
|
181 | 167 |
|
|
182 | %w(top left right).each {|f| | |
|
183 | layout[f] = (layout[f] || []) - group_items | |
|
184 | } | |
|
185 | layout[group] = group_items | |
|
186 | @user.pref.my_page_layout = layout | |
|
168 | group_items.each {|s| @user.pref.remove_block(s)} | |
|
169 | @user.pref.my_page_layout[group] = group_items | |
|
187 | 170 |
|
|
188 | 171 |
|
|
189 | end | |
|
190 | 172 | head 200 |
|
191 | 173 | end |
|
192 | 174 | end |
@@ -1,93 +1,111 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class UserPreference < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | |
|
21 | 21 | belongs_to :user |
|
22 | 22 | serialize :others |
|
23 | 23 | |
|
24 | 24 | attr_protected :others, :user_id |
|
25 | 25 | |
|
26 | 26 | before_save :set_others_hash |
|
27 | 27 | |
|
28 | 28 | safe_attributes 'hide_mail', |
|
29 | 29 | 'time_zone', |
|
30 | 30 | 'comments_sorting', |
|
31 | 31 | 'warn_on_leaving_unsaved', |
|
32 | 32 | 'no_self_notified', |
|
33 | 33 | 'textarea_font' |
|
34 | 34 | |
|
35 | 35 | TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional'] |
|
36 | 36 | |
|
37 | 37 | def initialize(attributes=nil, *args) |
|
38 | 38 | super |
|
39 | 39 | if new_record? && !(attributes && attributes.key?(:hide_mail)) |
|
40 | 40 | self.hide_mail = Setting.default_users_hide_mail? |
|
41 | 41 | end |
|
42 | 42 | if new_record? && !(attributes && attributes.key?(:no_self_notified)) |
|
43 | 43 | self.no_self_notified = true |
|
44 | 44 | end |
|
45 | 45 | self.others ||= {} |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | def set_others_hash |
|
49 | 49 | self.others ||= {} |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def [](attr_name) |
|
53 | 53 | if has_attribute? attr_name |
|
54 | 54 | super |
|
55 | 55 | else |
|
56 | 56 | others ? others[attr_name] : nil |
|
57 | 57 | end |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | def []=(attr_name, value) |
|
61 | 61 | if has_attribute? attr_name |
|
62 | 62 | super |
|
63 | 63 | else |
|
64 | 64 | h = (read_attribute(:others) || {}).dup |
|
65 | 65 | h.update(attr_name => value) |
|
66 | 66 | write_attribute(:others, h) |
|
67 | 67 | value |
|
68 | 68 | end |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def comments_sorting; self[:comments_sorting] end |
|
72 | 72 | def comments_sorting=(order); self[:comments_sorting]=order end |
|
73 | 73 | |
|
74 | 74 | def warn_on_leaving_unsaved; self[:warn_on_leaving_unsaved] || '1'; end |
|
75 | 75 | def warn_on_leaving_unsaved=(value); self[:warn_on_leaving_unsaved]=value; end |
|
76 | 76 | |
|
77 | 77 | def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end |
|
78 | 78 | def no_self_notified=(value); self[:no_self_notified]=value; end |
|
79 | 79 | |
|
80 | 80 | def activity_scope; Array(self[:activity_scope]) ; end |
|
81 | 81 | def activity_scope=(value); self[:activity_scope]=value ; end |
|
82 | 82 | |
|
83 | 83 | def textarea_font; self[:textarea_font] end |
|
84 | 84 | def textarea_font=(value); self[:textarea_font]=value; end |
|
85 | 85 | |
|
86 | 86 | def my_page_layout |
|
87 | 87 | self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | def my_page_layout=(arg) |
|
91 | 91 | self[:my_page_layout] = arg |
|
92 | 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 | 111 | end |
General Comments 0
You need to be logged in to leave comments.
Login now