2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
namespace App\Http\Controllers;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
use App;
|
2018-08-17 12:32:44 +08:00
|
|
|
use URL;
|
|
|
|
use Mail;
|
2016-09-04 15:35:12 +08:00
|
|
|
use View;
|
2018-08-17 12:32:44 +08:00
|
|
|
use Session;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Texture;
|
2016-09-04 15:35:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2018-08-17 12:32:44 +08:00
|
|
|
use App\Mail\EmailVerification;
|
2016-11-18 23:06:15 +08:00
|
|
|
use App\Events\UserProfileUpdated;
|
2018-07-20 14:42:43 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
class UserController extends Controller
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-08-17 12:32:44 +08:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware(function ($request, $next) {
|
|
|
|
if (! Auth::user()->verified) {
|
|
|
|
$this->sendVerificationEmail();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
})->only(['index', 'profile']);
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
public function index()
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2019-03-02 22:58:37 +08:00
|
|
|
|
2017-02-03 14:25:17 +08:00
|
|
|
return view('user.index')->with([
|
|
|
|
'statistics' => [
|
2018-07-20 14:42:43 +08:00
|
|
|
'players' => $this->calculatePercentageUsed($user->players->count(), option('score_per_player')),
|
2019-03-02 22:58:37 +08:00
|
|
|
'storage' => $this->calculatePercentageUsed($user->getStorageUsed(), option('score_per_storage')),
|
2018-07-22 16:32:34 +08:00
|
|
|
],
|
2019-03-02 22:58:37 +08:00
|
|
|
'announcement' => app('parsedown')->text(option_localized('announcement')),
|
2019-03-23 19:52:14 +08:00
|
|
|
'extra' => ['unverified' => option('require_verification') && ! $user->verified],
|
2017-02-03 14:25:17 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-08 09:50:35 +08:00
|
|
|
public function scoreInfo()
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
2019-03-02 22:58:37 +08:00
|
|
|
|
2019-04-23 19:14:41 +08:00
|
|
|
return json('', 0, [
|
2018-08-08 09:50:35 +08:00
|
|
|
'user' => [
|
|
|
|
'score' => $user->score,
|
|
|
|
'lastSignAt' => $user->last_sign_at,
|
|
|
|
],
|
|
|
|
'stats' => [
|
|
|
|
'players' => $this->calculatePercentageUsed($user->players->count(), option('score_per_player')),
|
2019-03-02 22:58:37 +08:00
|
|
|
'storage' => $this->calculatePercentageUsed($user->getStorageUsed(), option('score_per_storage')),
|
2018-08-08 09:50:35 +08:00
|
|
|
],
|
|
|
|
'signAfterZero' => option('sign_after_zero'),
|
2019-03-02 22:58:37 +08:00
|
|
|
'signGapTime' => option('sign_gap_time'),
|
2019-04-23 19:14:41 +08:00
|
|
|
]);
|
2018-08-08 09:50:35 +08:00
|
|
|
}
|
|
|
|
|
2017-02-03 14:25:17 +08:00
|
|
|
/**
|
|
|
|
* Calculate percentage of resources used by user.
|
|
|
|
*
|
|
|
|
* @param int $used
|
|
|
|
* @param int $rate
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function calculatePercentageUsed($used, $rate)
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2018-02-16 17:31:04 +08:00
|
|
|
// Initialize default value to avoid division by zero.
|
2019-03-02 22:58:37 +08:00
|
|
|
$result['used'] = $used;
|
|
|
|
$result['total'] = 'UNLIMITED';
|
2017-02-03 14:25:17 +08:00
|
|
|
$result['percentage'] = 0;
|
|
|
|
|
|
|
|
if ($rate != 0) {
|
2019-03-23 00:20:28 +08:00
|
|
|
$result['total'] = $used + floor($user->score / $rate);
|
2017-02-03 14:25:17 +08:00
|
|
|
$result['percentage'] = $result['total'] ? $used / $result['total'] * 100 : 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
2017-07-14 09:17:42 +08:00
|
|
|
* Handle user signing.
|
2016-09-04 15:35:12 +08:00
|
|
|
*
|
2017-11-18 13:25:08 +08:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-09-04 15:35:12 +08:00
|
|
|
*/
|
2017-07-14 09:17:42 +08:00
|
|
|
public function sign()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
if ($user->canSign()) {
|
|
|
|
$acquiredScore = $user->sign();
|
2019-02-16 18:16:57 +08:00
|
|
|
$gap = option('sign_gap_time');
|
2017-01-17 22:57:16 +08:00
|
|
|
|
2019-04-23 19:14:41 +08:00
|
|
|
return json(trans('user.sign-success', ['score' => $acquiredScore]), 0, [
|
|
|
|
'score' => $user->score,
|
|
|
|
'storage' => $this->calculatePercentageUsed($user->getStorageUsed(), option('score_per_storage')),
|
2019-03-02 22:58:37 +08:00
|
|
|
'remaining_time' => $gap > 1 ? round($gap) : $gap,
|
2016-07-21 22:01:57 +08:00
|
|
|
]);
|
|
|
|
} else {
|
2017-08-06 17:05:04 +08:00
|
|
|
$remaining_time = $this->getUserSignRemainingTimeWithPrecision();
|
2019-03-02 22:58:37 +08:00
|
|
|
|
2017-08-05 23:43:35 +08:00
|
|
|
return json(trans('user.cant-sign-until', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'time' => $remaining_time >= 1
|
|
|
|
? $remaining_time : round($remaining_time * 60),
|
2017-08-06 17:05:04 +08:00
|
|
|
'unit' => $remaining_time >= 1
|
2019-03-02 22:58:37 +08:00
|
|
|
? trans('user.time-unit-hour') : trans('user.time-unit-min'),
|
2017-08-05 23:43:35 +08:00
|
|
|
]), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:02:28 +08:00
|
|
|
public function getUserSignRemainingTimeWithPrecision($user = null)
|
2017-08-05 23:43:35 +08:00
|
|
|
{
|
2019-02-16 17:02:28 +08:00
|
|
|
$hours = ($user ?? Auth::user())->getSignRemainingTime() / 3600;
|
2017-08-05 23:43:35 +08:00
|
|
|
|
2017-08-06 17:05:04 +08:00
|
|
|
return $hours > 1 ? round($hours) : $hours;
|
2017-08-05 23:43:35 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 12:32:44 +08:00
|
|
|
public function sendVerificationEmail()
|
|
|
|
{
|
|
|
|
if (! option('require_verification')) {
|
|
|
|
return json(trans('user.verification.disabled'), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rate limit of 60s
|
|
|
|
$remain = 60 + session('last_mail_time', 0) - time();
|
|
|
|
|
|
|
|
if ($remain > 0) {
|
2019-04-23 19:14:41 +08:00
|
|
|
return json(trans('user.verification.frequent-mail'), 1);
|
2018-08-17 12:32:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
|
|
if ($user->verified) {
|
|
|
|
return json(trans('user.verification.verified'), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = URL::signedRoute('auth.verify', ['uid' => $user->uid]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Mail::to($user->email)->send(new EmailVerification($url));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Write the exception to log
|
|
|
|
report($e);
|
2019-03-02 22:58:37 +08:00
|
|
|
|
2018-08-17 12:32:44 +08:00
|
|
|
return json(trans('user.verification.failed', ['msg' => $e->getMessage()]), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::put('last_mail_time', time());
|
|
|
|
|
|
|
|
return json(trans('user.verification.success'), 0);
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
public function profile()
|
|
|
|
{
|
2019-03-23 19:52:14 +08:00
|
|
|
$user = Auth::user();
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-03-23 19:52:14 +08:00
|
|
|
return view('user.profile')
|
|
|
|
->with('extra', [
|
|
|
|
'unverified' => option('require_verification') && ! $user->verified,
|
|
|
|
'admin' => $user->isAdmin(),
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-03-23 11:06:36 +08:00
|
|
|
public function handleProfile(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-02-03 14:25:17 +08:00
|
|
|
$action = $request->input('action', '');
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2017-02-03 14:25:17 +08:00
|
|
|
|
|
|
|
switch ($action) {
|
2016-09-04 15:35:12 +08:00
|
|
|
case 'nickname':
|
2019-03-22 21:40:12 +08:00
|
|
|
if (option('single_player', false)) {
|
|
|
|
return json(trans('user.profile.nickname.single'), 1);
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_nickname' => 'required|no_special_chars|max:255',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$nickname = $request->input('new_nickname');
|
2019-03-23 00:20:28 +08:00
|
|
|
$user->nickname = $nickname;
|
|
|
|
$user->save();
|
|
|
|
event(new UserProfileUpdated($action, $user));
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-23 00:20:28 +08:00
|
|
|
return json(trans('user.profile.nickname.success', ['nickname' => $nickname]), 0);
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
case 'password':
|
|
|
|
$this->validate($request, [
|
2018-06-18 21:50:32 +08:00
|
|
|
'current_password' => 'required|min:6|max:32',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_password' => 'required|min:8|max:32',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $user->verifyPassword($request->input('current_password'))) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.password.wrong-password'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-09-04 15:35:12 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
if ($user->changePassword($request->input('new_password'))) {
|
|
|
|
event(new UserProfileUpdated($action, $user));
|
2018-02-16 19:54:07 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
Auth::logout();
|
2018-02-16 19:54:07 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
return json(trans('user.profile.password.success'), 0);
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
break; // @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
case 'email':
|
|
|
|
$this->validate($request, [
|
|
|
|
'new_email' => 'required|email',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => 'required|min:6|max:32',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-23 11:06:36 +08:00
|
|
|
if (User::where('email', $request->new_email)->count() > 0) {
|
2017-04-27 09:25:42 +08:00
|
|
|
return json(trans('user.profile.email.existed'), 1);
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $user->verifyPassword($request->input('password'))) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.email.wrong-password'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-23 00:20:28 +08:00
|
|
|
$user->email = $request->input('new_email');
|
|
|
|
$user->verified = false;
|
|
|
|
$user->save();
|
2018-02-16 19:54:07 +08:00
|
|
|
|
2019-03-23 00:20:28 +08:00
|
|
|
event(new UserProfileUpdated($action, $user));
|
2018-07-20 14:42:43 +08:00
|
|
|
|
2019-03-23 00:20:28 +08:00
|
|
|
Auth::logout();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-23 00:20:28 +08:00
|
|
|
return json(trans('user.profile.email.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
case 'delete':
|
|
|
|
$this->validate($request, [
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => 'required|min:6|max:32',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($user->isAdmin()) {
|
2018-08-02 10:21:25 +08:00
|
|
|
return json(trans('user.profile.delete.admin'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2018-08-02 10:21:25 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $user->verifyPassword($request->input('password'))) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.delete.wrong-password'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2018-08-02 10:21:25 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
Auth::logout();
|
2018-08-02 10:21:25 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
if ($user->delete()) {
|
2016-11-17 17:32:12 +08:00
|
|
|
session()->flush();
|
2016-09-04 15:35:12 +08:00
|
|
|
|
2019-04-23 19:14:41 +08:00
|
|
|
return json(trans('user.profile.delete.success'), 0);
|
2016-09-04 15:35:12 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
break; // @codeCoverageIgnore
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
default:
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('general.illegal-parameters'), 1);
|
2016-09-04 15:35:12 +08:00
|
|
|
break;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
2017-01-17 22:57:16 +08:00
|
|
|
* Set user avatar.
|
2016-09-04 15:35:12 +08:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
public function setAvatar(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
2019-03-02 22:58:37 +08:00
|
|
|
'tid' => 'required|integer',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2019-03-17 09:46:02 +08:00
|
|
|
$tid = $request->input('tid');
|
|
|
|
$user = auth()->user();
|
2016-09-04 15:35:12 +08:00
|
|
|
|
2019-03-17 09:46:02 +08:00
|
|
|
if ($tid == 0) {
|
|
|
|
$user->avatar = 0;
|
|
|
|
$user->save();
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-03-17 09:46:02 +08:00
|
|
|
return json(trans('user.profile.avatar.success'), 0);
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-17 09:46:02 +08:00
|
|
|
$result = Texture::find($tid);
|
2016-07-21 22:01:57 +08:00
|
|
|
if ($result) {
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($result->type == 'cape') {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.avatar.wrong-type'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-23 00:20:28 +08:00
|
|
|
$user->avatar = $tid;
|
|
|
|
$user->save();
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-03-23 00:20:28 +08:00
|
|
|
return json(trans('user.profile.avatar.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
} else {
|
2017-08-07 13:48:20 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
// @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|