Hi!
I´m working on a very small project. It has two purposes. Number one: learn PHP. Number two, learn a framework. Currently it contains of a login and simple CRUD were users can create notes.
But anyway.
I have an auth middleware
if (!$this->container->auth->check()) {
$this->container->flash->addMessage('error', 'Please sign in before doing that.');
return $response->withRedirect($this->container->router->pathFor('auth.signin'));
}
This auth middleware is attached as a group in the route file
For example:
$app->group('', function () {
$this->get('/dashboard', 'AuthController:dashboard')->setName('dashboard');
$this->get('/notes', 'NoteController:index')->setName('notes');
$this->post('/notes', 'NoteController:newNote')->setName('new.note');
$this->get('/notes/{note_id:[0-9]+}', 'NoteController:getEditNote');
$this->post('/notes/{note_id:[0-9]+}', 'NoteController:postEditNote')->setName('edit.note');
$this->get('/notes/deleteNote/{note_id:[0-9]+}', 'NoteController:deleteNote')->setName('delete.note');
})->add(new AuthMiddleware($container));
But now I’m looking into the option to add Admins and mods, as a user_type
. So i added user_account_type
tinyint(1) NOT NULL DEFAULT '1'
. And made a middleware, AdminMiddleware
{
/** Check if user is admin, if not. Return 404 */
if (!$this->container->auth->checkIsAdmin()) {
return $response->withStatus(404)->withHeader('Content-Type', 'text/html')->write('Page not found');
}
And somewhere here my questions popping up. I have for some testing just added this route
$app->get('/admin', function ($request, $response, $args) {
return 'You are signed in as admin';
})->add(new AdminMiddleware($container));
And as you can se, this Middleware dosen check if your are authenticated, but you have to be for the Model
to fetch that row:
public function checkIsAdmin()
{
if(User::where('user_id', $_SESSION['user_id'])->first()->user_account_type == '1'){
return true;
}else{
return false;
}
}
So what would the best practice be to check if someone is Admin/mod etc?
A route to /admin
would be pretty straight forward with a middleware, but for small things in every view, like edit, delete, ban etc
What is the best way to go/best practices of implementing admin and mods through the app?