User::getCountryAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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