feat: setup app, create admin user (WIP)
This commit is contained in:
parent
c871418d07
commit
846f137b7a
8
client/src/composables/engine/setupApp.ts
Normal file
8
client/src/composables/engine/setupApp.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { useUserinfoStore } from "@/stores/UserinfoStore"
|
||||
import { EngineContext } from '@/composables/useEngine'
|
||||
|
||||
export async function setupApp(this: EngineContext, authcode: string): Promise<void> {
|
||||
await this.callApi('/api/setupApp', {
|
||||
authcode,
|
||||
})
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import { Ref, ref } from 'vue'
|
||||
import { callApi, QueryParams } from '@/composables/engine/callApi'
|
||||
import { start, stop } from '@/composables/engine/startStop'
|
||||
import { setupApp } from '@/composables/engine/setupApp'
|
||||
import { fetchUpdate } from '@/composables/engine/fetchUpdate'
|
||||
import { loadQuotes, getQuotesRef, deleteQuote, saveQuote } from '@/composables/engine/quotes'
|
||||
import { fetchGameInfo, fetchGameInfos, setGameLang, setGameName, savePlayer, removePlayer, createGame, cameo, logoutCameo } from '@/composables/engine/gameManagement'
|
||||
@ -48,6 +49,7 @@ export interface useEngine {
|
||||
createGame: (name: string, teamname: string, lang: Lang) => Promise<CreateGameStatus>
|
||||
cameo: (authcode: string) => Promise<void>
|
||||
logoutCameo: () => Promise<void>
|
||||
setupApp: (authcode: string) => Promise<void>
|
||||
}
|
||||
|
||||
export default (): useEngine => {
|
||||
@ -92,5 +94,6 @@ export default (): useEngine => {
|
||||
createGame: (name: string, teamname: string, lang: Lang) => createGame.apply(context, [name, teamname, lang]),
|
||||
cameo: (authcode: string) => cameo.apply(context,[authcode]),
|
||||
logoutCameo: () => logoutCameo.apply(context),
|
||||
setupApp: (authcode) => setupApp.apply(context, [authcode]),
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@
|
||||
import { ref } from 'vue'
|
||||
import useAuth from '@/composables/useAuth'
|
||||
import useI18n from '@/composables/useI18n'
|
||||
import useEngine from '@/composables/useEngine'
|
||||
|
||||
const { $t } = useI18n({
|
||||
'create admin user': { en: 'create admin user', de: 'Admin-Benutzer anlegen' },
|
||||
@ -52,7 +53,7 @@ const openModal = () => {
|
||||
}
|
||||
|
||||
const createAdminAccount = () => {
|
||||
|
||||
useEngine().setupApp(authcode.value)
|
||||
}
|
||||
</script>
|
||||
|
||||
|
2
server/.gitignore
vendored
2
server/.gitignore
vendored
@ -1,2 +1,2 @@
|
||||
knowyt
|
||||
data/games/*/state.json
|
||||
data/
|
||||
|
@ -2,23 +2,29 @@ package application
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"path"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"sirlab.de/go/knowyt/game"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
func (app *Application) createUser(gm *game.Game, name, role, authcode string) (string, error) {
|
||||
gameId := ""
|
||||
if gm != nil {
|
||||
gameId = gm.GetId()
|
||||
}
|
||||
|
||||
if authcode != "" {
|
||||
if _, err := app.GetUserByAuthcode(authcode); err == nil {
|
||||
return "", fmt.Errorf("%s authcode already in use", gm.GetId())
|
||||
return "", fmt.Errorf("%s authcode already in use", gameId)
|
||||
}
|
||||
}
|
||||
|
||||
dirName := path.Join(app.config.DataDir, "users")
|
||||
userNewUuid := uuid.NewString()
|
||||
fileName := path.Join(dirName, userNewUuid+".json")
|
||||
newUser := user.CreateUser(fileName, gm.GetId())
|
||||
newUser := user.CreateUser(fileName, gameId)
|
||||
newUser.SetRole(role)
|
||||
newUser.SetName(name)
|
||||
newUser.SetAuthcode(authcode)
|
||||
|
37
server/src/application/setupApp.go
Normal file
37
server/src/application/setupApp.go
Normal file
@ -0,0 +1,37 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
func (app *Application) SetupApp(w http.ResponseWriter, r *http.Request) {
|
||||
authcode := r.URL.Query().Get("authcode")
|
||||
if match, err := regexp.MatchString("^\\d{6}$", authcode); err != nil || !match {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.Error("setupApp failed: invalid authcode does not consist of exactly 6 digits")
|
||||
fmt.Fprintf(w, "server error")
|
||||
return
|
||||
}
|
||||
|
||||
_, err := app.createUser(nil, "Admin", user.ROLE_ADMIN, authcode)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "server error")
|
||||
return
|
||||
}
|
||||
|
||||
// if err != nil {
|
||||
// w.WriteHeader(http.StatusNotFound)
|
||||
// fmt.Fprintf(w, "...")
|
||||
// return
|
||||
// }
|
||||
|
||||
log.Debug("setup app successful\n")
|
||||
fmt.Fprintf(w, "ok")
|
||||
}
|
@ -21,6 +21,7 @@ func main() {
|
||||
mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
|
||||
mux.PublicHandleFunc("/api/login", mux.Login)
|
||||
mux.PublicHandleFunc("/api/logout", mux.Logout)
|
||||
mux.PublicHandleFunc("/api/setupApp", app.SetupApp)
|
||||
mux.PublicHandleFunc("/api/createGame", app.CreateGame)
|
||||
mux.PrivateOrPublicHandleFunc("/api/userinfo", mux.GetUserInfo, mux.CheckSetup)
|
||||
mux.PrivateHandleFunc("/api/cameo", mux.Cameo)
|
||||
|
Loading…
Reference in New Issue
Block a user