Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
aggiunti gate per i ruoli
Browse files Browse the repository at this point in the history
si utilizza la direttiva @can('isAdmin'), ruoli:
- 'isUser'
- 'isMod'
- 'isAdmin'
  • Loading branch information
BroHPotato committed Mar 21, 2020
1 parent 0984f7c commit 1e1077e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -26,6 +27,21 @@ public function boot()
$this->registerPolicies();


/* define a admin user role */
Gate::define('isAdmin', function($user) {
return $user->type == 2;
});

/* define a manager user role */
Gate::define('isMod', function($user) {
return $user->type == 1;
});

/* define a user role */
Gate::define('isUser', function($user) {
return $user->type == 0;
});

Auth::provider('custom', function () {
// Return an instance of Illuminate\Contracts\Auth\UserProvider...

Expand Down
7 changes: 4 additions & 3 deletions app/Providers/UserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ public function retrieveByCredentials(array $credentials){
'body' => '{"username":"' . $credentials["email"] . '","password":"' . $credentials["password"] . '"}'
]);
$response = json_decode($this->request->post('auth')->getBody());
if ($response->tfa) {

if (property_exists($response, 'tfa')) {
session(['token' => $response->token]);
return redirect('/login/tfa');
} else {
$userarray = (array)$response->user;
$userarray['token'] = $response->jwt;
$userarray['token'] = $response->token;

session(['token' => $response->jwt]);
session(['token' => $response->token]);
$user = new User();
$user->fill($userarray);
$this->user = $user;
Expand Down
7 changes: 6 additions & 1 deletion resources/views/layouts/sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@
<span>Pagine view</span></a>
</li>

@canany(['isAdmin', 'isMod'])
<hr class="sidebar-divider">

<div class="sidebar-heading">
Centro di Controllo
</div>
@endcanany

<!-- TODO only for moderators -->
@can('isMod')
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseMod" aria-expanded="true" aria-controls="collapseTwo">
<i class="fas fa-fw fa-user-tie"></i>
Expand All @@ -71,9 +73,11 @@
</div>
</div>
</li>
@endcan


<!-- TODO only for admins -->
@can('isAdmin')
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseAdmin" aria-expanded="true" aria-controls="collapseTwo">
<i class="fas fa-fw fa-user-astronaut"></i>
Expand All @@ -89,6 +93,7 @@
</div>
</div>
</li>
@endcan


<hr class="sidebar-divider d-none d-md-block">
Expand Down

0 comments on commit 1e1077e

Please sign in to comment.