Skip to content

Commit 42c26da

Browse files
committed
Add console command for adding credentials
1 parent 5008995 commit 42c26da

4 files changed

Lines changed: 44 additions & 10 deletions

File tree

Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
1414
use System\Classes\PluginBase;
1515
use Vdlp\BasicAuthentication\Classes\Helper\AuthorizationHelper;
16+
use Vdlp\BasicAuthentication\Console\CreateCredentialsCommand;
1617
use Vdlp\BasicAuthentication\Models\Credential;
1718
use Vdlp\BasicAuthentication\ServiceProviders\BasicAuthenticationServiceProvider;
1819

@@ -42,6 +43,8 @@ public function pluginDetails(): array
4243
public function register(): void
4344
{
4445
$this->app->register(BasicAuthenticationServiceProvider::class);
46+
47+
$this->registerConsoleCommand(CreateCredentialsCommand::class, CreateCredentialsCommand::class);
4548
}
4649

4750
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vdlp\BasicAuthentication\Console;
6+
7+
use Illuminate\Console\Command;
8+
use Throwable;
9+
use Vdlp\BasicAuthentication\Models\Credential;
10+
11+
final class CreateCredentialsCommand extends Command
12+
{
13+
public function __construct()
14+
{
15+
$this->signature = 'vdlp:basicauthentication:create-credentials {hostname} {realm} {username} {password}';
16+
$this->description = 'Create Basic Authentication credentials.';
17+
18+
parent::__construct();
19+
}
20+
21+
public function handle(): void
22+
{
23+
try {
24+
Credential::query()
25+
->updateOrInsert([
26+
'hostname' => $this->argument('hostname'),
27+
], [
28+
'realm' => $this->argument('realm'),
29+
'username' => $this->argument('username'),
30+
'password' => $this->argument('password'),
31+
'is_enabled' => true,
32+
'updated_at' => now(),
33+
'created_at' => now(),
34+
]);
35+
36+
$this->info('Basic Authentication credentials have been added to the database.');
37+
} catch (Throwable $e) {
38+
$this->error('Could not create Basic Authentication credentials: ' . $e->getMessage());
39+
}
40+
}
41+
}

models/Credential.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44

55
namespace Vdlp\BasicAuthentication\Models;
66

7-
use Eloquent;
87
use October\Rain\Database\Model;
98

10-
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
11-
/** @noinspection LongInheritanceChainInspection */
12-
139
/**
1410
* Class Credential
1511
*
1612
* @package Vdlp\BasicAuthentication\Models
17-
* @mixin Eloquent
1813
*/
1914
class Credential extends Model
2015
{

models/ExcludedUrl.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44

55
namespace Vdlp\BasicAuthentication\Models;
66

7-
use Eloquent;
87
use October\Rain\Database\Model;
98

10-
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
11-
/** @noinspection LongInheritanceChainInspection */
12-
139
/**
1410
* Class ExcludedUrl
1511
*
1612
* @package Vdlp\BasicAuthentication\Models
17-
* @mixin Eloquent
1813
*/
1914
class ExcludedUrl extends Model
2015
{

0 commit comments

Comments
 (0)