UserController.php 12 KB

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