Skip to content

Commit

Permalink
start hacking on the settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Jan 16, 2023
1 parent c934715 commit 8abf437
Show file tree
Hide file tree
Showing 22 changed files with 529 additions and 233 deletions.
25 changes: 25 additions & 0 deletions app/Actions/User/TokenDisable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Actions\User;

use App\Exceptions\InvalidPropertyException;
use App\Exceptions\ModelDBException;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class TokenDisable
{
/**
* @throws InvalidPropertyException
* @throws ModelDBException
*/
public function do(): User
{
/** @var User $user */
$user = Auth::user();
$user->token = null;
$user->save();

return $user;
}
}
26 changes: 26 additions & 0 deletions app/Actions/User/TokenReset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Actions\User;

use App\Exceptions\InvalidPropertyException;
use App\Exceptions\ModelDBException;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class TokenReset
{
/**
* @throws InvalidPropertyException
* @throws ModelDBException
*/
public function do(): User
{
/** @var User $user */
$user = Auth::user();
$token = strtr(base64_encode(random_bytes(16)), '+/', '-_');
$user->token = hash('SHA512', $token);
$user->save();

return $user;
}
}
20 changes: 20 additions & 0 deletions app/Contracts/Http/RuleSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Contracts\Http;

/**
* In order to avoid code duplication, we centralize the rule sets
* used during the validation of requests as they are used both
* in Livewire and in the Requests class.
*/
interface RuleSet
{
/**
* Return an array containing the rules to be applied to the request attributes
*
* @return array
*/
public static function rules(): array;

// TODO: Associate error message to above rules.
}
19 changes: 7 additions & 12 deletions app/Http/Controllers/Administration/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Http\Controllers\Administration;

use App\Actions\Settings\UpdateLogin;
use App\Actions\User\TokenDisable;
use App\Actions\User\TokenReset;
use App\Contracts\Exceptions\InternalLycheeException;
use App\Exceptions\Internal\FrameworkException;
use App\Exceptions\ModelDBException;
Expand Down Expand Up @@ -93,15 +95,11 @@ public function getAuthenticatedUser(): ?User
* @throws ModelDBException
* @throws \Exception
*/
public function resetToken(ChangeTokenRequest $request): array
public function resetToken(ChangeTokenRequest $request, TokenReset $tokenReset): array
{
/** @var User $user */
$user = Auth::user();
$token = strtr(base64_encode(random_bytes(16)), '+/', '-_');
$user->token = hash('SHA512', $token);
$user->save();
$user = $tokenReset->do();

return ['token' => $token];
return ['token' => $user->token];
}

/**
Expand All @@ -112,11 +110,8 @@ public function resetToken(ChangeTokenRequest $request): array
* @throws UnauthenticatedException
* @throws ModelDBException
*/
public function unsetToken(ChangeTokenRequest $request): void
public function unsetToken(ChangeTokenRequest $request, TokenDisable $tokenDisable): void
{
/** @var User $user */
$user = Auth::user();
$user->token = null;
$user->save();
$tokenDisable->do();
}
}
32 changes: 32 additions & 0 deletions app/Http/Livewire/Components/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,36 @@ public function openLoginModal(): void
{
$this->openModal('forms.login');
}

/**
* Go back one step
*
* @return void
*/
public function back(): void
{
$this->emitTo('pages.gallery', 'back');
}

/**
* Open the Left menu.
*
* @return void
*/
public function openLeftMenu(): void
{
$this->emitTo('components.left-menu', 'open');
}

/**
* Toggle the side bar.
*
* @return void
*/
public function toggleSideBar(): void
{
$this->emitTo('components.sidebar', 'toggle');
}


}
47 changes: 47 additions & 0 deletions app/Http/Livewire/Forms/Settings/BooleanSetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Http\Livewire\Forms\Settings;

use App\Facades\Lang;
use App\Models\Configs;
use Illuminate\Database\Eloquent\InvalidCastException;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Livewire\Component;
use RuntimeException;

class BooleanSetting extends Component
{
public Configs $config;
public string $description;
public string $footer;
public bool $flag; //! Wired

public function mount(string $description, string $name, string $footer = '') {
$this->description = Lang::get($description);
$this->footer = $footer !== '' ? Lang::get($footer) : '';
$this->config = Configs::where('key', '=', $name)->firstOrFail();
}

public function render()
{
$this->flag = $this->config->value === '1';
return view('livewire.form.form-toggle');
}

/**
* This runs before a wired property is updated.
*
* @param mixed $field
* @param mixed $value
* @return void
* @throws InvalidCastException
* @throws JsonEncodingException
* @throws RuntimeException
*/
public function updating($field, $value)
{
$this->config->value = $value === true ? '1' : '0';
$this->config->save();
}

}
71 changes: 71 additions & 0 deletions app/Http/Livewire/Forms/Settings/GetApiToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Http\Livewire\Forms\Settings;

use App\Actions\User\TokenDisable;
use App\Actions\User\TokenReset;
use App\Http\Livewire\Traits\InteractWithModal;
use App\Models\User;
use App\Policies\UserPolicy;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

class GetApiToken extends Component
{
use InteractWithModal;
use AuthorizesRequests;

// String of the token or message
public string $token = '';

// token is disabled
public bool $isDisabled;

// token is hidden
public bool $isHidden;

public function mount() {
$user = Auth::user();

$this->isDisabled = !$user->has_token;
$this->isHidden = true;
}

public function render()
{
return view('livewire.form.form-get-api-token');
}

/**
* Add an handle to close the modal form from a user-land call.
*
* @return void
*/
public function close(): void
{
$this->closeModal();
}

public function resetToken(TokenReset $tokenReset) {
/**
* Authorize the request
*/
$this->authorize(UserPolicy::CAN_EDIT, [User::class]);

$this->token = $tokenReset->do()->token;
$this->isDisabled = false;
$this->isHidden = false;
}

public function disableToken(TokenDisable $tokenDisable) {
/**
* Authorize the request
*/
$this->authorize(UserPolicy::CAN_EDIT, [User::class]);

$tokenDisable->do();
$this->token = '';
$this->isDisabled = true;
}
}
73 changes: 73 additions & 0 deletions app/Http/Livewire/Forms/Settings/SetLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Http\Livewire\Forms\Settings;

use App\Actions\Settings\UpdateLogin;
use App\Http\Livewire\Traits\InteractWithModal;
use App\Http\RuleSets\ChangeLoginRuleSet;
use App\Models\User;
use App\Policies\UserPolicy;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

/**
* Because Livewire is sending the data to the client,
* we do not provide the model as public property.
*/
class SetLogin extends Component
{
use InteractWithModal;
use AuthorizesRequests;

public string $oldPassword = ''; //! wired
public string $username = ''; //! wired
public string $password = ''; //! wired
public string $confirm = ''; //! wired

public function render()
{
return view('livewire.form.form-set-login');
}

/**
* Update Username & Password of current user
*/
public function submit(UpdateLogin $updateLogin) {

/**
* For the validation to work it is important that the above wired property match
* the keys in the rules applied
*/
$this->validate(ChangeLoginRuleSet::rules());

/**
* Authorize the request
*/
$this->authorize(UserPolicy::CAN_EDIT, [User::class]);

$currentUser = $updateLogin->do(
$this->username,
$this->password,
$this->oldPassword,
request()->ip()
);

// Update the session with the new credentials of the user.
// Otherwise, the session is out-of-sync and falsely assumes the user
// to be unauthenticated upon the next request.
Auth::login($currentUser);
}


/**
* Open a login modal box.
*
* @return void
*/
public function openApiTokenModal(): void
{
$this->openModal('forms.settings.get-api-token');
}

}
13 changes: 1 addition & 12 deletions app/Http/Livewire/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Index extends Component

// listeners of click events
protected $listeners = [
'openLeftMenu',
'openPage',
'reloadPage',
];
Expand Down Expand Up @@ -81,16 +80,6 @@ private function getLayout(): array
];
}

/**
* Open the Left menu.
*
* @return void
*/
public function openLeftMenu(): void
{
$this->emitTo('components.left-menu', 'open');
}

/**
* Open page.
*
Expand All @@ -104,7 +93,7 @@ public function openPage(string $page): void

// update URL
$this->emitUrlChange($this->mode, $this->albumId ?? '', $this->photoId ?? '');

// $this->render();
}

/*
Expand Down
Loading

0 comments on commit 8abf437

Please sign in to comment.