feat(platform): align account user membership
This commit is contained in:
@@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -60,7 +61,7 @@ func (h *PlatformAccountUserHandler) Index(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, accountUsers)
|
||||
c.JSON(http.StatusOK, accountUsers)
|
||||
}
|
||||
|
||||
// Create adds a user to an account.
|
||||
@@ -84,39 +85,21 @@ func (h *PlatformAccountUserHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
var req struct {
|
||||
UserID uint `json:"user_id" binding:"required"`
|
||||
Role string `json:"role,omitempty"`
|
||||
UserID uint `json:"user_id" form:"user_id" binding:"required"`
|
||||
Role *string `json:"role,omitempty" form:"role"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Verify PlatformApp has permissible access to this user
|
||||
userPerm, err := h.permissibleRepo.FindByPlatformAppAndResource(c.Request.Context(), platformAppID, model.PermissibleTypeUser, req.UserID)
|
||||
if err != nil || userPerm == nil {
|
||||
response.AbortWithStatusError(c, http.StatusForbidden, response.ErrForbidden, "non permissible resource for user")
|
||||
return
|
||||
}
|
||||
|
||||
role := req.Role
|
||||
if role == "" {
|
||||
role = "agent"
|
||||
}
|
||||
|
||||
if err := h.accountRepo.AddUserToAccount(c.Request.Context(), accountID, req.UserID, role); err != nil {
|
||||
acctUser, err := h.accountRepo.UpsertAccountUser(c.Request.Context(), accountID, req.UserID, req.Role)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Return the created AccountUser
|
||||
acctUser, err := h.accountRepo.FindAccountUserByUserAndAccount(c.Request.Context(), accountID, req.UserID)
|
||||
if err != nil {
|
||||
response.OK(c, gin.H{"account_id": accountID, "user_id": req.UserID, "role": role})
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, acctUser)
|
||||
c.JSON(http.StatusOK, acctUser)
|
||||
}
|
||||
|
||||
// Destroy removes a user from an account.
|
||||
@@ -130,8 +113,8 @@ func (h *PlatformAccountUserHandler) Destroy(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, err := parseUintParam(c, "user_id")
|
||||
if err != nil {
|
||||
userID, err := platformAccountUserID(c)
|
||||
if err != nil || userID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid user ID")
|
||||
return
|
||||
}
|
||||
@@ -150,5 +133,19 @@ func (h *PlatformAccountUserHandler) Destroy(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(c)
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func platformAccountUserID(c *gin.Context) (uint, error) {
|
||||
if raw := c.Param("user_id"); raw != "" {
|
||||
id, err := strconv.ParseUint(raw, 10, 32)
|
||||
return uint(id), err
|
||||
}
|
||||
var req struct {
|
||||
UserID uint `json:"user_id" form:"user_id"`
|
||||
}
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return req.UserID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user