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

358 lines
7.9 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 App;
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
/**
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 $raw_passwd
* @return bool
*/
2016-07-21 22:01:57 +08:00
public function checkPasswd($raw_passwd)
{
2016-11-05 20:11:48 +08:00
$responses = event(new EncryptUserPassword($raw_passwd, $this));
2016-10-17 17:51:51 +08:00
if (isset($responses[0])) {
$encrypted = $responses[0];
} else {
$encrypted = app('cipher')->encrypt($raw_passwd, config('secure.salt'));
2016-10-17 17:51:51 +08:00
}
2016-11-05 20:11:48 +08:00
return ($encrypted == $this->password);
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;
2016-10-25 21:35:09 +08:00
$user->password = app('cipher')->encrypt($password, config('secure.salt'));
2016-10-23 11:41:52 +08:00
$callback($user);
$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')->encrypt($new_passwd, config('secure.salt'));
}
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->storage_used)) {
$this->storage_used = 0;
// recalculate
2016-08-25 13:26:53 +08:00
$sql = "SELECT SUM(`size`) AS total_size FROM `{table}` WHERE uploader = {$this->uid}";
2016-08-28 10:05:21 +08:00
$result = \Database::table('textures')->fetchArray($sql)['total_size'];
2016-08-25 13:26:53 +08:00
$this->storage_used = $result ?: 0;
2016-07-21 22:01:57 +08:00
}
return $this->storage_used;
}
2016-10-23 11:41:52 +08:00
/**
* Check in for the user, return false if unavailable.
*
* @return int|bool
*/
2016-09-24 22:49:20 +08:00
public function checkIn()
2016-07-21 22:01:57 +08:00
{
2016-09-24 22:49:20 +08:00
if ($this->canCheckIn()) {
2016-10-23 11:41:52 +08:00
$sign_score = explode(',', option('sign_score'));
2016-07-29 12:46:19 +08:00
$aquired_score = rand($sign_score[0], $sign_score[1]);
2016-07-21 22:01:57 +08:00
$this->setScore($aquired_score, 'plus');
2016-10-23 11:41:52 +08:00
$this->last_sign_at = Utils::getTimeFormatted();
$this->save();
2016-07-21 22:01:57 +08:00
return $aquired_score;
} else {
return false;
}
}
2016-10-23 11:41:52 +08:00
/**
* Check if checking in is available now.
*
* @param bool $return_remaining_time Return remaining time.
* @return int|bool
*/
2016-09-24 22:49:20 +08:00
public function canCheckIn($return_remaining_time = false)
2016-07-21 22:01:57 +08:00
{
// convert to timestamp
$last_sign_at = strtotime($this->getLastSignTime());
2016-10-23 11:41:52 +08:00
if (option('sign_after_zero') == "1") {
$remaining_time = (Carbon::tomorrow()->timestamp - time()) / 3600;
$can_check_in = $last_sign_at <= Carbon::today()->timestamp;
} else {
2016-10-23 11:41:52 +08:00
$remaining_time = ($last_sign_at + option('sign_gap_time') * 3600 - time()) / 3600;
$can_check_in = $remaining_time <= 0;
}
return $return_remaining_time ? round($remaining_time) : $can_check_in;
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
/**
* Get the last time of checking in.
*
* @return string Formatted time string.
*/
2016-07-21 22:01:57 +08:00
public function getLastSignTime()
{
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
}