diff --git a/cp/app/Auth/Auth.php b/cp/app/Auth/Auth.php index dd03975..024d3fd 100644 --- a/cp/app/Auth/Auth.php +++ b/cp/app/Auth/Auth.php @@ -322,6 +322,25 @@ class Auth } } + /** + * Impersonate a user + * @param $userId + * @throws \Pinga\Auth\AuthError + */ + public static function impersonateUser($userId){ + $auth = self::$auth; + try { + $auth->admin()->logInAsUserById($userId); + redirect()->route('home')->with('success','Registrar impersonation started'); + } + catch (UnknownIdException $e) { + redirect()->route('registrars')->with('error','Unknown ID'); + } + catch (EmailNotVerifiedException $e) { + redirect()->route('registrars')->with('error','Email address not verified'); + } + } + /** * @throws \Pinga\Auth\AuthError */ diff --git a/cp/app/Controllers/RegistrarsController.php b/cp/app/Controllers/RegistrarsController.php index 30243a7..3e965a5 100644 --- a/cp/app/Controllers/RegistrarsController.php +++ b/cp/app/Controllers/RegistrarsController.php @@ -7,6 +7,7 @@ use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Container\ContainerInterface; use League\ISO3166\ISO3166; use Respect\Validation\Validator as v; +use App\Auth\Auth; class RegistrarsController extends Controller { @@ -1223,4 +1224,27 @@ class RegistrarsController extends Controller } } + public function impersonateRegistrar(Request $request, Response $response, $args) + { + if ($_SESSION["auth_roles"] != 0) { + return $response->withHeader('Location', '/dashboard')->withStatus(302); + } + + $db = $this->container->get('db'); + + if ($args) { + $args = trim($args); + + $registrar_id = $db->selectValue('SELECT id FROM registrar WHERE clid = ?', + [ $args ]); + $user_id = $db->selectValue('SELECT user_id FROM registrar_users WHERE registrar_id = ?', + [ $registrar_id ]); + + Auth::impersonateUser($user_id); + } else { + // Redirect to the registrars view + return $response->withHeader('Location', '/registrars')->withStatus(302); + } + } + } \ No newline at end of file diff --git a/cp/resources/views/partials/js-registrars.twig b/cp/resources/views/partials/js-registrars.twig index 8ecedf2..52fb69f 100644 --- a/cp/resources/views/partials/js-registrars.twig +++ b/cp/resources/views/partials/js-registrars.twig @@ -13,7 +13,7 @@ function actionsFormatter(cell, formatterParams, onRendered) { return ` - + `; } diff --git a/cp/routes/web.php b/cp/routes/web.php index df568ce..1ba511f 100644 --- a/cp/routes/web.php +++ b/cp/routes/web.php @@ -95,6 +95,7 @@ $app->group('', function ($route) { $route->get('/registrar', RegistrarsController::class .':registrar')->setName('registrar'); $route->map(['GET', 'POST'], '/registrar/edit', RegistrarsController::class .':editRegistrar')->setName('editRegistrar'); $route->get('/registrar/check', RegistrarsController::class . ':oteCheck')->setName('oteCheck'); + $route->get('/registrar/impersonate/{registrar}', RegistrarsController::class . ':impersonateRegistrar')->setName('impersonateRegistrar'); $route->get('/users', UsersController::class .':listUsers')->setName('listUsers');