"create-admin" in config

Massimo Melina committed Oct 6, 2023 at 22:16 UTC 4f84e0bdaba2afee40ac06858912ad2e54161317
3 files changed +22 -14
README.md
+6 -5
@@ -57,11 +57,12 @@ NB: minimum Windows version required is 8.1 , Windows Server 2012 R2 (because of
57 - If you cannot find your system in the list, see next section [Other systems](#other-systems).
58 4. the browser should automatically open on `localhost` address, so you can configure the rest in the Admin-panel.
59 - if a browser cannot be opened on the computer where you are installing HFS,
60 - you should enter this command in HFS console: `create-admin <PASSWORD>`
61 - - if you are running as a service and cannot access the console, your best option is to stop it, launch it
62 - at command line (not as a service), and follow previous instruction.
63 - - if you can never access the console, even with the previous instructions,
64 - you can [edit config file add add your admin account](config.md#accounts)
60 + you should enter this command in the HFS console: `create-admin <PASSWORD>`
61 + - if you cannot access the console (like when you are running as a service),
62 + you can [edit the config file to add your admin account](config.md#accounts)
63 + - if you don't want to use an editor you can create the file with this command:
64 +
65 + `echo "create-admin: PASSWORD" > config.yaml`
66
67 If you access *Admin-panel* via localhost, by default HFS **won't** require you to login.
68 If you don't like this behavior, disable it in the Admin-panel or enter this console command `config localhost_admin false`.
src/commands.ts
+2 -9
@@ -1,6 +1,6 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 -import { addAccount, getAccount, updateAccount } from './perm'
3 +import { createAdmin, getAccount, updateAccount } from './perm'
4 import { getConfig, configKeyExists, setConfig } from './config'
5 import _ from 'lodash'
6 import { getUpdates, update } from './update'
@@ -58,14 +58,7 @@ const commands = {
58 },
59 'create-admin': {
60 params: '<password> [<username>=admin]',
61 - async cb(password: string, username='admin') {
62 - if (getAccount(username))
63 - throw `user ${username} already exists`
64 - const acc = addAccount(username, { admin: true })
65 - await updateAccount(acc!, acc => {
66 - acc.password = password
67 - })
68 - }
61 + cb: createAdmin
62 },
63 'change-password': {
64 params: '<user> <password>',
src/perm.ts
+14
@@ -53,6 +53,20 @@ export function saveSrpInfo(account:Account, salt:string | bigint, verifier: str
53
54 export const allowClearTextLogin = defineConfig('allow_clear_text_login', false)
55
56 +const createAdminConfig = defineConfig('create-admin', '')
57 +createAdminConfig.sub(v => {
58 + if (!v) return
59 + createAdmin(v)
60 + createAdminConfig.set('')
61 +})
62 +
63 +export function createAdmin(pass: string, username='admin') {
64 + const acc = addAccount(username, { admin: true })
65 + if (!acc) return console.log("cannot create, already exists")
66 + updateAccount(acc!, acc => { acc.password = pass })
67 + console.log("account admin created")
68 +}
69 +
70 const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters())
71
72 type Changer = (account:Account)=> void | Promise<void>