diff --git a/appinfo/app.php b/appinfo/app.php index d45f35d..d9b5241 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -9,6 +9,7 @@ * @copyright Jörn Friedrich Dreyer 2015 */ +\OCP\App::registerAdmin('impersonate', 'settings-admin'); // --- register js for user management------------------------------------------ $eventDispatcher = \OC::$server->getEventDispatcher(); $eventDispatcher->addListener( @@ -16,4 +17,10 @@ function() { \OCP\Util::addScript('impersonate', 'impersonate'); } -); \ No newline at end of file +); +$eventDispatcher->addListener( + 'OC\TemplateLayout::loadAdditionalScripts', + function() { + \OCP\Util::addScript('impersonate','impersonate_logout'); + } +); diff --git a/appinfo/info.xml b/appinfo/info.xml index c77a772..373b291 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -7,6 +7,6 @@ Jörn Friedrich Dreyer 0.0.5 - + - \ No newline at end of file + diff --git a/appinfo/routes.php b/appinfo/routes.php index 1d70164..f10d91f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -15,11 +15,28 @@ $this, [ 'routes' => [ + + // Land in users setting page ( for admin user only ) [ 'name' => 'Settings#impersonate', 'url' => '/user', 'verb' => 'POST', ], + + // Land in admin section to add settings + [ + 'name' => 'admin_settings#impersonateAdminTemplate', + 'url' => '/settings/impersonatetemplate', + 'verb' => 'POST', + ], + + //Land in index page + [ + 'name' => 'Logout#logoutcontroller', + 'url' => '/logout', + 'verb' => 'POST', + ], + ], ] -); \ No newline at end of file +); diff --git a/controller/logoutcontroller.php b/controller/logoutcontroller.php new file mode 100644 index 0000000..2eba848 --- /dev/null +++ b/controller/logoutcontroller.php @@ -0,0 +1,59 @@ +userManager = $userManager; + $this->userSession = $userSession; + $this->logger = $logger; + } + + /** + * @NoAdminRequired + * + * @param string userid + * @UseSession + * @return JSONResponse + */ + public function logoutcontroller($userid) { + $user = $this->userManager->get($_SESSION['oldUserId']); + + if($user === null) { + return new JSONResponse("No user found for $user", Http::STATUS_NOT_FOUND); + } else { + $this->userSession->setUser($user); + $this->logger->warning("Going to switch to a different user $oldUserId", ['app' => 'impersonate']); + list($_SESSION['oldUserId'],$_SESSION['newUserId']) = array($_SESSION['newUserId'], $_SESSION['oldUserId']); + } + return new JSONResponse(); + } +} diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php index afd5697..e2c5c19 100644 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -45,17 +45,19 @@ public function __construct($appName, IRequest $request, IUserManager $userManag */ public function impersonate($userid) { $oldUserId = $this->userSession->getUser()->getUID(); + $_SESSION['oldUserId'] = $oldUserId; $this->logger->warning("User $oldUserId trying to impersonate user $userid", ['app' => 'impersonate']); $user = $this->userManager->get($userid); + $_SESSION['newUserId'] = $userid; if ($user === null) { return new JSONResponse("No user found for $userid", Http::STATUS_NOT_FOUND); } else { $this->logger->warning("changing to user $userid", ['app' => 'impersonate']); $this->userSession->setUser($user); + \OC::$server->getAppConfig()->setValue('impersonate', 'impersonating_user_id', $oldUserId); } return new JSONResponse(); } - } diff --git a/css/settings-admin.css b/css/settings-admin.css new file mode 100644 index 0000000..e69de29 diff --git a/js/impersonate.js b/js/impersonate.js index b89758c..6eca069 100644 --- a/js/impersonate.js +++ b/js/impersonate.js @@ -1,38 +1,47 @@ (function(){ - $(document).ready(function() { + $(window).load(function () { function impersonate(userid) { + var currentUser = OC.getCurrentUser().uid; $.post( OC.generateUrl('apps/impersonate/user'), { userid: userid } ).done(function( result ) { + OC.AppConfig.setValue('impersonate','impersonating_user_id',currentUser); window.location = OC.generateUrl('apps/files'); }).fail(function( result ) { OC.dialogs.alert(result.responseJSON.message, t('impersonate', 'Could not impersonate user')); }); } - $(' ').insertAfter('#userlist #headerName'); - $('' + - '' + - '') - .insertAfter('#userlist .name'); + OC.AppConfig.getValue('impersonate','impersonate_include_groups_list',"[]", function (data) { + data = eval(data); + if (data.length !== 0) { + var newColumn = $("#userlist").find("tr:first-child"); + $('Impersonate').insertAfter(newColumn.find("#headerName")); + for(var i = 0; i < data.length; i++) { + var collectTr = $("#userlist tr"); + $.each($("#userlist tr"), function (){ + var textDisplayed = $(this).find('td.groups').text(); + if ($.trim(textDisplayed) === $.trim(data[i])) { + var addImpersonate ='' + + '' + + ''; + $(addImpersonate).insertAfter($(this).find('.name')); + } else { + var addImpersonate = ''; + $(addImpersonate).insertAfter($(this).find('.name')); + } + }); + } + } + }); $('#userlist').on('click', '.impersonate', function() { var userid = $(this).parents('tr').find('.name').text(); - OCdialogs.confirm( - t('impersonate', 'With great power comes great responsibility!'), - t('impersonate', 'Are you sure you want to impersonate {userid}?', - {userid: userid} ), - function(result) { - if (result) { - impersonate(userid); - } - }, - true - ); + impersonate(userid); }); }); diff --git a/js/impersonate_logout.js b/js/impersonate_logout.js new file mode 100644 index 0000000..891024d --- /dev/null +++ b/js/impersonate_logout.js @@ -0,0 +1,35 @@ +$(document).ready(function () { + + var text = t( + 'core', + '{displayText}', + { + docUrl: OC.generateUrl('apps/files'), + displayText: "Logged in as " + OC.getCurrentUser().uid, + } + ); + + var timeout = 15; + OC.Notification.showHtml( + text, + { + isHTML: true, timeout + } + ); + + function logoutHandler(userid) { + var promisObj = $.post( + OC.generateUrl('apps/impersonate/logout'), + {userid: userid} + ).promise(); + + promisObj.done(function () { + window.location = OC.generateUrl('apps/files'); + }); + } + + $("#logout").on('click', function () { + var userid = $("#expandDisplayName").text(); + logoutHandler(userid); + }); +}); diff --git a/js/settings-admin.js b/js/settings-admin.js new file mode 100644 index 0000000..44da341 --- /dev/null +++ b/js/settings-admin.js @@ -0,0 +1,17 @@ +$(document).ready(function () { + $("#impersonateIncludeGroups").change(function () { + $("#selectIncludedGroups").toggleClass('hidden', !this.checked); + var val = $("#impersonateIncludeGroups").is(":checked"); + OC.AppConfig.setValue('impersonate',$(this).attr('name'),val); + }); + + $('#includedGroups').each(function (index, element) { + OC.Settings.setupGroupsSelect($(element)); + $(element).change(function(ev) { + var groups = ev.val || []; + groups = JSON.stringify(groups); + OC.AppConfig.setValue('impersonate', $(this).attr('name'), groups); + }); + }); +}); + diff --git a/settings-admin.php b/settings-admin.php new file mode 100644 index 0000000..ebf6680 --- /dev/null +++ b/settings-admin.php @@ -0,0 +1,9 @@ +fetchPage(); diff --git a/templates/settings-admin.php b/templates/settings-admin.php new file mode 100644 index 0000000..b36ecc8 --- /dev/null +++ b/templates/settings-admin.php @@ -0,0 +1,22 @@ +
+ +

t('Impersonate Settings'));?>

+ +

+ getAppConfig()->getValue('impersonate', 'impersonate_include_groups','false') !== 'false') print_unescaped('checked="checked"'); ?> /> +
+

+

+ +
+ t('These groups will be able to impersonate.')); ?> +

+ +
+