diff --git a/app/Events/Auth/InvitationCreated.php b/app/Events/Auth/InvitationCreated.php deleted file mode 100644 index 07741f8a3..000000000 --- a/app/Events/Auth/InvitationCreated.php +++ /dev/null @@ -1,20 +0,0 @@ -invitation = $invitation; - } -} diff --git a/app/Http/Controllers/Auth/Register.php b/app/Http/Controllers/Auth/Register.php index 808cbce1f..4b8c711ed 100644 --- a/app/Http/Controllers/Auth/Register.php +++ b/app/Http/Controllers/Auth/Register.php @@ -46,6 +46,10 @@ class Register extends Controller { $invitation = UserInvitation::token($request->get('token'))->first(); + if (!$invitation) { + abort(403); + } + $user = $invitation->user; $this->dispatch(new DeleteInvitation($invitation)); diff --git a/app/Jobs/Auth/CreateInvitation.php b/app/Jobs/Auth/CreateInvitation.php index 6ca036b88..3d0fb9f2d 100644 --- a/app/Jobs/Auth/CreateInvitation.php +++ b/app/Jobs/Auth/CreateInvitation.php @@ -3,9 +3,11 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; -use App\Events\Auth\InvitationCreated; use App\Models\Auth\UserInvitation; +use App\Notifications\Auth\Invitation as Notification; +use Exception; use Illuminate\Support\Str; +use Symfony\Component\Mailer\Exception\TransportException; class CreateInvitation extends Job { @@ -13,31 +15,29 @@ class CreateInvitation extends Job protected $user; - protected $company; - - public function __construct($user, $company) + public function __construct($user) { $this->user = $user; - $this->company = $company; } public function handle(): UserInvitation { \DB::transaction(function () { - if ($this->user->hasPendingInvitation($this->company->id)) { - $pending_invitation = $this->user->getPendingInvitation($this->company->id); - - $this->dispatch(new DeleteInvitation($pending_invitation)); - } - $this->invitation = UserInvitation::create([ 'user_id' => $this->user->id, - 'company_id' => $this->company->id, 'token' => (string) Str::uuid(), ]); - }); - event(new InvitationCreated($this->invitation)); + $notification = new Notification($this->invitation); + + try { + $this->dispatch(new NotifyUser($this->user, $notification)); + } catch (TransportException $e) { + $message = trans('errors.title.500'); + + throw new Exception($message); + } + }); return $this->invitation; } diff --git a/app/Jobs/Auth/CreateUser.php b/app/Jobs/Auth/CreateUser.php index 2d653080c..ce341f03b 100644 --- a/app/Jobs/Auth/CreateUser.php +++ b/app/Jobs/Auth/CreateUser.php @@ -69,12 +69,10 @@ class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate 'user' => $this->model->id, 'company' => $company->id, ]); + } - if (app()->runningInConsole() || request()->isInstall()) { - continue; - } - - $this->dispatch(new CreateInvitation($this->model, $company)); + if ((! app()->runningInConsole() && ! request()->isInstall()) || app()->runningUnitTests()) { + $this->dispatch(new CreateInvitation($this->model)); } }); diff --git a/app/Jobs/Auth/DeleteUser.php b/app/Jobs/Auth/DeleteUser.php index c71b20302..e2dc1a722 100644 --- a/app/Jobs/Auth/DeleteUser.php +++ b/app/Jobs/Auth/DeleteUser.php @@ -16,6 +16,8 @@ class DeleteUser extends Job implements ShouldDelete event(new UserDeleting($this->model)); \DB::transaction(function () { + $this->deleteRelationships($this->model, ['invitation']); + $this->model->delete(); $this->model->flushCache(); diff --git a/app/Jobs/Auth/UpdateUser.php b/app/Jobs/Auth/UpdateUser.php index 7d3940e7a..56cc7c3d5 100644 --- a/app/Jobs/Auth/UpdateUser.php +++ b/app/Jobs/Auth/UpdateUser.php @@ -67,20 +67,6 @@ class UpdateUser extends Job implements ShouldUpdate 'user' => $this->model->id, 'company' => $company->id, ]); - - $this->dispatch(new CreateInvitation($this->model, $company)); - } - } - - if (isset($sync) && !empty($sync['detached'])) { - foreach ($sync['detached'] as $id) { - $company = Company::find($id); - - if ($this->model->hasPendingInvitation($company->id)) { - $pending_invitation = $this->model->getPendingInvitation($company->id); - - $this->dispatch(new DeleteInvitation($pending_invitation)); - } } } }); diff --git a/app/Listeners/Auth/DeleteUserInvitation.php b/app/Listeners/Auth/DeleteUserInvitation.php deleted file mode 100644 index b4eb8597a..000000000 --- a/app/Listeners/Auth/DeleteUserInvitation.php +++ /dev/null @@ -1,28 +0,0 @@ -user->id)->get(); - - foreach ($invitations as $invitation) { - $this->dispatch(new DeleteInvitation($invitation)); - } - } -} diff --git a/app/Listeners/Auth/SendUserInvitation.php b/app/Listeners/Auth/SendUserInvitation.php deleted file mode 100644 index 9d9d9cdf8..000000000 --- a/app/Listeners/Auth/SendUserInvitation.php +++ /dev/null @@ -1,22 +0,0 @@ -invitation; - - $invitation->user->notify(new Notification($invitation)); - } -} diff --git a/app/Listeners/Update/V30/Version304.php b/app/Listeners/Update/V30/Version304.php new file mode 100644 index 000000000..de4f6e069 --- /dev/null +++ b/app/Listeners/Update/V30/Version304.php @@ -0,0 +1,70 @@ +skipThisUpdate($event)) { + return; + } + + Log::channel('stderr')->info('Starting the Akaunting 3.0.4 update...'); + + $this->updateDatabase(); + + $this->deleteOldFiles(); + + Log::channel('stderr')->info('Akaunting 3.0.4 update finished.'); + } + + public function updateDatabase() + { + Log::channel('stderr')->info('Updating database...'); + + DB::table('migrations')->insert([ + 'id' => DB::table('migrations')->max('id') + 1, + 'migration' => '2022_06_28_000000_core_v304', + 'batch' => DB::table('migrations')->max('batch') + 1, + ]); + + Artisan::call('migrate', ['--force' => true]); + + Log::channel('stderr')->info('Database updated.'); + } + + public function deleteOldFiles() + { + Log::channel('stderr')->info('Deleting old files...'); + + $files = [ + 'app/Events/Auth/InvitationCreated.php', + 'app/Listeners/Auth/SendUserInvitation.php', + 'app/Listeners/Auth/DeleteUserInvitation.php', + ]; + + foreach ($files as $file) { + File::delete(base_path($file)); + } + + Log::channel('stderr')->info('Old files deleted.'); + } +} diff --git a/app/Models/Auth/User.php b/app/Models/Auth/User.php index 2d4d21c8a..bed0d6aa0 100644 --- a/app/Models/Auth/User.php +++ b/app/Models/Auth/User.php @@ -89,6 +89,11 @@ class User extends Authenticatable implements HasLocalePreference return $this->belongsToMany('App\Models\Common\Dashboard', 'App\Models\Auth\UserDashboard'); } + public function invitation() + { + return $this->hasOne('App\Models\Auth\UserInvitation', 'user_id', 'id'); + } + /** * Always capitalize the name when we retrieve it */ @@ -311,14 +316,12 @@ class User extends Authenticatable implements HasLocalePreference return $actions; } - if (! $this->hasPendingInvitation()) { - $actions[] = [ - 'title' => trans('general.edit'), - 'icon' => 'edit', - 'url' => route('users.edit', $this->id), - 'permission' => 'update-auth-users', - ]; - } + $actions[] = [ + 'title' => trans('general.edit'), + 'icon' => 'edit', + 'url' => route('users.edit', $this->id), + 'permission' => 'update-auth-users', + ]; if ($this->hasPendingInvitation()) { $actions[] = [ diff --git a/app/Models/Auth/UserInvitation.php b/app/Models/Auth/UserInvitation.php index 661088a5f..2b8a85b23 100644 --- a/app/Models/Auth/UserInvitation.php +++ b/app/Models/Auth/UserInvitation.php @@ -20,7 +20,7 @@ class UserInvitation extends Model * * @var string[] */ - protected $fillable = ['user_id', 'company_id', 'token']; + protected $fillable = ['user_id', 'token']; public function user() { diff --git a/app/Providers/Event.php b/app/Providers/Event.php index 0e9351031..05ad6759e 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -17,6 +17,7 @@ class Event extends Provider 'App\Listeners\Module\UpdateExtraModules', 'App\Listeners\Update\V30\Version300', 'App\Listeners\Update\V30\Version303', + 'App\Listeners\Update\V30\Version304', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login', @@ -31,12 +32,6 @@ class Event extends Provider 'App\Events\Auth\LandingPageShowing' => [ 'App\Listeners\Auth\AddLandingPages', ], - 'App\Events\Auth\InvitationCreated' => [ - 'App\Listeners\Auth\SendUserInvitation', - ], - 'App\Events\Auth\UserDeleted' => [ - 'App\Listeners\Auth\DeleteUserInvitation', - ], 'App\Events\Document\DocumentCreated' => [ 'App\Listeners\Document\CreateDocumentCreatedHistory', 'App\Listeners\Document\IncreaseNextDocumentNumber', diff --git a/app/Traits/Users.php b/app/Traits/Users.php index 4ef8dc26e..c127d214b 100644 --- a/app/Traits/Users.php +++ b/app/Traits/Users.php @@ -110,31 +110,25 @@ trait Users } /** - * Checks if the given user has a pending invitation for the - * provided Company. + * Checks if the given user has a pending invitation. * * @return bool */ - public function hasPendingInvitation($company_id = null) + public function hasPendingInvitation() { - $company_id = $company_id ?: company_id(); - - $invitation = UserInvitation::where('user_id', $this->id)->where('company_id', $company_id)->first(); + $invitation = UserInvitation::where('user_id', $this->id)->first(); return $invitation ? true : false; } /** - * Returns if the given user has a pending invitation for the - * provided Company. + * Returns if the given user has a pending invitation. * * @return null|UserInvitation */ - public function getPendingInvitation($company_id = null) + public function getPendingInvitation() { - $company_id = $company_id ?: company_id(); - - $invitation = UserInvitation::where('user_id', $this->id)->where('company_id', $company_id)->first(); + $invitation = UserInvitation::where('user_id', $this->id)->first(); return $invitation; } diff --git a/database/migrations/2022_06_28_000000_core_v304.php b/database/migrations/2022_06_28_000000_core_v304.php new file mode 100644 index 000000000..95dccd039 --- /dev/null +++ b/database/migrations/2022_06_28_000000_core_v304.php @@ -0,0 +1,30 @@ +dropColumn('company_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}; diff --git a/resources/views/auth/users/edit.blade.php b/resources/views/auth/users/edit.blade.php index 412fede3c..3908a8787 100644 --- a/resources/views/auth/users/edit.blade.php +++ b/resources/views/auth/users/edit.blade.php @@ -13,7 +13,7 @@