Created
March 15, 2018 00:55
-
-
Save wichaksono/afa84b461285bf735ecaa207f4358463 to your computer and use it in GitHub Desktop.
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\Notifications\Notifiable; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
class User extends Authenticatable | |
{ | |
use Notifiable; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', 'email', 'password', | |
]; | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', 'remember_token', | |
]; | |
/* | |
* Method untuk yang mendefinisikan relasi antara model user dan model Role | |
*/ | |
public function roles() | |
{ | |
return $this->belongsToMany(Role::class); | |
} | |
/* | |
* Method untuk menambahkan role (hak akses) baru pada user | |
*/ | |
public function putRole($role) | |
{ | |
if (is_string($role)) | |
{ | |
$role = Role::whereRoleName($role)->first(); | |
} | |
return $this->roles()->attach($role); | |
} | |
/* | |
* Method untuk menghapus role (hak akses) pada user | |
*/ | |
public function forgetRole($role) | |
{ | |
if (is_string($role)) | |
{ | |
$role = Role::whereRoleName($role)->first(); | |
} | |
return $this->roles()->detach($role); | |
} | |
/* | |
* Method untuk mengecek apakah user yang sedang login punya hak akses untuk mengakses page sesuai rolenya | |
*/ | |
public function hasRole($roleName) | |
{ | |
foreach ($this->roles as $role) | |
{ | |
if ($role->role_name === $roleName) return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment