Skip to content

Commit

Permalink
Merge pull request #268 from jeffgreco13/2.x-dev
Browse files Browse the repository at this point in the history
Revert "Merge pull request #263 from pepperfm/2.x"
  • Loading branch information
jeffgreco13 authored Oct 18, 2023
2 parents 66033c8 + 83b52f5 commit 69abe8d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 152 deletions.
133 changes: 42 additions & 91 deletions src/BreezyCore.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Jeffgreco13\FilamentBreezy;

use Closure;
Expand Down Expand Up @@ -34,33 +32,18 @@
class BreezyCore implements Plugin
{
use EvaluatesClosures;

protected Google2FA $engine;

/**
* @var Repository|null
*/
protected ?\Illuminate\Contracts\Cache\Repository $cache;

protected array $myProfile = [];

protected \Closure $avatarUploadComponent;

protected bool $twoFactorAuthentication;

protected bool $forceTwoFactorAuthentication;

protected string|Closure|array|null $twoFactorRouteAction;

protected array $registeredMyProfileComponents = [];

protected array $passwordUpdateRules = ['min:8'];

protected $engine;
protected $cache;
protected $myProfile;
protected $avatarUploadComponent;
protected $twoFactorAuthentication;
protected $forceTwoFactorAuthentication;
protected $twoFactorRouteAction;
protected $registeredMyProfileComponents = [];
protected $passwordUpdateRules = ['min:8'];
protected bool $passwordUpdateRequireCurrent = true;

protected bool $sanctumTokens = false;

protected array $sanctumPermissions = ["create", "view", "update", "delete"];
protected $sanctumTokens = false;
protected $sanctumPermissions = ["create", "view", "update", "delete"];

public function __construct(Google2FA $engine, Repository $cache = null)
{
Expand All @@ -77,7 +60,6 @@ public static function make(): static
{
return app(static::class);
}

public function register(Panel $panel): void
{
$panel
Expand All @@ -88,7 +70,6 @@ public function register(Panel $panel): void
Livewire::component('two-factor-page', Pages\TwoFactorPage::class);
}
}

protected function preparePages(): array
{
$collection = collect();
Expand All @@ -104,31 +85,29 @@ public function boot(Panel $panel): void
if ($this->sanctumTokens) {
Livewire::component('sanctum_tokens', SanctumTokens::class);
$this->myProfileComponents([
'sanctum_tokens' => SanctumTokens::class,
'sanctum_tokens' => SanctumTokens::class
]);
}
if ($this->twoFactorAuthentication) {
Livewire::component('two_factor_authentication', TwoFactorAuthentication::class);
$this->myProfileComponents([
'two_factor_authentication' => TwoFactorAuthentication::class,
'two_factor_authentication' => TwoFactorAuthentication::class
]);
}

Livewire::component('personal_info', PersonalInfo::class);
Livewire::component('update_password', UpdatePassword::class);
$this->myProfileComponents([
'personal_info' => PersonalInfo::class,
'update_password' => UpdatePassword::class,
'update_password' => UpdatePassword::class
]);

if ($this->myProfile['shouldRegisterUserMenu']) {
if ($panel->hasTenancy()) {
$tenantId = request()->route()->parameter('tenant');
if ($tenant = app($panel->getTenantModel())::where($panel->getTenantSlugAttribute() ?? 'id',
$tenantId)->first()) {
if ($tenant = app($panel->getTenantModel())::where($panel->getTenantSlugAttribute() ?? 'id', $tenantId)->first()){
$panel->userMenuItems([
'account' => MenuItem::make()->url(Pages\MyProfilePage::getUrl(panel: $panel->getId(),
tenant: $tenant)),
'account' => MenuItem::make()->url(Pages\MyProfilePage::getUrl(panel:$panel->getId(),tenant: $tenant)),
]);
}
} else {
Expand All @@ -140,26 +119,17 @@ public function boot(Panel $panel): void
}
}

/**
* @return \Illuminate\Auth\SessionGuard
*/
public function auth(): \Illuminate\Contracts\Auth\Guard
public function auth()
{
return Filament::getCurrentPanel()->auth();
}

public function getCurrentPanel(): ?Panel
public function getCurrentPanel()
{
return Filament::getCurrentPanel();
}

public function myProfile(
bool $condition = true,
bool $shouldRegisterUserMenu = true,
bool $shouldRegisterNavigation = false,
bool $hasAvatars = false,
string $slug = 'my-profile'
) {
public function myProfile(bool $condition = true, bool $shouldRegisterUserMenu = true, bool $shouldRegisterNavigation = false, bool $hasAvatars = false, string $slug = 'my-profile'){
$this->myProfile = get_defined_vars();
return $this;
}
Expand All @@ -174,29 +144,22 @@ public function slug()
return $this->myProfile['slug'];
}

public function avatarUploadComponent(Closure $component): static
public function avatarUploadComponent(Closure $component)
{
$this->avatarUploadComponent = $component;

return $this;
}

public function getAvatarUploadComponent()
{
$fileUpload = Forms\Components\FileUpload::make('avatar_url')
->label(__('filament-breezy::default.fields.avatar'))->avatar();

return is_null($this->avatarUploadComponent) ?
$fileUpload :
$this->evaluate(
$this->avatarUploadComponent,
namedInjections: [
'fileUpload' => $fileUpload,
]
);
return is_null($this->avatarUploadComponent) ? $fileUpload : $this->evaluate($this->avatarUploadComponent, namedInjections:[
'fileUpload' => $fileUpload
]);
}

public function myProfileComponents(array $components): static
public function myProfileComponents(array $components)
{
$this->registeredMyProfileComponents = [
...$components,
Expand All @@ -209,31 +172,30 @@ public function myProfileComponents(array $components): static
public function getRegisteredMyProfileComponents(): array
{
$components = collect($this->registeredMyProfileComponents)->filter(
fn(string $component) => $component::canView()
fn (string $component) => $component::canView()
)->sortBy(
fn(string $component) => $component::getSort()
fn (string $component) => $component::getSort()
);

if ($this->shouldForceTwoFactor()) {
if ($this->shouldForceTwoFactor()){
$components = $components->only(['two_factor_authentication']);
}
return $components->all();
}

public function passwordUpdateRules(array|Password $rules, bool $requiresCurrentPassword = true): static
public function passwordUpdateRules(array | Password $rules, bool $requiresCurrentPassword = true)
{
$this->passwordUpdateRequireCurrent = $requiresCurrentPassword;
$this->passwordUpdateRules = $rules;

return $this;
}

public function getPasswordUpdateRequiresCurrent(): bool
public function getPasswordUpdateRequiresCurrent()
{
return $this->passwordUpdateRequireCurrent;
}

public function getPasswordUpdateRules(): array
public function getPasswordUpdateRules()
{
return $this->passwordUpdateRules;
}
Expand All @@ -243,15 +205,11 @@ public function shouldRegisterNavigation(string $key)
return $this->{$key}['shouldRegisterNavigation'];
}

public function enableTwoFactorAuthentication(
bool $condition = true,
bool $force = false,
string|Closure|array|null $action = TwoFactorPage::class
): static {
public function enableTwoFactorAuthentication(bool $condition = true, bool $force = false, string | Closure | array | null $action = TwoFactorPage::class)
{
$this->twoFactorAuthentication = $condition;
$this->forceTwoFactorAuthentication = $force;
$this->twoFactorRouteAction = $action;

return $this;
}

Expand All @@ -260,22 +218,22 @@ public function getForceTwoFactorAuthentication(): bool
return $this->forceTwoFactorAuthentication;
}

public function getTwoFactorRouteAction(): string|Closure|array|null
public function getTwoFactorRouteAction(): string | Closure | array | null
{
return $this->twoFactorRouteAction;
}

public function getEngine(): Google2FA
public function getEngine()
{
return $this->engine;
}

public function generateSecretKey(): string
public function generateSecretKey()
{
return $this->engine->generateSecretKey();
}

public function getTwoFactorQrCodeSvg(string $url): string
public function getTwoFactorQrCodeSvg(string $url)
{
$svg = (new Writer(
new ImageRenderer(
Expand All @@ -287,20 +245,12 @@ public function getTwoFactorQrCodeSvg(string $url): string
return trim(substr($svg, strpos($svg, "\n") + 1));
}

public function getQrCodeUrl($companyName, $companyEmail, $secret): string
public function getQrCodeUrl($companyName, $companyEmail, $secret)
{
return $this->engine->getQRCodeUrl($companyName, $companyEmail, $secret);
}

/**
* @param string $code
* @param \App\Models\User|null $user
*
* @throws \Exception|\Psr\SimpleCache\InvalidArgumentException
*
* @return bool
*/
public function verify(string $code, ?Authenticatable $user = null): bool
public function verify(string $code, ?Authenticatable $user = null)
{
if (is_null($user)) {
$user = Filament::auth()->user();
Expand All @@ -327,20 +277,21 @@ public function shouldForceTwoFactor(): bool
return $this->forceTwoFactorAuthentication && !$this->auth()->user()?->hasConfirmedTwoFactor();
}

public function enableSanctumTokens(bool $condition = true, ?array $permissions = null): static
public function enableSanctumTokens(bool $condition = true,?array $permissions = null)
{
$this->sanctumTokens = $condition;
if (!is_null($permissions)) {
if (!is_null($permissions)){
$this->sanctumPermissions = $permissions;
}
return $this;
}

public function getSanctumPermissions(): array
{
return collect($this->sanctumPermissions)->mapWithKeys(function ($item, $key) {
return collect($this->sanctumPermissions)->mapWithKeys(function($item,$key){
$key = is_string($key) ? $key : strtolower($item);
return [$key => $item];
})->toArray();
}

}
2 changes: 1 addition & 1 deletion src/FilamentBreezyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function configurePackage(Package $package): void
*/
$package
->name('filament-breezy')
->hasRoute('web')
->hasRoute("web")
->hasViews()
->hasTranslations()
// ->hasMigration('add_two_factor_columns_to_table')
Expand Down
30 changes: 10 additions & 20 deletions src/Models/BreezySession.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Jeffgreco13\FilamentBreezy\Models;

use Filament\Facades\Filament;
Expand Down Expand Up @@ -53,46 +51,38 @@ protected function ipAddress()
return request()->ip();
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function authenticatable(): \Illuminate\Database\Eloquent\Relations\MorphTo
public function authenticatable()
{
return $this->morphTo();
}

public function confirm(): static
public function confirm()
{
event(new LoginSuccess($this->authenticatable));

$this->update([
'two_factor_confirmed_at' => now()
]);

return $this;
}

public function expire(): static
public function expire()
{
$this->update([
'expires_at' => now()->subMinute()
'expires_at' => now()->subMinutes(1)
]);

return $this;
}

public function setSession(?int $lifetime = null): void
public function setSession(?int $lifetime = null)
{
// /** @var \Jeffgreco13\FilamentBreezy\BreezyCore $breezyCore */
// $breezyCore = filament('filament-breezy');

session(['breezy_session_id' => md5($this->id)]);
// $this->update([
// 'panel_id' => $breezyCore->getCurrentPanel()->getId(),
// 'guard' => $breezyCore->getCurrentPanel()->getAuthGuard(),
// 'expires_at' => now()->addSeconds($lifetime ?? filament('filament-breezy')->getTwoFactorSessionLifetime())
// ]);
// PLUS
// $this->update([
// 'ip_address' => $this->ipAddress(),
// 'user_agent' => $this->userAgent(),
// 'expires_at' => now()->addSeconds($lifetime ?? 3600)
// 'expires_at' => now()->addSeconds($lifetime ?? filament('filament-breezy')->getTwoFactorSessionLifetime())
// ]);
}

Expand Down
Loading

0 comments on commit 69abe8d

Please sign in to comment.