Passed
Pull Request — 1.2 (#558)
by
unknown
09:10
created

User::stopImpersonating()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use A17\Twill\Models\Behaviors\HasMedias;
6
use A17\Twill\Models\Behaviors\HasPresenter;
7
use A17\Twill\Models\Behaviors\HasOauth;
8
use A17\Twill\Models\Enums\UserRole;
9
use A17\Twill\Notifications\Reset as ResetNotification;
10
use A17\Twill\Notifications\Welcome as WelcomeNotification;
11
use Illuminate\Auth\Authenticatable;
12
use Illuminate\Database\Eloquent\SoftDeletes;
13
use Illuminate\Foundation\Auth\Access\Authorizable;
14
use Illuminate\Foundation\Auth\User as AuthenticatableContract;
15
use Illuminate\Notifications\Notifiable;
16
use Illuminate\Support\Facades\Session;
17
use PragmaRX\Google2FAQRCode\Google2FA;
18
19
class User extends AuthenticatableContract
20
{
21
    use Authenticatable, Authorizable, HasMedias, Notifiable, HasPresenter, HasOauth, SoftDeletes;
0 ignored issues
show
introduced by
The trait A17\Twill\Models\Behaviors\HasMedias requires some properties which are not provided by A17\Twill\Models\User: $medias, $role, $pivot, $metadatas, $uuid, $height, $lqip_data, $crop_h, $locale, $video, $crop, $crop_w, $width
Loading history...
Bug introduced by
The trait Illuminate\Auth\Authenticatable requires the property $password which is not provided by A17\Twill\Models\User.
Loading history...
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $email which is not provided by A17\Twill\Models\User.
Loading history...
introduced by
The trait A17\Twill\Models\Behaviors\HasOauth requires some properties which are not provided by A17\Twill\Models\User: $token, $avatar
Loading history...
22
23
    public $timestamps = true;
24
25
    protected $fillable = [
26
        'email',
27
        'name',
28
        'role',
29
        'published',
30
        'title',
31
        'description',
32
        'google_2fa_enabled',
33
        'google_2fa_secret',
34
    ];
35
36
    protected $dates = [
37
        'deleted_at',
38
    ];
39
40
    protected $hidden = ['password', 'remember_token', 'google_2fa_secret'];
41
    public $checkboxes = ['published'];
42
43
    public $mediasParams = [
44
        'profile' => [
45
            'default' => [
46
                [
47
                    'name' => 'default',
48
                    'ratio' => 1,
49
                ],
50
            ],
51
        ],
52
    ];
53
54
    protected $casts = ['published' => 'boolean'];
55
56 55
    public function __construct(array $attributes = [])
57
    {
58 55
        $this->table = config('twill.users_table', 'twill_users');
59
60 55
        parent::__construct($attributes);
61 55
    }
62
63
    public function getTitleInBrowserAttribute()
64
    {
65
        return $this->name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
    }
67
68
    public function getRoleValueAttribute()
69
    {
70
        if (!empty($this->role)) {
0 ignored issues
show
Bug Best Practice introduced by
The property role does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
71
            if ($this->role == 'SUPERADMIN') {
72
                return "SUPERADMIN";
73
            }
74
75
            return UserRole::{$this->role}()->getValue();
76
        }
77
78
        return null;
79
    }
80
81
    public function getCanDeleteAttribute()
82
    {
83
        return auth('twill_users')->user()->id !== $this->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
84
    }
85
86 1
    public function scopePublished($query)
87
    {
88 1
        return $query->wherePublished(true);
89
    }
90
91 1
    public function scopeDraft($query)
92
    {
93 1
        return $query->wherePublished(false);
94
    }
95
96
    public function scopeOnlyTrashed($query)
97
    {
98
        return $query->whereNotNull('deleted_at');
99
    }
100
101 2
    public function setImpersonating($id)
102
    {
103 2
        Session::put('impersonate', $id);
104 2
    }
105
106 1
    public function stopImpersonating()
107
    {
108 1
        Session::forget('impersonate');
109 1
    }
110
111
    public function isImpersonating()
112
    {
113
        return Session::has('impersonate');
114
    }
115
116 2
    public function notifyWithCustomMarkdownTheme($instance)
117
    {
118 2
        $hostAppMailConfig = config('mail.markdown.paths');
119
120 2
        config([
121 2
            'mail.markdown.paths' => array_merge(
122 2
                [__DIR__ . '/../../views/emails'],
123
                $hostAppMailConfig
124
            ),
125
        ]);
126
127 2
        $this->notify($instance);
128
129 2
        config([
130 2
            'mail.markdown.paths' => $hostAppMailConfig,
131
        ]);
132
133 2
    }
134
135
    public function sendWelcomeNotification($token)
136
    {
137
        $this->notifyWithCustomMarkdownTheme(new WelcomeNotification($token));
138
    }
139
140 2
    public function sendPasswordResetNotification($token)
141
    {
142 2
        $this->notifyWithCustomMarkdownTheme(new ResetNotification($token));
143 2
    }
144
145 46
    public function isSuperAdmin()
146
    {
147 46
        return $this->role === 'SUPERADMIN';
0 ignored issues
show
Bug Best Practice introduced by
The property role does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
148
    }
149
150 46
    public function isPublished()
151
    {
152 46
        return (bool) $this->published;
0 ignored issues
show
Bug Best Practice introduced by
The property published does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
153
    }
154
155 2
    public function setGoogle2faSecretAttribute($secret)
156
    {
157 2
        $this->attributes['google_2fa_secret'] = filled($secret) ? \Crypt::encrypt($secret) : null;
158 2
    }
159
160 45
    public function getGoogle2faSecretAttribute($secret)
161
    {
162 45
        return filled($secret) ? \Crypt::decrypt($secret) : null;
163
    }
164
165 2
    public function generate2faSecretKey()
166
    {
167 2
        if (is_null($this->google_2fa_secret)) {
168 2
            $secret = (new Google2FA())->generateSecretKey();
169
170 2
            $this->google_2fa_secret = $secret;
0 ignored issues
show
Bug introduced by
The property google_2fa_secret does not seem to exist on A17\Twill\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
171
172 2
            $this->save();
173
        }
174 2
    }
175
176 1
    public function get2faQrCode()
177
    {
178 1
        return (new Google2FA())->getQRCodeInline(
179 1
            config('app.name'),
180 1
            $this->email,
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $this->email can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $holder of PragmaRX\Google2FAQRCode...e2FA::getQRCodeInline() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

180
            /** @scrutinizer ignore-type */ $this->email,
Loading history...
181 1
            $this->google_2fa_secret,
182 1
            200
183
        );
184
    }
185
}
186