| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Xetaravel\Livewire\Admin\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Contracts\View\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Contracts\View\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Foundation\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Http\RedirectResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Livewire\Attributes\On; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Livewire\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Masmerise\Toaster\Toastable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Spatie\MediaLibrary\MediaCollections\Exceptions\FileIsTooBig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Spatie\Permission\Models\Permission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Xetaravel\Livewire\Forms\UserForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Xetaravel\Models\Role; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Xetaravel\Models\User; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class UpdateUser extends Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use AuthorizesRequests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     use Toastable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * The form used to create/update a model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var UserForm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public UserForm $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Used to show the update modal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public bool $showModal = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function render(): View|Application|Factory|\Illuminate\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // Select only the roles attached to this site or the roles without assigned site_id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $roles = Role::where('level', '<=', auth()->user()->level) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ->select(['name', 'color']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ->orderBy('name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // Select all permissions except `bypass login` who is assigned to the `site_id` 0. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $permissions = Permission::where('name', '<>', 'bypass login') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ->select(['name', 'description']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ->orderBy('name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ->get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return view('livewire.admin.user.update-user', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             'roles' => $roles, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'permissions' => $permissions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * When a user click on 'Edit' open the modal and set the content. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param int $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     #[On('update-user')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function updateUser(int $id): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $user = User::withTrashed() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->with('account') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ->find($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->authorize('update', $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->form->reset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->form->user = $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->form->username = $user->username; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->form->email = $user->email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->form->first_name = $user->account?->first_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->form->last_name = $user->account?->last_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->form->facebook = $user->account?->facebook; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->form->twitter = $user->account?->twitter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->form->biography = $user->account?->biography; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->form->signature = $user->account?->signature; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->form->roles = $user->roles()->pluck('name')->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->form->permissions = $user->permissions()->pluck('name')->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->form->can_bypass = $user->hasPermissionTo('bypass login'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->showModal = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Update the user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @throws Throwable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function update(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->authorize('update', $this->form->user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->form->update(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         redirect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ->route('admin.user.index') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ->success('This user has been updated successfully !'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Restore a user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function restore(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->authorize('restore', User::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->form->user->restore(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         redirect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             ->route('admin.user.index') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             ->success('This user has been restored successfully !'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * Delete the avatar for the specified user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param int $id The id of the user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @throws FileDoesNotExist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @throws FileIsTooBig | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     public function deleteAvatar(int $id): void | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         $user = User::findOrFail($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |         $user->clearMediaCollection('avatar'); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |         $user->addMedia(public_path('images/avatar.png')) | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |             ->preservingOriginal() | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |             ->setName(mb_substr(md5($user->username), 0, 10)) | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |             ->setFileName(mb_substr(md5($user->username), 0, 10) . '.png') | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |             ->toMediaCollection('avatar'); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         redirect() | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |             ->route('admin.user.index') | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |             ->success('The avatar has been deleted successfully !'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  | 
            
                        
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: