From 67f6be1d378e767801d2d8fc5f0bbb857a66e525 Mon Sep 17 00:00:00 2001 From: Sujith H Date: Wed, 12 Apr 2017 10:33:44 +0530 Subject: [PATCH] Improvise impersonate app with changes Below are the changes implemented: 1) Support of groups which can be impersonated. 2) Support of logout which switches back to orignal user. 3) Support to display the user logged as information as notification. 4) Removed the confirmation dialog to impersonate. Signed-off-by: Sujith H --- appinfo/app.php | 5 ++- appinfo/info.xml | 4 +- appinfo/routes.php | 19 ++++++++- controller/logoutcontroller.php | 64 +++++++++++++++++++++++++++++++ controller/settingscontroller.php | 3 +- css/settings-admin.css | 0 js/impersonate.js | 55 +++++++++++++++++--------- js/impersonate_logout.js | 37 ++++++++++++++++++ js/settings-admin.js | 17 ++++++++ settings-admin.php | 9 +++++ templates/settings-admin.php | 22 +++++++++++ 11 files changed, 211 insertions(+), 24 deletions(-) create mode 100644 controller/logoutcontroller.php create mode 100644 css/settings-admin.css create mode 100644 js/impersonate_logout.js create mode 100644 js/settings-admin.js create mode 100644 settings-admin.php create mode 100644 templates/settings-admin.php diff --git a/appinfo/app.php b/appinfo/app.php index d45f35d..8c5246a 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -9,6 +9,8 @@ * @copyright Jörn Friedrich Dreyer 2015 */ +\OCP\App::registerAdmin('impersonate', 'settings-admin'); +\OCP\Util::addScript('impersonate','impersonate_logout'); // --- register js for user management------------------------------------------ $eventDispatcher = \OC::$server->getEventDispatcher(); $eventDispatcher->addListener( @@ -16,4 +18,5 @@ function() { \OCP\Util::addScript('impersonate', 'impersonate'); } -); \ No newline at end of file +); + 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..80cff93 --- /dev/null +++ b/controller/logoutcontroller.php @@ -0,0 +1,64 @@ +userManager = $userManager; + $this->userSession = $userSession; + $this->logger = $logger; + } + + /** + * @NoAdminRequired + * + * @param string userid + * @UseSession + * @return JSONResponse + */ + public function logoutcontroller($userid) { + $user = \OC::$server->getSession()->get('oldUserId'); + $user = $this->userManager->get($user); + + if($user === null) { + return new JSONResponse("No user found for $user", Http::STATUS_NOT_FOUND); + } else { + $this->userSession->setUser($user); + $this->logger->info("Going to switch to a different user $userid", ['app' => 'impersonate']); + //Swap old and new user id's for next logout. + $newUserId = \OC::$server->getSession()->get('newUserId'); + $oldUserId = \OC::$server->getSession()->get('oldUserId'); + \OC::$server->getSession()->set('newUserId',$oldUserId); + \OC::$server->getSession()->set('oldUserId',$newUserId); + } + return new JSONResponse(); + } +} diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php index afd5697..ffcda97 100644 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -45,9 +45,11 @@ public function __construct($appName, IRequest $request, IUserManager $userManag */ public function impersonate($userid) { $oldUserId = $this->userSession->getUser()->getUID(); + \OC::$server->getSession()->set('oldUserId',$oldUserId); $this->logger->warning("User $oldUserId trying to impersonate user $userid", ['app' => 'impersonate']); $user = $this->userManager->get($userid); + \OC::$server->getSession()->set('newUserId',$userid); if ($user === null) { return new JSONResponse("No user found for $userid", Http::STATUS_NOT_FOUND); } else { @@ -56,6 +58,5 @@ public function impersonate($userid) { } 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..17648ee 100644 --- a/js/impersonate.js +++ b/js/impersonate.js @@ -1,38 +1,55 @@ (function(){ - $(document).ready(function() { + $(document).ready(function () { function impersonate(userid) { + var currentUser = OC.getCurrentUser().uid; $.post( OC.generateUrl('apps/impersonate/user'), { userid: userid } ).done(function( result ) { - window.location = OC.generateUrl('apps/files'); + OC.redirect(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'); + var includedGroups; + OC.AppConfig.getValue('impersonate','impersonate_include_groups_list',"[]", function (data) { + includedGroups = $.parseJSON(data); + }); + + var addImpersonate; + var oldAdd = UserList.add; + var $newColumn = $("#userlist").find("tr:first-child"); + $('Impersonate').insertAfter($newColumn.find("#headerName")); + UserList.add = function () { + var $tr = oldAdd.apply(this,arguments); + var groupsSelectedByUser = $tr.find('.groups').text(); + var found = false; + for(var i=0; i < includedGroups.length; i++) { + if($.trim(includedGroups[i]) === $.trim(groupsSelectedByUser)) { + found = true; + addImpersonate = '' + + '' + + ''; + $(addImpersonate).insertAfter($tr.find('.name')); + } + } + if(found === false) { + addImpersonate = ''; + $(addImpersonate).insertAfter($tr.find('.name')); + } + if(!$tr) { + return; + } + return $tr; + }; $('#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..d05f73f --- /dev/null +++ b/js/impersonate_logout.js @@ -0,0 +1,37 @@ +$(document).ready(function () { + + $("#logout").attr("href","#"); + + 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 () { + OC.redirect('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.')); ?> +

+ +
+