blessing-skin-server/app/Models/User.php

253 lines
5.8 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
2016-10-23 11:41:52 +08:00
use DB;
use Carbon\Carbon;
2019-02-27 23:44:50 +08:00
use Illuminate\Support\Arr;
2019-04-25 23:24:24 +08:00
use Laravel\Passport\HasApiTokens;
2016-11-05 20:11:48 +08:00
use App\Events\EncryptUserPassword;
2019-04-23 10:05:58 +08:00
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
2016-07-29 12:46:19 +08:00
2019-04-23 10:05:58 +08:00
class User extends Authenticatable implements JWTSubject
2016-07-21 22:01:57 +08:00
{
2019-04-25 23:24:24 +08:00
use HasApiTokens;
2016-07-21 22:01:57 +08:00
/**
2016-10-23 11:41:52 +08:00
* Permissions.
2016-07-21 22:01:57 +08:00
*/
const BANNED = -1;
const NORMAL = 0;
const ADMIN = 1;
2016-10-23 11:41:52 +08:00
const SUPER_ADMIN = 2;
2016-07-21 22:01:57 +08:00
2016-10-23 11:41:52 +08:00
/**
* Properties for Eloquent Model.
*/
public $primaryKey = 'uid';
public $timestamps = false;
2016-10-23 11:41:52 +08:00
protected $fillable = ['email', 'nickname', 'permission'];
2016-07-21 22:01:57 +08:00
2018-02-23 09:51:23 +08:00
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'uid' => 'integer',
'score' => 'integer',
'avatar' => 'integer',
'permission' => 'integer',
2018-08-17 12:32:44 +08:00
'verified' => 'bool',
2018-02-23 09:51:23 +08:00
];
2019-03-23 17:40:02 +08:00
protected $hidden = ['password', 'remember_token'];
/**
* Storage size used by user in KiB.
*
* @var int
*/
protected $storageUsed;
2016-07-21 22:01:57 +08:00
/**
2016-10-23 11:41:52 +08:00
* Check if user is admin.
2016-07-21 22:01:57 +08:00
*
2016-10-23 11:41:52 +08:00
* @return bool
2016-07-21 22:01:57 +08:00
*/
2016-10-23 11:41:52 +08:00
public function isAdmin()
2016-07-21 22:01:57 +08:00
{
return $this->permission >= static::ADMIN;
2016-10-23 11:41:52 +08:00
}
2016-10-17 17:51:51 +08:00
2019-03-14 23:55:49 +08:00
public function closet()
2016-10-23 11:41:52 +08:00
{
2019-03-14 23:55:49 +08:00
return $this->belongsToMany(Texture::class, 'user_closet')->withPivot('item_name');
2016-07-21 22:01:57 +08:00
}
2019-03-22 21:40:12 +08:00
/**
* Retrieve the player name of first player.
*/
public function getPlayerNameAttribute()
{
$player = $this->players->first();
2019-04-19 19:36:36 +08:00
2019-03-22 21:40:12 +08:00
return $player ? $player->name : '';
}
/**
* Update the player name of first player.
*/
public function setPlayerNameAttribute($value)
{
$player = $this->players->first();
if ($player) {
$player->name = $value;
$player->save();
}
}
2019-04-03 10:12:51 +08:00
public function verifyPassword($raw)
2016-07-21 22:01:57 +08:00
{
2018-02-16 17:31:04 +08:00
// Compare directly if any responses is returned by event dispatcher
2019-04-03 10:12:51 +08:00
if ($result = static::getEncryptedPwdFromEvent($raw, $this)) {
return hash_equals($this->password, $result); // @codeCoverageIgnore
}
2019-04-03 10:12:51 +08:00
return app('cipher')->verify($raw, $this->password, config('secure.salt'));
}
2016-10-17 17:51:51 +08:00
/**
* Try to get encrypted password from event dispatcher.
*
2019-04-03 10:12:51 +08:00
* @param string $raw
* @param User $user
* @return mixed
*/
2019-04-03 10:12:51 +08:00
public static function getEncryptedPwdFromEvent($raw, User $user)
{
2019-04-03 10:12:51 +08:00
$responses = event(new EncryptUserPassword($raw, $user));
2016-11-05 20:11:48 +08:00
2019-02-27 23:44:50 +08:00
return Arr::get($responses, 0);
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Change password of the user.
*
2018-07-19 10:33:28 +08:00
* @param string $password New password that will be set.
2016-10-23 11:41:52 +08:00
* @return bool
*/
2018-07-19 10:33:28 +08:00
public function changePassword($password)
2016-07-21 22:01:57 +08:00
{
2018-07-19 10:33:28 +08:00
$responses = event(new EncryptUserPassword($password, $this));
2016-11-05 20:11:48 +08:00
if (isset($responses[0])) {
$this->password = $responses[0]; // @codeCoverageIgnore
2016-11-05 20:11:48 +08:00
} else {
2018-07-19 10:33:28 +08:00
$this->password = app('cipher')->hash($password, config('secure.salt'));
2016-11-05 20:11:48 +08:00
}
2016-10-23 11:41:52 +08:00
return $this->save();
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Set user score.
*
2017-11-04 17:18:01 +08:00
* @param int $score
* @param string $mode What operation should be done, set, plus or minus.
* @return bool
2016-10-23 11:41:52 +08:00
*/
public function setScore($score, $mode = 'set')
2016-07-21 22:01:57 +08:00
{
switch ($mode) {
case 'set':
2016-10-23 11:41:52 +08:00
$this->score = $score;
2016-07-21 22:01:57 +08:00
break;
case 'plus':
2016-10-23 11:41:52 +08:00
$this->score += $score;
2016-07-21 22:01:57 +08:00
break;
case 'minus':
2016-10-23 11:41:52 +08:00
$this->score -= $score;
2016-07-21 22:01:57 +08:00
break;
}
2016-10-23 11:41:52 +08:00
return $this->save();
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Get the size of storage units used by the user.
*
* @return int Size in KiloBytes.
*/
2016-07-21 22:01:57 +08:00
public function getStorageUsed()
{
if (is_null($this->storageUsed)) {
$this->storageUsed = 0;
$result = DB::table('textures')
->select(DB::raw('SUM(size) AS total_size'))
->where('uploader', $this->uid)
->first()->total_size;
$this->storageUsed = $result ?: 0;
2016-07-21 22:01:57 +08:00
}
return $this->storageUsed;
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
2017-07-14 09:17:42 +08:00
* Sign for the user, return false if unavailable.
2016-10-23 11:41:52 +08:00
*
* @return int|bool
*/
2017-07-14 09:17:42 +08:00
public function sign()
2016-07-21 22:01:57 +08:00
{
2017-07-14 09:17:42 +08:00
if ($this->canSign()) {
$scoreLimits = explode(',', option('sign_score'));
$acquiredScore = rand($scoreLimits[0], $scoreLimits[1]);
$this->setScore($acquiredScore, 'plus');
2018-08-17 22:54:26 +08:00
$this->last_sign_at = get_datetime_string();
2016-10-23 11:41:52 +08:00
$this->save();
return $acquiredScore;
2016-07-21 22:01:57 +08:00
} else {
return false;
}
}
2016-10-23 11:41:52 +08:00
/**
* Get remaining time before next signing is available.
2016-10-23 11:41:52 +08:00
*
* @return int Time in seconds.
2016-10-23 11:41:52 +08:00
*/
2017-07-14 09:17:42 +08:00
public function getSignRemainingTime()
2016-07-21 22:01:57 +08:00
{
2019-03-23 00:20:28 +08:00
$lastSignTime = Carbon::parse($this->last_sign_at);
2017-01-18 12:56:37 +08:00
if (option('sign_after_zero')) {
return Carbon::now()->diffInSeconds(
2019-02-17 20:12:42 +08:00
$lastSignTime <= Carbon::today() ? $lastSignTime : Carbon::tomorrow(),
false
);
2017-01-18 12:56:37 +08:00
}
2019-02-17 09:11:57 +08:00
return Carbon::now()->diffInSeconds($lastSignTime->addHours(option('sign_gap_time')), false);
}
2017-07-14 09:17:42 +08:00
public function canSign()
{
return $this->getSignRemainingTime() <= 0;
2016-07-21 22:01:57 +08:00
}
public function delete()
{
2016-10-16 18:16:15 +08:00
Player::where('uid', $this->uid)->delete();
2016-10-23 11:41:52 +08:00
return parent::delete();
}
2016-07-22 19:36:24 +08:00
2016-10-23 11:41:52 +08:00
public function players()
{
return $this->hasMany('App\Models\Player', 'uid');
}
2016-07-23 15:20:10 +08:00
public function getAuthIdentifier()
{
return $this->uid;
}
2019-04-23 10:05:58 +08:00
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
2016-07-21 22:01:57 +08:00
}