if its only 1 or 2 admins, and you don't need different levels of control, then a flag on the user model , and a simple middleware works just fine.
Oct 22, 2021
6
Level 1
What is the best way to implement an admin user
I've seen some ways to implement an admin account into a laravel project (with CRUD/dashboard acess privileges):
- Implementing an is_admin field into the User table.
- Implementing an Admin model with a middleware/controllers/guards
- Adding some keys to the .env file (name, email, password). Adding an admin.php file in the conf directory with access to those keys and a seeder.
- using administrative interface builders.
I want to know what is, in your opinion, the most efficient and secure way to do it.
Level 80
@galaners Simplest way is a is_admin column in your users table, and some middleware that you can apply to admin-only routes:
class EnsureUserIsAdministrator
{
public function handle(Request $request, Closure $next)
{
if ($request->user()->is_admin) {
return $next($request);
}
abort(403, 'You are not an administrator');
}
}
1 like
Please or to participate in this conversation.