| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Http\Controllers\Admin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Http\Controllers\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Http\Requests\UpdateUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Role; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Facades\Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class UserController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function index() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $activeUsers = User::whereNotNull('email_verified_at')->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         return view('admin.iam.users-and-roles', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             'users' => $activeUsers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             'roles' => Role::orderBy('name', 'ASC')->get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function displayUser(User $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         return view('admin.iam.manage-user', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'user' => $user, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             'roles' => Role::orderBy('name', 'ASC')->get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Update the user with the given email address, name and roles | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public function updateUser(User $user, UpdateUser $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $validated = $request->validated(); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         // Set the new submitted (direct) user properties | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $user->name = $validated['name']; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $user->email = $validated['email']; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $user->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         // Detach all roles from the user | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $user->roles()->detach(); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         // Attach only the given roles to the user | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         Log::info('Updating user#' . $user->id . ' with role-ids ' . implode('|', $validated['roles'])); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $user->roles()->attach($validated['roles']); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         // On successfull update redirect the browser to the user overview | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         return redirect() | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             ->route('admin.iam.user.manage', $user) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             ->with('status', 'Update successfull!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  |