Completed
Push — 2.0 ( 369485...d81882 )
by Nicolas
15:25
created

User::hasAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Modules\User\Entities\Sentinel;
4
5
use Cartalyst\Sentinel\Laravel\Facades\Activation;
6
use Cartalyst\Sentinel\Users\EloquentUser;
7
use Laracasts\Presenter\PresentableTrait;
8
use Modules\User\Entities\UserInterface;
9
use Modules\User\Entities\UserToken;
10
use Modules\User\Presenters\UserPresenter;
11
12
class User extends EloquentUser implements UserInterface
13
{
14
    use PresentableTrait;
15
16
    protected $fillable = [
17
        'email',
18
        'password',
19
        'permissions',
20
        'first_name',
21
        'last_name',
22
    ];
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    protected $loginNames = ['email'];
28
29
    protected $presenter = UserPresenter::class;
30
31
    public function __construct(array $attributes = [])
32
    {
33
        $this->loginNames = config('asgard.user.users.login-columns');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('asgard.user.users.login-columns') of type * is incompatible with the declared type array of property $loginNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
        $this->fillable = config('asgard.user.users.fillable');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('asgard.user.users.fillable') of type * is incompatible with the declared type array of property $fillable.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
36
        parent::__construct($attributes);
37
    }
38
39
    /**
40
     * Checks if a user belongs to the given Role ID
41
     * @param  int $roleId
42
     * @return bool
43
     */
44
    public function hasRoleId($roleId)
45
    {
46
        return $this->roles()->whereId($roleId)->count() >= 1;
47
    }
48
49
    /**
50
     * Checks if a user belongs to the given Role Name
51
     * @param  string $name
52
     * @return bool
53
     */
54
    public function hasRoleName($name)
55
    {
56
        return $this->roles()->whereName($name)->count() >= 1;
57
    }
58
59
    /**
60
     * Check if the current user is activated
61
     * @return bool
62
     */
63
    public function isActivated()
64
    {
65
        if (Activation::completed($this)) {
66
            return true;
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
74
     */
75
    public function api_keys()
76
    {
77
        return $this->hasMany(UserToken::class);
78
    }
79
80
    /**
81
     * Get the first available api key
82
     * @return string
83
     */
84
    public function getFirstApiKey()
85
    {
86
        $userToken = $this->api_keys->first();
87
88
        if ($userToken === null) {
89
            return '';
90
        }
91
92
        return $userToken->access_token;
93
    }
94
95
    public function __call($method, $parameters)
96
    {
97
        $class_name = class_basename($this);
98
99
        #i: Convert array to dot notation
100
        $config = implode('.', ['relations', $class_name, $method]);
101
102
        #i: Relation method resolver
103
        if (config()->has($config)) {
104
            $function = config()->get($config);
105
106
            return $function($this);
107
        }
108
109
        #i: No relation found, return the call to parent (Eloquent) to handle it.
110
        return parent::__call($method, $parameters);
111
    }
112
113
    /**
114
     * Check if the user has access to the given permission name
115
     * @param string $permission
116
     * @return boolean
117
     */
118
    public function hasAccess($permission)
119
    {
120
        $permissions = $this->getPermissionsInstance();
121
122
        return $permissions->hasAccess($permission);
123
    }
124
}
125