UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Classes\Pterodactyl;
  4. use App\Events\UserUpdateCreditsEvent;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\User;
  7. use App\Notifications\DynamicNotification;
  8. use Exception;
  9. use Illuminate\Contracts\Foundation\Application;
  10. use Illuminate\Contracts\View\Factory;
  11. use Illuminate\Contracts\View\View;
  12. use Illuminate\Http\RedirectResponse;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Http\Response;
  15. use Illuminate\Notifications\Messages\MailMessage;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Hash;
  19. use Illuminate\Support\Facades\Notification;
  20. use Illuminate\Support\HtmlString;
  21. use Illuminate\Validation\Rule;
  22. use Illuminate\Validation\ValidationException;
  23. use Spatie\QueryBuilder\QueryBuilder;
  24. class UserController extends Controller
  25. {
  26. private Pterodactyl $pterodactyl;
  27. public function __construct(Pterodactyl $pterodactyl)
  28. {
  29. $this->pterodactyl = $pterodactyl;
  30. }
  31. /**
  32. * Display a listing of the resource.
  33. *
  34. * @param Request $request
  35. * @return Application|Factory|View|Response
  36. */
  37. public function index(Request $request)
  38. {
  39. return view('admin.users.index');
  40. }
  41. /**
  42. * Display the specified resource.
  43. *
  44. * @param User $user
  45. * @return Application|Factory|View|Response
  46. */
  47. public function show(User $user)
  48. {
  49. //QUERY ALL REFERRALS A USER HAS
  50. //i am not proud of this at all.
  51. $allReferals = [];
  52. $referrals = DB::table('user_referrals')->where('referral_id', '=', $user->id)->get();
  53. foreach ($referrals as $referral) {
  54. array_push($allReferals, $allReferals['id'] = User::query()->findOrFail($referral->registered_user_id));
  55. }
  56. array_pop($allReferals);
  57. return view('admin.users.show')->with([
  58. 'user' => $user,
  59. 'referrals' => $allReferals,
  60. ]);
  61. }
  62. /**
  63. * Get a JSON response of users.
  64. *
  65. * @return \Illuminate\Support\Collection|\App\models\User
  66. */
  67. public function json(Request $request)
  68. {
  69. $users = QueryBuilder::for(User::query())
  70. ->allowedFilters(['id', 'name', 'pterodactyl_id', 'email'])
  71. ->paginate(25);
  72. if ($request->query('user_id')) {
  73. $user = User::query()->findOrFail($request->input('user_id'));
  74. $user->avatarUrl = $user->getAvatar();
  75. return $user;
  76. }
  77. return $users->map(function ($item) {
  78. $item->avatarUrl = $item->getAvatar();
  79. return $item;
  80. });
  81. }
  82. /**
  83. * Show the form for editing the specified resource.
  84. *
  85. * @param User $user
  86. * @return Application|Factory|View|Response
  87. */
  88. public function edit(User $user)
  89. {
  90. return view('admin.users.edit')->with([
  91. 'user' => $user,
  92. ]);
  93. }
  94. /**
  95. * Update the specified resource in storage.
  96. *
  97. * @param Request $request
  98. * @param User $user
  99. * @return RedirectResponse
  100. *
  101. * @throws Exception
  102. */
  103. public function update(Request $request, User $user)
  104. {
  105. $request->validate([
  106. 'name' => 'required|string|min:4|max:30',
  107. 'pterodactyl_id' => "required|numeric|unique:users,pterodactyl_id,{$user->id}",
  108. 'email' => 'required|string|email',
  109. 'credits' => 'required|numeric|min:0|max:99999999',
  110. 'server_limit' => 'required|numeric|min:0|max:1000000',
  111. 'role' => Rule::in(['admin', 'moderator', 'client', 'member']),
  112. 'referral_code' => "required|string|min:2|max:32|unique:users,referral_code,{$user->id}",
  113. ]);
  114. if (isset($this->pterodactyl->getUser($request->input('pterodactyl_id'))['errors'])) {
  115. throw ValidationException::withMessages([
  116. 'pterodactyl_id' => [__("User does not exists on pterodactyl's panel")],
  117. ]);
  118. }
  119. if (!is_null($request->input('new_password'))) {
  120. $request->validate([
  121. 'new_password' => 'required|string|min:8',
  122. 'new_password_confirmation' => 'required|same:new_password',
  123. ]);
  124. $user->update([
  125. 'password' => Hash::make($request->input('new_password')),
  126. ]);
  127. }
  128. $user->update($request->all());
  129. event(new UserUpdateCreditsEvent($user));
  130. return redirect()->route('admin.users.index')->with('success', 'User updated!');
  131. }
  132. /**
  133. * Remove the specified resource from storage.
  134. *
  135. * @param User $user
  136. * @return RedirectResponse
  137. */
  138. public function destroy(User $user)
  139. {
  140. $user->delete();
  141. return redirect()->back()->with('success', __('user has been removed!'));
  142. }
  143. /**
  144. * Verifys the users email
  145. *
  146. * @param User $user
  147. * @return RedirectResponse
  148. */
  149. public function verifyEmail(Request $request, User $user)
  150. {
  151. $user->verifyEmail();
  152. return redirect()->back()->with('success', __('Email has been verified!'));
  153. }
  154. /**
  155. * @param Request $request
  156. * @param User $user
  157. * @return RedirectResponse
  158. */
  159. public function loginAs(Request $request, User $user)
  160. {
  161. $request->session()->put('previousUser', Auth::user()->id);
  162. Auth::login($user);
  163. return redirect()->route('home');
  164. }
  165. /**
  166. * @param Request $request
  167. * @return RedirectResponse
  168. */
  169. public function logBackIn(Request $request)
  170. {
  171. Auth::loginUsingId($request->session()->get('previousUser'), true);
  172. $request->session()->remove('previousUser');
  173. return redirect()->route('admin.users.index');
  174. }
  175. /**
  176. * Show the form for seding notifications to the specified resource.
  177. *
  178. * @param User $user
  179. * @return Application|Factory|View|Response
  180. */
  181. public function notifications(User $user)
  182. {
  183. return view('admin.users.notifications');
  184. }
  185. /**
  186. * Notify the specified resource.
  187. *
  188. * @param Request $request
  189. * @param User $user
  190. * @return RedirectResponse
  191. *
  192. * @throws Exception
  193. */
  194. public function notify(Request $request)
  195. {
  196. $data = $request->validate([
  197. 'via' => 'required|min:1|array',
  198. 'via.*' => 'required|string|in:mail,database',
  199. 'all' => 'required_without:users|boolean',
  200. 'users' => 'required_without:all|min:1|array',
  201. 'users.*' => 'exists:users,id',
  202. 'title' => 'required|string|min:1',
  203. 'content' => 'required|string|min:1',
  204. ]);
  205. $mail = null;
  206. $database = null;
  207. if (in_array('database', $data['via'])) {
  208. $database = [
  209. 'title' => $data['title'],
  210. 'content' => $data['content'],
  211. ];
  212. }
  213. if (in_array('mail', $data['via'])) {
  214. $mail = (new MailMessage)
  215. ->subject($data['title'])
  216. ->line(new HtmlString($data['content']));
  217. }
  218. $all = $data['all'] ?? false;
  219. $users = $all ? User::all() : User::whereIn('id', $data['users'])->get();
  220. Notification::send($users, new DynamicNotification($data['via'], $database, $mail));
  221. return redirect()->route('admin.users.notifications')->with('success', __('Notification sent!'));
  222. }
  223. /**
  224. * @param User $user
  225. * @return RedirectResponse
  226. */
  227. public function toggleSuspended(User $user)
  228. {
  229. try {
  230. !$user->isSuspended() ? $user->suspend() : $user->unSuspend();
  231. } catch (Exception $exception) {
  232. return redirect()->back()->with('error', $exception->getMessage());
  233. }
  234. return redirect()->back()->with('success', __('User has been updated!'));
  235. }
  236. /**
  237. * @throws Exception
  238. */
  239. public function dataTable(Request $request)
  240. {
  241. $query = User::with('discordUser')->withCount('servers');
  242. // manually count referrals in user_referrals table
  243. $query->selectRaw('users.*, (SELECT COUNT(*) FROM user_referrals WHERE user_referrals.referral_id = users.id) as referrals_count');
  244. return datatables($query)
  245. ->addColumn('avatar', function (User $user) {
  246. return '<img width="28px" height="28px" class="rounded-circle ml-1" src="' . $user->getAvatar() . '">';
  247. })
  248. ->addColumn('credits', function (User $user) {
  249. return '<i class="fas fa-coins mr-2"></i> ' . $user->credits();
  250. })
  251. ->addColumn('verified', function (User $user) {
  252. return $user->getVerifiedStatus();
  253. })
  254. ->addColumn('discordId', function (User $user) {
  255. return $user->discordUser ? $user->discordUser->id : '';
  256. })
  257. ->addColumn('actions', function (User $user) {
  258. $suspendColor = $user->isSuspended() ? 'btn-success' : 'btn-warning';
  259. $suspendIcon = $user->isSuspended() ? 'fa-play-circle' : 'fa-pause-circle';
  260. $suspendText = $user->isSuspended() ? __('Unsuspend') : __('Suspend');
  261. return '
  262. <a data-content="' . __('Login as User') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.users.loginas', $user->id) . '" class="btn btn-sm btn-primary mr-1"><i class="fas fa-sign-in-alt"></i></a>
  263. <a data-content="' . __('Verify') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.users.verifyEmail', $user->id) . '" class="btn btn-sm btn-secondary mr-1"><i class="fas fa-envelope"></i></a>
  264. <a data-content="' . __('Show') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.users.show', $user->id) . '" class="btn btn-sm text-white btn-warning mr-1"><i class="fas fa-eye"></i></a>
  265. <a data-content="' . __('Edit') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.users.edit', $user->id) . '" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
  266. <form class="d-inline" method="post" action="' . route('admin.users.togglesuspend', $user->id) . '">
  267. ' . csrf_field() . '
  268. <button data-content="' . $suspendText . '" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm ' . $suspendColor . ' text-white mr-1"><i class="far ' . $suspendIcon . '"></i></button>
  269. </form>
  270. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.users.destroy', $user->id) . '">
  271. ' . csrf_field() . '
  272. ' . method_field('DELETE') . '
  273. <button data-content="' . __('Delete') . '" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
  274. </form>
  275. ';
  276. })
  277. ->editColumn('role', function (User $user) {
  278. switch ($user->role) {
  279. case 'admin':
  280. $badgeColor = 'badge-danger';
  281. break;
  282. case 'moderator':
  283. $badgeColor = 'badge-info';
  284. break;
  285. case 'client':
  286. $badgeColor = 'badge-success';
  287. break;
  288. default:
  289. $badgeColor = 'badge-secondary';
  290. break;
  291. }
  292. return '<span class="badge ' . $badgeColor . '">' . $user->role . '</span>';
  293. })
  294. ->editColumn('last_seen', function (User $user) {
  295. return $user->last_seen ? $user->last_seen->diffForHumans() : __('Never');
  296. })
  297. ->editColumn('name', function (User $user) {
  298. return '<a class="text-info" target="_blank" href="' . config('SETTINGS::SYSTEM:PTERODACTYL:URL') . '/admin/users/view/' . $user->pterodactyl_id . '">' . strip_tags($user->name) . '</a>';
  299. })
  300. ->rawColumns(['avatar', 'name', 'credits', 'role', 'usage', 'actions'])
  301. ->make();
  302. }
  303. }