reconstruction of the project
This commit is contained in:
@@ -1,166 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Configuration;
|
||||
use App\Models\Role;
|
||||
use App\Models\UserRole;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class RoleSwitcherController extends BaseApiController
|
||||
{
|
||||
protected UserRole $userRole;
|
||||
protected Role $role;
|
||||
protected Configuration $config;
|
||||
protected string $semester;
|
||||
protected string $schoolYear;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->userRole = model(UserRole::class);
|
||||
$this->role = model(Role::class);
|
||||
$this->config = model(Configuration::class);
|
||||
$this->semester = (string) ($this->config->getConfig('semester') ?? '');
|
||||
$this->schoolYear = (string) ($this->config->getConfig('school_year') ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/v1/role-switcher
|
||||
* Get available roles for the current user
|
||||
* If user has only one role, returns auto_redirect flag with route
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user) {
|
||||
return $this->error('Authentication required', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
try {
|
||||
$roles = $this->userRole
|
||||
->select('roles.name')
|
||||
->join('roles', 'roles.id', '=', 'user_roles.role_id')
|
||||
->where('user_roles.user_id', $user->id)
|
||||
->whereNull('user_roles.deleted_at')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
if (empty($roles)) {
|
||||
return $this->error('No roles assigned to this user', Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$roleNames = array_column($roles, 'name');
|
||||
|
||||
// If user has only one role, return auto-redirect information
|
||||
if (count($roleNames) === 1) {
|
||||
$roleName = $roleNames[0];
|
||||
$route = $this->getDashboardRoute($roleName);
|
||||
|
||||
return $this->success([
|
||||
'roles' => $roleNames,
|
||||
'auto_redirect' => true,
|
||||
'route' => $route,
|
||||
'role' => $roleName,
|
||||
], 'Single role found, auto-redirect available');
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'roles' => $roleNames,
|
||||
'auto_redirect' => false,
|
||||
'current_role' => Session::get('active_role'),
|
||||
], 'Roles retrieved successfully');
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Get roles error: ' . $e->getMessage());
|
||||
return $this->respondError('Failed to retrieve roles', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/v1/role-switcher/switch
|
||||
* Switch to a specific role (role in request body)
|
||||
*/
|
||||
public function switchRole()
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user) {
|
||||
return $this->error('Authentication required', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
$data = $this->payloadData();
|
||||
if (empty($data) || !isset($data['role'])) {
|
||||
return $this->error('Role is required', Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
return $this->switchToRole($data['role'], $user->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/v1/role-switcher/switch/{role}
|
||||
* Switch to a specific role (role in URL parameter)
|
||||
*/
|
||||
public function switchTo($role)
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user) {
|
||||
return $this->error('Authentication required', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
return $this->switchToRole($role, $user->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to handle role switching logic
|
||||
*/
|
||||
private function switchToRole(string $role, int $userId)
|
||||
{
|
||||
try {
|
||||
$roleKey = is_string($role) ? $role : (string) $role;
|
||||
|
||||
// Verify user has this role
|
||||
$hasRole = $this->userRole
|
||||
->select('roles.name')
|
||||
->join('roles', 'roles.id', '=', 'user_roles.role_id')
|
||||
->where('user_roles.user_id', $userId)
|
||||
->where(function ($query) use ($roleKey) {
|
||||
$query->where('LOWER(roles.name)', strtolower($roleKey))
|
||||
->orWhere('LOWER(roles.slug)', strtolower($roleKey));
|
||||
})
|
||||
->whereNull('user_roles.deleted_at')
|
||||
->first();
|
||||
|
||||
if (!$hasRole) {
|
||||
return $this->error('You do not have this role', Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
// Set active role in session
|
||||
Session::put('active_role', $roleKey);
|
||||
|
||||
// Get dashboard route for this role
|
||||
$route = $this->getDashboardRoute($roleKey);
|
||||
|
||||
log_message('debug', 'Redirecting user to: ' . $route);
|
||||
|
||||
return $this->success([
|
||||
'role' => $roleKey,
|
||||
'route' => $route,
|
||||
'message' => 'Role switched successfully',
|
||||
], 'Role switched successfully');
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Role switch error: ' . $e->getMessage());
|
||||
return $this->respondError('Failed to switch role', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dashboard route for a role
|
||||
*/
|
||||
private function getDashboardRoute(string $role): string
|
||||
{
|
||||
$key = is_string($role) ? $role : (string) $role;
|
||||
$route = $this->role->getRouteByNameOrSlug($key);
|
||||
|
||||
return $route ?? '/landing_page/guest_dashboard';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user