Completed
Push — development ( 9515cf...3cb16f )
by Claudio
02:12
created

User::getLastLoginAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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