Initial commit: Cloud Control Panel
This commit is contained in:
37
db/migration/001_create_users.php
Executable file
37
db/migration/001_create_users.php
Executable file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
return function (PDO $db): void {
|
||||
$db->exec("
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
");
|
||||
$stmt = $db->prepare(
|
||||
"SELECT COUNT(*) FROM users WHERE username = :username"
|
||||
);
|
||||
$stmt->execute(['username' => getenv('USER')]);
|
||||
|
||||
if ((int)$stmt->fetchColumn() > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$passwordHash = password_hash(
|
||||
getenv('PASSWORD'),
|
||||
PASSWORD_DEFAULT
|
||||
);
|
||||
|
||||
$stmt = $db->prepare("
|
||||
INSERT INTO users (username, password, created_at)
|
||||
VALUES (:username, :password, :created_at)
|
||||
");
|
||||
|
||||
$stmt->execute([
|
||||
'username' => $_ENV['USER'],
|
||||
'password' => $passwordHash,
|
||||
'created_at' => time(),
|
||||
]);
|
||||
};
|
||||
18
db/migration/002_create_sessions.php
Executable file
18
db/migration/002_create_sessions.php
Executable file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
return function (PDO $db): void {
|
||||
$db->exec("
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
auth_token TEXT NOT NULL UNIQUE,
|
||||
ip TEXT,
|
||||
user_agent TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
last_activity_at INTEGER NOT NULL,
|
||||
revoked_at INTEGER,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
");
|
||||
};
|
||||
13
db/migration/003_create_login_throttle.php
Executable file
13
db/migration/003_create_login_throttle.php
Executable file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
return function (PDO $db): void {
|
||||
$db->exec("
|
||||
CREATE TABLE IF NOT EXISTS login_throttle (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
ip TEXT NOT NULL,
|
||||
attempts INTEGER NOT NULL DEFAULT 1,
|
||||
last_attempt INTEGER NOT NULL
|
||||
);
|
||||
");
|
||||
};
|
||||
Reference in New Issue
Block a user