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

394 lines
8.6 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;
2016-07-29 12:46:19 +08:00
use Utils;
use Carbon\Carbon;
2016-11-05 20:11:48 +08:00
use App\Events\EncryptUserPassword;
2016-10-23 11:41:52 +08:00
use Illuminate\Database\Eloquent\Model;
2016-07-29 12:46:19 +08:00
2016-10-23 11:41:52 +08:00
class User extends Model
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
*/
2016-10-23 11:41:52 +08:00
const BANNED = -1;
const NORMAL = 0;
const ADMIN = 1;
const SUPER_ADMIN = 2;
2016-07-21 22:01:57 +08:00
/**
2016-10-23 11:41:52 +08:00
* User Token.
* @var string
2016-07-21 22:01:57 +08:00
*/
2016-10-23 11:41:52 +08:00
private $token;
2016-07-21 22:01:57 +08:00
/**
2016-10-23 11:41:52 +08:00
* Instance of Closet.
* @var App\Models\Closet
2016-07-21 22:01:57 +08:00
*/
2016-10-23 11:41:52 +08:00
private $closet;
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;
protected $fillable = ['email', 'nickname', 'permission'];
2016-07-21 22:01:57 +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
{
2016-10-23 11:41:52 +08:00
return ($this->permission >= static::ADMIN);
}
2016-10-17 17:51:51 +08:00
2016-10-23 11:41:52 +08:00
/**
* Get closet instance.
*
* @return App\Models\Closet
*/
public function getCloset()
{
2018-02-16 17:31:04 +08:00
if (! $this->closet) {
2016-10-23 11:41:52 +08:00
$this->closet = new Closet($this->uid);
2016-08-16 13:27:06 +08:00
}
2016-07-21 22:01:57 +08:00
2016-10-23 11:41:52 +08:00
return $this->closet;
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
*/
protected static function getEncryptedPwdFromEvent($rawPasswd, User $user)
{
$responses = event(new EncryptUserPassword($rawPasswd, $user));
2016-11-05 20:11:48 +08:00
2017-01-18 22:57:15 +08:00
return array_get($responses, 0);
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Register a new user.
*
* @param string $email
* @param string $password
* @param \Closure $callback
* @return User|bool
*/
public static function register($email, $password, \Closure $callback) {
$user = static::firstOrNew(['email' => $email]);
2018-02-16 17:31:04 +08:00
// If the email is already registered
if ($user->uid) return false;
2018-02-16 17:31:04 +08:00
// Pass the user instance to the callback
call_user_func($callback, $user);
2016-10-23 11:41:52 +08:00
2018-02-22 21:26:23 +08:00
// Save once to get uid
$user->password = '';
$user->save();
2018-02-16 17:31:04 +08:00
// Save again with password
$user->password = static::getEncryptedPwdFromEvent($password, $user) ?: app('cipher')->hash($password, config('secure.salt'));
2016-10-23 11:41:52 +08:00
$user->save();
return $user;
}
/**
* Change password of the user.
*
* @param string $new_passwd New password that will be set.
* @return bool
*/
2016-07-21 22:01:57 +08:00
public function changePasswd($new_passwd)
{
2016-11-05 20:11:48 +08:00
$responses = event(new EncryptUserPassword($new_passwd, $this));
if (isset($responses[0])) {
$this->password = $responses[0]; // @codeCoverageIgnore
2016-11-05 20:11:48 +08:00
} else {
$this->password = app('cipher')->hash($new_passwd, 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;
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 {
2016-10-23 11:41:52 +08:00
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 user token or generate one.
*
* @param bool $refresh Refresh token forcely.
* @return string
*/
2016-08-19 23:09:32 +08:00
public function getToken($refresh = false)
2016-07-21 22:01:57 +08:00
{
2018-02-16 17:31:04 +08:00
if (! $this->token || $refresh) {
2016-10-23 11:41:52 +08:00
$this->token = md5($this->email . $this->password . config('secure.salt'));
}
2016-07-21 22:01:57 +08:00
return $this->token;
}
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
*/
2016-07-21 22:01:57 +08:00
public function setScore($score, $mode = "set")
{
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');
2016-10-23 11:41:52 +08:00
$this->last_sign_at = Utils::getTimeFormatted();
$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
{
2018-02-16 17:31:04 +08:00
// Convert to timestamp
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(
2017-07-14 09:17:42 +08:00
(($lastSignTime <= Carbon::today()) ? $lastSignTime : Carbon::tomorrow())
2017-01-18 12:56:37 +08:00
, false);
}
2017-07-14 09:17:42 +08:00
return Carbon::now()->diffInSeconds($lastSignTime->addSeconds(option('sign_gap_time') * 3600), false);
}
/**
* Check if signing in is available now.
*
* @return bool
*/
2017-07-14 09:17:42 +08:00
public function canSign()
{
2017-07-14 09:17:42 +08:00
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;
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()
{
2018-02-16 17:31:04 +08:00
// Delete the players he owned
2016-10-16 18:16:15 +08:00
Player::where('uid', $this->uid)->delete();
2018-02-16 17:31:04 +08:00
// Delete his closet
2016-10-16 22:06:31 +08:00
DB::table('closets')->where('uid', $this->uid)->delete();
2016-07-21 22:01:57 +08:00
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%");
}
2016-07-21 22:01:57 +08:00
}