feat: admin can switch to another user

This commit is contained in:
Settel 2022-03-25 20:46:08 +01:00
parent e9f45e8176
commit 58a28b0d12
10 changed files with 105 additions and 13 deletions

View File

@ -67,6 +67,13 @@ export default {
this.$router.push({ path })
},
async logout() {
const user = this.$store.state.engine.user
if (user && user.isCameo) {
await this.$axios.$get('/api/cameo')
this.go('/admin')
return
}
this.authCode = ''
await this.$axios.$get('/api/logout')
this.go('/')

View File

@ -7,7 +7,12 @@
<th class="admin-tile-games__table-head">#&nbsp;players</th>
<th class="admin-tile-games__table-head">Gamemaster(s)</th>
</tr>
<tr v-for="id in Object.keys(games || {})" :key="id">
<tr
class="admin-tile-games__game"
@click="selectGame(id)"
v-for="id in Object.keys(games || {})"
:key="id"
>
<td>{{ games[id].name }}</td>
<td>{{ games[id].state }}</td>
<td>{{ games[id].players.length }}</td>
@ -39,6 +44,18 @@ export default {
return masters
},
},
methods: {
async selectGame(gameId) {
const game = this.games[gameId]
for (const player of game.players) {
if (player.role === 'gamemaster') {
await this.$axios.$get(`/api/cameo?code=${player.authcode}`)
break
}
}
this.$router.push('/gamemaster')
},
},
}
</script>
@ -52,5 +69,11 @@ export default {
&__table-head {
text-align: left;
}
&__game {
cursor: pointer;
&:hover {
color: #a0a0a0;
}
}
}
</style>

View File

@ -16,7 +16,12 @@
<th class="admin-tile-players__table-head">Score</th>
<th class="admin-tile-players__table-head">Status</th>
</tr>
<tr class="admin-tile-players__player" @click="editPlayer(player)" v-for="player in players" :key="player.id">
<tr
class="admin-tile-players__player"
@click="editPlayer(player)"
v-for="player in players"
:key="player.id"
>
<td>{{ player.name }}</td>
<td>{{ player.numQuotes }}</td>
<td>{{ player.score }}</td>

View File

@ -1,4 +0,0 @@
{
"quote": "Ich wurde jahrelang von meinen Eltern unter der Treppe eingesperrt.",
"source": "9db57a3f-7fa3-478f-86d5-24f24918fb91"
}

View File

@ -12,6 +12,7 @@ func (authMux *AuthMux) createCookie() *http.Cookie {
Path: "/",
HttpOnly: true,
MaxAge: -1,
Secure: true,
SameSite: http.SameSiteLaxMode,
}
}
@ -54,3 +55,33 @@ func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) {
return usr, nil
}
func (authMux *AuthMux) Cameo(usr *user.User, w http.ResponseWriter, r *http.Request) {
if !usr.IsAdmin() {
usrCameo := usr.GetCameo()
if usrCameo != nil && usrCameo.IsAdmin() {
cookie := authMux.createCookie()
cookie.Name = cookie.Name + "-cameo"
http.SetCookie(w, cookie)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "ok")
return
}
authMux.accessDenied(w, r)
return
}
cookie := authMux.createCookie()
cookie.Name = cookie.Name + "-cameo"
usrCameo, err := authMux.checkCode(r)
if err != nil {
http.SetCookie(w, cookie)
authMux.accessDenied(w, r)
return
}
cookie.Value = usrCameo.GetId()
cookie.MaxAge = 0
http.SetCookie(w, cookie)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "ok")
}

View File

@ -41,5 +41,14 @@ func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error)
return nil, fmt.Errorf("invalid cookie")
}
if usr.IsAdmin() {
if cookieCameo, err := r.Cookie("knowyt-auth-cameo"); err == nil {
if usrNew, err := authMux.app.GetUserById(cookieCameo.Value); err == nil {
usrNew.SetCameo(usr)
return usrNew, nil
}
}
}
return usr, nil
}

View File

@ -8,10 +8,11 @@ import (
)
type UserInfoJson struct {
Id string `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
GameId string `json:"game"`
Id string `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
GameId string `json:"game"`
IsCameo string `json:"isCameo",omitempty`
}
func (authMux *AuthMux) GetUserInfo(usr *user.User, w http.ResponseWriter, r *http.Request) {
@ -21,6 +22,9 @@ func (authMux *AuthMux) GetUserInfo(usr *user.User, w http.ResponseWriter, r *ht
Role: usr.GetRole(),
GameId: usr.GetGameId(),
}
if usr.GetCameo() != nil {
usrLight.IsCameo = "true"
}
w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(usrLight)

View File

@ -20,6 +20,7 @@ func main() {
mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
mux.PublicHandleFunc("/api/login", mux.Login)
mux.PublicHandleFunc("/api/logout", mux.Logout)
mux.PrivateHandleFunc("/api/cameo", mux.Cameo)
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
mux.PrivateHandleFunc("/api/games", app.GetGames)

View File

@ -18,6 +18,7 @@ type User struct {
name string
role string
gameId string
cameo *User
}
type UserJson struct {
@ -28,7 +29,8 @@ type UserJson struct {
}
type UserinfoJson struct {
Name string `json:"name"`
Role string `json:"role"`
GameId string `json:"game"`
Name string `json:"name"`
Role string `json:"role"`
GameId string `json:"game"`
IsCameo bool `json:"isCameo",omitempty`
}

View File

@ -139,3 +139,17 @@ func (usr *User) IsAdmin() bool {
return usr.role == ROLE_ADMIN
}
func (usr *User) SetCameo(usrCameo *User) {
usr.mu.Lock()
defer usr.mu.Unlock()
usr.cameo = usrCameo
}
func (usr *User) GetCameo() *User {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.cameo
}