Completed
Push — development ( f57bd7...a7d33c )
by Claudio
04:40
created

User::getIsStaffAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Auth\Authenticatable;
6
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
7
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Facades\Config;
10
use Laravel\Lumen\Auth\Authorizable;
11
use Sofa\Eloquence\Eloquence;
12
use Sofa\Eloquence\Mappable;
13
14
/**
15
 * Class User.
16
 *
17
 * @property string trusted
18
 * @property int uniqueId
19
 * @property string figureString
20
 * @property string name
21
 */
22
class User extends Model implements AuthenticatableContract, AuthorizableContract
23
{
24
    use Authenticatable, Authorizable, Eloquence, Mappable;
25
26
    /**
27
     * Disable Timestamps.
28
     *
29
     * @var bool
30
     */
31
    public $timestamps = false;
32
33
    /**
34
     * User Traits.
35
     *
36
     * @var array
37
     */
38
    public $traits = ['USER'];
39
40
    /**
41
     * The table associated with the model.
42
     *
43
     * @var string
44
     */
45
    protected $table = 'users';
46
47
    /**
48
     * Primary Key of the Table.
49
     *
50
     * @var string
51
     */
52
    protected $primaryKey = 'id';
53
54
    /**
55
     * The attributes that will be mapped.
56
     *
57
     * @var array
58
     */
59
    protected $maps = [
60
        'uniqueId' => 'id',
61
        'name' => 'username',
62
        'figureString' => 'look',
63
        'lastWebAccess' => 'last_login',
64
        'creationTime' => 'account_created',
65
        'email' => 'mail',
66
        'identityId' => 'id',
67
        'accountId' => 'id',
68
    ];
69
70
    /**
71
     * The Appender(s) of the Model.
72
     *
73
     * @var array
74
     */
75
    protected $appends = [
76
        'habboClubMember',
77
        'buildersClubMember',
78
        'sessionLoginId',
79
        'loginLogId',
80
        'identityVerified',
81
        'identityType',
82
        'trusted',
83
        'country',
84
        'traits',
85
        'uniqueId',
86
        'name',
87
        'figureString',
88
        'lastWebAccess',
89
        'creationTime',
90
        'email',
91
        'identityId',
92
        'emailVerified',
93
        'accountId',
94
        'memberSince',
95
        'isBanned',
96
        'banDetails',
97
        'isStaff'
98
    ];
99
100
    /**
101
     * The attributes that are mass assignable.
102
     *
103
     * @var array
104
     */
105
    protected $fillable = [
106
        'mail',
107
        'id',
108
        'username',
109
        'auth_ticket',
110
        'last_login',
111
        'ip_current',
112
        'ip_register',
113
        'mail_verified',
114
        'account_day_of_birth',
115
        'real_name',
116
        'look',
117
        'gender',
118
        'credits',
119
        'pixels',
120
        'home_room',
121
    ];
122
123
    /**
124
     * The attributes excluded from the model's JSON form.
125
     *
126
     * @var array
127
     */
128
    protected $hidden = [
129
        'id',
130
        'username',
131
        'mail',
132
        'account_created',
133
        'password',
134
        'mail_verified',
135
        'real_name',
136
        'account_day_of_birth',
137
        'last_online',
138
        'last_login',
139
        'ip_register',
140
        'auth_ticket',
141
        'home_room',
142
        'points',
143
        'look',
144
        'ip_current',
145
        'online',
146
        'pixels',
147
        'credits',
148
        'gender',
149
        'points',
150
        'rank',
151
    ];
152
153
    /**
154
     * The attributes that should be casted to native types.
155
     *
156
     * @var array
157
     */
158
    protected $casts = [
159
        'traits' => 'string',
160
    ];
161
162
    /**
163
     * Store an User on the Database.
164
     *
165
     * @param string $username
166
     * @param string $password
167
     * @param string $email
168
     * @param string $address
169
     * @param bool $newUser
170
     *
171
     * @return User
172
     */
173
    public function store(string $username, string $password, string $email, string $address = '', bool $newUser = true): User
174
    {
175
        $this->attributes['username'] = $username;
176
        $this->attributes['mail'] = $email;
177
178
        $this->attributes['motto'] = Config::get('chocolatey.motto');
179
        $this->attributes['look'] = Config::get('chocolatey.figure');
180
        $this->attributes['auth_ticket'] = '';
181
182
        $this->attributes['password'] = hash(Config::get('chocolatey.security.hash'), $password);
183
        $this->attributes['account_created'] = time();
184
185
        $this->attributes['ip_current'] = $address;
186
187
        $this->traits = $newUser ? ['NEW_USER', 'USER'] : ['USER'];
188
189
        $this->save();
190
        $this->createData();
191
192
        return $this;
193
    }
194
195
    /**
196
     * Store an User Alias Set on Database.
197
     */
198
    public function createData()
199
    {
200
        (new ChocolateyId())->store($this->attributes['id'], $this->attributes['mail'])->save();
201
202
        (new UserPreferences())->store($this->attributes['id'])->save();
203
    }
204
205
    /**
206
     * Get Is User is Banned.
207
     *
208
     * @return bool
209
     */
210
    public function getIsBannedAttribute(): bool
211
    {
212
        return Ban::where('user_id', $this->attributes['id'])->first() !== null;
213
    }
214
215
    /**
216
     * Check if Is Staff
217
     *
218
     * @return bool
219
     */
220
    public function getIsStaffAttribute(): bool
221
    {
222
        return array_key_exists('rank', $this->attributes) && $this->attributes['rank'] >= 5;
223
    }
224
225
    /**
226
     * Get Ban Details.
227
     *
228
     * @return Ban
229
     */
230
    public function getBanDetailsAttribute()
231
    {
232
        return Ban::where('user_id', $this->attributes['id'])->first();
233
    }
234
235
    /**
236
     * Get Current User Country.
237
     *
238
     * @TODO: Implement this in a proper way
239
     *
240
     * @return string
241
     */
242
    public function getCountryAttribute(): string
243
    {
244
        return 'com';
245
    }
246
247
    /**
248
     * Set the Trait Attribute.
249
     *
250
     * @param array $accountType
251
     */
252
    public function setTraitsAttribute(array $accountType)
253
    {
254
        $this->traits = $accountType;
255
    }
256
257
    /**
258
     * What is this field?
259
     *
260
     * @return array
261
     */
262
    public function getTraitsAttribute(): array
263
    {
264
        if (array_key_exists('rank', $this->attributes) && $this->attributes['rank'] >= 6) {
265
            return ['STAFF'];
266
        }
267
268
        return $this->traits;
269
    }
270
271
    /**
272
     * We don't care about this?
273
     *
274
     * @return bool
275
     */
276
    public function getTrustedAttribute(): bool
277
    {
278
        if (UserSecurity::find($this->attributes['id']) == null) {
279
            return true;
280
        }
281
282
        return in_array($this->attributes['ip_current'],
283
            UserSecurity::find($this->attributes['id'])->trustedDevices);
284
    }
285
286
    /**
287
     * What is this field?
288
     *
289
     * @return string
290
     */
291
    public function getIdentityTypeAttribute(): string
292
    {
293
        return 'HABBO';
294
    }
295
296
    /**
297
     * We don't care about this, every user is trusted.
298
     *
299
     * @return bool
300
     */
301
    public function getIdentityVerifiedAttribute(): bool
302
    {
303
        return true;
304
    }
305
306
    /**
307
     * We don't care about this.
308
     *
309
     * @return int
310
     */
311
    public function getLoginLogIdAttribute(): int
312
    {
313
        return 1;
314
    }
315
316
    /**
317
     * We don't care about this.
318
     *
319
     * @return int
320
     */
321
    public function getSessionLoginIdAttribute(): int
322
    {
323
        return 1;
324
    }
325
326
    /**
327
     * Get the HabboClub Attribute
328
     * In a Retro Habbo everyone is HC, yeah?
329
     *
330
     * @WARNING: This is used for Advertisement
331
     *
332
     * @return bool
333
     */
334
    public function getHabboClubMemberAttribute(): bool
335
    {
336
        return Config::get('chocolatey.ads.enabled') == false;
337
    }
338
339
    /**
340
     * Get the Builders Club Attribute
341
     * In a Retro Habbo everyone is BC, yeah?
342
     *
343
     * @WARNING: This is used for Advertisement
344
     *
345
     * @return bool
346
     */
347
    public function getBuildersClubMemberAttribute(): bool
348
    {
349
        return Config::get('chocolatey.ads.enabled') == false;
350
    }
351
352
    /**
353
     * Get GTimestamp in Habbo UserCurrency.
354
     *
355
     * @return string
356
     */
357
    public function getAccountCreatedAttribute(): string
358
    {
359
        $accountCreated = $this->attributes['account_created'] ?? time();
360
361
        return date('Y-m-d', $accountCreated) . 'T' . date('H:i:s.ZZZZ+ZZZZ', $accountCreated);
362
    }
363
364
    /**
365
     * Get GTimestamp in Habbo UserCurrency.
366
     *
367
     * @return string
368
     */
369
    public function getMemberSinceAttribute(): string
370
    {
371
        $accountCreated = $this->attributes['account_created'] ?? time();
372
373
        return date('Y-m-d', $accountCreated) . 'T' . date('H:i:s.ZZZZ+ZZZZ', $accountCreated);
374
    }
375
376
    /**
377
     * Retrieve User Figure String.
378
     *
379
     * @return string
380
     */
381
    public function getFigureStringAttribute(): string
382
    {
383
        return $this->attributes['look'] ?? 'hr-115-42.hd-195-19.ch-3030-82.lg-275-1408.fa-1201.ca-1804-64';
384
    }
385
386
    /**
387
     * Get GTimestamp in Habbo UserCurrency.
388
     *
389
     * @return false|string
390
     */
391
    public function getLastLoginAttribute(): string
392
    {
393
        $lastLogin = $this->attributes['last_login'] ?? time();
394
395
        return date('Y-m-d', $lastLogin) . 'T' . date('H:i:s.ZZZZ+ZZZZ', $lastLogin);
396
    }
397
398
    /**
399
     * Get E-Mail Verified Attribute.
400
     *
401
     * @return bool
402
     */
403
    public function getEmailVerifiedAttribute(): bool
404
    {
405
        return array_key_exists('mail_verified', $this->attributes)
406
            ? $this->attributes['mail_verified'] == true : false;
407
    }
408
}
409