feat: Implement user management functionality with impersonation support
This commit is contained in:
133
autoload/Controllers/UsersController.php
Normal file
133
autoload/Controllers/UsersController.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
namespace Controllers;
|
||||
|
||||
class UsersController
|
||||
{
|
||||
private const ADMIN_USER_ID = 1;
|
||||
private const IMPERSONATOR_SESSION_KEY = 'impersonator_user';
|
||||
|
||||
public static function mainView()
|
||||
{
|
||||
global $user;
|
||||
|
||||
if ( !$user )
|
||||
return \controls\Users::login_form();
|
||||
|
||||
$impersonator_user = self::getImpersonatorUser();
|
||||
if ( !self::canManageUsers( $user, $impersonator_user ) )
|
||||
self::forbiddenRedirect();
|
||||
|
||||
$users_repository = new \Domain\Users\UserRepository();
|
||||
|
||||
return \Tpl::view( 'users/main-view', self::buildMainViewModel(
|
||||
$user,
|
||||
$impersonator_user,
|
||||
$users_repository -> all()
|
||||
) );
|
||||
}
|
||||
|
||||
public static function loginAs()
|
||||
{
|
||||
global $user;
|
||||
|
||||
if ( !$user )
|
||||
return \controls\Users::login_form();
|
||||
|
||||
$impersonator_user = self::getImpersonatorUser();
|
||||
if ( !self::canManageUsers( $user, $impersonator_user ) )
|
||||
self::forbiddenRedirect();
|
||||
|
||||
$target_user_id = (int)\S::get( 'user_id' );
|
||||
$users_repository = new \Domain\Users\UserRepository();
|
||||
$target_user = $users_repository -> byId( $target_user_id );
|
||||
|
||||
if ( !$target_user )
|
||||
{
|
||||
\S::alert( 'Nie znaleziono wskazanego uzytkownika.' );
|
||||
header( 'Location: /users/main_view/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
$new_session_state = self::impersonationStateAfterLoginAs( $user, $target_user, $impersonator_user );
|
||||
|
||||
\S::set_session( 'user', $new_session_state['user'] );
|
||||
\S::set_session( self::IMPERSONATOR_SESSION_KEY, $new_session_state['impersonator_user'] );
|
||||
|
||||
\S::alert( 'Zalogowano jako: ' . $target_user['name'] . ' ' . $target_user['surname'] . '.' );
|
||||
header( 'Location: /' );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function switchBackToAdmin()
|
||||
{
|
||||
$impersonator_user = self::getImpersonatorUser();
|
||||
|
||||
if ( !$impersonator_user or !isset( $impersonator_user['id'] ) or (int)$impersonator_user['id'] !== self::ADMIN_USER_ID )
|
||||
{
|
||||
\S::alert( 'Brak aktywnej sesji podszywania.' );
|
||||
header( 'Location: /' );
|
||||
exit;
|
||||
}
|
||||
|
||||
\S::set_session( 'user', $impersonator_user );
|
||||
\S::del_session( self::IMPERSONATOR_SESSION_KEY );
|
||||
|
||||
\S::alert( 'Powrot do konta administratora.' );
|
||||
header( 'Location: /users/main_view/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function canManageUsers( $current_user, $impersonator_user = null )
|
||||
{
|
||||
if ( !is_array( $current_user ) )
|
||||
return false;
|
||||
|
||||
if ( isset( $current_user['id'] ) and (int)$current_user['id'] === self::ADMIN_USER_ID )
|
||||
return true;
|
||||
|
||||
if ( is_array( $impersonator_user ) and isset( $impersonator_user['id'] ) and (int)$impersonator_user['id'] === self::ADMIN_USER_ID )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function buildMainViewModel( $current_user, $impersonator_user, array $users )
|
||||
{
|
||||
return [
|
||||
'current_user' => $current_user,
|
||||
'impersonator_user' => $impersonator_user,
|
||||
'users' => $users,
|
||||
'can_switch_back' => is_array( $impersonator_user ) and isset( $impersonator_user['id'] ) and (int)$impersonator_user['id'] === self::ADMIN_USER_ID
|
||||
];
|
||||
}
|
||||
|
||||
public static function impersonationStateAfterLoginAs( $current_user, $target_user, $existing_impersonator_user = null )
|
||||
{
|
||||
$impersonator_user = $existing_impersonator_user;
|
||||
|
||||
if ( !is_array( $impersonator_user ) )
|
||||
$impersonator_user = ( is_array( $current_user ) and isset( $current_user['id'] ) and (int)$current_user['id'] === self::ADMIN_USER_ID ) ? $current_user : null;
|
||||
|
||||
return [
|
||||
'user' => $target_user,
|
||||
'impersonator_user' => $impersonator_user
|
||||
];
|
||||
}
|
||||
|
||||
private static function getImpersonatorUser()
|
||||
{
|
||||
$session_value = \S::get_session( self::IMPERSONATOR_SESSION_KEY );
|
||||
|
||||
if ( is_array( $session_value ) )
|
||||
return $session_value;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function forbiddenRedirect()
|
||||
{
|
||||
\S::alert( 'Brak uprawnien do zarzadzania uzytkownikami.' );
|
||||
header( 'Location: /' );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user