blessing-skin-server/app/Http/Controllers/UserController.php

373 lines
12 KiB
PHP
Raw Normal View History

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
2019-12-14 11:10:37 +08:00
use App\Events\UserProfileUpdated;
use App\Mail\EmailVerification;
2016-07-21 22:01:57 +08:00
use App\Models\Texture;
2019-12-14 11:10:37 +08:00
use App\Models\User;
use Auth;
use Blessing\Filter;
use Blessing\Rejection;
2019-12-14 11:10:37 +08:00
use Carbon\Carbon;
2019-09-04 19:31:44 +08:00
use Illuminate\Contracts\Events\Dispatcher;
2019-12-14 11:10:37 +08:00
use Illuminate\Http\Request;
use Mail;
2019-12-25 15:48:34 +08:00
use Parsedown;
2019-12-14 11:10:37 +08:00
use Session;
use URL;
2016-07-21 22:01:57 +08:00
class UserController extends Controller
2016-07-21 22:01:57 +08:00
{
2019-04-27 23:10:21 +08:00
public function user()
{
2019-12-31 22:39:33 +08:00
$user = auth()
->user()
->makeHidden(['password', 'ip', 'remember_token', 'verification_token'])
->toArray();
return json('', 0, $user);
2019-04-27 23:10:21 +08:00
}
2019-12-13 19:29:57 +08:00
public function index(Filter $filter)
2016-07-21 22:01:57 +08:00
{
$user = Auth::user();
2019-09-04 23:16:49 +08:00
[$from, $to] = explode(',', option('sign_score'));
2019-09-17 23:10:44 +08:00
$scoreIntro = trans('user.score-intro.introduction', [
2019-08-16 17:09:40 +08:00
'initial_score' => option('user_initial_score'),
2019-12-14 11:10:37 +08:00
'score-from' => $from,
'score-to' => $to,
'return-score' => option('return_score')
2019-08-16 17:09:40 +08:00
? trans('user.score-intro.will-return-score')
: trans('user.score-intro.no-return-score'),
2019-09-17 23:10:44 +08:00
]);
2019-08-16 17:09:40 +08:00
2019-12-13 19:29:57 +08:00
$grid = [
'layout' => [
['md-7', 'md-5'],
],
'widgets' => [
[
2020-02-12 10:02:15 +08:00
[
'user.widgets.email-verification',
'user.widgets.dashboard.usage',
],
2019-12-13 19:29:57 +08:00
['user.widgets.dashboard.announcement'],
2019-12-14 11:10:37 +08:00
],
2019-12-13 19:29:57 +08:00
],
];
$grid = $filter->apply('grid:user.index', $grid);
2019-12-25 15:48:34 +08:00
$parsedown = new Parsedown();
return view('user.index')->with([
'statistics' => [
'players' => $this->calculatePercentageUsed($user->players->count(), option('score_per_player')),
2019-07-30 14:29:02 +08:00
'storage' => $this->calculatePercentageUsed($this->getStorageUsed($user), option('score_per_storage')),
],
2019-09-17 23:10:44 +08:00
'score_intro' => $scoreIntro,
'rates' => [
'storage' => option('score_per_storage'),
'player' => option('score_per_player'),
'closet' => option('score_per_closet_item'),
],
2019-12-25 15:48:34 +08:00
'announcement' => $parsedown->text(option_localized('announcement')),
2019-12-13 19:29:57 +08:00
'grid' => $grid,
2019-12-14 11:10:37 +08:00
'extra' => ['unverified' => option('require_verification') && !$user->verified],
]);
}
2018-08-08 09:50:35 +08:00
public function scoreInfo()
{
$user = Auth::user();
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-07-30 14:29:02 +08:00
'storage' => $this->calculatePercentageUsed($this->getStorageUsed($user), option('score_per_storage')),
2018-08-08 09:50:35 +08:00
],
2020-01-31 15:58:37 +08:00
'signAfterZero' => (bool) option('sign_after_zero'),
'signGapTime' => (int) option('sign_gap_time'),
2019-04-23 19:14:41 +08:00
]);
2018-08-08 09:50:35 +08:00
}
2019-09-04 23:16:49 +08:00
protected function calculatePercentageUsed(int $used, int $rate): array
{
$user = Auth::user();
2018-02-16 17:31:04 +08:00
// Initialize default value to avoid division by zero.
$result['used'] = $used;
$result['total'] = 'UNLIMITED';
$result['percentage'] = 0;
if ($rate != 0) {
2019-03-23 00:20:28 +08:00
$result['total'] = $used + floor($user->score / $rate);
$result['percentage'] = $result['total'] ? $used / $result['total'] * 100 : 100;
}
return $result;
2016-07-21 22:01:57 +08:00
}
2019-07-30 14:29:02 +08:00
protected function getStorageUsed(User $user)
{
2019-07-30 14:37:31 +08:00
return Texture::where('uploader', $user->uid)->select('size')->sum('size');
2019-07-30 14:29:02 +08:00
}
2017-07-14 09:17:42 +08:00
public function sign()
2016-07-21 22:01:57 +08:00
{
$user = Auth::user();
2019-07-30 14:29:02 +08:00
if ($this->getSignRemainingTime($user) <= 0) {
2019-09-04 23:16:49 +08:00
$acquiredScore = rand(...explode(',', option('sign_score')));
2019-07-30 14:29:02 +08:00
$user->score += $acquiredScore;
2019-12-21 15:50:29 +08:00
$user->last_sign_at = Carbon::now();
2019-07-30 14:29:02 +08:00
$user->save();
2019-02-16 18:16:57 +08:00
$gap = option('sign_gap_time');
2019-04-23 19:14:41 +08:00
return json(trans('user.sign-success', ['score' => $acquiredScore]), 0, [
'score' => $user->score,
2019-07-30 14:29:02 +08:00
'storage' => $this->calculatePercentageUsed($this->getStorageUsed($user), option('score_per_storage')),
'remaining_time' => $gap > 1 ? round($gap) : $gap,
2016-07-21 22:01:57 +08:00
]);
} else {
2019-07-30 14:29:02 +08:00
$remaining_time = $this->getUserSignRemainingTimeWithPrecision($user);
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),
'unit' => $remaining_time >= 1
? trans('user.time-unit-hour') : trans('user.time-unit-min'),
]), 1);
2016-07-21 22:01:57 +08:00
}
}
2019-07-30 14:29:02 +08:00
protected function getUserSignRemainingTimeWithPrecision(User $user)
{
2019-07-30 14:29:02 +08:00
$hours = $this->getSignRemainingTime($user) / 3600;
return $hours > 1 ? round($hours) : $hours;
}
2019-07-30 14:29:02 +08:00
protected function getSignRemainingTime(User $user)
{
$lastSignTime = Carbon::parse($user->last_sign_at);
if (option('sign_after_zero')) {
return Carbon::now()->diffInSeconds(
$lastSignTime <= Carbon::today() ? $lastSignTime : Carbon::tomorrow(),
false
);
}
return Carbon::now()->diffInSeconds($lastSignTime->addHours(option('sign_gap_time')), false);
}
2018-08-17 12:32:44 +08:00
public function sendVerificationEmail()
{
2019-12-14 11:10:37 +08:00
if (!option('require_verification')) {
2018-08-17 12:32:44 +08:00
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], null, false);
2018-08-17 12:32:44 +08:00
try {
2020-03-29 09:53:24 +08:00
Mail::to($user->email)->send(new EmailVerification(url($url)));
2018-08-17 12:32:44 +08:00
} catch (\Exception $e) {
// Write the exception to log
report($e);
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);
}
2019-12-13 18:53:47 +08:00
public function profile(Filter $filter)
2016-07-21 22:01:57 +08:00
{
2019-03-23 19:52:14 +08:00
$user = Auth::user();
2019-04-19 19:36:36 +08:00
2019-12-13 18:53:47 +08:00
$grid = [
'layout' => [
['md-6', 'md-6'],
],
'widgets' => [
[
[
'user.widgets.profile.avatar',
2019-12-14 11:10:37 +08:00
'user.widgets.profile.password',
2019-12-13 18:53:47 +08:00
],
[
'user.widgets.profile.nickname',
'user.widgets.profile.email',
'user.widgets.profile.delete-account',
],
],
],
];
$grid = $filter->apply('grid:user.profile', $grid);
2019-03-23 19:52:14 +08:00
return view('user.profile')
->with('user', $user)
2019-12-13 18:53:47 +08:00
->with('grid', $grid)
->with('site_name', option_localized('site_name'));
2016-07-21 22:01:57 +08:00
}
2019-09-04 19:31:44 +08:00
public function handleProfile(Request $request, Filter $filter, Dispatcher $dispatcher)
2016-07-21 22:01:57 +08:00
{
$action = $request->input('action', '');
$user = Auth::user();
2019-09-04 19:31:44 +08:00
$addition = $request->except('action');
$can = $filter->apply('user_can_edit_profile', true, [$action, $addition]);
if ($can instanceof Rejection) {
return json($can->getReason(), 1);
}
$dispatcher->dispatch('user.profile.updating', [$user, $action, $addition]);
switch ($action) {
case 'nickname':
2019-03-22 21:40:12 +08:00
if (option('single_player', false)) {
return json(trans('user.profile.nickname.single'), 1);
}
$this->validate($request, ['new_nickname' => 'required']);
$nickname = $request->input('new_nickname');
2019-03-23 00:20:28 +08:00
$user->nickname = $nickname;
$user->save();
2019-09-04 19:31:44 +08:00
$dispatcher->dispatch('user.profile.updated', [$user, $action, $addition]);
2019-03-23 00:20:28 +08:00
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);
case 'password':
$this->validate($request, [
2018-06-18 21:50:32 +08:00
'current_password' => 'required|min:6|max:32',
2019-12-14 11:10:37 +08:00
'new_password' => 'required|min:8|max:32',
]);
2019-12-14 11:10: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);
}
2020-01-12 11:47:36 +08:00
$user->changePassword($request->input('new_password'));
$dispatcher->dispatch('user.profile.updated', [$user, $action, $addition]);
event(new UserProfileUpdated($action, $user));
2018-02-16 19:54:07 +08:00
2020-01-12 11:47:36 +08:00
Auth::logout();
2016-07-21 22:01:57 +08:00
2020-01-12 11:47:36 +08:00
return json(trans('user.profile.password.success'), 0);
2016-07-21 22:01:57 +08:00
case 'email':
$this->validate($request, [
'new_email' => 'required|email',
2019-12-14 11:10:37 +08:00
'password' => 'required|min:6|max:32',
]);
2016-07-21 22:01:57 +08:00
2019-08-24 10:22:26 +08:00
if (User::where('email', $request->new_email)->count() > 0) {
return json(trans('user.profile.email.existed'), 1);
}
2019-12-14 11:10: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);
}
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-09-04 19:31:44 +08:00
$dispatcher->dispatch('user.profile.updated', [$user, $action, $addition]);
2019-03-23 00:20:28 +08:00
event(new UserProfileUpdated($action, $user));
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
case 'delete':
$this->validate($request, [
'password' => 'required|min:6|max:32',
]);
2016-07-21 22:01:57 +08:00
if ($user->isAdmin()) {
2018-08-02 10:21:25 +08:00
return json(trans('user.profile.delete.admin'), 1);
}
2018-08-02 10:21:25 +08:00
2019-12-14 11:10: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);
}
2018-08-02 10:21:25 +08:00
Auth::logout();
2018-08-02 10:21:25 +08:00
2019-09-04 19:31:44 +08:00
$dispatcher->dispatch('user.deleting', [$user]);
2020-01-12 11:47:36 +08:00
$user->delete();
$dispatcher->dispatch('user.deleted', [$user]);
session()->flush();
2020-01-12 11:47:36 +08:00
return json(trans('user.profile.delete.success'), 0);
default:
2016-09-24 22:49:20 +08:00
return json(trans('general.illegal-parameters'), 1);
2016-07-21 22:01:57 +08:00
}
}
2019-09-04 19:31:44 +08:00
public function setAvatar(Request $request, Filter $filter, Dispatcher $dispatcher)
2016-07-21 22:01:57 +08:00
{
2019-09-04 19:31:44 +08:00
$this->validate($request, ['tid' => 'required|integer']);
2019-03-17 09:46:02 +08:00
$tid = $request->input('tid');
$user = auth()->user();
2019-09-04 19:31:44 +08:00
$can = $filter->apply('user_can_update_avatar', true, [$user, $tid]);
if ($can instanceof Rejection) {
return json($can->getReason(), 1);
}
$dispatcher->dispatch('user.avatar.updating', [$user, $tid]);
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-09-04 19:31:44 +08:00
$dispatcher->dispatch('user.avatar.updated', [$user, $tid]);
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-09-04 19:31:44 +08:00
$texture = Texture::find($tid);
if ($texture) {
if ($texture->type == 'cape') {
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.avatar.wrong-type'), 1);
}
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-09-04 19:31:44 +08:00
$dispatcher->dispatch('user.avatar.updated', [$user, $tid]);
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
}
}
2016-07-21 22:01:57 +08:00
}