Skip to content

Commit 2564ea5

Browse files
authored
Merge pull request #114 from nextcloud/fix/noid/20-ibootstrap
implement ibootstrap
2 parents d8920a1 + 6a9cc6d commit 2564ea5

File tree

9 files changed

+208
-96
lines changed

9 files changed

+208
-96
lines changed

.drone.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ steps:
1111
commands:
1212
- wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh
1313
- bash ./before_install.sh $APP_NAME $CORE_BRANCH $DATABASEHOST
14-
- cd ../server
14+
- cd ../server/apps/$APP_NAME
1515
- composer install
16-
- ./lib/composer/bin/parallel-lint apps/$APP_NAME/
16+
- ./vendor/bin/parallel-lint --exclude ./vendor/ .
1717
- name: syntax-php7.4
1818
image: nextcloudci/php7.4:2
1919
environment:
@@ -23,9 +23,9 @@ steps:
2323
commands:
2424
- wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh
2525
- bash ./before_install.sh $APP_NAME $CORE_BRANCH $DATABASEHOST
26-
- cd ../server
26+
- cd ../server/apps/$APP_NAME
2727
- composer install
28-
- ./lib/composer/bin/parallel-lint apps/$APP_NAME/
28+
- ./vendor/bin/parallel-lint --exclude ./vendor/ .
2929
- name: app-code-check
3030
image: nextcloudci/php7.3:php7.3-5
3131
environment:
@@ -47,6 +47,8 @@ trigger:
4747
- pull_request
4848
- push
4949

50+
type: docker
51+
5052
---
5153
kind: pipeline
5254
name: unit-sqlite-php7.2
@@ -77,6 +79,8 @@ trigger:
7779
- pull_request
7880
- push
7981

82+
type: docker
83+
8084
---
8185
kind: pipeline
8286
name: unit-sqlite-php7.3
@@ -107,6 +111,8 @@ trigger:
107111
- pull_request
108112
- push
109113

114+
type: docker
115+
110116
---
111117
kind: pipeline
112118
name: unit-sqlite-php7.4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
build
2+
/vendor/

appinfo/app.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require-dev": {
3+
"php-parallel-lint/php-parallel-lint": "^1.2"
4+
}
5+
}

js/admin_settings.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
*
1919
*/
2020

21-
$(document).ready(function() {
22-
var $authorized = $('#impersonate .authorized');
21+
/* global OC, OCA */
22+
(function(OC, OCA) {
23+
OCA.Impersonate = {
24+
initSettings: function() {
25+
var $authorized = $('#impersonate .authorized')
2326

24-
OC.Settings.setupGroupsSelect($authorized);
25-
$authorized.change(function(event) {
26-
var groups = event.val || ['admin'];
27-
groups = JSON.stringify(groups);
28-
OCP.AppConfig.setValue('impersonate', 'authorized', groups);
29-
});
30-
});
27+
OC.Settings.setupGroupsSelect($authorized)
28+
$authorized.change(function(event) {
29+
var groups = event.val || ['admin']
30+
groups = JSON.stringify(groups)
31+
OCP.AppConfig.setValue('impersonate', 'authorized', groups)
32+
})
33+
}
34+
}
35+
36+
document.addEventListener('DOMContentLoaded', OCA.Impersonate.initSettings)
37+
})(OC, OCA)

js/impersonate.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
1-
/* global OC, $ */
2-
(function(OC, $){
3-
4-
$(document).ready(function() {
5-
function impersonate(userId) {
6-
$.post(
7-
OC.generateUrl('apps/impersonate/user'),
8-
{ userId: userId }
9-
).done(function() {
10-
window.location = OC.generateUrl('apps/files');
11-
}).fail(function( result ) {
12-
OC.dialogs.alert(result.responseJSON.message, t('impersonate', 'Could not impersonate user'));
13-
});
14-
}
15-
16-
function impersonateDialog(event) {
17-
let userId = event.target.closest('.row').dataset.id;
18-
OC.dialogs.confirm(
19-
t('impersonate', 'Are you sure you want to impersonate "{userId}"?', {userId: userId}),
20-
t('impersonate', 'Impersonate user' ),
21-
function(result) {
22-
if (result) {
23-
impersonate(userId);
24-
}
25-
},
26-
true
27-
);
1+
/* global OC */
2+
(function(OC) {
3+
function impersonate(userId) {
4+
var xhr = new XMLHttpRequest()
5+
xhr.onreadystatechange = function(data) {
6+
if (xhr.readyState === XMLHttpRequest.DONE) {
7+
if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
8+
window.location = OC.generateUrl('/')
9+
} else {
10+
OC.dialogs.alert(JSON.parse(xhr.response).message, t('impersonate', 'Could not impersonate user'), undefined, undefined)
11+
}
12+
}
2813
}
14+
xhr.open('POST', OC.generateUrl('apps/impersonate/user'))
15+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
16+
xhr.send('userId=' + encodeURIComponent(userId) + '&requesttoken=' + encodeURIComponent(OC.requestToken))
17+
}
2918

30-
let registerFunction = function (delay) {
31-
if(OCA.Settings === undefined) {
32-
delay = delay * 2;
33-
if(delay === 0) {
34-
delay = 15;
19+
function impersonateDialog(event) {
20+
var userId = event.target.closest('.row').dataset.id
21+
OC.dialogs.confirm(
22+
t('impersonate', 'Are you sure you want to impersonate "{userId}"?', { userId: userId }),
23+
t('impersonate', 'Impersonate user'),
24+
function(result) {
25+
if (result) {
26+
impersonate(userId)
3527
}
36-
if(delay > 500) {
37-
console.warn("Could not register impersonate script");
38-
return;
39-
}
40-
setTimeout(function() {registerFunction(delay)}, delay);
41-
} else {
42-
OCA.Settings.UserList.registerAction('icon-user', t('impersonate', 'Impersonate'), impersonateDialog)
28+
},
29+
true
30+
)
31+
}
32+
33+
var registerFunction = function(delay) {
34+
delay = delay || 0
35+
if (OCA.Settings === undefined) {
36+
delay = delay * 2
37+
if (delay === 0) {
38+
delay = 15
39+
}
40+
if (delay > 500) {
41+
console.error('Could not register impersonate script')
42+
return
4343
}
44-
};
45-
registerFunction(0);
46-
});
44+
setTimeout(function() { registerFunction(delay) }, delay)
45+
} else {
46+
OCA.Settings.UserList.registerAction('icon-user', t('impersonate', 'Impersonate'), impersonateDialog)
47+
}
48+
}
49+
50+
document.addEventListener('DOMContentLoaded', registerFunction)
4751

48-
})(OC, $);
52+
})(OC)

js/impersonate_logout.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$(document).ready(function () {
1+
document.addEventListener('DOMContentLoaded', function() {
22

33
$("#logout").attr("href","#");
44

@@ -18,16 +18,16 @@ $(document).ready(function () {
1818
var promisObj = $.post(
1919
OC.generateUrl('apps/impersonate/logout'),
2020
{userId: userId}
21-
).promise();
21+
).promise()
2222

2323
promisObj.done(function () {
2424
OC.redirect(OC.generateUrl('settings/users'))
2525
});
2626
}
2727

2828
$('#settings ul li:last').on('click', function (event) {
29-
event.preventDefault();
30-
var userId = $("#expandDisplayName").text();
31-
logoutHandler(userId);
32-
});
33-
});
29+
event.preventDefault()
30+
var userId = $("#expandDisplayName").text()
31+
logoutHandler(userId)
32+
})
33+
})

lib/AppInfo/Application.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
6+
*
7+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\Impersonate\AppInfo;
27+
28+
use OCA\Impersonate\Listener\BeforeTemplateRenderedListener;
29+
use OCA\Settings\Events\BeforeTemplateRenderedEvent;
30+
use OCP\AppFramework\App;
31+
use OCP\AppFramework\Bootstrap\IBootContext;
32+
use OCP\AppFramework\Bootstrap\IBootstrap;
33+
use OCP\AppFramework\Bootstrap\IRegistrationContext;
34+
use OCP\ISession;
35+
use OCP\Util;
36+
37+
class Application extends App implements IBootstrap {
38+
public const APP_ID = 'impersonate';
39+
40+
public function __construct() {
41+
parent::__construct(self::APP_ID);
42+
}
43+
44+
public function register(IRegistrationContext $context): void {
45+
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
46+
}
47+
48+
public function boot(IBootContext $context): void {
49+
$session = $context->getServerContainer()->get(ISession::class);
50+
51+
if($session->get('oldUserId') !== null) {
52+
Util::addScript(self::APP_ID,'impersonate_logout');
53+
}
54+
}
55+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
6+
*
7+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\Impersonate\Listener;
27+
28+
use OCA\Impersonate\AppInfo\Application;
29+
use OCA\Settings\Events\BeforeTemplateRenderedEvent;
30+
use OCP\EventDispatcher\Event;
31+
use OCP\EventDispatcher\IEventListener;
32+
use OCP\IConfig;
33+
use OCP\IGroupManager;
34+
use OCP\IUserSession;
35+
use OCP\Util;
36+
37+
class BeforeTemplateRenderedListener implements IEventListener {
38+
39+
/** @var IConfig */
40+
private $config;
41+
/** @var IGroupManager */
42+
private $groupManager;
43+
/** @var IUserSession */
44+
private $userSession;
45+
46+
public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
47+
$this->config = $config;
48+
$this->groupManager = $groupManager;
49+
$this->userSession = $userSession;
50+
}
51+
52+
public function handle(Event $event): void {
53+
if (!$event instanceof BeforeTemplateRenderedEvent) {
54+
return;
55+
}
56+
57+
$authorized = json_decode($this->config->getAppValue(Application::APP_ID, 'authorized', '["admin"]'));
58+
59+
if (!empty($authorized)) {
60+
$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
61+
if (!array_intersect($userGroups, $authorized)) {
62+
return;
63+
}
64+
}
65+
Util::addScript(Application::APP_ID, 'impersonate');
66+
}
67+
}

0 commit comments

Comments
 (0)