Added an option to specify demo user, will be created on startup if it does not exist

This commit is contained in:
2026-03-02 15:28:00 +01:00
parent 660c44992e
commit 78c777170b
6 changed files with 17 additions and 7 deletions

View File

@@ -21,13 +21,13 @@
const appInfo = ref(null); const appInfo = ref(null);
const appInfoErr = ref(null); const appInfoErr = ref(null);
async function logInIfDemoMode(demoMode) { async function logInIfDemoMode(demoMode, demoUsername) {
if (!demoMode) { if (!demoMode) {
return; return;
} }
const demouser = ref({ const demouser = ref({
username: "demo", username: demoUsername,
password: "" password: ""
}); });
const res = await postLogin(demouser) const res = await postLogin(demouser)
@@ -37,7 +37,7 @@
onMounted(() => { onMounted(() => {
getAppInfo(appInfo, appInfoErr) getAppInfo(appInfo, appInfoErr)
.then(() => logInIfDemoMode(appInfo.value.demoMode)); .then(() => logInIfDemoMode(appInfo.value.demoMode, appInfo.value.demoUsername));
}) })
</script> </script>

View File

@@ -20,7 +20,8 @@ type Config struct {
Limit int `toml:"limit" default:"100" comment:"A single API call will return at most this number of records."` Limit int `toml:"limit" default:"100" comment:"A single API call will return at most this number of records."`
InventaireUrl string `toml:"inventaire-url" default:"https://inventaire.io" comment:"An inventaire.io instance URL."` InventaireUrl string `toml:"inventaire-url" default:"https://inventaire.io" comment:"An inventaire.io instance URL."`
DisableRegistration bool `toml:"disable-registration" default:"false" comment:"Disable new account creation."` DisableRegistration bool `toml:"disable-registration" default:"false" comment:"Disable new account creation."`
DemoMode bool `toml:"demo-mode" default:"false" comment:"Activate demo mode: anyone connecting to the instance will be logged in as user 'demo'"` DemoMode bool `toml:"demo-mode" default:"false" comment:"Activate demo mode: anyone connecting to the instance will be logged in as a single user."`
DemoUsername string `toml:"demo-username" default:"demo" comment:"Name of the single user used for the demo."`
AddUser UserListAsStrings `toml:"add-user" short:"a" comment:"Add an user on startup following htpasswd bcrypt format, example: [\"demo:$2y$10$UHR2646SZo2W.Rhna7bn5eWNLXWJZ/Sa3oLd9RlxlXs57Bwp6isOS\",\"user:$2y$10$3WYUp.VDpzJRywtrxO1s/uWfUIKpTE4yh5B1d2RCef3hvczYbEWTC\"]"` AddUser UserListAsStrings `toml:"add-user" short:"a" comment:"Add an user on startup following htpasswd bcrypt format, example: [\"demo:$2y$10$UHR2646SZo2W.Rhna7bn5eWNLXWJZ/Sa3oLd9RlxlXs57Bwp6isOS\",\"user:$2y$10$3WYUp.VDpzJRywtrxO1s/uWfUIKpTE4yh5B1d2RCef3hvczYbEWTC\"]"`
} }
@@ -46,6 +47,7 @@ func defaultConfig() Config {
InventaireUrl: "https://inventaire.io", InventaireUrl: "https://inventaire.io",
DisableRegistration: false, DisableRegistration: false,
DemoMode: false, DemoMode: false,
DemoUsername: "demo",
AddUser: []string{}, AddUser: []string{},
} }
} }

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"slices"
"strings" "strings"
"git.artlef.fr/PersonalLibraryManager/internal/config" "git.artlef.fr/PersonalLibraryManager/internal/config"
@@ -53,6 +54,11 @@ func CreateDefaultUsers(db *gorm.DB, config *config.Config) error {
usernames = append(usernames, splittedString[0]) usernames = append(usernames, splittedString[0])
usersPasswordMap[splittedString[0]] = splittedString[1] usersPasswordMap[splittedString[0]] = splittedString[1]
} }
if !slices.Contains(usernames, config.DemoUsername) {
usernames = append(usernames, config.DemoUsername)
usersPasswordMap[config.DemoUsername] = ""
}
var existingUsers []model.User var existingUsers []model.User
err := db.Where("name IN ?", usernames).Find(&existingUsers).Error err := db.Where("name IN ?", usernames).Find(&existingUsers).Error

View File

@@ -1,8 +1,9 @@
package dto package dto
type AppInfo struct { type AppInfo struct {
RegistrationDisabled bool `json:"registrationDisabled"` RegistrationDisabled bool `json:"registrationDisabled"`
DemoMode bool `json:"demoMode"` DemoMode bool `json:"demoMode"`
DemoUsername string `json:"demoUsername"`
} }
type BookGet struct { type BookGet struct {

View File

@@ -11,5 +11,6 @@ func GetAppInfo(ac appcontext.AppContext) {
ac.C.JSON(http.StatusOK, dto.AppInfo{ ac.C.JSON(http.StatusOK, dto.AppInfo{
RegistrationDisabled: ac.Config.DisableRegistration, RegistrationDisabled: ac.Config.DisableRegistration,
DemoMode: ac.Config.DemoMode, DemoMode: ac.Config.DemoMode,
DemoUsername: ac.Config.DemoUsername,
}) })
} }

View File

@@ -34,7 +34,7 @@ func PostLoginHandler(ac appcontext.AppContext) {
} }
username = user.Username username = user.Username
} else { } else {
username = "demo" username = ac.Config.DemoUsername
} }
var jwtToken string var jwtToken string