Changeset 0.28.0.7 (#179)
This commit is contained in:
@@ -30,7 +30,10 @@ func (h *PersonaHandler) ListUserPersonas(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"personas": personas})
|
||||
if personas == nil {
|
||||
personas = []models.Persona{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": personas})
|
||||
}
|
||||
|
||||
func (h *PersonaHandler) CreateUserPersona(c *gin.Context) {
|
||||
@@ -121,7 +124,10 @@ func (h *PersonaHandler) ListTeamPersonas(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"personas": personas})
|
||||
if personas == nil {
|
||||
personas = []models.Persona{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": personas})
|
||||
}
|
||||
|
||||
func (h *PersonaHandler) CreateTeamPersona(c *gin.Context) {
|
||||
@@ -147,7 +153,34 @@ func (h *PersonaHandler) CreateTeamPersona(c *gin.Context) {
|
||||
c.JSON(http.StatusCreated, persona)
|
||||
}
|
||||
|
||||
// UpdateTeamPersona updates a team-scoped persona. Verifies the persona
|
||||
// belongs to the team specified in the URL path.
|
||||
func (h *PersonaHandler) UpdateTeamPersona(c *gin.Context) {
|
||||
if p := h.requireTeamPersona(c); p == nil {
|
||||
return
|
||||
}
|
||||
id := c.Param("id")
|
||||
|
||||
var patch models.PersonaPatch
|
||||
if err := c.ShouldBindJSON(&patch); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.stores.Personas.Update(c.Request.Context(), id, patch); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update persona"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "persona updated"})
|
||||
}
|
||||
|
||||
// DeleteTeamPersona deletes a team-scoped persona. Verifies the persona
|
||||
// belongs to the team specified in the URL path before deleting.
|
||||
func (h *PersonaHandler) DeleteTeamPersona(c *gin.Context) {
|
||||
if p := h.requireTeamPersona(c); p == nil {
|
||||
return
|
||||
}
|
||||
id := c.Param("id")
|
||||
|
||||
if err := h.stores.Personas.Delete(c.Request.Context(), id); err != nil {
|
||||
@@ -167,7 +200,10 @@ func (h *PersonaHandler) ListAdminPersonas(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"personas": personas})
|
||||
if personas == nil {
|
||||
personas = []models.Persona{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": personas})
|
||||
}
|
||||
|
||||
func (h *PersonaHandler) CreateAdminPersona(c *gin.Context) {
|
||||
@@ -286,6 +322,21 @@ func (h *PersonaHandler) GetPersonaKBs(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"data": kbs})
|
||||
}
|
||||
|
||||
// GetTeamPersonaKBs returns KBs bound to a persona, verifying team ownership.
|
||||
func (h *PersonaHandler) GetTeamPersonaKBs(c *gin.Context) {
|
||||
if p := h.requireTeamPersona(c); p == nil {
|
||||
return
|
||||
}
|
||||
personaID := c.Param("id")
|
||||
|
||||
kbs, err := h.stores.Personas.GetKBs(c.Request.Context(), personaID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load persona KBs"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": kbs})
|
||||
}
|
||||
|
||||
// SetPersonaKBs replaces the knowledge bases bound to a persona.
|
||||
func (h *PersonaHandler) SetPersonaKBs(c *gin.Context) {
|
||||
personaID := c.Param("id")
|
||||
@@ -307,6 +358,30 @@ func (h *PersonaHandler) SetPersonaKBs(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
|
||||
// SetTeamPersonaKBs replaces KBs bound to a persona, verifying team ownership.
|
||||
func (h *PersonaHandler) SetTeamPersonaKBs(c *gin.Context) {
|
||||
if p := h.requireTeamPersona(c); p == nil {
|
||||
return
|
||||
}
|
||||
personaID := c.Param("id")
|
||||
|
||||
var req struct {
|
||||
KBIDs []string `json:"kb_ids"`
|
||||
AutoSearch map[string]bool `json:"auto_search"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.stores.Personas.SetKBs(c.Request.Context(), personaID, req.KBIDs, req.AutoSearch); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update persona KBs"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
|
||||
// ── Persona Tool Grants (v0.25.0) ───────────
|
||||
|
||||
// GetPersonaToolGrants returns the tool names granted to a persona.
|
||||
@@ -318,6 +393,9 @@ func (h *PersonaHandler) GetPersonaToolGrants(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load tool grants"})
|
||||
return
|
||||
}
|
||||
if grants == nil {
|
||||
grants = []string{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": grants})
|
||||
}
|
||||
|
||||
@@ -351,3 +429,141 @@ func (h *PersonaHandler) SetPersonaToolGrants(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
|
||||
// ── User-Scoped Persona Avatar (with ownership check) ─────
|
||||
|
||||
// UploadUserPersonaAvatar uploads an avatar for a personal persona,
|
||||
// verifying the caller owns it.
|
||||
func (h *PersonaHandler) UploadUserPersonaAvatar(c *gin.Context) {
|
||||
userID := getUserID(c)
|
||||
personaID := c.Param("id")
|
||||
|
||||
// Verify ownership
|
||||
existing, err := h.stores.Personas.GetByID(c.Request.Context(), personaID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "persona not found"})
|
||||
return
|
||||
}
|
||||
if existing.Scope != models.ScopePersonal || existing.OwnerID == nil || *existing.OwnerID != userID {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "cannot modify this persona's avatar"})
|
||||
return
|
||||
}
|
||||
|
||||
// Delegate to the shared avatar handler
|
||||
UploadPersonaAvatar(c)
|
||||
}
|
||||
|
||||
// DeleteUserPersonaAvatar deletes an avatar for a personal persona,
|
||||
// verifying the caller owns it.
|
||||
func (h *PersonaHandler) DeleteUserPersonaAvatar(c *gin.Context) {
|
||||
userID := getUserID(c)
|
||||
personaID := c.Param("id")
|
||||
|
||||
// Verify ownership
|
||||
existing, err := h.stores.Personas.GetByID(c.Request.Context(), personaID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "persona not found"})
|
||||
return
|
||||
}
|
||||
if existing.Scope != models.ScopePersonal || existing.OwnerID == nil || *existing.OwnerID != userID {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "cannot modify this persona's avatar"})
|
||||
return
|
||||
}
|
||||
|
||||
// Delegate to the shared avatar handler
|
||||
DeletePersonaAvatar(c)
|
||||
}
|
||||
|
||||
// ── Team-Scoped Helpers ─────────────────────
|
||||
|
||||
// requireTeamPersona loads a persona by ID and verifies it belongs to
|
||||
// the team in the URL path. Returns the persona on success or writes
|
||||
// an error response and returns nil.
|
||||
func (h *PersonaHandler) requireTeamPersona(c *gin.Context) *models.Persona {
|
||||
teamID := c.Param("teamId")
|
||||
personaID := c.Param("id")
|
||||
|
||||
existing, err := h.stores.Personas.GetByID(c.Request.Context(), personaID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "persona not found"})
|
||||
return nil
|
||||
}
|
||||
if existing.Scope != models.ScopeTeam || existing.OwnerID == nil || *existing.OwnerID != teamID {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "persona does not belong to this team"})
|
||||
return nil
|
||||
}
|
||||
return existing
|
||||
}
|
||||
|
||||
// ── Team-Scoped Persona Tool Grants ─────────
|
||||
|
||||
// GetTeamPersonaToolGrants returns tool grants for a team persona,
|
||||
// verifying team ownership.
|
||||
func (h *PersonaHandler) GetTeamPersonaToolGrants(c *gin.Context) {
|
||||
if p := h.requireTeamPersona(c); p == nil {
|
||||
return
|
||||
}
|
||||
personaID := c.Param("id")
|
||||
grants, err := h.stores.Personas.GetToolGrants(c.Request.Context(), personaID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load tool grants"})
|
||||
return
|
||||
}
|
||||
if grants == nil {
|
||||
grants = []string{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": grants})
|
||||
}
|
||||
|
||||
// SetTeamPersonaToolGrants replaces tool grants for a team persona,
|
||||
// verifying team ownership.
|
||||
func (h *PersonaHandler) SetTeamPersonaToolGrants(c *gin.Context) {
|
||||
if p := h.requireTeamPersona(c); p == nil {
|
||||
return
|
||||
}
|
||||
personaID := c.Param("id")
|
||||
|
||||
var req struct {
|
||||
ToolNames []string `json:"tool_names"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
grants := make([]models.Grant, 0, len(req.ToolNames))
|
||||
for _, name := range req.ToolNames {
|
||||
grants = append(grants, models.Grant{
|
||||
PersonaID: personaID,
|
||||
GrantType: models.GrantTypeTool,
|
||||
GrantRef: name,
|
||||
})
|
||||
}
|
||||
|
||||
if err := h.stores.Personas.SetGrants(c.Request.Context(), personaID, grants); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update tool grants"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
|
||||
// ── Team-Scoped Persona Avatar ──────────────
|
||||
|
||||
// UploadTeamPersonaAvatar uploads an avatar for a team persona,
|
||||
// verifying team ownership.
|
||||
func (h *PersonaHandler) UploadTeamPersonaAvatar(c *gin.Context) {
|
||||
if p := h.requireTeamPersona(c); p == nil {
|
||||
return
|
||||
}
|
||||
UploadPersonaAvatar(c)
|
||||
}
|
||||
|
||||
// DeleteTeamPersonaAvatar deletes an avatar for a team persona,
|
||||
// verifying team ownership.
|
||||
func (h *PersonaHandler) DeleteTeamPersonaAvatar(c *gin.Context) {
|
||||
if p := h.requireTeamPersona(c); p == nil {
|
||||
return
|
||||
}
|
||||
DeletePersonaAvatar(c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user