Fix #14, convert the flexisip-tester account seeder MySQL script to a proper Laravel Seeder

This commit is contained in:
Timothée Jaussoin 2021-06-01 17:28:59 +02:00
parent 398867fa39
commit 0220010dc7
9 changed files with 610 additions and 379 deletions

View file

@ -174,4 +174,10 @@ FlexiAPI is providing endpoints to provision Liblinphone powered devices. You ca
The XML returned by the provisioning endpoint can be completed using hooks.
To do so, copy and rename the `provisioning_hooks.php.example` file into `provisioning_hooks.php` in the configuration directory and complete the functions in the file.
The functions already contains example codes to show you how the XML can be enhanced or completed.
The functions already contains example codes to show you how the XML can be enhanced or completed.
### Seed liblinphone test accounts
You can also seed the tables with test accounts for the liblinphone test suite with the following command (check LiblinphoneTesterAccoutSeeder for the JSON syntax):
php artisan accounts:seed /path/to/accounts.json

View file

@ -1,4 +1,21 @@
<?php
/*
Flexisip Account Manager is a set of tools to manage SIP accounts.
Copyright (C) 2020 Belledonne Communications SARL, All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App\Console\Commands;

View file

@ -1,4 +1,21 @@
<?php
/*
Flexisip Account Manager is a set of tools to manage SIP accounts.
Copyright (C) 2020 Belledonne Communications SARL, All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App\Console\Commands;
@ -11,35 +28,14 @@ use Carbon\Carbon;
class CreateAdminAccountTest extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'accounts:create-admin-test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a test admin account, only for tests purpose';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$username = 'admin_test';

View file

@ -1,4 +1,21 @@
<?php
/*
Flexisip Account Manager is a set of tools to manage SIP accounts.
Copyright (C) 2020 Belledonne Communications SARL, All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App\Console\Commands;

View file

@ -0,0 +1,57 @@
<?php
/*
Flexisip Account Manager is a set of tools to manage SIP accounts.
Copyright (C) 2020 Belledonne Communications SARL, All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App\Console\Commands;
use Database\Seeders\LiblinphoneTesterAccoutSeeder;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
class RunAccountSeeder extends Command
{
protected $signature = 'accounts:seed {json-file-path}';
protected $description = 'Seed some accounts from a JSON file';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$file = $this->argument('json-file-path');
if (!file_exists($file)) {
$this->info('The JSON file doesn\'t exists');
return Command::FAILURE;
}
$json = json_decode(file_get_contents($file));
if ($json == null || $json == false) {
$this->info('Malformed JSON file');
return Command::FAILURE;
}
$seeder = App::make(LiblinphoneTesterAccoutSeeder::class);
$seeder->run($json);
return Command::SUCCESS;
}
}

697
flexiapi/composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -2,6 +2,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MakeAccountTypesKeyUnique extends Migration
@ -9,18 +10,21 @@ class MakeAccountTypesKeyUnique extends Migration
public function up()
{
Schema::table('account_types', function (Blueprint $table) {
$table->string('key', 64)->change();
// Required for old MySQL, will break with SQLite
if (DB::getDriverName() === 'mysql') {
$table->string('key', 64)->change();
}
$table->unique('key');
});
}
public function down()
{
Schema::table('account_types', function (Blueprint $table) {
$table->dropUnique('account_types_key_unique');
$table->string('key', 256)->change();
if (DB::getDriverName() === 'mysql') {
$table->string('key', 256)->change();
}
});
}
}

View file

@ -0,0 +1,141 @@
<?php
namespace Database\Seeders;
use App\Account;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
/**
* This seeder is only used for liblinphone related tests
* The JSON MUST respect the following format. type: range requires a range object.
* alias and passwords are optionnal.
* [
* {"type": "account", "id": <id>, "username": "<username>", "domain": "<domain>",
* "passwords": [{ "hash": "<hash>", "algorithm": "<algorithm>"}],
* "alias": { "alias": "<alias>", "domain": "<domain>"}
* },
* {"type": "range", "id": "%range%", "username": "user_%range%", "domain": "<domain>",
* "range": {"from": 5, "to": 10},
* "passwords": [{ "hash": "<hash>", "algorithm": "<algorithm>"}]
* }
* ]
*/
class LiblinphoneTesterAccoutSeeder extends Seeder
{
public function run($json)
{
$accounts = [];
$passwords = [];
$aliases = [];
foreach ($json as $element) {
if ($element->type == 'account') {
array_push(
$accounts,
$this->generateAccountArray(
$element->id,
$element->username,
$element->domain,
$element->activated ?? true
)
);
if (isset($element->passwords)) {
foreach ($element->passwords as $password) {
array_push(
$passwords,
$this->generatePasswordArray(
$element->id,
$password->hash,
$password->algorithm
)
);
}
}
if (isset($element->alias)) {
array_push(
$aliases,
$this->generateAliasArray(
$element->id,
$element->alias->alias,
$element->alias->domain
)
);
}
}
if ($element->type == 'range') {
for ($i = $element->range->from; $i <= $element->range->to; $i++) {
array_push(
$accounts,
$this->generateAccountArray(
str_replace('%range%', $i, $element->id),
str_replace('%range%', $i, $element->username),
str_replace('%range%', $i, $element->domain),
$element->activated ?? true
)
);
array_push(
$passwords,
$this->generatePasswordArray($i, 'secret', 'CLRTXT')
);
}
}
}
// Ensure that we clear previous ones
$ids = array_map(function($account) { return (int)$account['id']; }, $accounts);
Account::withoutGlobalScopes()->whereIn('id', $ids)->delete();
// And seed the fresh ones
DB::table('accounts')->insert($accounts);
DB::table('passwords')->insert($passwords);
DB::table('aliases')->insert($aliases);
}
private function generateAccountArray(
int $id, string $username,
string $domain, bool $activated = true,
string $confirmationKey = null
): array {
return [
'id' => $id,
'username' => $username,
'domain' => $domain,
'email' => rawurlencode($username) . '@' . $domain,
'activated' => $activated,
'ip_address' => '',
'confirmation_key' => $confirmationKey,
'user_agent' => 'FlexiAPI Seeder',
'creation_time' => '2010-01-03 04:30:43'
];
}
private function generatePasswordArray(
int $accountId,
string $password,
string $algorythm
): array {
return [
'account_id' => $accountId,
'password' => $password,
'algorithm' => $algorythm
];
}
private function generateAliasArray(
int $accountId,
string $alias,
string $domain
): array {
return [
'account_id' => $accountId,
'alias' => $alias,
'domain' => $domain
];
}
}

View file

@ -8,7 +8,7 @@
#%define _datadir %{_datarootdir}
#%define _docdir %{_datadir}/doc
%define build_number 115
%define build_number 116
%define var_dir /var/opt/belledonne-communications
%define opt_dir /opt/belledonne-communications/share/flexisip-account-manager