bugfix: set cameo flag only for admin user (clone user object)

This commit is contained in:
Settel 2022-12-09 20:48:33 +01:00
parent 2f3c34f5d9
commit 4827ff10b7
2 changed files with 24 additions and 3 deletions

View File

@ -3,8 +3,9 @@ package handler
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/user"
"strings" "strings"
"sirlab.de/go/knowyt/user"
) )
func (authMux *AuthMux) PrivateHandleFunc(pattern string, handlerFunc PrivateHandlerFunc) { func (authMux *AuthMux) PrivateHandleFunc(pattern string, handlerFunc PrivateHandlerFunc) {
@ -43,7 +44,8 @@ func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error)
if usr.IsAdmin() { if usr.IsAdmin() {
if cookieCameo, err := r.Cookie("knowyt-auth-cameo"); err == nil { if cookieCameo, err := r.Cookie("knowyt-auth-cameo"); err == nil {
if usrNew, err := authMux.app.GetUserById(cookieCameo.Value); err == nil { if usrCameo, err := authMux.app.GetUserById(cookieCameo.Value); err == nil {
usrNew := usrCameo.DeepCloneUserObj()
usrNew.SetCameo(usr) usrNew.SetCameo(usr)
return usrNew, nil return usrNew, nil
} }

View File

@ -5,9 +5,10 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"sirlab.de/go/knowyt/fileutil"
"strings" "strings"
"time" "time"
"sirlab.de/go/knowyt/fileutil"
) )
func NewUserFromFile(fileName string) (*User, error) { func NewUserFromFile(fileName string) (*User, error) {
@ -48,6 +49,24 @@ func CreateUser(fileName, gameId string) *User {
} }
} }
func (usr *User) DeepCloneUserObj() *User {
usr.mu.Lock()
defer usr.mu.Unlock()
usrNew := User{
id: usr.id,
filename: usr.filename,
authcode: usr.authcode,
name: usr.name,
role: usr.role,
gameId: usr.gameId,
created: usr.created,
lastLoggedIn: usr.lastLoggedIn,
cameo: usr.cameo,
}
return &usrNew
}
func (usr *User) SaveUser() error { func (usr *User) SaveUser() error {
usr.mu.Lock() usr.mu.Lock()
defer usr.mu.Unlock() defer usr.mu.Unlock()