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

348 lines
7.4 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;
2016-11-05 20:11:48 +08:00
use App\Events\EncryptUserPassword;
use Illuminate\Foundation\Auth\User as Authenticatable;
2016-07-29 12:46:19 +08:00
class User extends Authenticatable
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
];
/**
* 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
}
2016-10-23 11:41:52 +08:00
/**
* Check if given password is correct.
*
* @param string $rawPasswd
2016-10-23 11:41:52 +08:00
* @return bool
*/
public function verifyPassword($rawPasswd)
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
if ($result = static::getEncryptedPwdFromEvent($rawPasswd, $this)) {
return hash_equals($this->password, $result); // @codeCoverageIgnore
}
return app('cipher')->verify($rawPasswd, $this->password, config('secure.salt'));
}
2016-10-17 17:51:51 +08:00
/**
* Try to get encrypted password from event dispatcher.
*
* @param string $rawPasswd
* @param User $user
* @return mixed
*/
public static function getEncryptedPwdFromEvent($rawPasswd, User $user)
{
$responses = event(new EncryptUserPassword($rawPasswd, $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
/**
* Get user permission.
*
* @return int
*/
2016-07-23 15:20:10 +08:00
public function getPermission()
{
2016-10-23 11:41:52 +08:00
return $this->permission;
2016-07-23 15:20:10 +08:00
}
/**
2016-10-23 11:41:52 +08:00
* Set user permission.
*
* @param int $permission
* @return bool
2016-07-23 15:20:10 +08:00
*/
public function setPermission($permission)
{
2016-10-23 11:41:52 +08:00
return $this->update(['permission' => $permission]);
2016-07-23 15:20:10 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Set new email for user.
*
2017-11-18 13:25:08 +08:00
* @param string $new_email
* @return bool
2016-10-23 11:41:52 +08:00
*/
2016-07-21 22:01:57 +08:00
public function setEmail($new_email)
{
2016-10-23 11:41:52 +08:00
$this->email = $new_email;
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
/**
* Return Email if nickname is not set.
*
* @return string
*/
2016-07-21 22:01:57 +08:00
public function getNickName()
{
2018-02-16 17:31:04 +08:00
if (! $this->uid) {
2016-09-24 22:49:20 +08:00
return trans('general.unexistent-user');
2016-07-24 11:12:52 +08:00
} else {
return ($this->nickname == '') ? $this->email : $this->nickname;
2016-07-24 11:12:52 +08:00
}
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Set nickname for the user.
*
2018-02-16 17:31:04 +08:00
* @param string $newNickName
2016-10-23 11:41:52 +08:00
* @return bool
*/
2018-02-16 17:31:04 +08:00
public function setNickName($newNickName)
2016-07-21 22:01:57 +08:00
{
2018-02-16 17:31:04 +08:00
$this->nickname = $newNickName;
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 current score of user.
*
* @return int
*/
2016-07-21 22:01:57 +08:00
public function getScore()
{
2016-10-23 11:41:52 +08:00
return $this->score;
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
{
2017-07-14 09:17:42 +08:00
$lastSignTime = Carbon::parse($this->getLastSignTime());
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);
}
/**
* Check if signing in is available now.
*
* @return bool
*/
2017-07-14 09:17:42 +08:00
public function canSign()
{
return $this->getSignRemainingTime() <= 0;
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Get the last time of signing in.
2016-10-23 11:41:52 +08:00
*
* @return string Formatted time string.
*/
2017-07-14 09:17:42 +08:00
public function getLastSignTime()
2016-07-21 22:01:57 +08:00
{
2016-10-23 11:41:52 +08:00
return $this->last_sign_at;
2016-07-21 22:01:57 +08:00
}
/**
2016-10-23 11:41:52 +08:00
* Get the texture id of user's avatar.
*
* @return int
2016-07-21 22:01:57 +08:00
*/
public function getAvatarId()
{
2016-10-23 11:41:52 +08:00
return $this->avatar;
}
2016-10-23 11:41:52 +08:00
/**
* Set user avatar.
*
* @param int $tid
* @return bool
*/
2016-07-21 22:01:57 +08:00
public function setAvatar($tid)
{
2016-10-23 11:41:52 +08:00
$this->avatar = $tid;
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
/**
* Delete the user.
*
* @return bool
*/
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
/**
* Get the players which are owned by the user.
*
* @return Illuminate\Database\Eloquent\Collection
*/
public function players()
{
return $this->hasMany('App\Models\Player', 'uid');
}
2016-07-23 15:20:10 +08:00
2016-10-23 11:41:52 +08:00
/**
* Expand like scope for Eloquent Model.
*/
2016-07-22 19:36:24 +08:00
public function scopeLike($query, $field, $value)
{
return $query->where($field, 'LIKE', "%$value%");
}
public function getAuthIdentifier()
{
return $this->uid;
}
2016-07-21 22:01:57 +08:00
}