/** * Add a menu item to the topbar * * @param string $hook the name of the hook * @param string $type the type of the hook * @param \ElggMenuItem[] $return_value current menu items * @param array $params supplied params * * @return void|\ElggMenuItem[] */ public static function registerTopbar($hook, $type, $return_value, $params) { $user = elgg_get_logged_in_user_entity(); if (empty($user)) { return; } // check if the user is an admin if (!admin_tools_is_admin_user($user)) { return; } if ($user->isAdmin()) { $text = elgg_echo('admin_tools:switch_to_user'); } else { $text = elgg_echo('admin_tools:switch_to_admin'); } $return_value[] = \ElggMenuItem::factory(['name' => 'switch_admin', 'text' => $text, 'href' => 'action/admin_tools/toggle_admin?user_guid=' . $user->getGUID(), 'is_action' => true, 'is_trusted' => true, 'section' => 'alt', 'parent_name' => elgg_is_active_plugin('aalborg_theme') ? 'account' : '']); return $return_value; }
<?php /** * Toggle admin on/off for admins */ $user_guid = (int) get_input('user_guid'); if (empty($user_guid) || $user_guid != elgg_get_logged_in_user_guid()) { register_error(elgg_echo('actionunauthorized')); forward(REFERER); } $user = get_user($user_guid); if (empty($user)) { forward(REFERER); } if (!admin_tools_is_admin_user($user)) { register_error(elgg_echo('actionunauthorized')); forward(REFERER); } if ($user->isAdmin()) { // make the user a normal user $secret = admin_tools_make_switch_admin_secret($user); if (!empty($secret)) { $user->removeAdmin(); elgg_set_plugin_user_setting('switched_admin', $secret, $user->getGUID(), 'admin_tools'); system_message(elgg_echo('admin_tools:action:toggle_admin:success:user')); } else { register_error(elgg_echo('save:fail')); } } else { // make the user an admin $user->makeAdmin();