Last active
April 17, 2018 17:20
-
-
Save sergionader/797e78791df28a0feaea0973ca7a4517 to your computer and use it in GitHub Desktop.
From https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/16 - tested on version 5.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Permission; | |
use Illuminate\Support\Facades\Gate; | |
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; | |
class AuthServiceProvider extends ServiceProvider | |
{ | |
/** | |
* The policy mappings for the application. | |
* | |
* @var array | |
*/ | |
protected $policies = [ | |
'App\Model' => 'App\Policies\ModelPolicy', | |
]; | |
/** | |
* Register any authentication / authorization services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
$this->registerPolicies(); | |
foreach($this->getPermissions() as $permission){ | |
Gate::define($permission->name, function($user ) use ($permission){ | |
return $user->hasRole($permission->roles); | |
}); | |
} | |
} | |
protected function getPermissions(){ | |
return Permission::with('roles')->get(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
trait HasRoles | |
{ | |
/** | |
* A user may have multiple roles. | |
* | |
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
*/ | |
public function roles() | |
{ | |
return $this->belongsToMany(Role::class); | |
} | |
/** | |
* Assign the given role to the user. | |
* | |
* @param string $role | |
* @return mixed | |
*/ | |
public function assignRole($role) | |
{ | |
return $this->roles()->save( | |
Role::whereName($role)->firstOrFail() | |
); | |
} | |
/** | |
* Determine if the user has the given role. | |
* | |
* @param mixed $role | |
* @return boolean | |
*/ | |
public function hasRole($role) | |
{ | |
if (is_string($role)) { | |
return $this->roles->contains('name', $role); | |
} | |
return !! $role->intersect($this->roles)->count(); | |
} | |
/** | |
* Determine if the user may perform the given permission. | |
* | |
* @param Permission $permission | |
* @return boolean | |
*/ | |
public function hasPermission(Permission $permission) | |
{ | |
return $this->hasRole($permission->roles); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Permission extends Model | |
{ | |
/** | |
* A permission can be applied to roles. | |
* | |
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
*/ | |
public function roles() | |
{ | |
return $this->belongsToMany(Role::class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Role extends Model | |
{ | |
/** | |
* A role may be given various permissions. | |
* | |
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
*/ | |
public function permissions() | |
{ | |
return $this->belongsToMany(Permission::class); | |
} | |
/** | |
* Grant the given permission to a role. | |
* | |
* @param Permission $permission | |
* @return mixed | |
*/ | |
public function givePermissionTo(Permission $permission) | |
{ | |
return $this->permissions()->save($permission); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Auth\Authenticatable; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Auth\Passwords\CanResetPassword; | |
use Illuminate\Foundation\Auth\Access\Authorizable; | |
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; | |
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; | |
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; | |
class User extends Model implements AuthenticatableContract, | |
AuthorizableContract, | |
CanResetPasswordContract | |
{ | |
use Authenticatable, Authorizable, CanResetPassword, HasRoles; | |
/** | |
* The database table used by the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'users'; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = ['name', 'email', 'password']; | |
/** | |
* The attributes excluded from the model's JSON form. | |
* | |
* @var array | |
*/ | |
protected $hidden = ['password', 'remember_token']; | |
public function roles(){ | |
return $this->belongsToMany(Role::class); | |
} | |
public function assingRole($role){ | |
return $this->roles()->save( | |
Role::whereName($role)->firstOrFail() | |
); | |
} | |
public function hasRole($role){ | |
if(is_string($role)){ | |
return $this->roles->contains('name', $role); | |
} | |
return !! $role->intersect($this->roles)->count(); | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('/', function () { | |
Auth::loginUsingId(1); | |
return view('welcome'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Laravel</title> | |
<link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css"> | |
</head> | |
<body> | |
<h1>Hello World</h1> | |
<!-- Simulate various abilities. --> | |
@can('edit_forum') | |
<a href="#">Edit the Forum</a> | |
@endcan | |
@can('manage_money') | |
<a href="#">Manage the Funds</a> | |
@endcan | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment