It's not an ACL for this simple functionality you don't even need a database table roles
you can add extra tinyInteger
status
column and add numbers for example:
- 0 = Disabled
- 1 = Visitor
- 2 = Admin.
To make it functional add following code to your User.php
.
public function isDisabled ()
{
return $this->statusCheck();
}
public function isVisitor ()
{
return $this->statusCheck(1);
}
public function isAdmin ()
{
return $this->statusCheck(2);
}
protected function statusCheck ($status = 0)
{
return $this->status === $status ? true : false;
}
To check in blade
template you can add
@if(Auth::user()->isDisabled())
You are not Active
@elseif(Auth::user()->isVisitor())
Welcome to example.com
@elseif(Auth::user()->isAdmin())
Welcome Admin
@endif
Moreover you can make blade custom directives, paste this code to your app/providers/AppServiceProvider.php
in boot()
method.
// Blade custom directives for isAdmin
Blade::directive('isAdmin', function() {
return "<?php if(Auth::user()->isAdmin()): ?>";
});
Blade::directive('endisAdmin', function() {
return "<?php endif; ?>";
});
// Blade custom directives for isVisitor
Blade::directive('isVisitor', function() {
return "<?php if(Auth::user()->isVisitor()): ?>";
});
Blade::directive('endisVisitor', function() {
return "<?php endif; ?>";
});
// Blade custom directives for isDisabled
Blade::directive('isDisabled', function() {
return "<?php if(Auth::user()->isDisabled()): ?>";
});
Blade::directive('endisDisabled', function() {
return "<?php endif; ?>";
});
To call this you use need to write following lines in your blade view
@isAdmin()
Welcome Admin
@endisAdmin
@isVisitor()
Welcome to example.com
@endisVisitor
@isDisabled()
Your are not active
@endisDisabled
In short laravel provides you a number of ways to solve a problem, it just depend on your need and application structure.