Passed
Push — dev ( 35d96c...d805ad )
by Tristan
16:59 queued 08:51
created

User::authorizeRoles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
/**
4
 * Created by Reliese Model.
5
 * Date: Thu, 12 Jul 2018 22:39:28 +0000.
6
 */
7
8
namespace App\Models;
9
10
use Illuminate\Auth\Authenticatable;
11
use Illuminate\Auth\Passwords\CanResetPassword;
12
use Illuminate\Foundation\Auth\Access\Authorizable;
13
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
14
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
15
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
16
use Illuminate\Notifications\Notifiable;
17
use App\Events\UserCreated;
18
use App\Events\UserUpdated;
19
use App\Notifications\ResetPasswordNotification;
20
use App\CRUD\TalentCloudCrudTrait as CrudTrait;
21
22
/**
23
 * Class User
24
 *
25
 * @property int $id
26
 * @property string $email
27
 * @property string $name
28
 * @property string $password
29
 * @property boolean $is_confirmed
30
 * @property boolean $is_priority
31
 * @property int $user_role_id
32
 * @property \Jenssegers\Date\Date $created_at
33
 * @property \Jenssegers\Date\Date $updated_at
34
 *
35
 * @property \App\Models\Applicant $applicant
36
 * @property \App\Models\Manager $manager
37
 * @property \App\Models\ProfilePic $profile_pic
38
 * @property \App\Models\UserRole $user_role
39
 */
40
class User extends BaseModel implements
41
    // Laravel contracts for native login
42
    AuthenticatableContract,
43
    CanResetPasswordContract,
44
    // Contract for use with Gates and Policies
45
    AuthorizableContract
46
    // Custom contract for use with openid login
47
    // \App\Services\Auth\Contracts\OidcAuthenticatable
48
{
49
50
    //Traits for Laravel basic authentication
51
    use Authenticatable, CanResetPassword;
52
    // Trait for working with Gates and Policies
53
    use Authorizable;
54
    // Trait for notifications
55
    use Notifiable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $phone_number which is not provided by App\Models\User.
Loading history...
56
    // Trait for Backpack
57
    use CrudTrait;
58
59
    protected $casts = [
1 ignored issue
show
introduced by
Property \App\Models\User::$casts does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
60
        'is_confirmed' => 'boolean',
61
        'is_priority' => 'boolean',
62
        'user_role_id' => 'int',
63
        'email' => 'string',
64
    ];
65
    protected $fillable = [
1 ignored issue
show
introduced by
Property \App\Models\User::$fillable does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
66
        'name', 'email', 'password', 'is_priority'
67
    ];
68
    protected $with = ['user_role'];
1 ignored issue
show
introduced by
Property \App\Models\User::$with does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
69
    protected $hidden = [
1 ignored issue
show
introduced by
Property \App\Models\User::$hidden does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
70
        'password', 'remember_token',
71
    ];
72
73
    /**
74
     * The event map for the model.
75
     *
76
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \App\Models\User::$dispatchesEvents does not specify type hint for its items.
Loading history...
77
     */
78
    protected $dispatchesEvents = [
79
        'created' => UserCreated::class,
80
        'updated' => UserUpdated::class,
81
    ];
82
83 5
    public function applicant() {
1 ignored issue
show
introduced by
Method \App\Models\User::applicant() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function applicant()
Loading history...
84 5
        return $this->hasOne(\App\Models\Applicant::class);
85
    }
86
87 13
    public function manager() {
1 ignored issue
show
introduced by
Method \App\Models\User::manager() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function manager()
Loading history...
88 13
        return $this->hasOne(\App\Models\Manager::class);
89
    }
90
91
    public function profile_pic() {
1 ignored issue
show
Coding Style introduced by
Method name "User::profile_pic" is not in camel caps format
Loading history...
introduced by
Method \App\Models\User::profile_pic() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function profile_pic()
Loading history...
92
        return $this->hasOne(\App\Models\ProfilePic::class);
93
    }
94
95 38
    public function user_role() {
1 ignored issue
show
Coding Style introduced by
Method name "User::user_role" is not in camel caps format
Loading history...
introduced by
Method \App\Models\User::user_role() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function user_role()
Loading history...
96 38
        return $this->belongsTo(\App\Models\UserRole::class);
97
    }
98
99
    //Role related functions
100
101
    /**
102
    * Abort with an HTTP error if user doesn't have correct roles
103
    * @param string|array $roles
1 ignored issue
show
introduced by
@param annotation of method \App\Models\User::authorizeRoles() does not specify type hint for items of its traversable parameter $roles.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
104
    */
1 ignored issue
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
105
    public function authorizeRoles($roles)
0 ignored issues
show
introduced by
Method \App\Models\User::authorizeRoles() does not have return type hint nor @return annotation for its return value.
Loading history...
106
    {
107
      if (is_array($roles)) {
108
          return $this->hasAnyRole($roles) ||
109
                 abort(401, 'This action is unauthorized.');
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort(401, 'This action is unauthorized.') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
      }
111
      return $this->hasRole($roles) ||
112
             abort(401, 'This action is unauthorized.');
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort(401, 'This action is unauthorized.') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
113
    }
114
115
    /**
116
    * Check multiple roles
117
    * @param array $roles
1 ignored issue
show
introduced by
@param annotation of method \App\Models\User::hasAnyRole() does not specify type hint for items of its traversable parameter $roles.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
118
    */
1 ignored issue
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
119
    public function hasAnyRole($roles)
1 ignored issue
show
introduced by
Method \App\Models\User::hasAnyRole() does not have parameter type hint for its parameter $roles but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \App\Models\User::hasAnyRole() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style introduced by
Type hint "array" missing for $roles
Loading history...
120
    {
121
        return in_array($this->user_role->name, $roles);
122
        //return null !== $this->roles()->whereIn(‘name’, $roles)->first();
123
    }
124
125
    /**
126
    * Check one role
127
    * @param string $role
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
128
    */
1 ignored issue
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
129 13
    public function hasRole($role)
1 ignored issue
show
introduced by
Method \App\Models\User::hasRole() does not have parameter type hint for its parameter $role but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \App\Models\User::hasRole() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style introduced by
Type hint "string" missing for $role
Loading history...
130
    {
131 13
        return $this->user_role->name == $role;
132
        //return null !== $this->roles()->where(‘name’, $role)->first();
133
    }
134
135
    /**
136
     * OVERRIDE
137
     * Send the password reset notification.
138
     *
139
     * @param  string  $token
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
140
     *
141
     * @return void
142
     */
143
    public function sendPasswordResetNotification($token)
1 ignored issue
show
introduced by
Method \App\Models\User::sendPasswordResetNotification() does not have parameter type hint for its parameter $token but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \App\Models\User::sendPasswordResetNotification() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
Coding Style introduced by
Type hint "string" missing for $token
Loading history...
144
    {
145
        $this->notify(new ResetPasswordNotification($token));
146
    }
147
}
148