diff --git a/README.md b/README.md index 2610350..e101733 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,14 @@ Several other parameters are also available to customize the migration process, php artisan -h db:import +### Create an admin account + +Create an admin account, an API Key will also be generated along the way, it might expire after a while. + +If no parameters are put, a default admin account will be created. + + php artisan accounts:create-admin-account {--u|username=} {--p|password=} {--d|domain=} + ### Clear the expired API Keys This will remove the API Keys that were not used after `x minutes`. @@ -189,7 +197,7 @@ The base request will not delete the related tombstones by default. You need to ### Set an account admin -This command will set the admin role to any available Flexisip account (the external Flexisip database need to be configured beforehand). You need to use the account DB id as a parameter in this command. +This command will set the admin role to any available Flexisip account. You need to use the account DB id as a parameter in this command. php artisan accounts:set-admin {account_id} diff --git a/flexiapi/app/Console/Commands/CreateAdminAccount.php b/flexiapi/app/Console/Commands/CreateAdminAccount.php new file mode 100644 index 0000000..bab0bae --- /dev/null +++ b/flexiapi/app/Console/Commands/CreateAdminAccount.php @@ -0,0 +1,102 @@ +. +*/ + +namespace App\Console\Commands; + +use Illuminate\Console\Command; +use Illuminate\Support\Str; + +use App\Account; +use App\Admin; +use App\ApiKey; +use Carbon\Carbon; + +class CreateAdminAccount extends Command +{ + protected $signature = 'accounts:create-admin-account {--u|username=} {--p|password=} {--d|domain=}'; + protected $description = 'Create an admin account'; + + public function __construct() + { + parent::__construct(); + } + + public function handle() + { + $this->info('Your will create a new admin account in the database, existing accounts with the same credentials will be overwritten'); + + $username = $this->option('username'); + $domain = $this->option('domain'); + $password = $this->option('password'); + + if (!$this->option('username')) { + $username = $this->ask('What will be the admin username? Default: admin'); + } + + if (!$this->option('domain')) { + $domain = $this->ask('What will be the admin domain? Default: ' . config('app.sip_domain')); + } + + if (!$this->option('password')) { + $password = $this->ask('What will be the admin password? Default: changeme'); + } + + $username = $username ?? 'admin'; + $domain = $domain ?? config('app.sip_domain'); + $password = $password ?? 'change_me'; + + // Delete the account if it already exists + $account = Account::withoutGlobalScopes() + ->where('username', $username) + ->where('domain', $domain) + ->first(); + + if ($account) { + $account->delete(); + } + + $account = new Account; + $account->username = $username; + $account->domain = $domain; + $account->email = 'admin_test@sip.example.org'; + $account->activated = true; + $account->user_agent = 'Test'; + $account->ip_address = '0.0.0.0'; + + // Create an "old" account to prevent unwanted deletion on the test server + $account->created_at = Carbon::now()->subYears(3); + $account->save(); + + $admin = new Admin; + $admin->account_id = $account->id; + $admin->save(); + + $apiKey = new ApiKey; + $apiKey->account_id = $account->id; + $apiKey->last_used_at = Carbon::now(); + $apiKey->key = Str::random(10); + $apiKey->save(); + + $account->updatePassword($password); + + $this->info('Admin test account created: "' . $username . '@' . $domain . '" | Password: "' . $password . '" | API Key: "' . $apiKey->key . '"'); + + return 0; + } +} diff --git a/flexiapi/app/Console/Commands/CreateAdminAccountTest.php b/flexiapi/app/Console/Commands/CreateAdminAccountTest.php index d3146e4..9c33216 100644 --- a/flexiapi/app/Console/Commands/CreateAdminAccountTest.php +++ b/flexiapi/app/Console/Commands/CreateAdminAccountTest.php @@ -22,7 +22,6 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use App\Account; -use App\Admin; use App\ApiKey; use Carbon\Carbon; @@ -40,38 +39,21 @@ class CreateAdminAccountTest extends Command { $username = 'admin_test'; $domain = 'sip.example.org'; + + $this->call('accounts:create-admin-account', [ + '--username' => $username, + '--domain' => $domain, + '--password' => 'admin_test_password' + ]); + $secret = 'no_secret_at_all'; - // Delete the existing keys - ApiKey::where('key', $secret)->delete(); - - // Delete the account if it already exists $account = Account::withoutGlobalScopes() - ->where('username', $username) - ->where('domain', $domain) - ->first(); + ->where('username', $username) + ->where('domain', $domain) + ->first(); - if ($account) { - // We don't have foreign keys yet… - $account->admin()->delete(); - $account->delete(); - } - - $account = new Account; - $account->username = $username; - $account->domain = $domain; - $account->email = 'admin_test@sip.example.org'; - $account->activated = true; - $account->user_agent = 'Test'; - $account->ip_address = '0.0.0.0'; - - // Create an "old" account to prevent unwanted deletion on the test server - $account->created_at = Carbon::now()->subYears(3); - $account->save(); - - $admin = new Admin; - $admin->account_id = $account->id; - $admin->save(); + ApiKey::where('account_id', $account->id)->delete(); $apiKey = new ApiKey; $apiKey->account_id = $account->id; @@ -79,7 +61,7 @@ class CreateAdminAccountTest extends Command $apiKey->key = $secret; $apiKey->save(); - $this->info('Admin test account created: "sip:' . $username . '@' . $domain . '" | API Key: "' . $secret . '"'); + $this->info('API Key updated to: ' . $secret); return 0; }