From bd77efe4b47835295e845b5fefd3c1e3ce760607 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ant=C3=B3nio=20Duarte?=
<72362116+Hiekki4@users.noreply.github.com>
Date: Fri, 23 Jul 2021 00:07:01 +0100
Subject: [PATCH 01/37] Update main.blade.php
---
resources/views/layouts/main.blade.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/resources/views/layouts/main.blade.php b/resources/views/layouts/main.blade.php
index c9ff4bfd..b9f0a780 100644
--- a/resources/views/layouts/main.blade.php
+++ b/resources/views/layouts/main.blade.php
@@ -316,7 +316,7 @@
From 28ab548094c139d2c5487bcba4edf6f2ba1bf26e Mon Sep 17 00:00:00 2001
From: Hiekki <72362116+Hiekki4@users.noreply.github.com>
Date: Fri, 23 Jul 2021 20:41:49 +0100
Subject: [PATCH 02/37] credits endpoint [WORK IN PROGRESS]
---
app/Http/Controllers/Api/UserController.php | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index 7c905328..aa982e19 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -63,6 +63,27 @@ class UserController extends Controller
return $user;
}
+ /**
+ * Give credits to a user.
+ *
+ * @param Request $request
+ * @param int $id
+ * @return User
+ */
+ public function addCredits(Request $request, int $id)
+ {
+ $discordUser = DiscordUser::find($id);
+ $user = $discordUser ? $discordUser->user : User::findOrFail($id);
+
+ $request->validate([
+ "credits" => "require|numeric|min:0|max:1000000",
+ ]);
+
+ $user->increment('credits', $request);
+
+ return $user;
+ }
+
/**
* Remove the specified resource from storage.
*
From 32f6fb51985809abcf4d8a6f13ad86a0bf289f4f Mon Sep 17 00:00:00 2001
From: Hiekki <72362116+Hiekki4@users.noreply.github.com>
Date: Sun, 25 Jul 2021 17:07:03 +0100
Subject: [PATCH 03/37] AddCredits endpoint is working
---
app/Http/Controllers/Api/UserController.php | 4 ++--
routes/api.php | 1 +
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index aa982e19..a78b8f7f 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -76,10 +76,10 @@ class UserController extends Controller
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
$request->validate([
- "credits" => "require|numeric|min:0|max:1000000",
+ "credits" => "required|numeric|min:0|max:1000000",
]);
- $user->increment('credits', $request);
+ $user->increment('credits', $request->credits);
return $user;
}
diff --git a/routes/api.php b/routes/api.php
index 5ffa4d9d..8afc6587 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -17,6 +17,7 @@ use Illuminate\Support\Facades\Route;
*/
Route::middleware('api.token')->group(function () {
+ Route::patch('/users/{server}/addcredits', [UserController::class, 'addCredits']);
Route::resource('users', UserController::class)->except(['store', 'create']);
Route::patch('/servers/{server}/suspend', [ServerController::class, 'suspend']);
From 87f57af51007ddb074af1a3c8b1328c3b08c715a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ant=C3=B3nio=20Duarte?=
<72362116+Hiekki4@users.noreply.github.com>
Date: Sun, 25 Jul 2021 19:04:44 +0100
Subject: [PATCH 04/37] Update api.php
---
routes/api.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/routes/api.php b/routes/api.php
index 8afc6587..3f475be8 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -17,7 +17,7 @@ use Illuminate\Support\Facades\Route;
*/
Route::middleware('api.token')->group(function () {
- Route::patch('/users/{server}/addcredits', [UserController::class, 'addCredits']);
+ Route::patch('/users/{user}/addcredits', [UserController::class, 'addCredits']);
Route::resource('users', UserController::class)->except(['store', 'create']);
Route::patch('/servers/{server}/suspend', [ServerController::class, 'suspend']);
From dbf389e71751538bb71495be145f146eacc8cf2b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ant=C3=B3nio=20Duarte?=
<72362116+Hiekki4@users.noreply.github.com>
Date: Sun, 25 Jul 2021 19:19:59 +0100
Subject: [PATCH 05/37] Update UserController.php
---
app/Http/Controllers/Api/UserController.php | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index a78b8f7f..dac3d412 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -78,7 +78,11 @@ class UserController extends Controller
$request->validate([
"credits" => "required|numeric|min:0|max:1000000",
]);
-
+
+ if ($request->user()->credits + $request->credits >= 99999999) throw ValidationException::withMessages([
+ 'code' => "You can't add this amount of credits because you would exceed the credit limit"
+ ]);
+
$user->increment('credits', $request->credits);
return $user;
From 57edcd0b571b3e89c24a54e4a60073e266d78712 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ant=C3=B3nio=20Duarte?=
<72362116+Hiekki4@users.noreply.github.com>
Date: Sun, 25 Jul 2021 19:20:43 +0100
Subject: [PATCH 06/37] Update UserController.php
---
app/Http/Controllers/Api/UserController.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index dac3d412..aed2c10c 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -80,7 +80,7 @@ class UserController extends Controller
]);
if ($request->user()->credits + $request->credits >= 99999999) throw ValidationException::withMessages([
- 'code' => "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"
]);
$user->increment('credits', $request->credits);
From f331049ec03c35e0e6534bfdde6861d0507cb083 Mon Sep 17 00:00:00 2001
From: Hiekki <72362116+Hiekki4@users.noreply.github.com>
Date: Sun, 25 Jul 2021 23:38:12 +0100
Subject: [PATCH 07/37] AVMG
---
app/Http/Controllers/Api/UserController.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index aed2c10c..55c2c595 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -78,8 +78,8 @@ class UserController extends Controller
$request->validate([
"credits" => "required|numeric|min:0|max:1000000",
]);
-
- if ($request->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"
]);
From 07e4537e01189a5a9fc2e2f3ae92aa6927295a1e Mon Sep 17 00:00:00 2001
From: Jovan Jovanovic
Date: Mon, 26 Jul 2021 01:05:26 +0200
Subject: [PATCH 08/37] add trustedproxy config
---
bootstrap/cache/.gitignore | 0
config/trustedproxy.php | 54 +++++++++++++++++++++++++
storage/app/.gitignore | 0
storage/app/public/.gitignore | 0
storage/framework/.gitignore | 0
storage/framework/cache/.gitignore | 0
storage/framework/cache/data/.gitignore | 0
storage/framework/sessions/.gitignore | 0
storage/framework/testing/.gitignore | 0
storage/framework/views/.gitignore | 0
storage/logs/.gitignore | 0
11 files changed, 54 insertions(+)
mode change 100644 => 100755 bootstrap/cache/.gitignore
create mode 100755 config/trustedproxy.php
mode change 100644 => 100755 storage/app/.gitignore
mode change 100644 => 100755 storage/app/public/.gitignore
mode change 100644 => 100755 storage/framework/.gitignore
mode change 100644 => 100755 storage/framework/cache/.gitignore
mode change 100644 => 100755 storage/framework/cache/data/.gitignore
mode change 100644 => 100755 storage/framework/sessions/.gitignore
mode change 100644 => 100755 storage/framework/testing/.gitignore
mode change 100644 => 100755 storage/framework/views/.gitignore
mode change 100644 => 100755 storage/logs/.gitignore
diff --git a/bootstrap/cache/.gitignore b/bootstrap/cache/.gitignore
old mode 100644
new mode 100755
diff --git a/config/trustedproxy.php b/config/trustedproxy.php
new file mode 100755
index 00000000..8cfd785e
--- /dev/null
+++ b/config/trustedproxy.php
@@ -0,0 +1,54 @@
+getClientIp()
+ * always gets the originating client IP, no matter
+ * how many proxies that client's request has
+ * subsequently passed through.
+ */
+ 'proxies' => in_array(env('TRUSTED_PROXIES', []), ['*', '**']) ?
+ env('TRUSTED_PROXIES') : explode(',', env('TRUSTED_PROXIES', null)),
+
+ /*
+ * Or, to trust all proxies that connect
+ * directly to your server, uncomment this:
+ */
+ // 'proxies' => '*',
+
+ /*
+ * Or, to trust ALL proxies, including those that
+ * are in a chain of forwarding, uncomment this:
+ */
+ // 'proxies' => '**',
+
+ /*
+ * Default Header Names
+ *
+ * Change these if the proxy does
+ * not send the default header names.
+ *
+ * Note that headers such as X-Forwarded-For
+ * are transformed to HTTP_X_FORWARDED_FOR format.
+ *
+ * The following are Symfony defaults, found in
+ * \Symfony\Component\HttpFoundation\Request::$trustedHeaders
+ */
+ 'headers' => \Illuminate\Http\Request::HEADER_X_FORWARDED_ALL,
+];
\ No newline at end of file
diff --git a/storage/app/.gitignore b/storage/app/.gitignore
old mode 100644
new mode 100755
diff --git a/storage/app/public/.gitignore b/storage/app/public/.gitignore
old mode 100644
new mode 100755
diff --git a/storage/framework/.gitignore b/storage/framework/.gitignore
old mode 100644
new mode 100755
diff --git a/storage/framework/cache/.gitignore b/storage/framework/cache/.gitignore
old mode 100644
new mode 100755
diff --git a/storage/framework/cache/data/.gitignore b/storage/framework/cache/data/.gitignore
old mode 100644
new mode 100755
diff --git a/storage/framework/sessions/.gitignore b/storage/framework/sessions/.gitignore
old mode 100644
new mode 100755
diff --git a/storage/framework/testing/.gitignore b/storage/framework/testing/.gitignore
old mode 100644
new mode 100755
diff --git a/storage/framework/views/.gitignore b/storage/framework/views/.gitignore
old mode 100644
new mode 100755
diff --git a/storage/logs/.gitignore b/storage/logs/.gitignore
old mode 100644
new mode 100755
From bdc1981c3604a815b4296dfb5151a98d57068362 Mon Sep 17 00:00:00 2001
From: Jovan Jovanovic
Date: Mon, 26 Jul 2021 04:24:55 +0200
Subject: [PATCH 09/37] Add DynamicNotification and notification api endpoints
---
.../Api/NotificationController.php | 139 ++++++++++++++++++
app/Notifications/DynamicNotification.php | 57 +++++++
routes/api.php | 16 +-
3 files changed, 207 insertions(+), 5 deletions(-)
create mode 100644 app/Http/Controllers/Api/NotificationController.php
create mode 100644 app/Notifications/DynamicNotification.php
diff --git a/app/Http/Controllers/Api/NotificationController.php b/app/Http/Controllers/Api/NotificationController.php
new file mode 100644
index 00000000..2b0005fe
--- /dev/null
+++ b/app/Http/Controllers/Api/NotificationController.php
@@ -0,0 +1,139 @@
+user : User::findOrFail($userId);
+
+ return $user->notifications()->paginate($request->query("per_page", 50));
+ }
+
+ /**
+ * Displays a specific notification
+ *
+ * @param int $userId
+ * @param int $notificationId
+ * @return JsonResponse
+ */
+ public function view(int $userId, $notificationId)
+ {
+ $discordUser = DiscordUser::find($userId);
+ $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+ $notification = $user->notifications()->where("id", $notificationId)->get()->first();
+
+ if (!$notification) {
+ return response()->json(["message" => "Notification not found."], 404);
+ }
+
+ return $notification;
+ }
+ /**
+ * Shows all unread notifications of an user.
+ * @param Request $request
+ * @param int $userId
+ * @return Response
+ */
+ public function indexUnread(Request $request, int $userId)
+ {
+ $discordUser = DiscordUser::find($userId);
+ $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+ return $user->unreadNotifications()->paginate($request->query("per_page", 50));
+ }
+
+ /**
+ * Send a notification to an user.
+ *
+ * @param Request $request
+ * @param int $userId
+ * @return JsonResponse
+ */
+ public function send(Request $request, int $userId)
+ {
+ $discordUser = DiscordUser::find($userId);
+ $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+ $body = $request->validate([
+ "title" => "required:string|min:0",
+ "content" => "required:string|min:0"
+ ]);
+
+ $user->notify(
+ new DynamicNotification($body["title"], $body["content"])
+ );
+
+ return response()->json(["message" => "Notification successfully sent."]);
+ }
+
+ /**
+ * Delete all notifications from an user
+ *
+ * @param int $userId
+ * @return JsonResponse
+ */
+ public function delete(int $userId)
+ {
+ $discordUser = DiscordUser::find($userId);
+ $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+ $count = $user->notifications()->delete();
+
+ return response()->json(["message" => "All notifications have been successfully deleted.", "count" => $count]);
+ }
+
+ /**
+ * Delete all read notifications from an user
+ *
+ * @param int $userId
+ * @return JsonResponse
+ */
+ public function deleteRead(int $userId)
+ {
+ $discordUser = DiscordUser::find($userId);
+ $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+ $count = $user->notifications()->whereNotNull("read_at")->delete();
+
+ return response()->json(["message" => "All read notifications have been successfully deleted.", "count" => $count]);
+ }
+
+ /**
+ * Deletes a specific notification
+ *
+ * @param int $userId
+ * @param int $notificationId
+ * @return JsonResponse
+ */
+ public function deleteOne(int $userId, $notificationid)
+ {
+ $discordUser = DiscordUser::find($userId);
+ $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+ $notification = $user->notifications()->where("id", $notificationid)->get()->first();
+
+ if (!$notification) {
+ return response()->json(["message" => "Notification not found."], 404);
+ }
+
+ $notification->delete();
+ return response()->json($notification);
+ }
+}
diff --git a/app/Notifications/DynamicNotification.php b/app/Notifications/DynamicNotification.php
new file mode 100644
index 00000000..0075de13
--- /dev/null
+++ b/app/Notifications/DynamicNotification.php
@@ -0,0 +1,57 @@
+title = $title;
+ $this->content = $content;
+ }
+
+ /**
+ * Get the notification's delivery channels.
+ *
+ * @param mixed $notifiable
+ * @return array
+ */
+ public function via()
+ {
+ return ['database'];
+ }
+
+ /**
+ * Get the array representation of the notification.
+ *
+ * @param mixed $notifiable
+ * @return array
+ */
+ public function toArray()
+ {
+ return [
+ 'title' => $this->title,
+ 'content' => $this->content,
+ ];
+ }
+}
diff --git a/routes/api.php b/routes/api.php
index 5ffa4d9d..88865810 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -1,5 +1,6 @@
group(function () {
Route::patch('/servers/{server}/unsuspend', [ServerController::class, 'unSuspend']);
Route::resource('servers', ServerController::class)->except(['store', 'create', 'edit', 'update']);
-// Route::get('/vouchers/{voucher}/users' , [VoucherController::class , 'users']);
- Route::resource('vouchers', VoucherController::class)->except('create' , 'edit');
+ // Route::get('/vouchers/{voucher}/users' , [VoucherController::class , 'users']);
+ Route::resource('vouchers', VoucherController::class)->except('create', 'edit');
+
+ Route::get('/notifications/{user}', [NotificationController::class, 'index']);
+ Route::get('/notifications/{user}/unread', [NotificationController::class, 'indexUnread']);
+ Route::get('/notifications/{user}/{notification}', [NotificationController::class, 'view']);
+ Route::post('/notifications/{user}', [NotificationController::class, 'send']);
+ Route::delete('/notifications/{user}', [NotificationController::class, 'delete']);
+ Route::delete('/notifications/{user}/read', [NotificationController::class, 'deleteRead']);
+ Route::delete('/notifications/{user}/{notification}', [NotificationController::class, 'deleteOne']);
});
-
-
-
From 696ce64df3bd334bc8db9d1df28eea7aed668b69 Mon Sep 17 00:00:00 2001
From: Hiekki <72362116+Hiekki4@users.noreply.github.com>
Date: Tue, 27 Jul 2021 09:15:10 +0100
Subject: [PATCH 10/37] [WIP] increment
---
app/Http/Controllers/Api/UserController.php | 22 +++++++++++++--------
routes/api.php | 2 +-
2 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index 55c2c595..6946f7e3 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -64,26 +64,32 @@ class UserController extends Controller
}
/**
- * Give credits to a user.
+ *
*
* @param Request $request
* @param int $id
* @return User
*/
- public function addCredits(Request $request, int $id)
+ public function increment(Request $request, int $id)
{
$discordUser = DiscordUser::find($id);
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
$request->validate([
- "credits" => "required|numeric|min:0|max:1000000",
- ]);
-
- 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" => "sometimes|numeric|min:0|max:1000000",
+ "server_limit" => "sometimes|numeric|min:0|max:1000000",
]);
- $user->increment('credits', $request->credits);
+ if($request->credits){
+ 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"
+ ]);
+ $user->increment('credits', $request->credits);
+ }
+
+ if($request->server_limit){
+ $user->increment('server_limit', $request->server_limit);
+ }
return $user;
}
diff --git a/routes/api.php b/routes/api.php
index 3f475be8..7a1f8dae 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -17,7 +17,7 @@ use Illuminate\Support\Facades\Route;
*/
Route::middleware('api.token')->group(function () {
- Route::patch('/users/{user}/addcredits', [UserController::class, 'addCredits']);
+ Route::patch('/users/{user}/increment', [UserController::class, 'increment']);
Route::resource('users', UserController::class)->except(['store', 'create']);
Route::patch('/servers/{server}/suspend', [ServerController::class, 'suspend']);
From b12007aea318a9ff831fa0a25b75bed9b12b5bac Mon Sep 17 00:00:00 2001
From: Hiekki <72362116+Hiekki4@users.noreply.github.com>
Date: Tue, 27 Jul 2021 13:20:31 +0100
Subject: [PATCH 11/37] decrement and increment endpoints
---
app/Http/Controllers/Api/UserController.php | 39 ++++++++++++++++++++-
routes/api.php | 1 +
2 files changed, 39 insertions(+), 1 deletion(-)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index 6946f7e3..702be6c3 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -64,8 +64,8 @@ class UserController extends Controller
}
/**
+ * increments the users credits or/and server_limit
*
- *
* @param Request $request
* @param int $id
* @return User
@@ -88,12 +88,49 @@ class UserController extends Controller
}
if($request->server_limit){
+ 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."
+ ]);
$user->increment('server_limit', $request->server_limit);
}
return $user;
}
+ /**
+ * decrements the users credits or/and server_limit
+ *
+ * @param Request $request
+ * @param int $id
+ * @return User
+ */
+ public function decrement(Request $request, int $id)
+ {
+ $discordUser = DiscordUser::find($id);
+ $user = $discordUser ? $discordUser->user : User::findOrFail($id);
+
+ $request->validate([
+ "credits" => "sometimes|numeric|min:0|max:1000000",
+ "server_limit" => "sometimes|numeric|min:0|max:1000000",
+ ]);
+
+ if($request->credits){
+ if ($user->credits - $request->credits >= 99999999) throw ValidationException::withMessages([
+ 'credits' => "You cannot remove this amount of credits because you would exceed the minimum credit"
+ ]);
+ $user->decrement('credits', $request->credits);
+ }
+
+ if($request->server_limit){
+ if ($user->server_limit - $request->server_limit >= 2147483647) throw ValidationException::withMessages([
+ 'server_limit' => "You cannot remove this amount of servers because it would exceed the minimum server."
+ ]);
+ $user->decrement('server_limit', $request->server_limit);
+ }
+
+ return $user;
+ }
+
/**
* Remove the specified resource from storage.
*
diff --git a/routes/api.php b/routes/api.php
index 7a1f8dae..3c5f1fb9 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -18,6 +18,7 @@ use Illuminate\Support\Facades\Route;
Route::middleware('api.token')->group(function () {
Route::patch('/users/{user}/increment', [UserController::class, 'increment']);
+ Route::patch('/users/{user}/decrement', [UserController::class, 'decrement']);
Route::resource('users', UserController::class)->except(['store', 'create']);
Route::patch('/servers/{server}/suspend', [ServerController::class, 'suspend']);
From a5d030e71002de437a7815039ae10cd034a5933e Mon Sep 17 00:00:00 2001
From: AVMG20
Date: Wed, 28 Jul 2021 10:05:01 +0200
Subject: [PATCH 12/37] improved the make user command to give better feedback
---
app/Classes/Pterodactyl.php | 6 ++--
app/Console/Commands/MakeUserCommand.php | 29 ++++++++++++++-----
app/Http/Controllers/Admin/UserController.php | 3 +-
3 files changed, 25 insertions(+), 13 deletions(-)
diff --git a/app/Classes/Pterodactyl.php b/app/Classes/Pterodactyl.php
index a48b569f..22bce864 100644
--- a/app/Classes/Pterodactyl.php
+++ b/app/Classes/Pterodactyl.php
@@ -11,6 +11,7 @@ use Exception;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
+use Illuminate\Validation\Validator;
class Pterodactyl
{
@@ -34,9 +35,8 @@ class Pterodactyl
*/
public function getUser(int $pterodactylId){
$response = self::client()->get("/application/users/{$pterodactylId}");
- if ($response->failed()) {
- return [];
- }
+
+ if ($response->failed()) return $response->json();
return $response->json()['attributes'];
}
diff --git a/app/Console/Commands/MakeUserCommand.php b/app/Console/Commands/MakeUserCommand.php
index ef3b1372..4d4a7ed5 100644
--- a/app/Console/Commands/MakeUserCommand.php
+++ b/app/Console/Commands/MakeUserCommand.php
@@ -6,6 +6,8 @@ use App\Classes\Pterodactyl;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\ValidationException;
class MakeUserCommand extends Command
{
@@ -46,24 +48,35 @@ class MakeUserCommand extends Command
$ptero_id = $this->option('ptero_id') ?? $this->ask('Please specify your Pterodactyl ID.');
$password = $this->option('password') ?? $this->ask('Please specify your password.');
- if (strlen($password) < 8) {
- $this->alert('Your password need to be at least 8 characters long');
+ // Validate user input
+ $validator = Validator::make([
+ 'ptero_id' => $ptero_id,
+ 'password' => $password,
+ ], [
+ 'ptero_id' => 'required|numeric|integer|min:1|max:2147483647',
+ 'password' => 'required|string|min:8|max:60',
+ ]);
+
+ if ($validator->fails()) {
+ $this->error($validator->errors()->first());
return 0;
}
//TODO: Do something with response (check for status code and give hints based upon that)
$response = $this->pterodactyl->getUser($ptero_id);
- if ($response === []) {
- $this->alert('It seems that your Pterodactyl ID is not correct. Rerun the command and input an correct ID');
+ if (isset($response['errors'])) {
+ if (isset($response['errors'][0]['code'])) $this->error("code: {$response['errors'][0]['code']}");
+ if (isset($response['errors'][0]['status'])) $this->error("status: {$response['errors'][0]['status']}");
+ if (isset($response['errors'][0]['detail'])) $this->error("detail: {$response['errors'][0]['detail']}");
return 0;
}
$user = User::create([
- 'name' => $response['first_name'],
- 'email' => $response['email'],
- 'role' => 'admin',
- 'password' => Hash::make($password),
+ 'name' => $response['first_name'],
+ 'email' => $response['email'],
+ 'role' => 'admin',
+ 'password' => Hash::make($password),
'pterodactyl_id' => $response['id']
]);
diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php
index 496c1bf3..132dc154 100644
--- a/app/Http/Controllers/Admin/UserController.php
+++ b/app/Http/Controllers/Admin/UserController.php
@@ -83,13 +83,12 @@ class UserController extends Controller
"role" => Rule::in(['admin', 'mod', 'client', 'member']),
]);
- if (empty($this->pterodactyl->getUser($request->input('pterodactyl_id')))) {
+ if (isset($this->pterodactyl->getUser($request->input('pterodactyl_id'))['errors'])) {
throw ValidationException::withMessages([
'pterodactyl_id' => ["User does not exists on pterodactyl's panel"]
]);
}
-
if (!is_null($request->input('new_password'))) {
$request->validate([
'new_password' => 'required|string|min:8',
From adba5b1fdbb22137189a09e47c77f322ff3884db Mon Sep 17 00:00:00 2001
From: Hiekki <72362116+Hiekki4@users.noreply.github.com>
Date: Wed, 28 Jul 2021 09:47:10 +0100
Subject: [PATCH 13/37] Validation for negative values
---
app/Http/Controllers/Api/UserController.php | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index 702be6c3..d9fb64fe 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -10,6 +10,7 @@ use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\Rule;
+use Illuminate\Validation\ValidationException;
class UserController extends Controller
{
@@ -116,7 +117,10 @@ class UserController extends Controller
if($request->credits){
if ($user->credits - $request->credits >= 99999999) throw ValidationException::withMessages([
- 'credits' => "You cannot remove this amount of credits because you would exceed the minimum credit"
+ 'credits' => "You cannot remove this amount of credits because you would exceed the minimum credit limit"
+ ]);
+ elseif($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"
]);
$user->decrement('credits', $request->credits);
}
@@ -125,6 +129,9 @@ class UserController extends Controller
if ($user->server_limit - $request->server_limit >= 2147483647) throw ValidationException::withMessages([
'server_limit' => "You cannot remove this amount of servers because it would exceed the minimum server."
]);
+ elseif($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."
+ ]);
$user->decrement('server_limit', $request->server_limit);
}
From 916fbad8af84f4c49fed636baf047b05e90da880 Mon Sep 17 00:00:00 2001
From: Jovan Jovanovic
Date: Wed, 28 Jul 2021 16:56:02 +0200
Subject: [PATCH 14/37] Clean up some code
---
.../Api/NotificationController.php | 34 ++-----------------
.../Controllers/Api/VoucherController.php | 5 +--
routes/api.php | 2 --
3 files changed, 6 insertions(+), 35 deletions(-)
diff --git a/app/Http/Controllers/Api/NotificationController.php b/app/Http/Controllers/Api/NotificationController.php
index 2b0005fe..84e89547 100644
--- a/app/Http/Controllers/Api/NotificationController.php
+++ b/app/Http/Controllers/Api/NotificationController.php
@@ -12,7 +12,7 @@ use Illuminate\Http\Request;
class NotificationController extends Controller
{
/**
- * Displays all notifications of an user.
+ * Display all notifications of an user.
* @param Request $request
* @param int $userId
* @return Response
@@ -26,7 +26,7 @@ class NotificationController extends Controller
}
/**
- * Displays a specific notification
+ * Display a specific notification
*
* @param int $userId
* @param int $notificationId
@@ -45,19 +45,6 @@ class NotificationController extends Controller
return $notification;
}
- /**
- * Shows all unread notifications of an user.
- * @param Request $request
- * @param int $userId
- * @return Response
- */
- public function indexUnread(Request $request, int $userId)
- {
- $discordUser = DiscordUser::find($userId);
- $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
-
- return $user->unreadNotifications()->paginate($request->query("per_page", 50));
- }
/**
* Send a notification to an user.
@@ -99,24 +86,9 @@ class NotificationController extends Controller
return response()->json(["message" => "All notifications have been successfully deleted.", "count" => $count]);
}
- /**
- * Delete all read notifications from an user
- *
- * @param int $userId
- * @return JsonResponse
- */
- public function deleteRead(int $userId)
- {
- $discordUser = DiscordUser::find($userId);
- $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
-
- $count = $user->notifications()->whereNotNull("read_at")->delete();
-
- return response()->json(["message" => "All read notifications have been successfully deleted.", "count" => $count]);
- }
/**
- * Deletes a specific notification
+ * Delete a specific notification
*
* @param int $userId
* @param int $notificationId
diff --git a/app/Http/Controllers/Api/VoucherController.php b/app/Http/Controllers/Api/VoucherController.php
index 9b70277f..99d9507a 100644
--- a/app/Http/Controllers/Api/VoucherController.php
+++ b/app/Http/Controllers/Api/VoucherController.php
@@ -116,7 +116,8 @@ class VoucherController extends Controller
* @param Voucher $voucher
* @return LengthAwarePaginator
*/
- public function users(Request $request, Voucher $voucher){
+ public function users(Request $request, Voucher $voucher)
+ {
$request->validate([
'include' => [
'nullable',
@@ -125,7 +126,7 @@ class VoucherController extends Controller
]
]);
- if($request->input('include') == 'discorduser'){
+ if ($request->input('include') == 'discorduser') {
return $voucher->users()->with('discordUser')->paginate($request->query('per_page') ?? 50);
}
diff --git a/routes/api.php b/routes/api.php
index 88865810..d83ba203 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -28,10 +28,8 @@ Route::middleware('api.token')->group(function () {
Route::resource('vouchers', VoucherController::class)->except('create', 'edit');
Route::get('/notifications/{user}', [NotificationController::class, 'index']);
- Route::get('/notifications/{user}/unread', [NotificationController::class, 'indexUnread']);
Route::get('/notifications/{user}/{notification}', [NotificationController::class, 'view']);
Route::post('/notifications/{user}', [NotificationController::class, 'send']);
Route::delete('/notifications/{user}', [NotificationController::class, 'delete']);
- Route::delete('/notifications/{user}/read', [NotificationController::class, 'deleteRead']);
Route::delete('/notifications/{user}/{notification}', [NotificationController::class, 'deleteOne']);
});
From f0a31fd4e267a999f3ea478473cd5c8e193fdfd6 Mon Sep 17 00:00:00 2001
From: Jovan Jovanovic
Date: Wed, 28 Jul 2021 18:22:12 +0200
Subject: [PATCH 15/37] Add support to add time for voucher expires_at
---
.../Controllers/Admin/VoucherController.php | 11 +-
.../Controllers/Api/VoucherController.php | 9 +-
app/Providers/AppServiceProvider.php | 23 ++
.../views/admin/vouchers/create.blade.php | 289 +++++++++--------
resources/views/admin/vouchers/edit.blade.php | 292 +++++++++---------
5 files changed, 310 insertions(+), 314 deletions(-)
diff --git a/app/Http/Controllers/Admin/VoucherController.php b/app/Http/Controllers/Admin/VoucherController.php
index e1a71580..b0955d3a 100644
--- a/app/Http/Controllers/Admin/VoucherController.php
+++ b/app/Http/Controllers/Admin/VoucherController.php
@@ -48,7 +48,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
- 'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:10 years"],
+ 'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years',
]);
Voucher::create($request->except('_token'));
@@ -75,7 +75,7 @@ class VoucherController extends Controller
*/
public function edit(Voucher $voucher)
{
- return view('admin.vouchers.edit' , [
+ return view('admin.vouchers.edit', [
'voucher' => $voucher
]);
}
@@ -94,7 +94,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
- 'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:10 years"],
+ 'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years',
]);
$voucher->update($request->except('_token'));
@@ -127,7 +127,7 @@ class VoucherController extends Controller
]);
#get voucher by code
- $voucher = Voucher::where('code' , '=' , $request->input('code'))->firstOrFail();
+ $voucher = Voucher::where('code', '=', $request->input('code'))->firstOrFail();
#extra validations
if ($voucher->getStatus() == 'USES_LIMIT_REACHED') throw ValidationException::withMessages([
@@ -138,7 +138,7 @@ class VoucherController extends Controller
'code' => 'This voucher has expired'
]);
- if (!$request->user()->vouchers()->where('id' , '=' , $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([
+ if (!$request->user()->vouchers()->where('id', '=', $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([
'code' => 'You already redeemed this voucher code'
]);
@@ -191,5 +191,4 @@ class VoucherController extends Controller
->rawColumns(['actions', 'code', 'status'])
->make();
}
-
}
diff --git a/app/Http/Controllers/Api/VoucherController.php b/app/Http/Controllers/Api/VoucherController.php
index 9b70277f..9b9e06e8 100644
--- a/app/Http/Controllers/Api/VoucherController.php
+++ b/app/Http/Controllers/Api/VoucherController.php
@@ -44,7 +44,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
- 'expires_at' => 'nullable|date_format:d-m-Y|after:today|before:10 years'
+ 'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years'
]);
return Voucher::create($request->all());
@@ -88,7 +88,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
- 'expires_at' => 'nullable|date_format:d-m-Y|after:today|before:10 years'
+ 'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years'
]);
$voucher->update($request->all());
@@ -116,7 +116,8 @@ class VoucherController extends Controller
* @param Voucher $voucher
* @return LengthAwarePaginator
*/
- public function users(Request $request, Voucher $voucher){
+ public function users(Request $request, Voucher $voucher)
+ {
$request->validate([
'include' => [
'nullable',
@@ -125,7 +126,7 @@ class VoucherController extends Controller
]
]);
- if($request->input('include') == 'discorduser'){
+ if ($request->input('include') == 'discorduser') {
return $voucher->users()->with('discordUser')->paginate($request->query('per_page') ?? 50);
}
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index c220bc7f..84f68299 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -4,6 +4,7 @@ namespace App\Providers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Schema;
+use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@@ -27,5 +28,27 @@ class AppServiceProvider extends ServiceProvider
{
Paginator::useBootstrap();
Schema::defaultStringLength(191);
+
+ Validator::extend('multiple_date_format', function ($attribute, $value, $parameters, $validator) {
+
+ $ok = true;
+
+ $result = [];
+
+ // iterate through all formats
+ foreach ($parameters as $parameter) {
+
+ //validate with laravels standard date format validation
+ $result[] = $validator->validateDateFormat($attribute, $value, [$parameter]);
+ }
+
+ //if none of result array is true. it sets ok to false
+ if (!in_array(true, $result)) {
+ $ok = false;
+ $validator->setCustomMessages(['multiple_date_format' => 'The format must be one of ' . join(",", $parameters)]);
+ }
+
+ return $ok;
+ });
}
}
diff --git a/resources/views/admin/vouchers/create.blade.php b/resources/views/admin/vouchers/create.blade.php
index 1aca487d..430c62b1 100644
--- a/resources/views/admin/vouchers/create.blade.php
+++ b/resources/views/admin/vouchers/create.blade.php
@@ -1,181 +1,170 @@
@extends('layouts.main')
@section('content')
-
-