improved user controller
This commit is contained in:
parent
e53a055bc2
commit
3665b9ff55
5 changed files with 95 additions and 30 deletions
|
@ -7,23 +7,33 @@ use App\Http\Controllers\Controller;
|
||||||
use App\Models\DiscordUser;
|
use App\Models\DiscordUser;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
use Illuminate\Contracts\Foundation\Application;
|
||||||
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
use Illuminate\Contracts\Routing\ResponseFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Spatie\QueryBuilder\QueryBuilder;
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
|
const ALLOWED_INCLUDES = ['servers', 'payments', 'vouchers', 'discordUser'];
|
||||||
|
const ALLOWED_FILTERS = ['email', 'pterodactyl_id', 'role', 'suspended'];
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return Response
|
* @return LengthAwarePaginator
|
||||||
*/
|
*/
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
return User::paginate($request->query('per_page') ?? 50);
|
$query = QueryBuilder::for(User::class)
|
||||||
|
->allowedIncludes(self::ALLOWED_INCLUDES)
|
||||||
|
->allowedFilters(self::ALLOWED_FILTERS);
|
||||||
|
|
||||||
|
return $query->paginate($request->input('per_page') ?? 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,12 +41,22 @@ class UserController extends Controller
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
*
|
*
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return User
|
* @return User|Collection
|
||||||
*/
|
*/
|
||||||
public function show(int $id)
|
public function show(int $id)
|
||||||
{
|
{
|
||||||
$discordUser = DiscordUser::find($id);
|
$discordUser = DiscordUser::find($id);
|
||||||
return $discordUser ? $discordUser->user : User::findOrFail($id);
|
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
||||||
|
|
||||||
|
$query = QueryBuilder::for($user)
|
||||||
|
->with('discordUser')
|
||||||
|
->allowedIncludes(self::ALLOWED_INCLUDES)
|
||||||
|
->where('users.id' , '=' , $id)
|
||||||
|
->orWhereHas('discordUser' , function (Builder $builder) use ($id) {
|
||||||
|
$builder->where('id' , '=' , $id);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $query->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,11 +73,11 @@ class UserController extends Controller
|
||||||
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
||||||
|
|
||||||
$request->validate([
|
$request->validate([
|
||||||
"name" => "sometimes|string|min:4|max:30",
|
"name" => "sometimes|string|min:4|max:30",
|
||||||
"email" => "sometimes|string|email",
|
"email" => "sometimes|string|email",
|
||||||
"credits" => "sometimes|numeric|min:0|max:1000000",
|
"credits" => "sometimes|numeric|min:0|max:1000000",
|
||||||
"server_limit" => "sometimes|numeric|min:0|max:1000000",
|
"server_limit" => "sometimes|numeric|min:0|max:1000000",
|
||||||
"role" => ['sometimes', Rule::in(['admin', 'mod', 'client', 'member'])],
|
"role" => ['sometimes', Rule::in(['admin', 'mod', 'client', 'member'])],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user->update($request->all());
|
$user->update($request->all());
|
||||||
|
@ -81,23 +101,23 @@ class UserController extends Controller
|
||||||
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
||||||
|
|
||||||
$request->validate([
|
$request->validate([
|
||||||
"credits" => "sometimes|numeric|min:0|max:1000000",
|
"credits" => "sometimes|numeric|min:0|max:1000000",
|
||||||
"server_limit" => "sometimes|numeric|min:0|max:1000000",
|
"server_limit" => "sometimes|numeric|min:0|max:1000000",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if($request->credits){
|
if ($request->credits) {
|
||||||
if ($user->credits + $request->credits >= 99999999) throw ValidationException::withMessages([
|
if ($user->credits + $request->credits >= 99999999) throw ValidationException::withMessages([
|
||||||
'credits' => "You can't add this amount of credits because you would exceed the credit limit"
|
'credits' => "You can't add this amount of credits because you would exceed the credit limit"
|
||||||
]);
|
]);
|
||||||
event(new UserUpdateCreditsEvent($user));
|
event(new UserUpdateCreditsEvent($user));
|
||||||
$user->increment('credits', $request->credits);
|
$user->increment('credits', $request->credits);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($request->server_limit){
|
if ($request->server_limit) {
|
||||||
if ($user->server_limit + $request->server_limit >= 2147483647) throw ValidationException::withMessages([
|
if ($user->server_limit + $request->server_limit >= 2147483647) throw ValidationException::withMessages([
|
||||||
'server_limit' => "You cannot add this amount of servers because it would exceed the server limit."
|
'server_limit' => "You cannot add this amount of servers because it would exceed the server limit."
|
||||||
]);
|
]);
|
||||||
$user->increment('server_limit', $request->server_limit);
|
$user->increment('server_limit', $request->server_limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
|
@ -117,22 +137,22 @@ class UserController extends Controller
|
||||||
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
||||||
|
|
||||||
$request->validate([
|
$request->validate([
|
||||||
"credits" => "sometimes|numeric|min:0|max:1000000",
|
"credits" => "sometimes|numeric|min:0|max:1000000",
|
||||||
"server_limit" => "sometimes|numeric|min:0|max:1000000",
|
"server_limit" => "sometimes|numeric|min:0|max:1000000",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if($request->credits){
|
if ($request->credits) {
|
||||||
if($user->credits - $request->credits < 0) throw ValidationException::withMessages([
|
if ($user->credits - $request->credits < 0) throw ValidationException::withMessages([
|
||||||
'credits' => "You can't remove this amount of credits because you would exceed the minimum credit limit"
|
'credits' => "You can't remove this amount of credits because you would exceed the minimum credit limit"
|
||||||
]);
|
]);
|
||||||
$user->decrement('credits', $request->credits);
|
$user->decrement('credits', $request->credits);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($request->server_limit){
|
if ($request->server_limit) {
|
||||||
if($user->server_limit - $request->server_limit < 0) throw ValidationException::withMessages([
|
if ($user->server_limit - $request->server_limit < 0) throw ValidationException::withMessages([
|
||||||
'server_limit' => "You cannot remove this amount of servers because it would exceed the minimum server."
|
'server_limit' => "You cannot remove this amount of servers because it would exceed the minimum server."
|
||||||
]);
|
]);
|
||||||
$user->decrement('server_limit', $request->server_limit);
|
$user->decrement('server_limit', $request->server_limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
|
|
|
@ -29,7 +29,6 @@ class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
Paginator::useBootstrap();
|
Paginator::useBootstrap();
|
||||||
Schema::defaultStringLength(191);
|
Schema::defaultStringLength(191);
|
||||||
QueryBuilderRequest::setArrayValueDelimiter('|');
|
|
||||||
|
|
||||||
Validator::extend('multiple_date_format', function ($attribute, $value, $parameters, $validator) {
|
Validator::extend('multiple_date_format', function ($attribute, $value, $parameters, $validator) {
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
"paypal/rest-api-sdk-php": "^1.14",
|
"paypal/rest-api-sdk-php": "^1.14",
|
||||||
"socialiteproviders/discord": "^4.1",
|
"socialiteproviders/discord": "^4.1",
|
||||||
"spatie/laravel-activitylog": "^3.16",
|
"spatie/laravel-activitylog": "^3.16",
|
||||||
"spatie/laravel-query-builder": "^3.5",
|
"spatie/laravel-query-builder": "^3.6",
|
||||||
"spatie/laravel-validation-rules": "^3.0",
|
"spatie/laravel-validation-rules": "^3.0",
|
||||||
"yajra/laravel-datatables-oracle": "~9.0"
|
"yajra/laravel-datatables-oracle": "~9.0"
|
||||||
},
|
},
|
||||||
|
|
14
composer.lock
generated
14
composer.lock
generated
|
@ -4,7 +4,7 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "b3b61a46d5d4d6560d052cfda863d12c",
|
"content-hash": "f7ba581ff6641d3ab79d558070e99f3c",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "asm89/stack-cors",
|
"name": "asm89/stack-cors",
|
||||||
|
@ -3462,16 +3462,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "spatie/laravel-query-builder",
|
"name": "spatie/laravel-query-builder",
|
||||||
"version": "3.5.0",
|
"version": "3.6.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/spatie/laravel-query-builder.git",
|
"url": "https://github.com/spatie/laravel-query-builder.git",
|
||||||
"reference": "4e5257be24139836dc092f618d7c73bcb1c00302"
|
"reference": "03d8e1307dcb58b16fcc9c4947633fc60ae74802"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/spatie/laravel-query-builder/zipball/4e5257be24139836dc092f618d7c73bcb1c00302",
|
"url": "https://api.github.com/repos/spatie/laravel-query-builder/zipball/03d8e1307dcb58b16fcc9c4947633fc60ae74802",
|
||||||
"reference": "4e5257be24139836dc092f618d7c73bcb1c00302",
|
"reference": "03d8e1307dcb58b16fcc9c4947633fc60ae74802",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -3528,7 +3528,7 @@
|
||||||
"type": "custom"
|
"type": "custom"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2021-07-05T14:17:44+00:00"
|
"time": "2021-09-06T08:03:10+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "spatie/laravel-validation-rules",
|
"name": "spatie/laravel-validation-rules",
|
||||||
|
@ -8713,5 +8713,5 @@
|
||||||
"ext-intl": "*"
|
"ext-intl": "*"
|
||||||
},
|
},
|
||||||
"platform-dev": [],
|
"platform-dev": [],
|
||||||
"plugin-api-version": "2.0.0"
|
"plugin-api-version": "2.1.0"
|
||||||
}
|
}
|
||||||
|
|
46
config/query-builder.php
Normal file
46
config/query-builder.php
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @see https://github.com/spatie/laravel-query-builder
|
||||||
|
*/
|
||||||
|
|
||||||
|
return [
|
||||||
|
|
||||||
|
/*
|
||||||
|
* By default the package will use the `include`, `filter`, `sort`
|
||||||
|
* and `fields` query parameters as described in the readme.
|
||||||
|
*
|
||||||
|
* You can customize these query string parameters here.
|
||||||
|
*/
|
||||||
|
'parameters' => [
|
||||||
|
'include' => 'include',
|
||||||
|
|
||||||
|
'filter' => 'filter',
|
||||||
|
|
||||||
|
'sort' => 'sort',
|
||||||
|
|
||||||
|
'fields' => 'fields',
|
||||||
|
|
||||||
|
'append' => 'append',
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Related model counts are included using the relationship name suffixed with this string.
|
||||||
|
* For example: GET /users?include=postsCount
|
||||||
|
*/
|
||||||
|
'count_suffix' => 'Count',
|
||||||
|
|
||||||
|
/*
|
||||||
|
* By default the package will throw an `InvalidFilterQuery` exception when a filter in the
|
||||||
|
* URL is not allowed in the `allowedFilters()` method.
|
||||||
|
*/
|
||||||
|
'disable_invalid_filter_query_exception' => false,
|
||||||
|
|
||||||
|
/*
|
||||||
|
* By default the package inspects query string of request using $request->query().
|
||||||
|
* You can change this behavior to inspect the request body using $request->input()
|
||||||
|
* by setting this value to `body`.
|
||||||
|
*
|
||||||
|
* Possible values: `query_string`, `body`
|
||||||
|
*/
|
||||||
|
'request_data_source' => 'query_string',
|
||||||
|
];
|
Loading…
Reference in a new issue