Premier commit
This commit is contained in:
52
cli/init_user.php
Normal file
52
cli/init_user.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
if (PHP_SAPI != 'cli') {
|
||||
exit('PHP SAPI must be cli');
|
||||
}
|
||||
|
||||
use app\modules\user\models\User;
|
||||
use app\modules\user\Rbac;
|
||||
|
||||
require(__DIR__ . '/../vendor/autoload.php');
|
||||
|
||||
|
||||
foreach (require __DIR__ . '/../env.php' as $key => $val) {
|
||||
putenv("{$key}={$val}");
|
||||
}
|
||||
|
||||
$db = new PDO('sqlite:' . getenv('SQLITE_DB'));
|
||||
|
||||
Rbac::$db = $db;
|
||||
|
||||
$query = file_get_contents(__DIR__ . '/../modules/user/sql/install-sqlite.sql');
|
||||
|
||||
if ($db->exec($query) === false) {
|
||||
$error = $db->errorInfo();
|
||||
throw new RuntimeException("Query failed with error : {$error[2]}");
|
||||
}
|
||||
|
||||
echo "Users table created.\n";
|
||||
|
||||
echo "Create admin user\n";
|
||||
|
||||
$name = readline("Nom : ");
|
||||
$email = readline("Email : ");
|
||||
$username = readline("Nom d'utilisateur : ");
|
||||
$password = readline("Mot de passe : ");
|
||||
|
||||
$user = new User($db);
|
||||
$user->bind([
|
||||
'name' => $name,
|
||||
'username' => $username,
|
||||
'email' => $email,
|
||||
'password' => $password,
|
||||
]);
|
||||
|
||||
if ($user->save()) {
|
||||
echo "Utilisateur $username créé.\n";
|
||||
}
|
||||
|
||||
if (!Rbac::roleExists('admin')) {
|
||||
Rbac::createRole('admin');
|
||||
}
|
||||
|
||||
Rbac::assignRole($user->id, 'admin');
|
||||
Reference in New Issue
Block a user