From e593c34fddf8231de96b5c81647225540fc86f99 Mon Sep 17 00:00:00 2001 From: Pranav Kant Date: Thu, 13 Oct 2016 19:08:27 +0530 Subject: [PATCH 1/3] New test server option for specific groups --- controller/documentcontroller.php | 38 ++++++++++++++-- controller/settingscontroller.php | 18 ++++++-- js/admin.js | 76 ++++++++++++++++++++++++++++++- templates/admin.php | 19 ++++++-- 4 files changed, 140 insertions(+), 11 deletions(-) diff --git a/controller/documentcontroller.php b/controller/documentcontroller.php index 20045d28..61b0f20e 100644 --- a/controller/documentcontroller.php +++ b/controller/documentcontroller.php @@ -121,12 +121,42 @@ class DocumentController extends Controller { return $response; } + /** + * Return the original wopi url or test wopi url + * This depends on whether current user is the member of one of the groups + * mentioned in settings in which case different wopi url (aka + * test_wopi_url) is used + */ + private function getWopiUrl() { + $wopiurl = $this->appConfig->getAppValue('wopi_url'); + + $user = \OC::$server->getUserSession()->getUser()->getUID(); + $testgroups = array_filter(explode('|', $this->appConfig->getAppValue('test_server_groups'))); + \OC::$server->getLogger()->debug('Testgroups are {testgroups}', [ + 'app' => $this->appName, + 'testgroups' => $testgroups + ]); + foreach ($testgroups as $testgroup) { + $test = \OC::$server->getGroupManager()->get($testgroup); + if (sizeof($test->searchUsers($user)) > 0) { + \OC::$server->getLogger()->debug('User {user} found in {group}', [ + 'app' => $this->appName, + 'user' => $user, + 'group' => $testgroup + ]); + return $this->appConfig->getAppValue('test_wopi_url'); + } + } + + return $wopiurl; + } + /** Return the content of discovery.xml - either from cache, or download it. */ private function getDiscovery(){ \OC::$server->getLogger()->debug('getDiscovery(): Getting discovery.xml from the cache.'); - $wopiRemote = $this->appConfig->getAppValue('wopi_url'); + $wopiRemote = $this->getWopiUrl(); // Provides access to information about the capabilities of a WOPI client // and the mechanisms for invoking those abilities through URIs. @@ -188,7 +218,7 @@ class DocumentController extends Controller { if ($discovery_parsed === false) { $this->cache->remove('discovery.xml'); - $wopiRemote = $this->appConfig->getAppValue('wopi_url'); + $wopiRemote = $this->getWopiUrl(); return array( 'status' => 'error', @@ -246,7 +276,7 @@ class DocumentController extends Controller { * @NoCSRFRequired */ public function index(){ - $wopiRemote = $this->appConfig->getAppValue('wopi_url'); + $wopiRemote = $this->getWopiUrl(); if (($parts = parse_url($wopiRemote)) && isset($parts['scheme']) && isset($parts['host'])) { $webSocketProtocol = "ws://"; if ($parts['scheme'] == "https") { @@ -350,7 +380,7 @@ class DocumentController extends Controller { if ($discovery_parsed === false) { $this->cache->remove('discovery.xml'); - $wopiRemote = $this->appConfig->getAppValue('wopi_url'); + $wopiRemote = $this->getWopiUrl(); return array( 'status' => 'error', diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php index 945b021b..bebc7720 100644 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -49,7 +49,9 @@ class SettingsController extends Controller{ public function getSettings() { return array( 'doc_format' => $this->appConfig->getAppValue('doc_format'), - 'wopi_url' => $this->appConfig->getAppValue('wopi_url') + 'wopi_url' => $this->appConfig->getAppValue('wopi_url'), + 'test_wopi_url' => $this->appConfig->getAppValue('test_wopi_url'), + 'test_server_groups' => $this->appConfig->getAppValue('test_server_groups') ); } @@ -74,13 +76,15 @@ class SettingsController extends Controller{ [ 'wopi_url' => $this->appConfig->getAppValue('wopi_url'), 'edit_groups' => $this->appConfig->getAppValue('edit_groups'), - 'doc_format' => $this->appConfig->getAppValue('doc_format') + 'doc_format' => $this->appConfig->getAppValue('doc_format'), + 'test_wopi_url' => $this->appConfig->getAppValue('test_wopi_url'), + 'test_server_groups' => $this->appConfig->getAppValue('test_server_groups') ], 'blank' ); } - public function setSettings($wopi_url, $edit_groups, $doc_format){ + public function setSettings($wopi_url, $edit_groups, $doc_format, $test_wopi_url, $test_server_groups){ if (!is_null($wopi_url)){ $this->appConfig->setAppValue('wopi_url', $wopi_url); } @@ -93,6 +97,14 @@ class SettingsController extends Controller{ $this->appConfig->setAppValue('doc_format', $doc_format); } + if (!is_null($test_wopi_url)){ + $this->appConfig->setAppValue('test_wopi_url', $test_wopi_url); + } + + if (!is_null($test_server_groups)){ + $this->appConfig->setAppValue('test_server_groups', $test_server_groups); + } + $richMemCache = \OC::$server->getMemCacheFactory()->create('richdocuments'); $richMemCache->clear('discovery.xml'); diff --git a/js/admin.js b/js/admin.js index 223b1173..62a48c32 100644 --- a/js/admin.js +++ b/js/admin.js @@ -33,6 +33,25 @@ var documentsSettings = { ); }, + saveTestWopi: function(groups, server) { + var data = { + 'test_wopi_url': server, + 'test_server_groups': groups + }; + + OC.msg.startAction('#test-documents-admin-msg', t('richdocuments', 'Saving...')); + $.post( + OC.filePath('richdocuments', 'ajax', 'admin.php'), + data, + documentsSettings.afterSaveTestWopi + ); + }, + + afterSaveTestWopi: function(response) { + $('#test_wopi_apply').attr('disabled', false); + OC.msg.finishedAction('#test-documents-admin-msg', response); + }, + afterSave : function(response){ $('#wopi_apply').attr('disabled', false); OC.msg.finishedAction('#documents-admin-msg', response); @@ -48,9 +67,64 @@ var documentsSettings = { } }, + initTestWopiServer: function() { + var groups = $(document).find('#test_server_group_select').val(); + var testserver = $(document).find('#test_wopi_url').val(); + + if (groups === '' || testserver === '') { + $(document).find('label[for="test_server_group_select"]').hide(); + $(document).find('label[for="test_wopi_url"]').hide(); + $(document).find('#test_wopi_url').hide(); + $(document).find('#test_wopi_apply').hide(); + $('.test-server-enable').attr('checked', null); + } else { + OC.Settings.setupGroupsSelect($('#test_server_group_select')); + $('.test-server-enable').attr('checked', 'checked'); + } + }, + initialize: function() { - $('#wopi_apply').on('click', documentsSettings.save); documentsSettings.initEditGroups(); + documentsSettings.initTestWopiServer(); + + $('#wopi_apply').on('click', documentsSettings.save); + + $(document).on('change', '.test-server-enable', function() { + var page = $(this).parent(); + var $select = page.find('#test_server_group_select'); + $select.val(''); + + if (this.checked) { + page.find('label[for="test_server_group_select"]').show(); + page.find('label[for="test_wopi_url"]').show(); + page.find('#test_wopi_apply').show(); + page.find('#test_wopi_url').show(); + + OC.Settings.setupGroupsSelect($select, { + placeholder: t('core', 'None') + }); + } else { + page.find('label[for="test_server_group_select"]').hide(); + page.find('label[for="test_wopi_url"]').hide(); + page.find('#test_wopi_apply').hide(); + page.find('#test_wopi_url').val(''); + page.find('#test_wopi_url').hide(); + $select.select2('destroy'); + + documentsSettings.saveTestWopi('', ''); + } + }); + + $(document).on('click', '#test_wopi_apply', function() { + var groups = $(this).parent().find('#test_server_group_select').val(); + var testserver = $(this).parent().find('#test_wopi_url').val(); + + if (groups !== '' && testserver !== '') { + documentsSettings.saveTestWopi(groups, testserver); + } else { + OC.msg.finishedError('#test-documents-admin-msg', 'Both fields required'); + } + }); $(document).on('change', '.doc-format-ooxml', function() { var ooxml = this.checked; diff --git a/templates/admin.php b/templates/admin.php index 412c47d6..30b381b1 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -9,10 +9,23 @@ script('richdocuments', 'admin');

- + + + +
+ + + + + +
+ + +
+
- data-appid="richdocuments" /> + /> - + \ No newline at end of file From de62725ed20e2895bb97147306ba59a822858e23 Mon Sep 17 00:00:00 2001 From: Pranav Kant Date: Thu, 13 Oct 2016 20:16:02 +0530 Subject: [PATCH 2/3] Some beautification of admin settings --- js/admin.js | 16 ++-------------- templates/admin.php | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/js/admin.js b/js/admin.js index 62a48c32..7a781f30 100644 --- a/js/admin.js +++ b/js/admin.js @@ -72,10 +72,6 @@ var documentsSettings = { var testserver = $(document).find('#test_wopi_url').val(); if (groups === '' || testserver === '') { - $(document).find('label[for="test_server_group_select"]').hide(); - $(document).find('label[for="test_wopi_url"]').hide(); - $(document).find('#test_wopi_url').hide(); - $(document).find('#test_wopi_apply').hide(); $('.test-server-enable').attr('checked', null); } else { OC.Settings.setupGroupsSelect($('#test_server_group_select')); @@ -94,22 +90,14 @@ var documentsSettings = { var $select = page.find('#test_server_group_select'); $select.val(''); + page.find('#test-server-section').toggleClass('hidden', !this.checked); if (this.checked) { - page.find('label[for="test_server_group_select"]').show(); - page.find('label[for="test_wopi_url"]').show(); - page.find('#test_wopi_apply').show(); - page.find('#test_wopi_url').show(); - OC.Settings.setupGroupsSelect($select, { placeholder: t('core', 'None') }); } else { - page.find('label[for="test_server_group_select"]').hide(); - page.find('label[for="test_wopi_url"]').hide(); - page.find('#test_wopi_apply').hide(); - page.find('#test_wopi_url').val(''); - page.find('#test_wopi_url').hide(); $select.select2('destroy'); + page.find('#test_wopi_url').val(''); documentsSettings.saveTestWopi('', ''); } diff --git a/templates/admin.php b/templates/admin.php index 30b381b1..efdc57b0 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -5,23 +5,24 @@ script('richdocuments', 'admin');

t('Collabora Online')) ?>

-
t('URL (and port) of the Collabora Online server that provides the editing functionality as a WOPI client.')) ?> -
+
t('URL (and port) of the Collabora Online server that provides the editing functionality as a WOPI client.')) ?> +

- -
- - +
+

+ +
- - -
- - -
+ +
+ t('URL (and port) of the Collabora Online test server.')) ?>
+ + + +

From 1ad1f667ee1c9dbc3470299bb5b441ffae1d6bc4 Mon Sep 17 00:00:00 2001 From: Pranav Kant Date: Thu, 13 Oct 2016 20:50:48 +0530 Subject: [PATCH 3/3] Simplify edit group checking algorithm Rather than making a server call, which seems to be not working always anyways, use a foolproof method for this. --- controller/documentcontroller.php | 12 +++++++++--- js/documents.js | 28 +++++++++------------------- templates/documents.php | 1 + 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/controller/documentcontroller.php b/controller/documentcontroller.php index 61b0f20e..ec43bdc2 100644 --- a/controller/documentcontroller.php +++ b/controller/documentcontroller.php @@ -292,16 +292,22 @@ class DocumentController extends Controller { return $this->responseError($this->l10n->t('Collabora Online: Invalid URL "%s".', array($wopiRemote)), $this->l10n->t('Please ask your administrator to check the Collabora Online server setting.')); } + $user = \OC::$server->getUserSession()->getUser(); + $usergroups = array_filter(\OC::$server->getGroupManager()->getUserGroupIds($user)); + $usergroups = join('|', $usergroups); + \OC::$server->getLogger()->debug('User is in groups: {groups}', [ 'app' => $this->appName, 'groups' => $usergroups ]); + \OC::$server->getNavigationManager()->setActiveEntry( 'richdocuments_index' ); $maxUploadFilesize = \OCP\Util::maxUploadFilesize("/"); $response = new TemplateResponse('richdocuments', 'documents', [ - 'enable_previews' => $this->settings->getSystemValue('enable_previews', true), + 'enable_previews' => $this->settings->getSystemValue('enable_previews', true), 'uploadMaxFilesize' => $maxUploadFilesize, 'uploadMaxHumanFilesize' => \OCP\Util::humanFileSize($maxUploadFilesize), - 'allowShareWithLink' => $this->settings->getAppValue('core', 'shareapi_allow_links', 'yes'), + 'allowShareWithLink' => $this->settings->getAppValue('core', 'shareapi_allow_links', 'yes'), 'wopi_url' => $webSocket, 'edit_groups' => $this->appConfig->getAppValue('edit_groups'), - 'doc_format' => $this->appConfig->getAppValue('doc_format') + 'doc_format' => $this->appConfig->getAppValue('doc_format'), + 'usergroups' => $usergroups ]); $policy = new ContentSecurityPolicy(); diff --git a/js/documents.js b/js/documents.js index 809c2b0b..a8e81839 100644 --- a/js/documents.js +++ b/js/documents.js @@ -19,27 +19,17 @@ $.widget('oc.documentGrid', { that._render(); if (!documentsMain.isGuest) { - $.ajax({ - url: OC.generateUrl('/settings/users/users'), - type: 'get', - data: { limit: 1, pattern: OC.currentUser }, - async: false, - success: function(result) { - var editGroups = $('#edit_groups').val(); - documentsMain.canEdit = (editGroups === ''); - if (!documentsMain.canEdit && result.length >= 1) { - for (var idx in result[0].groups) { - if (editGroups.indexOf(result[0].groups[idx]) !== -1) { - documentsMain.canEdit = true; - break; - } - } + var editGroups = $('#edit_groups').val().split('|'); + var usergroups = $('#usergroups').val().split('|'); + documentsMain.canEdit = (editGroups.length === 0); + if (!documentsMain.canEdit && usergroups.length >= 1) { + for (var idx in usergroups) { + if (editGroups.indexOf(usergroups[idx]) !== -1) { + documentsMain.canEdit = true; + break; } - }, - error: function() { - console.log('Error fetching information about current user.'); } - }); + } } documentsMain.renderComplete = true; diff --git a/templates/documents.php b/templates/documents.php index 898e2219..3840eafd 100644 --- a/templates/documents.php +++ b/templates/documents.php @@ -53,3 +53,4 @@ script('files', 'jquery.fileupload'); +