Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Department;
  4. use App\Faculty;
  5. use App\User;
  6. use Exception;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Gate;
  11. class UserController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function index()
  19. {
  20. //
  21. }
  22. /**
  23. * Show the form for creating a new resource.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function create()
  28. {
  29. // DB::enableQueryLog();
  30. $user = Auth::user()->loadMissing(['faculties.departments', 'departments']);
  31. if ($user->is_admin) {
  32. $faculties = Faculty::all();
  33. $departments = Department::all();
  34. } else {
  35. $faculties = $user->faculties;
  36. $departments = $user->departments->keyBy('id');
  37. foreach($faculties as $faculty) {
  38. $departments = $departments->union($faculty->departments->keyBy('id'));
  39. }
  40. }
  41. return view('dashboard.register', compact('faculties', 'departments'));
  42. }
  43. /**
  44. * Store a newly created resource in storage.
  45. *
  46. * @param \Illuminate\Http\Request $request
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function store(Request $request)
  50. {
  51. // dump($request);
  52. $userData = $request->validate([
  53. 'email' => ['required', 'email', 'regex:/.+@upr\.edu$/'],
  54. ]);
  55. // TODO: Uncomment when added_by column is added to table
  56. $userData['added_by'] = Auth::user()->id;
  57. try {
  58. if (User::where('email', '=', $userData['email'])->get()->isNotEmpty()) {
  59. throw new Exception('User with that email already exists.');
  60. }
  61. $user = User::create($userData);
  62. } catch (Exception $e) {
  63. return redirect()->back()->withErrors(['Failed to add user.', $e->getMessage()]);
  64. }
  65. $permissionData = $request->validate([
  66. 'departments' => ['nullable', 'array'],
  67. 'departments.*' => ['exists:departments,id'],
  68. 'faculties' => ['nullable', 'array'],
  69. 'faculties.*' => ['exists:faculties,id'],
  70. 'admin' => ['nullable', 'boolean'],
  71. ]);
  72. // dd($permissionData);
  73. if (isset($permissionData['departments'])) {
  74. foreach($permissionData['departments'] as $department_id) {
  75. if (Gate::allows('add-permission', [1, $department_id])) {
  76. $user->permissions()->firstOrCreate([
  77. 'level' => 1,
  78. 'division_id' => $department_id,
  79. ]);
  80. } else {
  81. return redirect()->back()->withErrors('You do not have permission to department ' . Department::find($department_id)->title . '.');
  82. }
  83. }
  84. }
  85. if (isset($permissionData['faculties'])) {
  86. foreach($permissionData['faculties'] as $faculty_id) {
  87. if (Gate::allows('add-permission', [2, $faculty_id])) {
  88. $user->permissions()->firstOrCreate([
  89. 'level' => 2,
  90. 'division_id' => $faculty_id,
  91. ]);
  92. } else {
  93. return redirect()->back()->withErrors('You do not have permission to faculty ' . Faculty::find($faculty_id)->name . '.');
  94. }
  95. }
  96. }
  97. if (isset($permissionData['admin']) && $permissionData['admin']) {
  98. if (Gate::allows('add-permission', [3, 0])) {
  99. $user->permissions()->firstOrCreate([
  100. 'level' => 3,
  101. 'division_id' => 0,
  102. ]);
  103. } else {
  104. return redirect()->back()->withErrors('You do not have campus-wide permissions.');
  105. }
  106. }
  107. return redirect('/dashboard');
  108. }
  109. /**
  110. * Display the specified resource.
  111. *
  112. * @param \App\User $user
  113. * @return \Illuminate\Http\Response
  114. */
  115. public function show(User $user)
  116. {
  117. //
  118. }
  119. /**
  120. * Show the form for editing the specified resource.
  121. *
  122. * @param \App\User $user
  123. * @return \Illuminate\Http\Response
  124. */
  125. public function edit(User $user)
  126. {
  127. //
  128. }
  129. /**
  130. * Update the specified resource in storage.
  131. *
  132. * @param \Illuminate\Http\Request $request
  133. * @param \App\User $user
  134. * @return \Illuminate\Http\Response
  135. */
  136. public function update(Request $request, User $user)
  137. {
  138. //
  139. }
  140. /**
  141. * Remove the specified resource from storage.
  142. *
  143. * @param \App\User $user
  144. * @return \Illuminate\Http\Response
  145. */
  146. public function destroy(User $user)
  147. {
  148. //
  149. }
  150. }