Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
implement ibootstrap
Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz committed Sep 8, 2020
commit 1276de1f7cf3a6514c6c7799557da6f40a8d8d0d
33 changes: 0 additions & 33 deletions appinfo/app.php

This file was deleted.

25 changes: 16 additions & 9 deletions js/admin_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
90 changes: 47 additions & 43 deletions js/impersonate.js
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 7 additions & 7 deletions js/impersonate_logout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$(document).ready(function () {
document.addEventListener('DOMContentLoaded', function() {

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

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

promisObj.done(function () {
OC.redirect(OC.generateUrl('settings/users'))
});
}

$('#settings ul li:last').on('click', function (event) {
event.preventDefault();
var userId = $("#expandDisplayName").text();
logoutHandler(userId);
});
});
event.preventDefault()
var userId = $("#expandDisplayName").text()
logoutHandler(userId)
})
})
55 changes: 55 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Arthur Schiwon <[email protected]>
*
* @author Arthur Schiwon <[email protected]>
*
* @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');
}
}
}
67 changes: 67 additions & 0 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Arthur Schiwon <[email protected]>
*
* @author Arthur Schiwon <[email protected]>
*
* @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');
}
}