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) { // } }