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