forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaborations_controller.rb
More file actions
421 lines (387 loc) · 16.7 KB
/
Copy pathcollaborations_controller.rb
File metadata and controls
421 lines (387 loc) · 16.7 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#
# Copyright (C) 2011 - present 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/>.
#
# @API Collaborations
# API for accessing course and group collaboration information.
#
# @model Collaboration
# {
# "id": "Collaboration",
# "description": "",
# "properties": {
# "id": {
# "description": "The unique identifier for the collaboration",
# "example": 43,
# "type": "integer"
# },
# "collaboration_type": {
# "description": "A name for the type of collaboration",
# "example": "Microsoft Office",
# "type": "string"
# },
# "document_id": {
# "description": "The collaboration document identifier for the collaboration provider",
# "example": "oinwoenfe8w8ef_onweufe89fef",
# "type": "string"
# },
# "user_id": {
# "description": "The canvas id of the user who created the collaboration",
# "example": 92,
# "type": "integer"
# },
# "context_id": {
# "description": "The canvas id of the course or group to which the collaboration belongs",
# "example": 77,
# "type": "integer"
# },
# "context_type": {
# "description": "The canvas type of the course or group to which the collaboration belongs",
# "example": "Course",
# "type": "string"
# },
# "url": {
# "description": "The LTI launch url to view collaboration.",
# "type": "string"
# },
# "created_at": {
# "description": "The timestamp when the collaboration was created",
# "example": "2012-06-01T00:00:00-06:00",
# "type": "datetime"
# },
# "updated_at": {
# "description": "The timestamp when the collaboration was last modified",
# "example": "2012-06-01T00:00:00-06:00",
# "type": "datetime"
# },
# "description": {
# "type": "string"
# },
# "title": {
# "type": "string"
# },
# "type": {
# "description": "Another representation of the collaboration type",
# "example": "ExternalToolCollaboration",
# "type": "string"
# },
# "update_url": {
# "description": "The LTI launch url to edit the collaboration",
# "type": "string"
# },
# "user_name": {
# "description": "The name of the user who owns the collaboration",
# "example": "John Danger",
# "type": "string"
# }
# }
# }
#
# @model Collaborator
# {
# "id": "Collaborator",
# "description": "",
# "required": ["id"],
# "properties": {
# "id": {
# "description": "The unique user or group identifier for the collaborator.",
# "example": 12345,
# "type": "integer"
# },
# "type": {
# "description": "The type of collaborator (e.g. 'user' or 'group').",
# "example": "user",
# "type": "string",
# "allowableValues": {
# "values": [
# "user",
# "group"
# ]
# }
# },
# "name": {
# "description": "The name of the collaborator.",
# "example": "Don Draper",
# "type": "string"
# }
# }
# }
#
class CollaborationsController < ApplicationController
before_action :require_context, :except => [:members]
before_action :require_collaboration_and_context, :only => [:members]
before_action :require_collaborations_configured
before_action :reject_student_view_student
before_action { |c| c.active_tab = "collaborations" }
include Api::V1::Collaborator
include Api::V1::Collaboration
include Api::V1::User
def index
return unless authorized_action(@context, @current_user, :read) &&
tab_enabled?(@context.class::TAB_COLLABORATIONS)
add_crumb(t('#crumbs.collaborations', "Collaborations"), polymorphic_path([@context, :collaborations]))
@collaborations = @context.collaborations.active.select { |c| can_do(c, @current_user, :read) }
log_asset_access([ "collaborations", @context ], "collaborations", "other")
# this will set @user_has_google_drive
user_has_google_drive
@sunsetting_etherpad = EtherpadCollaboration.config.try(:[], :domain) == "etherpad.instructure.com/p"
@has_etherpad_collaborations = @collaborations.any? {|c| c.collaboration_type == 'EtherPad'}
@etherpad_only = Collaboration.collaboration_types.length == 1 &&
Collaboration.collaboration_types[0]['type'] == "etherpad"
@hide_create_ui = @sunsetting_etherpad && @etherpad_only
js_env :TITLE_MAX_LEN => Collaboration::TITLE_MAX_LENGTH,
:CAN_MANAGE_GROUPS => @context.grants_right?(@current_user, session, :manage_groups),
:collaboration_types => Collaboration.collaboration_types,
:POTENTIAL_COLLABORATORS_URL => polymorphic_url([:api_v1, @context, :potential_collaborators])
set_tutorial_js_env
end
# @API List collaborations
# List collaborations the current user has access to in the context of the course
# provided in the url. NOTE: this only returns ExternalToolCollaboration type
# collaborations.
#
# curl https://<canvas>/api/v1/courses/1/collaborations/
#
# @returns [Collaboration]
def api_index
return unless authorized_action(@context, @current_user, :read) &&
(tab_enabled?(@context.class::TAB_COLLABORATIONS) || tab_enabled?(@context.class::TAB_COLLABORATIONS_NEW))
url = @context.instance_of?(Course) ? api_v1_course_collaborations_index_url : api_v1_group_collaborations_index_url
collaborations_query = @context.collaborations.active.
eager_load(:user).
where(type: 'ExternalToolCollaboration')
unless @context.grants_right?(@current_user, session, :manage_content)
where_collaborators = Collaboration.arel_table[:user_id].eq(@current_user.id).
or(Collaborator.arel_table[:user_id].eq(@current_user.id))
if @context.instance_of?(Course)
users_course_groups = @context.groups.joins(:users).where(User.arel_table[:id].eq(@current_user.id)).pluck(:id)
where_collaborators = where_collaborators.or(Collaborator.arel_table[:group_id].in(users_course_groups))
end
collaborations_query = collaborations_query.
eager_load(:collaborators).
where(where_collaborators)
end
collaborations = Api.paginate(
collaborations_query,
self,
url
)
render :json => collaborations.map { |c| collaboration_json(c, @current_user, session) }
end
def show
@collaboration = @context.collaborations.find(params[:id])
if authorized_action(@collaboration, @current_user, :read)
@collaboration.touch
begin
if @collaboration.valid_user?(@current_user)
@collaboration.authorize_user(@current_user)
log_asset_access(@collaboration, "collaborations", "other", 'participate')
if @collaboration.is_a? ExternalToolCollaboration
url = external_tool_launch_url(@collaboration.url)
else
url = @collaboration.url
end
redirect_to url
elsif @collaboration.is_a?(GoogleDocsCollaboration)
redirect_to oauth_url(:service => :google_drive, :return_to => request.url)
else
flash[:error] = t 'errors.cannot_load_collaboration', "Cannot load collaboration"
redirect_to named_context_url(@context, :context_collaborations_url)
end
rescue GoogleDrive::ConnectionException => drive_exception
Canvas::Errors.capture(drive_exception)
flash[:error] = t 'errors.cannot_load_collaboration', "Cannot load collaboration"
redirect_to named_context_url(@context, :context_collaborations_url)
end
end
end
def lti_index
return unless authorized_action(@context, @current_user, :read) &&
tab_enabled?(@context.class::TAB_COLLABORATIONS)
@page_title = t('lti_collaborations', 'LTICollaborations')
@body_classes << 'full-width padless-content'
js_bundle :react_collaborations
css_bundle :react_collaborations
add_crumb(t('#crumbs.collaborations', "Collaborations"), polymorphic_path([@context, :lti_collaborations]))
if @context.instance_of? Group
parent_context = @context.context
js_env :PARENT_CONTEXT => {
:context_asset_string => parent_context.try(:asset_string)
}
end
set_tutorial_js_env
render :text => "".html_safe, :layout => true
end
def create
return unless authorized_action(@context.collaborations.build, @current_user, :create)
content_item = params['contentItems'] ? JSON.parse(params['contentItems']).first : nil
if content_item
@collaboration = collaboration_from_content_item(content_item)
users, group_ids = content_item_visibility(content_item)
else
users = User.where(:id => Array(params[:user])).to_a
group_ids = Array(params[:group])
collaboration_params = params.require(:collaboration).permit(:title, :description, :url)
collaboration_params[:user] = @current_user
@collaboration = Collaboration.typed_collaboration_instance(params[:collaboration].delete(:collaboration_type))
collaboration_params.delete(:url) unless @collaboration.is_a?(ExternalToolCollaboration)
@collaboration.attributes = collaboration_params
end
@collaboration.context = @context
respond_to do |format|
if @collaboration.save
Lti::ContentItemUtil.new(content_item).success_callback if content_item
# After saved, update the members
@collaboration.update_members(users, group_ids)
format.html { redirect_to @collaboration.url }
format.json { render :json => @collaboration.as_json(:methods => [:collaborator_ids], :permissions => {:user => @current_user, :session => session}) }
else
Lti::ContentItemUtil.new(content_item).failure_callback if content_item
flash[:error] = t 'errors.create_failed', "Collaboration creation failed"
format.html { redirect_to named_context_url(@context, :context_collaborations_url) }
format.json { render :json => @collaboration.errors, :status => :bad_request }
end
end
end
def update
@collaboration = @context.collaborations.find(params[:id])
return unless authorized_action(@collaboration, @current_user, :update)
content_item = params['contentItems'] ? JSON.parse(params['contentItems']).first : nil
begin
if content_item
@collaboration = collaboration_from_content_item(content_item, @collaboration)
users, group_ids = content_item_visibility(content_item)
else
users = User.where(:id => Array(params[:user])).to_a
group_ids = Array(params[:group])
@collaboration.attributes = params.require(:collaboration).permit(:title, :description, :url)
end
@collaboration.update_members(users, group_ids)
respond_to do |format|
if @collaboration.save
Lti::ContentItemUtil.new(content_item).success_callback if content_item
format.html { redirect_to named_context_url(@context, :context_collaborations_url) }
format.json { render :json => @collaboration.as_json(
:methods => [:collaborator_ids],
:permissions => {
:user => @current_user,
:session => session
}
)}
else
Lti::ContentItemUtil.new(content_item).failure_callback if content_item
flash[:error] = t 'errors.update_failed', "Collaboration update failed"
format.html { redirect_to named_context_url(@context, :context_collaborations_url) }
format.json { render :json => @collaboration.errors, :status => :bad_request }
end
end
rescue GoogleDrive::ConnectionException => error
Rails.logger.warn error
flash[:error] = t 'errors.update_failed', "Collaboration update failed" # generic failure message
if error.message.include?('File not found')
flash[:error] = t 'google_drive.file_not_found', "Collaboration file not found"
end
raise error unless error.message.include?('File not found')
redirect_to named_context_url(@context, :context_collaborations_url)
end
end
def destroy
@collaboration = @context.collaborations.find(params[:id])
if authorized_action(@collaboration, @current_user, :delete)
@collaboration.delete_document if value_to_boolean(params[:delete_doc])
@collaboration.destroy
respond_to do |format|
format.html { redirect_to named_context_url(@context, :collaborations_url) }
format.json { render :json => @collaboration }
end
end
end
# @API List members of a collaboration.
#
# List the collaborators of a given collaboration
#
# @argument include[] [String, "collaborator_lti_id"|"avatar_image_url"]
# - "collaborator_lti_id": Optional information to include with each member.
# Represents an identifier to be used for the member in an LTI context.
# - "avatar_image_url": Optional information to include with each member.
# The url for the avatar of a collaborator with type 'user'.
#
# @example_request
#
# curl https://<canvas>/api/v1/courses/1/collaborations/1/members
#
# @returns [Collaborator]
def members
return unless authorized_action(@collaboration, @current_user, :read)
options = {:include => params[:include]}
collaborators = @collaboration.collaborators.preload(:group, :user)
collaborators = Api.paginate(collaborators,
self,
api_v1_collaboration_members_url)
render :json => collaborators.map { |c| collaborator_json(c, @current_user, session, options) }
end
# @API List potential members
#
# List the users who can potentially be added to a collaboration in the given context.
#
# For courses, this consists of all enrolled users. For groups, it is comprised of the
# group members plus the admins of the course containing the group.
#
# @returns [User]
def potential_collaborators
return unless authorized_action(@context, @current_user, :read_roster)
scope = @context.potential_collaborators.order(:sortable_name)
users = Api.paginate(scope, self, polymorphic_url([:api_v1, @context, :potential_collaborators]))
render :json => users.map { |u| user_json(u, @current_user, session) }
end
private
def require_collaboration_and_context
@collaboration = if @context.present?
@context.collaborations.find(params[:id])
else
Collaboration.find(params[:id])
end
@context = @collaboration.context
end
def require_collaborations_configured
unless Collaboration.any_collaborations_configured?(@context) || @domain_root_account.feature_enabled?(:new_collaborations)
flash[:error] = t 'errors.not_enabled', "Collaborations have not been enabled for this Canvas site"
redirect_to named_context_url(@context, :context_url)
return false
end
end
def collaboration_from_content_item(content_item, collaboration = ExternalToolCollaboration.new)
collaboration.attributes = {
title: content_item['title'],
description: content_item['text'],
user: @current_user
}
collaboration.data = content_item
collaboration.url = content_item['url']
collaboration
end
def external_tool_launch_url(url)
polymorphic_url([:retrieve, @context, :external_tools], url: url, display: 'borderless')
end
def content_item_visibility(content_item)
visibility = content_item['ext_canvas_visibility']
lti_user_ids = visibility && visibility['users'] || []
lti_group_ids = visibility && visibility['groups'] || []
users = User.where(lti_context_id: lti_user_ids).to_a
group_ids = Group.where(lti_context_id: lti_group_ids).map(&:id)
[users, group_ids]
end
end