Skip to content

Commit

Permalink
feat: display vaults (monicahq/chandler#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Dec 7, 2021
1 parent 250cd1e commit 4ebac76
Show file tree
Hide file tree
Showing 45 changed files with 1,923 additions and 236,146 deletions.
30 changes: 0 additions & 30 deletions app/Features/Account/ManageAccount/Services/CreateAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace App\Features\Account\ManageAccount\Services;

use App\Models\User;
use App\Models\Vault;
use App\Models\Account;
use App\Jobs\SetupAccount;
use App\Jobs\CreateAuditLog;
use App\Services\BaseService;
use Illuminate\Support\Facades\DB;
use App\Interfaces\ServiceInterface;
use Illuminate\Support\Facades\Hash;

Expand All @@ -17,7 +15,6 @@ class CreateAccount extends BaseService implements ServiceInterface
private User $user;
private Account $account;
private array $data;
public Vault $vault;

/**
* Get the validation rules that apply to the service.
Expand Down Expand Up @@ -47,7 +44,6 @@ public function execute(array $data): User

$this->account = Account::create();
$this->addFirstUser();
$this->createFirstVault();
$this->addLogs();

SetupAccount::dispatch($this->user)->onQueue('low');
Expand All @@ -67,21 +63,6 @@ private function addFirstUser(): void
]);
}

private function createFirstVault(): void
{
$this->vault = Vault::create([
'account_id' => $this->account->id,
'type' => Vault::TYPE_PERSONAL,
'name' => trans('account.default_vault_name'),
]);

DB::table('user_vault')->insert([
'vault_id' => $this->vault->id,
'user_id' => $this->user->id,
'permission' => Vault::PERMISSION_MANAGE,
]);
}

private function addLogs(): void
{
CreateAuditLog::dispatch([
Expand All @@ -91,16 +72,5 @@ private function addLogs(): void
'action_name' => 'account_created',
'objects' => json_encode([]),
]);

CreateAuditLog::dispatch([
'account_id' => $this->account->id,
'author_id' => $this->user->id,
'author_name' => $this->user->name,
'action_name' => 'vault_created',
'objects' => json_encode([
'vault_id' => $this->vault->id,
'vault_name' => $this->vault->name,
]),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Features\Vault\ManageVault\ViewHelpers;

class VaultCreateViewHelper
{
/**
* Get all the data needed for the page.
*
* @return array
*/
public static function data(): array
{
return [
'url' => [
'store' => route('vault.store'),
'back' => route('vault.index'),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Features\Vault\ManageVault\ViewHelpers;

use App\Models\Vault;
use App\Models\Account;
use Illuminate\Support\Facades\Auth;

class VaultIndexViewHelper
{
/**
* Get all the data needed for the general layout page.
*
* @param Vault $vault
* @return array
*/
public static function layoutData(Vault $vault = null): array
{
return [
'user' => [
'name' => Auth::user()->name,
],
'vault' => $vault ? [
'id' => $vault->id,
'name' => $vault->name,
] : null,
'url' => [
'vaults' => route('vault.index'),
'logout' => route('logout'),
],
];
}

/**
* Get all the data needed for the general layout page.
*
* @param Account $account
* @return array
*/
public static function data(Account $account): array
{
$vaults = Vault::where('account_id', $account->id)
->orderBy('name', 'asc')
->get();

$vaultCollection = collect();
foreach ($vaults as $vault) {
$vaultCollection->push([
'id' => $vault->id,
'name' => $vault->name,
'description' => $vault->description,
'url' => [
'show' => route('vault.show', [
'vault' => $vault,
]),
],
]);
}

return [
'vaults' => $vaultCollection,
'url' => [
'vault' => [
'new' => route('vault.new'),
],
],
];
}
}
82 changes: 82 additions & 0 deletions app/Http/Controllers/Vault/VaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Http\Controllers\Vault;

use Inertia\Inertia;
use App\Models\Vault;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Features\Vault\ManageVault\Services\CreateVault;
use App\Features\Vault\ManageVault\ViewHelpers\VaultIndexViewHelper;
use App\Features\Vault\ManageVault\ViewHelpers\VaultCreateViewHelper;

class VaultController extends Controller
{
/**
* Show all the vaults of the user.
*
* @return Response
*/
public function index()
{
return Inertia::render('Vault/Index', [
'layoutData' => VaultIndexViewHelper::layoutData(),
'data' => VaultIndexViewHelper::data(Auth::user()->account),
]);
}

/**
* Display the create vault page.
*
* @return Response
*/
public function new()
{
return Inertia::render('Vault/Create', [
'layoutData' => VaultIndexViewHelper::layoutData(),
'data' => VaultCreateViewHelper::data(),
]);
}

/**
* Store the vault.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'type' => Vault::TYPE_PERSONAL,
'name' => $request->input('name'),
'description' => $request->input('description'),
];

(new CreateVault)->execute($data);

return response()->json([
'data' => route('vault.index'),
], 201);
}

/**
* Display the vault.
*
* @param Request $request
* @param int $vaultId
* @return Response
*/
public function show(Request $request, int $vaultId)
{
$vault = Vault::find($vaultId);

return Inertia::render('Vault/Show', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => VaultCreateViewHelper::data(),
]);
}
}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http;

use App\Http\Middleware\CheckVaultAccess;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
Expand Down Expand Up @@ -64,5 +65,6 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'vault' => CheckVaultAccess::class,
];
}
33 changes: 33 additions & 0 deletions app/Http/Middleware/CheckVaultAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

class CheckVaultAccess
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$requestedVaultId = $request->route()->parameter('vault');

$exists = DB::table('user_vault')->where('user_id', Auth::user()->id)
->where('vault_id', $requestedVaultId)
->count() > 0;

if ($exists) {
return $next($request);
} else {
abort(401);
}
}
}
1 change: 0 additions & 1 deletion app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function share(Request $request)
{
return array_merge(parent::share($request), [
'auth' => [
'user' => $request->user(),
],
]);
}
Expand Down
6 changes: 3 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace App\Models;

use Laravel\Sanctum\HasApiTokens;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class User extends Authenticatable
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable, HasFactory, HasApiTokens, MustVerifyEmail;
use Notifiable, HasFactory, HasApiTokens;

/**
* The attributes that are mass assignable.
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
public const HOME = '/dashboard';
public const HOME = '/vaults';

/**
* The controller namespace for the application.
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"inertiajs/inertia-laravel": "^0.4.3",
"itsgoingd/clockwork": "^5.1",
"laravel/framework": "^8.65",
"laravel/sanctum": "^2.6",
"laravel/tinker": "^2.5",
"tightenco/ziggy": "^1.0"
},
"require-dev": {
"brianium/paratest": "^6.4",
"facade/ignition": "^2.5",
"fakerphp/faker": "^1.9.1",
"laravel/breeze": "^1.4",
Expand Down
Loading

0 comments on commit 4ebac76

Please sign in to comment.