UserController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Events\UserUpdateCreditsEvent;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\DiscordUser;
  6. use App\Models\User;
  7. use App\Notifications\ReferralNotification;
  8. use App\Traits\Referral;
  9. use Carbon\Carbon;
  10. use Illuminate\Contracts\Foundation\Application;
  11. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  12. use Illuminate\Contracts\Routing\ResponseFactory;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Database\Eloquent\Collection;
  15. use Illuminate\Database\Eloquent\Model;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Http\Response;
  18. use Illuminate\Support\Facades\App;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Hash;
  21. use Illuminate\Support\Str;
  22. use Illuminate\Validation\Rule;
  23. use Illuminate\Validation\ValidationException;
  24. use Spatie\QueryBuilder\QueryBuilder;
  25. class UserController extends Controller
  26. {
  27. use Referral;
  28. const ALLOWED_INCLUDES = ['servers', 'notifications', 'payments', 'vouchers', 'discordUser'];
  29. const ALLOWED_FILTERS = ['name', 'server_limit', 'email', 'pterodactyl_id', 'role', 'suspended'];
  30. /**
  31. * Display a listing of the resource.
  32. *
  33. * @param Request $request
  34. * @return LengthAwarePaginator
  35. */
  36. public function index(Request $request)
  37. {
  38. $query = QueryBuilder::for(User::class)
  39. ->allowedIncludes(self::ALLOWED_INCLUDES)
  40. ->allowedFilters(self::ALLOWED_FILTERS);
  41. return $query->paginate($request->input('per_page') ?? 50);
  42. }
  43. /**
  44. * Display the specified resource.
  45. *
  46. * @param int $id
  47. * @return User|Builder|Collection|Model
  48. */
  49. public function show(int $id)
  50. {
  51. $discordUser = DiscordUser::find($id);
  52. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  53. $query = QueryBuilder::for($user)
  54. ->with('discordUser')
  55. ->allowedIncludes(self::ALLOWED_INCLUDES)
  56. ->where('users.id', '=', $id)
  57. ->orWhereHas('discordUser', function (Builder $builder) use ($id) {
  58. $builder->where('id', '=', $id);
  59. });
  60. return $query->firstOrFail();
  61. }
  62. /**
  63. * Update the specified resource in storage.
  64. *
  65. * @param Request $request
  66. * @param int $id
  67. * @return User
  68. */
  69. public function update(Request $request, int $id)
  70. {
  71. $discordUser = DiscordUser::find($id);
  72. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  73. $request->validate([
  74. 'name' => 'sometimes|string|min:4|max:30',
  75. 'email' => 'sometimes|string|email',
  76. 'credits' => 'sometimes|numeric|min:0|max:1000000',
  77. 'server_limit' => 'sometimes|numeric|min:0|max:1000000',
  78. 'role' => ['sometimes', Rule::in(['admin', 'moderator', 'client', 'member'])],
  79. ]);
  80. event(new UserUpdateCreditsEvent($user));
  81. //Update Users Password on Pterodactyl
  82. //Username,Mail,First and Lastname are required aswell
  83. $response = $this->pterodactyl->client_admin->patch('/application/users/' . $user->pterodactyl_id, [
  84. 'username' => $request->name,
  85. 'first_name' => $request->name,
  86. 'last_name' => $request->name,
  87. 'email' => $request->email,
  88. ]);
  89. if ($response->failed()) {
  90. throw ValidationException::withMessages([
  91. 'pterodactyl_error_message' => $response->toException()->getMessage(),
  92. 'pterodactyl_error_status' => $response->toException()->getCode(),
  93. ]);
  94. }
  95. $user->update($request->all());
  96. return $user;
  97. }
  98. /**
  99. * increments the users credits or/and server_limit
  100. *
  101. * @param Request $request
  102. * @param int $id
  103. * @return User
  104. *
  105. * @throws ValidationException
  106. */
  107. public function increment(Request $request, int $id)
  108. {
  109. $discordUser = DiscordUser::find($id);
  110. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  111. $request->validate([
  112. 'credits' => 'sometimes|numeric|min:0|max:1000000',
  113. 'server_limit' => 'sometimes|numeric|min:0|max:1000000',
  114. ]);
  115. if ($request->credits) {
  116. if ($user->credits + $request->credits >= 99999999) {
  117. throw ValidationException::withMessages([
  118. 'credits' => "You can't add this amount of credits because you would exceed the credit limit",
  119. ]);
  120. }
  121. event(new UserUpdateCreditsEvent($user));
  122. $user->increment('credits', $request->credits);
  123. }
  124. if ($request->server_limit) {
  125. if ($user->server_limit + $request->server_limit >= 2147483647) {
  126. throw ValidationException::withMessages([
  127. 'server_limit' => 'You cannot add this amount of servers because it would exceed the server limit.',
  128. ]);
  129. }
  130. $user->increment('server_limit', $request->server_limit);
  131. }
  132. return $user;
  133. }
  134. /**
  135. * decrements the users credits or/and server_limit
  136. *
  137. * @param Request $request
  138. * @param int $id
  139. * @return User
  140. *
  141. * @throws ValidationException
  142. */
  143. public function decrement(Request $request, int $id)
  144. {
  145. $discordUser = DiscordUser::find($id);
  146. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  147. $request->validate([
  148. 'credits' => 'sometimes|numeric|min:0|max:1000000',
  149. 'server_limit' => 'sometimes|numeric|min:0|max:1000000',
  150. ]);
  151. if ($request->credits) {
  152. if ($user->credits - $request->credits < 0) {
  153. throw ValidationException::withMessages([
  154. 'credits' => "You can't remove this amount of credits because you would exceed the minimum credit limit",
  155. ]);
  156. }
  157. $user->decrement('credits', $request->credits);
  158. }
  159. if ($request->server_limit) {
  160. if ($user->server_limit - $request->server_limit < 0) {
  161. throw ValidationException::withMessages([
  162. 'server_limit' => 'You cannot remove this amount of servers because it would exceed the minimum server.',
  163. ]);
  164. }
  165. $user->decrement('server_limit', $request->server_limit);
  166. }
  167. return $user;
  168. }
  169. /**
  170. * Suspends the user
  171. *
  172. * @param Request $request
  173. * @param int $id
  174. * @return bool
  175. *
  176. * @throws ValidationException
  177. */
  178. public function suspend(int $id)
  179. {
  180. $discordUser = DiscordUser::find($id);
  181. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  182. if ($user->isSuspended()) {
  183. throw ValidationException::withMessages([
  184. 'error' => 'The user is already suspended',
  185. ]);
  186. }
  187. $user->suspend();
  188. return $user;
  189. }
  190. /**
  191. * Unsuspend the user
  192. *
  193. * @param Request $request
  194. * @param int $id
  195. * @return bool
  196. *
  197. * @throws ValidationException
  198. */
  199. public function unsuspend(int $id)
  200. {
  201. $discordUser = DiscordUser::find($id);
  202. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  203. if (!$user->isSuspended()) {
  204. throw ValidationException::withMessages([
  205. 'error' => 'You cannot unsuspend an User who is not suspended.',
  206. ]);
  207. }
  208. $user->unSuspend();
  209. return $user;
  210. }
  211. /**
  212. * @throws ValidationException
  213. */
  214. public function store(Request $request)
  215. {
  216. $request->validate([
  217. 'name' => ['required', 'string', 'max:30', 'min:4', 'alpha_num', 'unique:users'],
  218. 'email' => ['required', 'string', 'email', 'max:64', 'unique:users'],
  219. 'password' => ['required', 'string', 'min:8', 'max:191'],
  220. ]);
  221. // Prevent the creation of new users via API if this is enabled.
  222. if (!config('SETTINGS::SYSTEM:CREATION_OF_NEW_USERS', 'true')) {
  223. throw ValidationException::withMessages([
  224. 'error' => 'The creation of new users has been blocked by the system administrator.',
  225. ]);
  226. }
  227. $user = User::create([
  228. 'name' => $request->input('name'),
  229. 'email' => $request->input('email'),
  230. 'credits' => config('SETTINGS::USER:INITIAL_CREDITS', 150),
  231. 'server_limit' => config('SETTINGS::USER:INITIAL_SERVER_LIMIT', 1),
  232. 'password' => Hash::make($request->input('password')),
  233. 'referral_code' => $this->createReferralCode(),
  234. ]);
  235. $response = $this->pterodactyl->client_admin->post('/application/users', [
  236. 'external_id' => App::environment('local') ? Str::random(16) : (string) $user->id,
  237. 'username' => $user->name,
  238. 'email' => $user->email,
  239. 'first_name' => $user->name,
  240. 'last_name' => $user->name,
  241. 'password' => $request->input('password'),
  242. 'root_admin' => false,
  243. 'language' => 'en',
  244. ]);
  245. if ($response->failed()) {
  246. $user->delete();
  247. throw ValidationException::withMessages([
  248. 'pterodactyl_error_message' => $response->toException()->getMessage(),
  249. 'pterodactyl_error_status' => $response->toException()->getCode(),
  250. ]);
  251. }
  252. $user->update([
  253. 'pterodactyl_id' => $response->json()['attributes']['id'],
  254. ]);
  255. //INCREMENT REFERRAL-USER CREDITS
  256. if (!empty($request->input('referral_code'))) {
  257. $ref_code = $request->input('referral_code');
  258. $new_user = $user->id;
  259. if ($ref_user = User::query()->where('referral_code', '=', $ref_code)->first()) {
  260. if (config('SETTINGS::REFERRAL:MODE') == 'register' || config('SETTINGS::REFERRAL:MODE') == 'both') {
  261. $ref_user->increment('credits', config('SETTINGS::REFERRAL::REWARD'));
  262. $ref_user->notify(new ReferralNotification($ref_user->id, $new_user));
  263. }
  264. //INSERT INTO USER_REFERRALS TABLE
  265. DB::table('user_referrals')->insert([
  266. 'referral_id' => $ref_user->id,
  267. 'registered_user_id' => $user->id,
  268. 'created_at' => Carbon::now(),
  269. 'updated_at' => Carbon::now(),
  270. ]);
  271. }
  272. }
  273. $user->sendEmailVerificationNotification();
  274. return $user;
  275. }
  276. /**
  277. * Remove the specified resource from storage.
  278. *
  279. * @param int $id
  280. * @return Application|Response|ResponseFactory
  281. */
  282. public function destroy(int $id)
  283. {
  284. $discordUser = DiscordUser::find($id);
  285. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  286. $user->delete();
  287. return response($user, 200);
  288. }
  289. }