123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Department;
- use App\Faculty;
- use App\User;
- use Exception;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Gate;
-
- class UserController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- }
-
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- // DB::enableQueryLog();
- $user = Auth::user()->loadMissing(['faculties.departments', 'departments']);
- if ($user->is_admin) {
- $faculties = Faculty::all();
- $departments = Department::all();
- } else {
- $faculties = $user->faculties;
- $departments = $user->departments->keyBy('id');
- foreach($faculties as $faculty) {
- $departments = $departments->union($faculty->departments->keyBy('id'));
- }
- }
- return view('dashboard.register', compact('faculties', 'departments'));
-
- }
-
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- // dump($request);
- $userData = $request->validate([
- 'email' => ['required', 'email', 'regex:/.+@upr\.edu$/'],
- ]);
- // TODO: Uncomment when added_by column is added to table
- $userData['added_by'] = Auth::user()->id;
- try {
- if (User::where('email', '=', $userData['email'])->get()->isNotEmpty()) {
- throw new Exception('User with that email already exists.');
- }
- $user = User::create($userData);
-
- } catch (Exception $e) {
- return redirect()->back()->withErrors(['Failed to add user.', $e->getMessage()]);
- }
-
- $permissionData = $request->validate([
- 'departments' => ['nullable', 'array'],
- 'departments.*' => ['exists:departments,id'],
- 'faculties' => ['nullable', 'array'],
- 'faculties.*' => ['exists:faculties,id'],
- 'admin' => ['nullable', 'boolean'],
- ]);
-
-
- // dd($permissionData);
- if (isset($permissionData['departments'])) {
- foreach($permissionData['departments'] as $department_id) {
- if (Gate::allows('add-permission', [1, $department_id])) {
- $user->permissions()->firstOrCreate([
- 'level' => 1,
- 'division_id' => $department_id,
- ]);
- } else {
- return redirect()->back()->withErrors('You do not have permission to department ' . Department::find($department_id)->title . '.');
- }
- }
- }
- if (isset($permissionData['faculties'])) {
- foreach($permissionData['faculties'] as $faculty_id) {
- if (Gate::allows('add-permission', [2, $faculty_id])) {
- $user->permissions()->firstOrCreate([
- 'level' => 2,
- 'division_id' => $faculty_id,
- ]);
- } else {
- return redirect()->back()->withErrors('You do not have permission to faculty ' . Faculty::find($faculty_id)->name . '.');
- }
- }
- }
- if (isset($permissionData['admin']) && $permissionData['admin']) {
- if (Gate::allows('add-permission', [3, 0])) {
- $user->permissions()->firstOrCreate([
- 'level' => 3,
- 'division_id' => 0,
- ]);
- } else {
- return redirect()->back()->withErrors('You do not have campus-wide permissions.');
- }
- }
-
- return redirect('/dashboard');
- }
-
- /**
- * Display the specified resource.
- *
- * @param \App\User $user
- * @return \Illuminate\Http\Response
- */
- public function show(User $user)
- {
- //
- }
-
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\User $user
- * @return \Illuminate\Http\Response
- */
- public function edit(User $user)
- {
- //
- }
-
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\User $user
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, User $user)
- {
- //
- }
-
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\User $user
- * @return \Illuminate\Http\Response
- */
- public function destroy(User $user)
- {
- //
- }
- }
|