Resolve User class from service container

This commit is contained in:
Pig Fang 2019-08-04 10:56:15 +08:00
parent abaa88444f
commit 4c51924940
9 changed files with 64 additions and 54 deletions

View File

@ -19,7 +19,7 @@ use Illuminate\Support\Facades\Redis;
class AdminController extends Controller
{
public function chartData()
public function chartData(User $users)
{
$today = Carbon::today()->timestamp;
@ -45,7 +45,7 @@ class AdminController extends Controller
};
};
$userRegistration = User::where('register_at', '>=', $oneMonthAgo)
$userRegistration = $users->where('register_at', '>=', $oneMonthAgo)
->select('register_at')
->get()
->groupBy($grouping('register_at'))
@ -70,7 +70,7 @@ class AdminController extends Controller
];
}
public function sendNotification(Request $request)
public function sendNotification(Request $request, User $users)
{
$data = $this->validate($request, [
'receiver' => 'required|in:all,normal,uid,email',
@ -84,16 +84,16 @@ class AdminController extends Controller
switch ($data['receiver']) {
case 'all':
$users = User::all();
$users = $users->all();
break;
case 'normal':
$users = User::where('permission', User::NORMAL)->get();
$users = $users->where('permission', User::NORMAL)->get();
break;
case 'uid':
$users = User::where('uid', $data['uid'])->get();
$users = $users->where('uid', $data['uid'])->get();
break;
case 'email':
$users = User::where('email', $data['email'])->get();
$users = $users->where('email', $data['email'])->get();
break;
}
Notification::send($users, $notification);
@ -360,12 +360,12 @@ class AdminController extends Controller
->with('forms', compact('resources', 'redis', 'cache'));
}
public function getUserData(Request $request)
public function getUserData(Request $request, User $users)
{
$isSingleUser = $request->has('uid');
if ($isSingleUser) {
$users = User::select(['uid', 'email', 'nickname', 'score', 'permission', 'register_at', 'verified'])
$users = $users->select(['uid', 'email', 'nickname', 'score', 'permission', 'register_at', 'verified'])
->where('uid', intval($request->input('uid')))
->get();
} else {
@ -375,7 +375,7 @@ class AdminController extends Controller
$page = $request->input('page', 1);
$perPage = $request->input('perPage', 10);
$users = User::select(['uid', 'email', 'nickname', 'score', 'permission', 'register_at', 'verified'])
$users = $users->select(['uid', 'email', 'nickname', 'score', 'permission', 'register_at', 'verified'])
->where('uid', 'like', '%'.$search.'%')
->orWhere('email', 'like', '%'.$search.'%')
->orWhere('nickname', 'like', '%'.$search.'%')
@ -394,7 +394,7 @@ class AdminController extends Controller
});
return [
'totalRecords' => $isSingleUser ? 1 : User::count(),
'totalRecords' => $isSingleUser ? 1 : $users->count(),
'data' => $users,
];
}
@ -430,10 +430,10 @@ class AdminController extends Controller
];
}
public function userAjaxHandler(Request $request)
public function userAjaxHandler(Request $request, User $users)
{
$action = $request->input('action');
$user = User::find($request->uid);
$user = $users->find($request->uid);
$currentUser = Auth::user();
if (! $user) {
@ -449,7 +449,7 @@ class AdminController extends Controller
'email' => 'required|email',
]);
if (User::where('email', $request->email)->count() != 0) {
if ($users->where('email', $request->email)->count() != 0) {
return json(trans('admin.users.operations.email.existed', ['email' => $request->input('email')]), 1);
}
@ -509,7 +509,7 @@ class AdminController extends Controller
}
}
public function playerAjaxHandler(Request $request)
public function playerAjaxHandler(Request $request, User $users)
{
$action = $request->input('action');
$currentUser = Auth::user();
@ -547,7 +547,7 @@ class AdminController extends Controller
'uid' => 'required|integer',
]);
$user = User::find($request->uid);
$user = $users->find($request->uid);
if (! $user) {
return json(trans('admin.users.operations.non-existent'), 1);

View File

@ -18,7 +18,7 @@ use App\Exceptions\PrettyPageException;
class AuthController extends Controller
{
public function handleLogin(Request $request, Captcha $captcha)
public function handleLogin(Request $request, Captcha $captcha, User $users)
{
$this->validate($request, [
'identification' => 'required',
@ -33,7 +33,7 @@ class AuthController extends Controller
event(new Events\UserTryToLogin($identification, $authType));
if ($authType == 'email') {
$user = User::where('email', $identification)->first();
$user = $users->where('email', $identification)->first();
} else {
$player = Player::where('name', $identification)->first();
$user = $player ? $player->user : null;
@ -99,7 +99,7 @@ class AuthController extends Controller
}
}
public function handleRegister(Request $request, Captcha $captcha)
public function handleRegister(Request $request, Captcha $captcha, User $users)
{
if (! option('user_can_register')) {
return json(trans('auth.register.close'), 7);
@ -124,7 +124,7 @@ class AuthController extends Controller
// If amount of registered accounts of IP is more than allowed amounts,
// then reject the register.
if (User::where('ip', get_client_ip())->count() >= option('regs_per_ip')) {
if ($users->where('ip', get_client_ip())->count() >= option('regs_per_ip')) {
return json(trans('auth.register.max', ['regs' => option('regs_per_ip')]), 7);
}
@ -173,7 +173,7 @@ class AuthController extends Controller
}
}
public function handleForgot(Request $request, Captcha $captcha)
public function handleForgot(Request $request, Captcha $captcha, User $users)
{
$this->validate($request, [
'captcha' => ['required', $captcha],
@ -192,7 +192,7 @@ class AuthController extends Controller
return json(trans('auth.forgot.frequent-mail'), 2);
}
$user = User::where('email', $request->email)->first();
$user = $users->where('email', $request->email)->first();
if (! $user) {
return json(trans('auth.forgot.unregistered'), 1);
@ -213,18 +213,18 @@ class AuthController extends Controller
return json(trans('auth.forgot.success'), 0);
}
public function reset($uid)
public function reset(User $users, $uid)
{
return view('auth.reset')->with('user', User::find($uid));
return view('auth.reset')->with('user', $users->find($uid));
}
public function handleReset($uid, Request $request)
public function handleReset(Request $request, User $users, $uid)
{
$validated = $this->validate($request, [
'password' => 'required|min:8|max:32',
]);
User::find($uid)->changePassword($validated['password']);
$users->find($uid)->changePassword($validated['password']);
return json(trans('auth.reset.success'), 0);
}
@ -239,13 +239,13 @@ class AuthController extends Controller
return redirect('/user');
}
public function verify($uid)
public function verify(User $users, $uid)
{
if (! option('require_verification')) {
throw new PrettyPageException(trans('user.verification.disabled'), 1);
}
$user = User::find($uid);
$user = $users->find($uid);
if (! $user || $user->verified) {
throw new PrettyPageException(trans('auth.verify.invalid'), 1);

View File

@ -2,8 +2,6 @@
namespace App\Http\Controllers;
use View;
use Option;
use App\Models\User;
use App\Models\Texture;
use Illuminate\Http\Request;
@ -65,7 +63,7 @@ class ClosetController extends Controller
]);
}
public function add(Request $request)
public function add(Request $request, User $users)
{
$this->validate($request, [
'tid' => 'required|integer',
@ -99,7 +97,7 @@ class ClosetController extends Controller
$texture->likes++;
$texture->save();
$uploader = User::find($texture->uploader);
$uploader = $users->find($texture->uploader);
if ($uploader && $uploader->uid != $user->uid) {
$uploader->score += option('score_award_per_like', 0);
$uploader->save();
@ -122,7 +120,7 @@ class ClosetController extends Controller
return json(trans('user.closet.rename.success', ['name' => $request->name]), 0);
}
public function remove($tid)
public function remove(User $users, $tid)
{
$user = auth()->user();
@ -141,7 +139,7 @@ class ClosetController extends Controller
$texture->likes--;
$texture->save();
$uploader = User::find($texture->uploader);
$uploader = $users->find($texture->uploader);
$uploader->score -= option('score_award_per_like', 0);
$uploader->save();

View File

@ -47,7 +47,7 @@ class ReportController extends Controller
->get();
}
public function manage(Request $request)
public function manage(Request $request, User $users)
{
$search = $request->input('search', '');
$sortField = $request->input('sortField', 'report_at');
@ -63,8 +63,8 @@ class ReportController extends Controller
->limit($perPage)
->get()
->makeHidden(['informer'])
->map(function ($report) {
$uploader = User::find($report->uploader);
->map(function ($report) use ($users) {
$uploader = $users->find($report->uploader);
if ($uploader) {
$report->uploaderName = $uploader->nickname;
}
@ -81,7 +81,7 @@ class ReportController extends Controller
];
}
public function review(Request $request)
public function review(Request $request, User $users)
{
$data = $this->validate($request, [
'id' => 'required|exists:reports',
@ -118,7 +118,7 @@ class ReportController extends Controller
}
break;
case 'ban':
$uploader = User::find($report->uploader);
$uploader = $users->find($report->uploader);
if (! $uploader) {
return json(trans('admin.users.operations.non-existent'), 1);
}

View File

@ -116,7 +116,7 @@ class SkinlibController extends Controller
]);
}
public function show($tid)
public function show(User $users, $tid)
{
$texture = Texture::find($tid);
$user = Auth::user();
@ -147,7 +147,7 @@ class SkinlibController extends Controller
'currentUid' => $user ? $user->uid : 0,
'admin' => $user && $user->isAdmin(),
'inCloset' => $user && $user->closet()->where('tid', $texture->tid)->count() > 0,
'nickname' => ($up = User::find($texture->uploader)) ? $up->nickname : null,
'nickname' => ($up = $users->find($texture->uploader)) ? $up->nickname : null,
'report' => intval(option('reporter_score_modification', 0)),
]);
}
@ -264,10 +264,10 @@ class SkinlibController extends Controller
return json(trans('skinlib.delete.success'), 0);
}
public function privacy(Request $request)
public function privacy(Request $request, User $users)
{
$t = Texture::find($request->input('tid'));
$user = Auth::user();
$user = $request->user();
if (! $t) {
return json(trans('skinlib.non-existent'), 1);
@ -277,7 +277,7 @@ class SkinlibController extends Controller
return json(trans('skinlib.no-permission'), 1);
}
$uploader = User::find($t->uploader);
$uploader = $users->find($t->uploader);
$score_diff = $t->size * (option('private_score_per_storage') - option('score_per_storage')) * ($t->public ? -1 : 1);
if ($t->public && option('take_back_scores_after_deletion', true)) {
$score_diff -= option('score_award_per_texture', 0);
@ -318,7 +318,7 @@ class SkinlibController extends Controller
'tid' => 'required|integer',
'new_name' => 'required|no_special_chars',
]);
$user = Auth::user();
$user = $request->user();
$t = Texture::find($request->input('tid'));
if (! $t) {
@ -340,7 +340,7 @@ class SkinlibController extends Controller
public function model(Request $request)
{
$user = Auth::user();
$user = $request->user();
$data = $this->validate($request, [
'tid' => 'required|integer',
'model' => 'required|in:steve,alex,cape',

View File

@ -128,9 +128,9 @@ class TextureController extends Controller
return $this->avatarByTid($tid, $size);
}
public function avatar($base64_email, $size = 128)
public function avatar(User $users, $base64_email, $size = 128)
{
$user = User::where('email', base64_decode($base64_email))->first();
$user = $users->where('email', base64_decode($base64_email))->first();
if ($user) {
return $this->avatarByTid($user->avatar, $size);
@ -139,9 +139,9 @@ class TextureController extends Controller
return response()->file(storage_path('static_textures/avatar.png'));
}
public function avatarWithSize($size, $base64_email)
public function avatarWithSize(User $users, $size, $base64_email)
{
return $this->avatar($base64_email, $size);
return $this->avatar($users, $base64_email, $size);
}
public function preview($tid, $size = 250)

View File

@ -194,7 +194,7 @@ class UserController extends Controller
]);
}
public function handleProfile(Request $request)
public function handleProfile(Request $request, User $users)
{
$action = $request->input('action', '');
$user = Auth::user();
@ -242,7 +242,7 @@ class UserController extends Controller
'password' => 'required|min:6|max:32',
]);
if (User::where('email', $request->new_email)->count() > 0) {
if ($users->where('email', $request->new_email)->count() > 0) {
return json(trans('user.profile.email.existed'), 1);
}

View File

@ -2,8 +2,20 @@
namespace App\Listeners;
use App\Models\User;
class TextureRemoved
{
/**
* @var User
*/
protected $users;
public function __construct(User $users)
{
$this->users = $users;
}
public function handle(\App\Events\TextureDeleting $event)
{
$texture = $event->texture;
@ -16,7 +28,7 @@ class TextureRemoved
}
});
if ($uploader = \App\Models\User::find($texture->uploader)) {
if ($uploader = $this->users->find($texture->uploader)) {
$ret = 0;
if (option('return_score')) {
$ret += $texture->size * (

View File

@ -21,7 +21,7 @@
<div class="col-md-6">
<div class="small-box bg-aqua">
<div class="inner">
<h3>{{ App\Models\User::count() }}</h3>
<h3>{{ resolve(\App\Models\User::class)->count() }}</h3>
<p>@lang('admin.index.total-users')</p>
</div>
<div class="icon"><i class="fas fa-users"></i></div>