@@ -1,144 +1,149 | |||||
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 WatchersController < ApplicationController |
|
18 | class WatchersController < ApplicationController | |
19 | before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] |
|
19 | before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] | |
20 |
|
20 | |||
21 | def watch |
|
21 | def watch | |
22 | set_watcher(@watchables, User.current, true) |
|
22 | set_watcher(@watchables, User.current, true) | |
23 | end |
|
23 | end | |
24 |
|
24 | |||
25 | def unwatch |
|
25 | def unwatch | |
26 | set_watcher(@watchables, User.current, false) |
|
26 | set_watcher(@watchables, User.current, false) | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
29 | before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user] |
|
29 | before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user] | |
30 | accept_api_auth :create, :destroy |
|
30 | accept_api_auth :create, :destroy | |
31 |
|
31 | |||
32 | def new |
|
32 | def new | |
33 | @users = users_for_new_watcher |
|
33 | @users = users_for_new_watcher | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | def create |
|
36 | def create | |
37 | user_ids = [] |
|
37 | user_ids = [] | |
38 | if params[:watcher].is_a?(Hash) |
|
38 | if params[:watcher].is_a?(Hash) | |
39 | user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id]) |
|
39 | user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id]) | |
40 | else |
|
40 | else | |
41 | user_ids << params[:user_id] |
|
41 | user_ids << params[:user_id] | |
42 | end |
|
42 | end | |
43 | users = User.active.visible.where(:id => user_ids.flatten.compact.uniq) |
|
43 | users = User.active.visible.where(:id => user_ids.flatten.compact.uniq) | |
44 | users.each do |user| |
|
44 | users.each do |user| | |
45 | @watchables.each do |watchable| |
|
45 | @watchables.each do |watchable| | |
46 | Watcher.create(:watchable => watchable, :user => user) |
|
46 | Watcher.create(:watchable => watchable, :user => user) | |
47 | end |
|
47 | end | |
48 | end |
|
48 | end | |
49 | respond_to do |format| |
|
49 | respond_to do |format| | |
50 | format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} |
|
50 | format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} | |
51 | format.js { @users = users_for_new_watcher } |
|
51 | format.js { @users = users_for_new_watcher } | |
52 | format.api { render_api_ok } |
|
52 | format.api { render_api_ok } | |
53 | end |
|
53 | end | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def append |
|
56 | def append | |
57 | if params[:watcher].is_a?(Hash) |
|
57 | if params[:watcher].is_a?(Hash) | |
58 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] |
|
58 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] | |
59 | @users = User.active.visible.where(:id => user_ids).to_a |
|
59 | @users = User.active.visible.where(:id => user_ids).to_a | |
60 | end |
|
60 | end | |
61 | if @users.blank? |
|
61 | if @users.blank? | |
62 | render :nothing => true |
|
62 | render :nothing => true | |
63 | end |
|
63 | end | |
64 | end |
|
64 | end | |
65 |
|
65 | |||
66 | def destroy |
|
66 | def destroy | |
67 | user = User.find(params[:user_id]) |
|
67 | user = User.find(params[:user_id]) | |
68 | @watchables.each do |watchable| |
|
68 | @watchables.each do |watchable| | |
69 | watchable.set_watcher(user, false) |
|
69 | watchable.set_watcher(user, false) | |
70 | end |
|
70 | end | |
71 | respond_to do |format| |
|
71 | respond_to do |format| | |
72 | format.html { redirect_to :back } |
|
72 | format.html { redirect_to :back } | |
73 | format.js |
|
73 | format.js | |
74 | format.api { render_api_ok } |
|
74 | format.api { render_api_ok } | |
75 | end |
|
75 | end | |
76 | rescue ActiveRecord::RecordNotFound |
|
76 | rescue ActiveRecord::RecordNotFound | |
77 | render_404 |
|
77 | render_404 | |
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | def autocomplete_for_user |
|
80 | def autocomplete_for_user | |
81 | @users = users_for_new_watcher |
|
81 | @users = users_for_new_watcher | |
82 | render :layout => false |
|
82 | render :layout => false | |
83 | end |
|
83 | end | |
84 |
|
84 | |||
85 | private |
|
85 | private | |
86 |
|
86 | |||
87 | def find_project |
|
87 | def find_project | |
88 | if params[:object_type] && params[:object_id] |
|
88 | if params[:object_type] && params[:object_id] | |
89 | @watchables = find_objets_from_params |
|
89 | @watchables = find_objets_from_params | |
90 | @projects = @watchables.map(&:project).uniq |
|
90 | @projects = @watchables.map(&:project).uniq | |
91 | if @projects.size == 1 |
|
91 | if @projects.size == 1 | |
92 | @project = @projects.first |
|
92 | @project = @projects.first | |
93 | end |
|
93 | end | |
94 | elsif params[:project_id] |
|
94 | elsif params[:project_id] | |
95 | @project = Project.visible.find_by_param(params[:project_id]) |
|
95 | @project = Project.visible.find_by_param(params[:project_id]) | |
96 | end |
|
96 | end | |
97 | end |
|
97 | end | |
98 |
|
98 | |||
99 | def find_watchables |
|
99 | def find_watchables | |
100 | @watchables = find_objets_from_params |
|
100 | @watchables = find_objets_from_params | |
101 | unless @watchables.present? |
|
101 | unless @watchables.present? | |
102 | render_404 |
|
102 | render_404 | |
103 | end |
|
103 | end | |
104 | end |
|
104 | end | |
105 |
|
105 | |||
106 | def set_watcher(watchables, user, watching) |
|
106 | def set_watcher(watchables, user, watching) | |
107 | watchables.each do |watchable| |
|
107 | watchables.each do |watchable| | |
108 | watchable.set_watcher(user, watching) |
|
108 | watchable.set_watcher(user, watching) | |
109 | end |
|
109 | end | |
110 | respond_to do |format| |
|
110 | respond_to do |format| | |
111 | format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} |
|
111 | format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} | |
112 | format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } |
|
112 | format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } | |
113 | end |
|
113 | end | |
114 | end |
|
114 | end | |
115 |
|
115 | |||
116 | def users_for_new_watcher |
|
116 | def users_for_new_watcher | |
117 | scope = nil |
|
117 | scope = nil | |
118 | if params[:q].blank? && @project.present? |
|
118 | if params[:q].blank? && @project.present? | |
119 | scope = @project.users |
|
119 | scope = @project.users | |
120 | else |
|
120 | else | |
121 | scope = User.all.limit(100) |
|
121 | scope = User.all.limit(100) | |
122 | end |
|
122 | end | |
123 | users = scope.active.visible.sorted.like(params[:q]).to_a |
|
123 | users = scope.active.visible.sorted.like(params[:q]).to_a | |
124 | if @watchables && @watchables.size == 1 |
|
124 | if @watchables && @watchables.size == 1 | |
125 | users -= @watchables.first.watcher_users |
|
125 | users -= @watchables.first.watcher_users | |
126 | end |
|
126 | end | |
127 | users |
|
127 | users | |
128 | end |
|
128 | end | |
129 |
|
129 | |||
130 | def find_objets_from_params |
|
130 | def find_objets_from_params | |
131 | klass = Object.const_get(params[:object_type].camelcase) rescue nil |
|
131 | klass = Object.const_get(params[:object_type].camelcase) rescue nil | |
132 | return unless klass && klass.respond_to?('watched_by') |
|
132 | return unless klass && klass.respond_to?('watched_by') | |
133 |
|
133 | |||
134 |
|
|
134 | scope = klass.where(:id => Array.wrap(params[:object_id])) | |
|
135 | if klass.reflect_on_association(:project) | |||
|
136 | scope = scope.preload(:project => :enabled_modules) | |||
|
137 | end | |||
|
138 | objects = scope.to_a | |||
|
139 | ||||
135 | raise Unauthorized if objects.any? do |w| |
|
140 | raise Unauthorized if objects.any? do |w| | |
136 | if w.respond_to?(:visible?) |
|
141 | if w.respond_to?(:visible?) | |
137 | !w.visible? |
|
142 | !w.visible? | |
138 | elsif w.respond_to?(:project) && w.project |
|
143 | elsif w.respond_to?(:project) && w.project | |
139 | !w.project.visible? |
|
144 | !w.project.visible? | |
140 | end |
|
145 | end | |
141 | end |
|
146 | end | |
142 | objects |
|
147 | objects | |
143 | end |
|
148 | end | |
144 | end |
|
149 | end |
General Comments 0
You need to be logged in to leave comments.
Login now