diff options
author | blizzz <blizzz@arthur-schiwon.de> | 2020-09-10 00:45:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-10 00:45:54 +0300 |
commit | 2564ea51ac3b4dc328f1d0854cdfcff378779fd1 (patch) | |
tree | 0b529ff7460a6bb06dd7101105291b36451ac1cf | |
parent | d8920a1ed756a303c1b0a24aa9ec3503b1993a02 (diff) | |
parent | 6a9cc6d71dd1905dd010e444ae2f26d6515bb0d6 (diff) |
Merge pull request #114 from nextcloud/fix/noid/20-ibootstrapv1.7.0
implement ibootstrap
-rw-r--r-- | .drone.yml | 14 | ||||
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | appinfo/app.php | 33 | ||||
-rw-r--r-- | composer.json | 5 | ||||
-rw-r--r-- | js/admin_settings.js | 25 | ||||
-rw-r--r-- | js/impersonate.js | 90 | ||||
-rw-r--r-- | js/impersonate_logout.js | 14 | ||||
-rw-r--r-- | lib/AppInfo/Application.php | 55 | ||||
-rw-r--r-- | lib/Listener/BeforeTemplateRenderedListener.php | 67 |
9 files changed, 208 insertions, 96 deletions
@@ -11,9 +11,9 @@ steps: commands: - wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh - bash ./before_install.sh $APP_NAME $CORE_BRANCH $DATABASEHOST - - cd ../server + - cd ../server/apps/$APP_NAME - composer install - - ./lib/composer/bin/parallel-lint apps/$APP_NAME/ + - ./vendor/bin/parallel-lint --exclude ./vendor/ . - name: syntax-php7.4 image: nextcloudci/php7.4:2 environment: @@ -23,9 +23,9 @@ steps: commands: - wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh - bash ./before_install.sh $APP_NAME $CORE_BRANCH $DATABASEHOST - - cd ../server + - cd ../server/apps/$APP_NAME - composer install - - ./lib/composer/bin/parallel-lint apps/$APP_NAME/ + - ./vendor/bin/parallel-lint --exclude ./vendor/ . - name: app-code-check image: nextcloudci/php7.3:php7.3-5 environment: @@ -47,6 +47,8 @@ trigger: - pull_request - push +type: docker + --- kind: pipeline name: unit-sqlite-php7.2 @@ -77,6 +79,8 @@ trigger: - pull_request - push +type: docker + --- kind: pipeline name: unit-sqlite-php7.3 @@ -107,6 +111,8 @@ trigger: - pull_request - push +type: docker + --- kind: pipeline name: unit-sqlite-php7.4 @@ -1 +1,2 @@ build +/vendor/ diff --git a/appinfo/app.php b/appinfo/app.php deleted file mode 100644 index 6b5bce0..0000000 --- a/appinfo/app.php +++ /dev/null @@ -1,33 +0,0 @@ -<?php -/** - * ownCloud - impersonate - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Jörn Friedrich Dreyer <jfd@owncloud.com> - * @copyright Jörn Friedrich Dreyer 2015 - */ - -if(\OC::$server->getSession()->get('oldUserId') !== null) { - \OCP\Util::addScript('impersonate','impersonate_logout'); -} -// --- register js for user management------------------------------------------ -$eventDispatcher = \OC::$server->getEventDispatcher(); -$eventDispatcher->addListener( - 'OC\Settings\Users::loadAdditionalScripts', - function() { - $authorized = json_decode(\OC::$server->getConfig()->getAppValue('impersonate', 'authorized', '["admin"]')); - - $loadScript = true; - if(!empty($authorized)) { - $userGroups = \OC::$server->getGroupManager()->getUserGroupIds(\OC::$server->getUserSession()->getUser()); - if (!array_intersect($userGroups, $authorized)) { - $loadScript = false; - } - } - if($loadScript){ - \OCP\Util::addScript('impersonate', 'impersonate'); - } - } -); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8d00bd3 --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require-dev": { + "php-parallel-lint/php-parallel-lint": "^1.2" + } +} diff --git a/js/admin_settings.js b/js/admin_settings.js index f95571e..f014f77 100644 --- a/js/admin_settings.js +++ b/js/admin_settings.js @@ -18,13 +18,20 @@ * */ -$(document).ready(function() { - var $authorized = $('#impersonate .authorized'); +/* global OC, OCA */ +(function(OC, OCA) { + OCA.Impersonate = { + initSettings: function() { + var $authorized = $('#impersonate .authorized') - OC.Settings.setupGroupsSelect($authorized); - $authorized.change(function(event) { - var groups = event.val || ['admin']; - groups = JSON.stringify(groups); - OCP.AppConfig.setValue('impersonate', 'authorized', groups); - }); -}); + OC.Settings.setupGroupsSelect($authorized) + $authorized.change(function(event) { + var groups = event.val || ['admin'] + groups = JSON.stringify(groups) + OCP.AppConfig.setValue('impersonate', 'authorized', groups) + }) + } + } + + document.addEventListener('DOMContentLoaded', OCA.Impersonate.initSettings) +})(OC, OCA) diff --git a/js/impersonate.js b/js/impersonate.js index 8e13817..7ea6459 100644 --- a/js/impersonate.js +++ b/js/impersonate.js @@ -1,48 +1,52 @@ -/* global OC, $ */ -(function(OC, $){ - - $(document).ready(function() { - function impersonate(userId) { - $.post( - OC.generateUrl('apps/impersonate/user'), - { userId: userId } - ).done(function() { - window.location = OC.generateUrl('apps/files'); - }).fail(function( result ) { - OC.dialogs.alert(result.responseJSON.message, t('impersonate', 'Could not impersonate user')); - }); - } - - function impersonateDialog(event) { - let userId = event.target.closest('.row').dataset.id; - OC.dialogs.confirm( - t('impersonate', 'Are you sure you want to impersonate "{userId}"?', {userId: userId}), - t('impersonate', 'Impersonate user' ), - function(result) { - if (result) { - impersonate(userId); - } - }, - true - ); +/* global OC */ +(function(OC) { + function impersonate(userId) { + var xhr = new XMLHttpRequest() + xhr.onreadystatechange = function(data) { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) { + window.location = OC.generateUrl('/') + } else { + OC.dialogs.alert(JSON.parse(xhr.response).message, t('impersonate', 'Could not impersonate user'), undefined, undefined) + } + } } + xhr.open('POST', OC.generateUrl('apps/impersonate/user')) + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') + xhr.send('userId=' + encodeURIComponent(userId) + '&requesttoken=' + encodeURIComponent(OC.requestToken)) + } - let registerFunction = function (delay) { - if(OCA.Settings === undefined) { - delay = delay * 2; - if(delay === 0) { - delay = 15; + function impersonateDialog(event) { + var userId = event.target.closest('.row').dataset.id + OC.dialogs.confirm( + t('impersonate', 'Are you sure you want to impersonate "{userId}"?', { userId: userId }), + t('impersonate', 'Impersonate user'), + function(result) { + if (result) { + impersonate(userId) } - if(delay > 500) { - console.warn("Could not register impersonate script"); - return; - } - setTimeout(function() {registerFunction(delay)}, delay); - } else { - OCA.Settings.UserList.registerAction('icon-user', t('impersonate', 'Impersonate'), impersonateDialog) + }, + true + ) + } + + var registerFunction = function(delay) { + delay = delay || 0 + if (OCA.Settings === undefined) { + delay = delay * 2 + if (delay === 0) { + delay = 15 + } + if (delay > 500) { + console.error('Could not register impersonate script') + return } - }; - registerFunction(0); - }); + setTimeout(function() { registerFunction(delay) }, delay) + } else { + OCA.Settings.UserList.registerAction('icon-user', t('impersonate', 'Impersonate'), impersonateDialog) + } + } + + document.addEventListener('DOMContentLoaded', registerFunction) -})(OC, $); +})(OC) diff --git a/js/impersonate_logout.js b/js/impersonate_logout.js index 4e41d7c..281b8f2 100644 --- a/js/impersonate_logout.js +++ b/js/impersonate_logout.js @@ -1,4 +1,4 @@ -$(document).ready(function () { +document.addEventListener('DOMContentLoaded', function() { $("#logout").attr("href","#"); @@ -18,7 +18,7 @@ $(document).ready(function () { var promisObj = $.post( OC.generateUrl('apps/impersonate/logout'), {userId: userId} - ).promise(); + ).promise() promisObj.done(function () { OC.redirect(OC.generateUrl('settings/users')) @@ -26,8 +26,8 @@ $(document).ready(function () { } $('#settings ul li:last').on('click', function (event) { - event.preventDefault(); - var userId = $("#expandDisplayName").text(); - logoutHandler(userId); - }); -}); + event.preventDefault() + var userId = $("#expandDisplayName").text() + logoutHandler(userId) + }) +}) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php new file mode 100644 index 0000000..0c87686 --- /dev/null +++ b/lib/AppInfo/Application.php @@ -0,0 +1,55 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program 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, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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/>. + * + */ + +namespace OCA\Impersonate\AppInfo; + +use OCA\Impersonate\Listener\BeforeTemplateRenderedListener; +use OCA\Settings\Events\BeforeTemplateRenderedEvent; +use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\ISession; +use OCP\Util; + +class Application extends App implements IBootstrap { + public const APP_ID = 'impersonate'; + + public function __construct() { + parent::__construct(self::APP_ID); + } + + public function register(IRegistrationContext $context): void { + $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); + } + + public function boot(IBootContext $context): void { + $session = $context->getServerContainer()->get(ISession::class); + + if($session->get('oldUserId') !== null) { + Util::addScript(self::APP_ID,'impersonate_logout'); + } + } +} diff --git a/lib/Listener/BeforeTemplateRenderedListener.php b/lib/Listener/BeforeTemplateRenderedListener.php new file mode 100644 index 0000000..54fdc4d --- /dev/null +++ b/lib/Listener/BeforeTemplateRenderedListener.php @@ -0,0 +1,67 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program 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, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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/>. + * + */ + +namespace OCA\Impersonate\Listener; + +use OCA\Impersonate\AppInfo\Application; +use OCA\Settings\Events\BeforeTemplateRenderedEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IUserSession; +use OCP\Util; + +class BeforeTemplateRenderedListener implements IEventListener { + + /** @var IConfig */ + private $config; + /** @var IGroupManager */ + private $groupManager; + /** @var IUserSession */ + private $userSession; + + public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) { + $this->config = $config; + $this->groupManager = $groupManager; + $this->userSession = $userSession; + } + + public function handle(Event $event): void { + if (!$event instanceof BeforeTemplateRenderedEvent) { + return; + } + + $authorized = json_decode($this->config->getAppValue(Application::APP_ID, 'authorized', '["admin"]')); + + if (!empty($authorized)) { + $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); + if (!array_intersect($userGroups, $authorized)) { + return; + } + } + Util::addScript(Application::APP_ID, 'impersonate'); + } +} |