commit
b9e50a6ea7
@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events\Auth;
|
||||
|
||||
use App\Abstracts\Event;
|
||||
|
||||
class InvitationCreated extends Event
|
||||
{
|
||||
public $invitation;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param $invitation
|
||||
*/
|
||||
public function __construct($invitation)
|
||||
{
|
||||
$this->invitation = $invitation;
|
||||
}
|
||||
}
|
@ -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));
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners\Auth;
|
||||
|
||||
use App\Events\Auth\UserDeleted as Event;
|
||||
use App\Jobs\Auth\DeleteInvitation;
|
||||
use App\Models\Auth\UserInvitation;
|
||||
use App\Traits\Jobs;
|
||||
|
||||
class DeleteUserInvitation
|
||||
{
|
||||
use Jobs;
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Event $event)
|
||||
{
|
||||
$invitations = UserInvitation::where('user_id', $event->user->id)->get();
|
||||
|
||||
foreach ($invitations as $invitation) {
|
||||
$this->dispatch(new DeleteInvitation($invitation));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners\Auth;
|
||||
|
||||
use App\Events\Auth\InvitationCreated as Event;
|
||||
use App\Notifications\Auth\Invitation as Notification;
|
||||
|
||||
class SendUserInvitation
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Event $event)
|
||||
{
|
||||
$invitation = $event->invitation;
|
||||
|
||||
$invitation->user->notify(new Notification($invitation));
|
||||
}
|
||||
}
|
70
app/Listeners/Update/V30/Version304.php
Normal file
70
app/Listeners/Update/V30/Version304.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners\Update\V30;
|
||||
|
||||
use App\Abstracts\Listeners\Update as Listener;
|
||||
use App\Events\Install\UpdateFinished as Event;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Version304 extends Listener
|
||||
{
|
||||
const ALIAS = 'core';
|
||||
|
||||
const VERSION = '3.0.4';
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Event $event)
|
||||
{
|
||||
if ($this->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.');
|
||||
}
|
||||
}
|
@ -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[] = [
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -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',
|
||||
|
@ -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;
|
||||
}
|
||||
|
30
database/migrations/2022_06_28_000000_core_v304.php
Normal file
30
database/migrations/2022_06_28_000000_core_v304.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('user_invitations', function (Blueprint $table) {
|
||||
$table->dropColumn('company_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
};
|
@ -13,7 +13,7 @@
|
||||
<div class="sm:col-span-3 grid gap-x-8 gap-y-6 {{ user()->id == $user->id ? 'grid-rows-3' : 'grid-rows-2' }}">
|
||||
<x-form.group.text name="name" label="{{ trans('general.name') }}" />
|
||||
|
||||
<x-form.group.email name="email" label="{{ trans('general.email') }}" />
|
||||
<x-form.group.email name="email" label="{{ trans('general.email') }}" ::disabled="{{ $user->hasPendingInvitation() ? 'true' : 'false' }}" />
|
||||
|
||||
@if (user()->id == $user->id)
|
||||
<x-form.group.checkbox name="change_password" :options="['1' => trans('auth.change_password')]" form-group-class="sm:col-span-3" @input="onChangePassword($event)" />
|
||||
|
@ -48,15 +48,11 @@
|
||||
@foreach($users as $item)
|
||||
<x-table.tr href="{{ route('users.edit', $item->id) }}">
|
||||
<x-table.td class="ltr:pr-6 rtl:pl-6 hidden sm:table-cell" override="class">
|
||||
@if (user()->id != $item->id)
|
||||
<x-index.bulkaction.single
|
||||
id="{{ $item->id }}"
|
||||
name="{{ $item->name }}"
|
||||
:disabled="($item->hasPendingInvitation() || $item->multiplexed) ? true : false"
|
||||
/>
|
||||
@else
|
||||
<x-index.bulkaction.single id="{{ $item->id }}" name="{{ $item->name }}" disabled />
|
||||
@endif
|
||||
<x-index.bulkaction.single
|
||||
id="{{ $item->id }}"
|
||||
name="{{ $item->name }}"
|
||||
:disabled="($item->hasPendingInvitation() || user()->id == $item->id) ? true : false"
|
||||
/>
|
||||
</x-table.td>
|
||||
|
||||
<x-table.td class="w-4/12 sm:w-5/12">
|
||||
|
@ -4,6 +4,8 @@ namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Jobs\Auth\CreateUser;
|
||||
use App\Models\Auth\User;
|
||||
use App\Notifications\Auth\Invitation;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
class UsersTest extends FeatureTestCase
|
||||
@ -16,6 +18,22 @@ class UsersTest extends FeatureTestCase
|
||||
->assertSeeText(trans_choice('general.users', 2));
|
||||
}
|
||||
|
||||
public function testItShouldSeePendingUserListPage()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
|
||||
$user = $this->dispatch(new CreateUser($request));
|
||||
|
||||
$this->loginAs()
|
||||
->get(route('users.index'))
|
||||
->assertOk()
|
||||
->assertSeeTextInOrder([
|
||||
$user->name,
|
||||
trans('documents.statuses.pending')
|
||||
])
|
||||
->assertSee(route('users.invite', $user->id));
|
||||
}
|
||||
|
||||
public function testItShouldSeeUserCreatePage()
|
||||
{
|
||||
$this->loginAs()
|
||||
@ -26,15 +44,30 @@ class UsersTest extends FeatureTestCase
|
||||
|
||||
public function testItShouldCreateUser()
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$request = $this->getRequest();
|
||||
|
||||
$this->loginAs()
|
||||
$response = $this->loginAs()
|
||||
->post(route('users.store'), $request)
|
||||
->assertOk();
|
||||
->assertOk()
|
||||
->assertJson([
|
||||
'success' => true,
|
||||
'error' => false,
|
||||
'message' => '',
|
||||
'redirect' => route('users.index'),
|
||||
])
|
||||
->json();
|
||||
|
||||
$user = User::findOrFail($response['data']['id']);
|
||||
|
||||
$this->assertFlashLevel('success');
|
||||
|
||||
$this->assertDatabaseHas('users', $this->getAssertRequest($request));
|
||||
$this->assertModelExists($user);
|
||||
|
||||
$this->assertModelExists($user->invitation);
|
||||
|
||||
Notification::assertSentTo([$user], Invitation::class);
|
||||
}
|
||||
|
||||
public function testItShouldSeeUserUpdatePage()
|
||||
@ -60,7 +93,7 @@ class UsersTest extends FeatureTestCase
|
||||
$this->loginAs()
|
||||
->patch(route('users.update', $user->id), $request)
|
||||
->assertOk()
|
||||
->assertSee($request['email']);
|
||||
->assertSee($request['email']);
|
||||
|
||||
$this->assertFlashLevel('success');
|
||||
|
||||
@ -80,6 +113,8 @@ class UsersTest extends FeatureTestCase
|
||||
$this->assertFlashLevel('success');
|
||||
|
||||
$this->assertSoftDeleted('users', $this->getAssertRequest($request));
|
||||
|
||||
$this->assertSoftDeleted('user_invitations', ['user_id' => $user->id]);
|
||||
}
|
||||
|
||||
public function testItShouldSeeLoginPage()
|
||||
@ -127,6 +162,71 @@ class UsersTest extends FeatureTestCase
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function testItShouldSeeRegisterPage()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
|
||||
$user = $this->dispatch(new CreateUser($request));
|
||||
|
||||
$this->get(route('register', ['token' => $user->invitation->token]))
|
||||
->assertOk();
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function testItShouldNotSeeRegisterPage()
|
||||
{
|
||||
$this->withExceptionHandling()
|
||||
->get(route('register', ['token' => $this->faker->uuid]))
|
||||
->assertForbidden();
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function testItShouldRegisterUser()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
|
||||
$user = $this->dispatch(new CreateUser($request));
|
||||
|
||||
$password = $this->faker->password;
|
||||
|
||||
$data = [
|
||||
'token' => $user->invitation->token,
|
||||
'password' => $password,
|
||||
'password_confirmation' => $password,
|
||||
];
|
||||
|
||||
$this->post(route('register.store'), $data)
|
||||
->assertOk()
|
||||
->assertJson([
|
||||
'redirect' => url('/'),
|
||||
]);
|
||||
|
||||
$this->assertFlashLevel('success');
|
||||
|
||||
$this->assertSoftDeleted('user_invitations', ['user_id' => $user->id]);
|
||||
|
||||
$this->isAuthenticated($user->user);
|
||||
}
|
||||
|
||||
public function testItShouldNotRegisterUser()
|
||||
{
|
||||
$password = $this->faker->password;
|
||||
|
||||
$data = [
|
||||
'token' => $this->faker->uuid,
|
||||
'password' => $password,
|
||||
'password_confirmation' => $password,
|
||||
];
|
||||
|
||||
$this->withExceptionHandling()
|
||||
->post(route('register.store'), $data)
|
||||
->assertForbidden();
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function getRequest()
|
||||
{
|
||||
return User::factory()->enabled()->raw();
|
||||
|
Loading…
x
Reference in New Issue
Block a user