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

392 lines
8.5 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()
{
if (!$this->closet) {
$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
{
// compare directly if any responses is returned by event dispatcher
if ($result = static::getEncryptedPwdFromEvent($rawPasswd, $this)) {
return hash_equals($this->password, $result);
}
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]);
// if the email is already registered
if ($user->uid) return false;
// pass the user instance to the callback
call_user_func($callback, $user);
2016-10-23 11:41:52 +08:00
// save to get uid
$user->save();
// 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];
} 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.
*
* @param string $new_email
*/
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()
{
2016-10-23 11:41:52 +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.
*
* @param string $new_nickname
* @return bool
*/
2016-07-21 22:01:57 +08:00
public function setNickName($new_nickname)
{
2016-10-23 11:41:52 +08:00
$this->nickname = $new_nickname;
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
{
2016-10-23 11:41:52 +08:00
if (!$this->token || $refresh) {
$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.
*
* @param int $score
* @param string $mode What operation should be done, set, plus or minus.
*/
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
/**
* Sign in for the user, return false if unavailable.
2016-10-23 11:41:52 +08:00
*
* @return int|bool
*/
public function signIn()
2016-07-21 22:01:57 +08:00
{
if ($this->canSignIn()) {
$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
*/
public function getSignInRemainingTime()
2016-07-21 22:01:57 +08:00
{
// convert to timestamp
2017-01-18 12:56:37 +08:00
$lastSignInTime = Carbon::parse($this->getLastSignInTime());
2017-01-18 12:56:37 +08:00
if (option('sign_after_zero')) {
return Carbon::now()->diffInSeconds(
(($lastSignInTime <= Carbon::today()) ? $lastSignInTime : Carbon::tomorrow())
, false);
}
return Carbon::now()->diffInSeconds($lastSignInTime->addSeconds(option('sign_gap_time') * 3600), false);
}
/**
* Check if signing in is available now.
*
* @return bool
*/
public function canSignIn()
{
return ($this->getSignInRemainingTime() <= 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.
*/
public function getLastSignInTime()
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()
{
2016-10-23 11:41:52 +08:00
// delete the players he owned
2016-10-16 18:16:15 +08:00
Player::where('uid', $this->uid)->delete();
2016-10-23 11:41:52 +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-10-23 11:41:52 +08:00
2016-07-21 22:01:57 +08:00
}