Add impersonation route
This commit is contained in:
parent
7540190794
commit
95692db8bd
3 changed files with 58 additions and 6 deletions
26
auth.go
26
auth.go
|
@ -27,6 +27,32 @@ func declareAPIAuthRoutes(router *gin.RouterGroup) {
|
|||
router.POST("/auth/logout", logout)
|
||||
}
|
||||
|
||||
func declareAPIAdminAuthRoutes(router *gin.RouterGroup) {
|
||||
router.POST("/auth/impersonate", func(c *gin.Context) {
|
||||
session := c.MustGet("Session").(*Session)
|
||||
|
||||
var u *User
|
||||
if err := c.ShouldBindJSON(&u); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
newuser, err := getUser(int(u.Id))
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
session.IdUser = &newuser.Id
|
||||
session.Update()
|
||||
|
||||
c.JSON(http.StatusOK, authToken{
|
||||
User: newuser,
|
||||
CurrentPromo: currentPromo,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
type authToken struct {
|
||||
*User
|
||||
CurrentPromo uint `json:"current_promo"`
|
||||
|
|
Reference in a new issue