Demo mode: automatically connect as demo user
This commit is contained in:
@@ -27,4 +27,5 @@ func TestGetAppInfo_Ok(t *testing.T) {
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
assert.Equal(t, false, appInfo.RegistrationDisabled)
|
||||
assert.Equal(t, false, appInfo.DemoMode)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ type Config struct {
|
||||
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."`
|
||||
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'"`
|
||||
}
|
||||
|
||||
func defaultConfig() Config {
|
||||
@@ -31,6 +32,7 @@ func defaultConfig() Config {
|
||||
Limit: 100,
|
||||
InventaireUrl: "https://inventaire.io",
|
||||
DisableRegistration: false,
|
||||
DemoMode: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package dto
|
||||
|
||||
type AppInfo struct {
|
||||
RegistrationDisabled bool `json:"registrationDisabled"`
|
||||
DemoMode bool `json:"demoMode"`
|
||||
}
|
||||
|
||||
type BookGet struct {
|
||||
|
||||
@@ -8,5 +8,8 @@ import (
|
||||
)
|
||||
|
||||
func GetAppInfo(ac appcontext.AppContext) {
|
||||
ac.C.JSON(http.StatusOK, dto.AppInfo{RegistrationDisabled: ac.Config.DisableRegistration})
|
||||
ac.C.JSON(http.StatusOK, dto.AppInfo{
|
||||
RegistrationDisabled: ac.Config.DisableRegistration,
|
||||
DemoMode: ac.Config.DemoMode,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -16,21 +16,29 @@ import (
|
||||
)
|
||||
|
||||
func PostLoginHandler(ac appcontext.AppContext) {
|
||||
var user dto.UserLogin
|
||||
err := ac.C.ShouldBindJSON(&user)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
|
||||
if !isUserAndPasswordOk(ac.Db, user.Username, user.Password) {
|
||||
ac.C.JSON(http.StatusUnauthorized,
|
||||
gin.H{"error": i18nresource.GetTranslatedMessage(&ac, "InvalidCredentials")})
|
||||
return
|
||||
var username string
|
||||
|
||||
if !ac.Config.DemoMode {
|
||||
var user dto.UserLogin
|
||||
err := ac.C.ShouldBindJSON(&user)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
|
||||
if !ac.Config.DemoMode && !isUserAndPasswordOk(ac.Db, user.Username, user.Password) {
|
||||
ac.C.JSON(http.StatusUnauthorized,
|
||||
gin.H{"error": i18nresource.GetTranslatedMessage(&ac, "InvalidCredentials")})
|
||||
return
|
||||
}
|
||||
username = user.Username
|
||||
} else {
|
||||
username = "demo"
|
||||
}
|
||||
|
||||
var jwtToken string
|
||||
jwtToken, err = jwtauth.GenerateJwtToken(user.Username)
|
||||
jwtToken, err := jwtauth.GenerateJwtToken(username)
|
||||
if err != nil {
|
||||
ac.C.JSON(http.StatusUnauthorized,
|
||||
gin.H{"error": fmt.Errorf("Error when generating JWT token: %w", err)})
|
||||
|
||||
Reference in New Issue
Block a user