2011-02-01 09:57:29 +08:00
|
|
|
#
|
2017-04-28 03:45:05 +08:00
|
|
|
# Copyright (C) 2011 - present Instructure, Inc.
|
2011-02-01 09:57:29 +08:00
|
|
|
#
|
|
|
|
# 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 GoogleDocsCollaboration < Collaboration
|
2015-02-10 06:13:53 +08:00
|
|
|
GOOGLE_DRIVE_SERVICE = "drive.google.com"
|
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def style_class
|
|
|
|
'google_docs'
|
|
|
|
end
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def service_name
|
|
|
|
"Google Docs"
|
|
|
|
end
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def delete_document
|
2015-02-10 06:13:53 +08:00
|
|
|
if self.document_id && self.user
|
|
|
|
# google docs expected an object
|
|
|
|
# drive just wants an id
|
2015-12-17 05:53:11 +08:00
|
|
|
doc = self.document_id
|
2015-02-10 06:13:53 +08:00
|
|
|
google_adapter_for_user.delete_doc(doc)
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
end
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def initialize_document
|
|
|
|
if !self.document_id && self.user
|
2014-04-01 22:21:48 +08:00
|
|
|
name = self.title
|
|
|
|
name = nil if name && name.empty?
|
|
|
|
name ||= I18n.t('lib.google_docs.default_document_name', "Instructure Doc")
|
|
|
|
|
2015-02-10 06:13:53 +08:00
|
|
|
result = google_adapter_for_user.create_doc(name)
|
|
|
|
if result
|
2015-12-17 05:53:11 +08:00
|
|
|
self.document_id = result.data.id
|
|
|
|
self.data = result.data.to_json
|
|
|
|
|
|
|
|
self.url = result.data.alternateLink
|
2015-02-10 06:13:53 +08:00
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
end
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def user_can_access_document_type?(user)
|
2015-03-14 04:02:29 +08:00
|
|
|
return !!google_adapter_user_service(user) if self.user && user
|
2015-02-10 06:13:53 +08:00
|
|
|
false
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def authorize_user(user)
|
|
|
|
return unless self.document_id
|
2015-02-10 06:13:53 +08:00
|
|
|
service_user_id = google_adapter_user_service(user).service_user_id rescue nil
|
2014-09-12 03:44:34 +08:00
|
|
|
collaborator = self.collaborators.where(user_id: user).first
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2015-03-19 06:10:02 +08:00
|
|
|
if collaborator
|
|
|
|
if collaborator.authorized_service_user_id != service_user_id
|
|
|
|
google_adapter_for_user.acl_remove(self.document_id, [collaborator.authorized_service_user_id]) if collaborator.authorized_service_user_id
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2015-12-17 05:53:11 +08:00
|
|
|
user_param = service_user_id
|
2015-03-19 06:10:02 +08:00
|
|
|
google_adapter_for_user.acl_add(self.document_id, [user_param])
|
|
|
|
collaborator.update_attributes(:authorized_service_user_id => service_user_id)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# no collaboration for this user, lets create it
|
|
|
|
add_users_to_collaborators([user])
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
end
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def remove_users_from_document(users_to_remove)
|
2015-12-17 05:53:11 +08:00
|
|
|
users_to_remove = users_to_remove.map do |user|
|
|
|
|
user_service = google_user_service(user, GOOGLE_DRIVE_SERVICE) and user_service.service_user_id
|
2015-02-10 06:13:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
google_adapter_for_user.acl_remove(self.document_id, users_to_remove) if self.document_id
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2013-11-19 03:41:10 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def add_users_to_document(new_users)
|
2013-11-19 03:41:10 +08:00
|
|
|
if document_id
|
2015-02-10 06:13:53 +08:00
|
|
|
domain = if context.root_account.feature_enabled?(:google_docs_domain_restriction)
|
|
|
|
context.root_account.settings[:google_docs_domain]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2015-12-17 05:53:11 +08:00
|
|
|
user_ids = new_users.map do |user|
|
|
|
|
google_user_service(user, GOOGLE_DRIVE_SERVICE).service_user_id rescue nil
|
|
|
|
end.compact
|
2015-02-10 06:13:53 +08:00
|
|
|
|
|
|
|
google_adapter_for_user.acl_add(self.document_id, user_ids, domain)
|
2013-11-19 03:41:10 +08:00
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2013-11-19 03:41:10 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def parse_data
|
2015-12-17 05:53:11 +08:00
|
|
|
@entry_data ||= JSON.parse(self.data)
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def self.config
|
2015-12-17 05:53:11 +08:00
|
|
|
GoogleDrive::Connection.config
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2014-04-08 00:11:01 +08:00
|
|
|
|
2016-12-23 05:38:45 +08:00
|
|
|
def authorized_service_user_id_for(user)
|
|
|
|
service = google_adapter_user_service(user)
|
|
|
|
service ? service.service_user_id : nil
|
2015-03-14 04:02:29 +08:00
|
|
|
end
|
|
|
|
|
2014-04-08 00:11:01 +08:00
|
|
|
private
|
2015-02-10 06:13:53 +08:00
|
|
|
|
2015-12-17 05:53:11 +08:00
|
|
|
def google_user_service(user, service_domain=GOOGLE_DRIVE_SERVICE)
|
2015-02-10 06:13:53 +08:00
|
|
|
google_services = user.user_services.where(service_domain: service_domain).to_a
|
|
|
|
google_services.find{|s| s.service_user_id}
|
|
|
|
end
|
|
|
|
|
|
|
|
def google_drive_for_user
|
|
|
|
refresh_token, access_token = Rails.cache.fetch(['google_drive_tokens', self.user].cache_key) do
|
|
|
|
service = self.user.user_services.where(service: "google_drive").first
|
|
|
|
service && [service.token, service.secret]
|
|
|
|
end
|
2015-12-17 05:53:11 +08:00
|
|
|
raise GoogleDrive::NoTokenError unless refresh_token && access_token
|
|
|
|
GoogleDrive::Connection.new(refresh_token, access_token, ApplicationController.google_drive_timeout)
|
2015-02-10 06:13:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def google_adapter_for_user
|
2015-12-17 05:53:11 +08:00
|
|
|
google_drive_for_user
|
2015-02-10 06:13:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def google_adapter_user_service(user)
|
2015-12-17 05:53:11 +08:00
|
|
|
google_user_service(user)
|
2015-02-10 06:13:53 +08:00
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|