mirror of
https://github.com/h44z/wg-portal.git
synced 2025-08-12 16:22:23 +00:00
refactor startup admin context
This commit is contained in:
parent
a9136edaa7
commit
e43647426c
@ -38,8 +38,8 @@ func New(cfg *config.Config, bus evbus.MessageBus, authenticator Authenticator,
|
|||||||
startupContext, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
startupContext, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// The first user in the DB is admin.
|
// Switch to admin user context
|
||||||
startupContext = context.WithValue(startupContext, domain.CtxUserInfo, domain.GetAdminInfo())
|
startupContext = domain.SetUserInfo(startupContext, domain.SystemAdminContextUserInfo())
|
||||||
|
|
||||||
if err := a.createDefaultUser(startupContext); err != nil {
|
if err := a.createDefaultUser(startupContext); err != nil {
|
||||||
return nil, fmt.Errorf("failed to create default user: %w", err)
|
return nil, fmt.Errorf("failed to create default user: %w", err)
|
||||||
|
@ -20,6 +20,13 @@ func DefaultContextUserInfo() *ContextUserInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SystemAdminContextUserInfo() *ContextUserInfo {
|
||||||
|
return &ContextUserInfo{
|
||||||
|
Id: "_WG_SYS_ADMIN_",
|
||||||
|
IsAdmin: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func SetUserInfoFromGin(c *gin.Context) context.Context {
|
func SetUserInfoFromGin(c *gin.Context) context.Context {
|
||||||
ginUserInfo, exists := c.Get(CtxUserInfo)
|
ginUserInfo, exists := c.Get(CtxUserInfo)
|
||||||
|
|
||||||
@ -30,7 +37,7 @@ func SetUserInfoFromGin(c *gin.Context) context.Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.WithValue(c.Request.Context(), CtxUserInfo, info)
|
ctx := SetUserInfo(c.Request.Context(), info)
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,9 +58,3 @@ func GetUserInfo(ctx context.Context) *ContextUserInfo {
|
|||||||
|
|
||||||
return DefaultContextUserInfo()
|
return DefaultContextUserInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAdminInfo() *ContextUserInfo {
|
|
||||||
userInfo := DefaultContextUserInfo()
|
|
||||||
userInfo.IsAdmin = true
|
|
||||||
return userInfo
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user