-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-user.js
More file actions
134 lines (114 loc) · 3.74 KB
/
create-user.js
File metadata and controls
134 lines (114 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const Database = require('better-sqlite3');
const bcrypt = require('bcrypt');
const path = require('path');
const fs = require('fs');
const readline = require('readline');
const dbDir = path.join(process.cwd(), 'database');
const dbPath = path.join(dbDir, 'users.sqlite');
// Ensure database directory exists
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}
const db = new Database(dbPath);
// Initialize tables if they don't exist
db.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE,
password TEXT,
can_upload INTEGER DEFAULT 0,
can_delete INTEGER DEFAULT 0,
can_access_private INTEGER DEFAULT 0,
is_admin INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS shares (
id TEXT PRIMARY KEY,
file_path TEXT NOT NULL,
password TEXT,
created_by INTEGER,
expires_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT
);
CREATE TABLE IF NOT EXISTS api_keys (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
key_hash TEXT NOT NULL UNIQUE,
name TEXT,
last_used_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
`);
// Add columns if they don't exist (migration for existing databases)
try {
db.exec(`ALTER TABLE users ADD COLUMN can_upload INTEGER DEFAULT 0`);
} catch (e) { /* Column exists */ }
try {
db.exec(`ALTER TABLE users ADD COLUMN can_delete INTEGER DEFAULT 0`);
} catch (e) { /* Column exists */ }
try {
db.exec(`ALTER TABLE users ADD COLUMN can_access_private INTEGER DEFAULT 0`);
} catch (e) { /* Column exists */ }
try {
db.exec(`ALTER TABLE users ADD COLUMN is_admin INTEGER DEFAULT 0`);
} catch (e) { /* Column exists */ }
try {
db.exec(`ALTER TABLE shares ADD COLUMN created_by INTEGER`);
} catch (e) { /* Column exists */ }
try {
db.exec(`ALTER TABLE shares ADD COLUMN expires_at DATETIME`);
} catch (e) { /* Column exists */ }
async function createUser(username, password) {
try {
const existingUser = db.prepare('SELECT * FROM users WHERE username = ?').get(username);
if (existingUser) {
console.error(`User '${username}' already exists.`);
process.exit(1);
}
const saltRounds = 10;
const hash = bcrypt.hashSync(password, saltRounds);
// Create user as admin with full permissions (upload, delete, private access, admin)
const stmt = db.prepare(`
INSERT INTO users (username, password, can_upload, can_delete, can_access_private, is_admin)
VALUES (?, ?, 1, 1, 1, 1)
`);
stmt.run(username, hash);
console.log(`User '${username}' created successfully as admin with full permissions.`);
process.exit(0);
} catch (error) {
console.error('Error creating user:', error);
process.exit(1);
}
}
async function prompt(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => rl.question(query, ans => {
rl.close();
resolve(ans);
}));
}
async function main() {
const args = process.argv.slice(2);
let username = args[0];
let password = args[1];
if (!username) {
username = await prompt('Enter username: ');
}
if (!password) {
password = await prompt('Enter password: ');
}
if (!username || !password) {
console.error('Error: Username and password are required.');
process.exit(1);
}
await createUser(username, password);
}
main();