forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaboration.rb
More file actions
385 lines (329 loc) · 12 KB
/
Copy pathcollaboration.rb
File metadata and controls
385 lines (329 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# Copyright (C) 2011-2012 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class Collaboration < ActiveRecord::Base
include Workflow
include SendToStream
attr_readonly :collaboration_type
belongs_to :context, polymorphic: [:course, :group]
belongs_to :user
has_many :collaborators, :dependent => :destroy
has_many :users, :through => :collaborators
before_destroy { |record| Collaborator.where(:collaboration_id => record).destroy_all }
before_save :assign_uuid
before_save :set_context_code
after_save :include_author_as_collaborator
after_save :touch_context
after_commit :generate_document, on: :create
TITLE_MAX_LENGTH = 255
validates_presence_of :title, :workflow_state, :context_id, :context_type
validates_length_of :title, :maximum => TITLE_MAX_LENGTH
validates_length_of :description, :maximum => maximum_text_length, :allow_nil => true, :allow_blank => true
serialize :data
alias_method :destroy_permanently!, :destroy
workflow do
state :active
state :deleted
end
on_create_send_to_streams do
[self.user_id] + self.collaborators.map(&:user_id)
end
set_policy do
given { |user|
user &&
!self.new_record? &&
(self.user_id == user.id ||
self.users.include?(user) ||
Collaborator.
joins("INNER JOIN #{GroupMembership.quoted_table_name} ON collaborators.group_id = group_memberships.group_id").
where('collaborators.group_id IS NOT NULL AND
group_memberships.user_id = ? AND
collaborators.collaboration_id = ?', user, self).exists?)
}
can :read
given { |user, session| self.context.grants_right?(user, session, :create_collaborations) }
can :create
given { |user, session| self.context.grants_right?(user, session, :manage_content) }
can :read and can :update and can :delete
given { |user, session|
user && self.user_id == user.id &&
self.context.grants_right?(user, session, :create_collaborations) }
can :read and can :update and can :delete
end
scope :active, -> { where("collaborations.workflow_state<>'deleted'") }
scope :after, lambda { |date| where("collaborations.updated_at>?", date) }
scope :for_context_codes, lambda { |context_codes| where(:context_code => context_codes) }
scope :for_context, lambda { |context| where(context_type: context.class.reflection_type_name, context_id: context) }
# These methods should be implemented in child classes.
def service_name; 'Collaboration'; end
def delete_document; end
def initialize_document; end
def user_can_access_document_type?(user); true; end
def authorize_user(user); end
#def remove_users_from_document(users_to_remove); end
#def add_users_to_document(users_to_add); end
def config; raise 'Not implemented'; end
def parse_data; nil; end
# Public: Find the class of for the given type.
#
# type - The string name of the collaboration type (e.g. 'GoogleDocs' or 'EtherPad').
#
# Returns a class or nil.
def self.collaboration_class(type)
if klass = "#{type}Collaboration".constantize
klass.ancestors.include?(Collaboration) && klass.config ? klass : nil
end
rescue NameError
nil
end
# Public: Create a new collaboration of the given type.
#
# name - The string name of the collaboration type.
#
# Returns a collaboration instance or raises an exception if type unknown.
def self.typed_collaboration_instance(name)
class_config = Collaboration.collaboration_types.find { |c| c['name'] == name }
klass = collaboration_class(class_config['type'].titleize.gsub(/\s/, ''))
if klass
collaboration = klass.new
collaboration.collaboration_type = class_config['name']
else
raise "Unrecognized collaboration type #{type}."
end
collaboration
end
# Public: Find the available collaboration types.
#
# Returns an array of type hashes w/ 'name' and 'type' keys.
def self.collaboration_types
Canvas::Plugin.all_for_tag(:collaborations).select(&:enabled?).map do |plugin|
# google_drive is really a google_docs_collaboration
# eventually this will go away. baby steps...
if plugin.id == 'google_drive'
type = 'google_docs'
name = 'Google Docs'
else
type = plugin.id
name = plugin.name
end
HashWithIndifferentAccess.new({ 'name' => name, 'type' => type })
end
end
# Public: Determine if any collaborations plugin is enabled.
#
# Returns true/false.
def self.any_collaborations_configured?(context)
plugin_collabs = collaboration_types.any? do |type|
collaboration_class(type['type'].titleize.gsub(/\s/, '')).present?
end
external_tool_collabs = ContextExternalTool.all_tools_for(context, placements: :collaboration).exists?
plugin_collabs || external_tool_collabs
end
# Public: Declare excluded serialization fields.
#
# Returns an array.
def self.serialization_excludes; [:uuid]; end
# Public: Soft-delete this collaboration.
#
# Returns true.
def destroy
self.workflow_state = 'deleted'
self.deleted_at = Time.now
save!
end
# Public: Un-delete this collaboration.
#
# Returns a success boolean.
def restore
update_attribute(:workflow_state, 'active')
end
# Internal: Add the author of the collaboration to its collaborators.
#
# Returns nothing.
def include_author_as_collaborator
return unless self.user.present?
author = collaborators.where(:user_id => self.user_id).first
unless author
collaborator = Collaborator.new(:collaboration => self)
collaborator.user_id = self.user_id
collaborator.authorized_service_user_id = authorized_service_user_id_for(self.user)
collaborator.save
end
end
# Public: Create a CSS style string.
#
# NOTE: I assume this is for compatibility w/ something in Canvas' bowels,
# but it may not be needed anymore.
#
# Returns nil.
def style_class
nil
end
# Public: Create a list of collaborator IDs.
#
# Returns a comma-seperated list of collaborator user IDs.
def collaborator_ids
self.collaborators.pluck(:user_id).join(',')
end
# Internal: Create the collaboration document in the remote service.
#
# Returns nothing.
def generate_document
return if @generated
@generated = true
assign_uuid
initialize_document
save!
end
# Public: Determine if a given user can access this collaboration.
#
# user - The user to test.
#
# Returns a boolean.
def valid_user?(user)
if grants_right?(user, nil, :read)
user_can_access_document_type?(user)
else
false
end
end
# Public: Update user and group collaborators for this collaboration.
#
# Any current collaborators not passed to this method will be destroyed.
#
# users - An array of users to include as collaborators.
# groups - An array of groups or group ids to include as collaborators.
#
# Returns nothing.
def update_members(users = [], groups = [])
group_ids = groups.map {|g| g.try(:id) || g }
save! if new_record?
generate_document
users << user if user.present? && !users.include?(user)
update_user_collaborators(users)
update_group_collaborators(group_ids)
if respond_to?(:add_users_to_document)
group_users_to_add = User.
distinct.
joins(:group_memberships).
where('group_memberships.group_id' => group_ids).to_a
add_users_to_document((users + group_users_to_add).uniq)
end
end
# Internal: Create a new UUID for this collaboration if one does not exist.
#
# Returns a UUID string.
def assign_uuid
self.uuid ||= CanvasSlug.generate_securish_uuid
end
protected :assign_uuid
# Internal: Set the context code for this collaboration.
#
# Returns a context code.
def set_context_code
self.context_code = "#{self.context_type.underscore}_#{self.context_id}"
rescue NoMethodError
nil
end
protected :set_context_code
# Internal: Delete existing collaborating users and add new ones.
#
# users - The array of users to add. Any duplicates with current users
# will not be removed or re-added.
#
# Returns nothing.
def update_user_collaborators(users)
if respond_to?(:remove_users_from_document)
# need to get everyone added to the document, cause we're going to re-add them all
users_to_remove = collaborators.where("user_id IS NOT NULL").pluck(:user_id)
group_ids = collaborators.where("group_id IS NOT NULL").pluck(:group_id)
if !group_ids.empty?
users_to_remove += GroupMembership.where(group_id: group_ids).distinct.pluck(:user_id)
users_to_remove.uniq!
end
# make real user objects, instead of just ids, cause that's what this code expects
users_to_remove.reject! {|id| id == self.user.id}
users_to_remove = users_to_remove.map { |id| User.send(:instantiate, 'id' => id) }
remove_users_from_document(users_to_remove)
end
remove_users_from_collaborators(users)
add_users_to_collaborators(users)
end
protected :update_user_collaborators
# Internal: Remove old group collaborators and add new ones.
#
# group_ids - An array of IDs for groups to be added. Any duplicates w/
# existing groups will not be deleted/added.
def update_group_collaborators(group_ids)
remove_groups_from_collaborators(group_ids)
add_groups_to_collaborators(group_ids)
end
protected :update_group_collaborators
# Internal: Delete groups no longer being collaborated with.
#
# group_ids - An array of group IDs that will be used as collaborators.
#
# Returns nothing.
def remove_groups_from_collaborators(group_ids)
collaborators.where.not(group_id: group_ids.presence).delete_all
end
protected :remove_groups_from_collaborators
# Internal: Delete users no longer being collaborated with.
#
# users - An array of users that will be used as collaborators.
#
# Returns nothing.
def remove_users_from_collaborators(users)
collaborators.where.not(user_id: users.presence).delete_all
end
protected :remove_users_from_collaborators
# Internal: Update collaborators with the given groups.
#
# group_ids - An array of group IDs to add as collaborators.
#
# Returns nothing.
def add_groups_to_collaborators(group_ids)
return unless context.respond_to?(:groups)
if group_ids.length > 0
existing_groups = collaborators.where(:group_id => group_ids).select(:group_id)
context.groups.where(:id => group_ids).where.not(:id => existing_groups).each do |g|
collaborator = collaborators.build
collaborator.group_id = g
collaborator.save
end
end
end
private :add_groups_to_collaborators
# Internal: Update collaborators with the given groups.
#
# users - An array of users to add as collaborators.
#
# Returns nothing.
def add_users_to_collaborators(users)
if users.length > 0
existing_users = collaborators.where(:user_id => users).select(:user_id)
context.potential_collaborators.where(:id => users).where.not(:id => existing_users).each do |u|
collaborators.create(:user => u, :authorized_service_user_id => authorized_service_user_id_for(u))
end
end
end
private :add_users_to_collaborators
# Internal: Get the authorized_service_user_id for a user.
# May be overridden by other collaboration types.
protected def authorized_service_user_id_for(user)
user.gmail
end
end