Passed
Push — emailfix ( 0d6707...7fc2cb )
by Tristan
10:42
created

User::makeEmailsCaseInsensitive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Created by Reliese Model.
5
 * Date: Thu, 12 Jul 2018 22:39:28 +0000.
6
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
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
21
/**
22
 * Class User
23
 *
24
 * @property int $id
25
 * @property string $email
26
 * @property string $name
27
 * @property string $password
28
 * @property bool $is_confirmed
29
 * @property int $user_role_id
30
 * @property \Jenssegers\Date\Date $created_at
31
 * @property \Jenssegers\Date\Date $updated_at
32
 *
33
 * @property \App\Models\Applicant $applicant
34
 * @property \App\Models\Manager $manager
35
 * @property \App\Models\ProfilePic $profile_pic
36
 * @property \App\Models\UserRole $user_role
37
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
38
class User extends BaseModel implements
39
    // Laravel contracts for native login
40
        AuthenticatableContract,
41
        CanResetPasswordContract,
42
    // Contract for use with Gates and Policies
43
        AuthorizableContract
44
    // Custom contract for use with openid login
45
    //    \App\Services\Auth\Contracts\OidcAuthenticatable
46
{
47
48
    //Traits for Laravel basic authentication
49
    use Authenticatable, CanResetPassword;
50
    // Trait for working with Gates and Policies
51
    use Authorizable;
52
    // Trait for notifications
53
    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...
54
55
    protected $casts = [
56
        'is_confirmed' => 'bool',
57
        'user_role_id' => 'int'
58
    ];
59
    protected $fillable = [
60
        'name', 'email', 'password',
61
    ];
62
    protected $with = ['user_role'];
63
    protected $hidden = [
64
        'password', 'remember_token',
65
    ];
66
67
    /**
68
     * The event map for the model.
69
     *
70
     * @var array
71
     */
72
    protected $dispatchesEvents = [
73
        'created' => UserCreated::class,
74
        'updated' => UserUpdated::class,
75
    ];
76
77
    public function applicant() {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function applicant()
Loading history...
78
        return $this->hasOne(\App\Models\Applicant::class);
79
    }
80
81
    public function manager() {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function manager()
Loading history...
82
        return $this->hasOne(\App\Models\Manager::class);
83
    }
84
85
    public function profile_pic() {
0 ignored issues
show
Coding Style introduced by
Public method name "User::profile_pic" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function profile_pic()
Loading history...
86
        return $this->hasOne(\App\Models\ProfilePic::class);
87
    }
88
89
    public function user_role() {
0 ignored issues
show
Coding Style introduced by
Public method name "User::user_role" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function user_role()
Loading history...
90
        return $this->belongsTo(\App\Models\UserRole::class);
91
    }
92
93
    // Mutators
94
95
    // public function setEmailAttribute($value)
96
    // {
97
    //     $this->attributes['email'] = strtolower($value);
98
    // }
99
100
    // Accessors
101
102
    public function getEmailAttribute($value)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
103
    {
104
        return strtolower($value);
105
    }
106
107
    //Role related functions
108
109
    /**
110
    * Abort with an HTTP error if user doesn't have correct roles
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
111
    * @param string|array $roles
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
112
    */
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
Coding Style introduced by
Missing @return tag in function comment
Loading history...
113
    public function authorizeRoles($roles)
114
    {
115
      if (is_array($roles)) {
116
          return $this->hasAnyRole($roles) ||
117
                 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...
118
      }
119
      return $this->hasRole($roles) ||
120
             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...
121
    }
122
123
    /**
124
    * Check multiple roles
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
125
    * @param array $roles
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
126
    */
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
Coding Style introduced by
Missing @return tag in function comment
Loading history...
127
    public function hasAnyRole($roles)
128
    {
129
        return in_array($this->user_role->name, $roles);
130
        //return null !== $this->roles()->whereIn(‘name’, $roles)->first();
131
    }
132
133
    /**
134
    * Check one role
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
135
    * @param string $role
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
136
    */
0 ignored issues
show
Coding Style introduced by
Expected 5 space(s) before asterisk; 4 found
Loading history...
Coding Style introduced by
Missing @return tag in function comment
Loading history...
137
    public function hasRole($role)
138
    {
139
        return $this->user_role->name == $role;
140
        //return null !== $this->roles()->where(‘name’, $role)->first();
141
    }
142
143
    /**
144
     * OVERRIDE
145
     * Send the password reset notification.
146
     *
147
     * @param  string  $token
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
148
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
149
     */
150
    public function sendPasswordResetNotification($token)
151
    {
152
        $this->notify(new ResetPasswordNotification($token));
153
    }
154
155
}
156