-
-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
529 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.