|
1
|
|
|
<?php namespace Modules\User\Entities\Sentry; |
|
2
|
|
|
|
|
3
|
|
|
use Cartalyst\Sentry\Facades\Laravel\Sentry; |
|
4
|
|
|
use Cartalyst\Sentry\Users\Eloquent\User as SentryModel; |
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
|
6
|
|
|
use Laracasts\Presenter\PresentableTrait; |
|
7
|
|
|
use Modules\User\Entities\UserInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @property bool activated |
|
11
|
|
|
*/ |
|
12
|
|
|
class User extends SentryModel implements UserInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use PresentableTrait; |
|
15
|
|
|
|
|
16
|
|
|
protected $fillable = [ |
|
17
|
|
|
'email', |
|
18
|
|
|
'password', |
|
19
|
|
|
'permissions', |
|
20
|
|
|
'first_name', |
|
21
|
|
|
'last_name', |
|
22
|
|
|
'activated', |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
protected $presenter = 'Modules\User\Presenters\UserPresenter'; |
|
26
|
|
|
|
|
27
|
|
|
public function groups() |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->belongsToMany(static::$groupModel, static::$userGroupsPivot, 'user_id'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Checks if a user belongs to the given Role ID |
|
34
|
|
|
* @param int $roleId |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function hasRoleId($roleId) |
|
38
|
|
|
{ |
|
39
|
|
|
$role = Sentry::findGroupById($roleId); |
|
40
|
|
|
|
|
41
|
|
|
return $this->inGroup($role); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Checks if a user belongs to the given Role Name |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public function hasRoleName($name) |
|
50
|
|
|
{ |
|
51
|
|
|
$role = Sentry::findGroupByName($name); |
|
52
|
|
|
|
|
53
|
|
|
return $this->inGroup($role); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Check if the current user is activated |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function isActivated() |
|
61
|
|
|
{ |
|
62
|
|
|
return (bool) $this->activated; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function __call($method, $parameters) |
|
66
|
|
|
{ |
|
67
|
|
|
$class_name = class_basename($this); |
|
68
|
|
|
|
|
69
|
|
|
#i: Convert array to dot notation |
|
70
|
|
|
$config = implode('.', ['relations', $class_name, $method]); |
|
71
|
|
|
|
|
72
|
|
|
#i: Relation method resolver |
|
73
|
|
|
if (Config::has($config)) { |
|
74
|
|
|
$function = Config::get($config); |
|
75
|
|
|
|
|
76
|
|
|
return $function($this); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
#i: No relation found, return the call to parent (Eloquent) to handle it. |
|
80
|
|
|
return parent::__call($method, $parameters); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|