if ($token) {
// типа сайт для авторизованного
} else {
// форма авторизации
}
if (!$this->user and $this->controller != 'Main') $this->redirect('/'); // Main у нас пустая страница, то есть / abstract class AuthController
{
// [] фронтенд, ['logged'] закрытый фронтенд, ['logged', 'admin'] бэкенд
protected $authRoles = [];
protected function checkAuth()
{
$auth = Auth::instance($_SESSION ?? []);
if (! $auth->getUser()) {
throw new HttpException(null, 401);
}
if (! $auth->loggedIn($this->authRoles)) {
throw new HttpException(null, 403);
}
}
} if (! $auth->getUser()) {
throw new HttpException(null, 401);
}abstract class AuthController extends Controller abstract class AuthController extends Controller
abstract class AppController extends Controllerclass ЛюбойController extends AppControllerесли у Вас не 2 роли, то и сбивает с толку - куда отправлять 'logged' пользователя если он не 'admin'
if ($user['role'] == 'admin') echo 'Админ';
if ($user['id'] == 1) echo 'Админ'; // естественно владелец регается первый зная о том, что админ - это id = 1 class AuthController extends AppController {}
class PageController extends AppController {}
class BackendPageController extends AuthController {} // [] фронтенд, ['logged'] закрытый фронтенд, ['logged', 'admin'] бэкенд
protected $authRoles = []; private function authRedirect()
{
if ($this->controller == 'Main') {
$this->alertRedirect('success', 'Добро пожаловать', '/menu');
}
}
private function noAuthRedirect()
{
if ($this->controller != 'Main') {
$this->alertRedirect('danger', 'Всего доброго', '/');
}
}
if ($this->checkSessionId()) {
$this->authRedirect();
} else {
$this->noAuthRedirect();
} class AuthController extends AppController {}
class PageController extends AppController {}
class BackendPageController extends AuthController {}