5 use Illuminate\Auth\Authenticatable;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Auth\Passwords\CanResetPassword;
8 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
9 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
11 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
13 use Authenticatable, CanResetPassword;
16 * The database table used by the model.
20 protected $table = 'users';
23 * The attributes that are mass assignable.
27 protected $fillable = ['name', 'email', 'password'];
30 * The attributes excluded from the model's JSON form.
34 protected $hidden = ['password', 'remember_token'];
37 * Returns a default guest user.
39 public static function getDefault()
48 * Permissions and roles
52 * The roles that belong to the user.
54 public function roles()
56 return $this->belongsToMany('Oxbow\Role');
59 public function getRoleAttribute()
61 return $this->roles()->first();
65 * Check if the user has a particular permission.
66 * @param $permissionName
69 public function can($permissionName)
71 if($this->email == 'guest') {
74 $permissions = $this->role->permissions()->get();
75 $permissionSearch = $permissions->search(function ($item, $key) use ($permissionName) {
76 return $item->name == $permissionName;
78 return $permissionSearch !== false;
82 * Attach a role to this user.
85 public function attachRole(Role $role)
87 $this->attachRoleId($role->id);
91 * Attach a role id to this user.
94 public function attachRoleId($id)
96 $this->roles()->sync([$id]);
100 * Returns the user's avatar,
101 * Uses Gravatar as the avatar service.
106 public function getAvatar($size = 50)
108 $emailHash = md5(strtolower(trim($this->email)));
109 return '//www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';