mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
ai inherited five identity nouns from casibase — users, applications,
permissions, providers, sessions — and published all five under /v1/ai/*. IAM
publishes the same five words at /v1/iam/*, which cloud proxies wholesale to the
identity service. One noun, two addresses, and no way for a reader to know which
of them held the row. Each was answered separately, from the handler down.
permissions was a MIRROR. Every method in controllers/permission.go was an iam.*
call — the REST client in internal/iam, straight to the IAM server — so
/v1/ai/permissions held nothing and was a second address for
/v1/iam/permissions. Deleted, along with the controller that existed only to
serve it. The console's two live calls now go to IAM directly, which is already
where PermissionUtil sends the user to review what it just created.
users was USAGE wearing an identity name: object.GetUsers reads a store's
MESSAGES and returns the distinct senders, and its sibling rolls those same
messages up per user. Both are cuts of the usage record, so they are
sub-collections of it — /v1/ai/usages/{user-names,by-user}. Neither was ever a
user directory.
applications are KUBERNETES DEPLOYMENTS: a template rendered through kustomize
into a manifest, applied to a namespace, with a pod phase for Status and a
service URL. Nothing about an OAuth client. Now /v1/ai/deployments.
sessions are ai's OWN cookie sessions — Signin writes the beego session id,
Signout deletes it — so IAM cannot see them and /v1/iam/sessions is a different
set of rows. They stay, as /v1/ai/signin-sessions.
providers stay exactly where they are: ai's own provider table (model,
embedding, storage, tts, stt, agent, scan), super-admin gated, and the one
console module already calling the right address.
The POLICY KEYS DO NOT MOVE. The filters hold hand-curated super-admin and
balance sets keyed by the old flat names, so key/keyPlural pin "application" and
"session", and the two usage cuts keep get-users / get-user-table-infos through
kebab(method). A renamed URL with a renamed key would silently un-gate an admin
endpoint; TestPolicyKeyRoundTrip now asserts each one at its new address.
Fixed while here: the sessions member GET resolved to "GetSession", which the
embedded web.Controller also defines as its session-value getter. Reflection
found that one — so the table test passed — and dispatch then called a
one-argument method with no arguments, panicking. readMethod names
GetSingleSession, which now reads the composite id its route actually supplies.
The published spec is regenerated from the table (hanzoai/openapi, same branch):
107 paths, drift test green.
Co-authored-by: Hanzo Dev <[email protected]>
176 lines
5.5 KiB
Go
176 lines
5.5 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2025 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
// GetSessions
|
|
// @Title GetSessions
|
|
// @Tag Session API
|
|
// @Description Get organization user sessions.
|
|
// @Param owner query string true "The organization name"
|
|
// @Success 200 {array} string The Response object
|
|
// @router /get-sessions [get]
|
|
func (c *ApiController) GetSessions() {
|
|
limit := c.Input().Get("pageSize")
|
|
page := c.Input().Get("p")
|
|
field := c.Input().Get("field")
|
|
value := c.Input().Get("value")
|
|
sortField := c.Input().Get("sortField")
|
|
sortOrder := c.Input().Get("sortOrder")
|
|
owner, allowed := c.GetScopedOwner()
|
|
if !allowed {
|
|
return
|
|
}
|
|
|
|
if limit == "" || page == "" {
|
|
sessions, err := object.GetSessions(owner)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(sessions)
|
|
} else {
|
|
limit := util.ParseInt(limit)
|
|
count, err := object.GetSessionCount(owner, field, value)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
paginator := util.NewPaginator(c.Ctx.Request, limit, count)
|
|
sessions, err := object.GetPaginationSessions(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(sessions, paginator.Nums())
|
|
}
|
|
}
|
|
|
|
// GetSingleSession
|
|
// @Title GetSingleSession
|
|
// @Tag Session API
|
|
// @Description Get session for one user in one application.
|
|
// @Param id query string true "The id(organization/user) of session"
|
|
// @Success 200 {array} string The Response object
|
|
// @router /get-session [get]
|
|
func (c *ApiController) GetSingleSession() {
|
|
// "id", like every other member read: the router composes it from the
|
|
// :owner/:name segments of the member URL (web/router.go). Reading a
|
|
// differently-named parameter would leave this handler the only one in the
|
|
// surface that its own route cannot address.
|
|
id := c.Input().Get("id")
|
|
|
|
session, err := object.GetSession(id)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(session)
|
|
}
|
|
|
|
// UpdateSession
|
|
// @Title UpdateSession
|
|
// @Tag Session API
|
|
// @Description Update session for one user in one application.
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
// @Success 200 {array} string The Response object
|
|
// @router /update-session [post]
|
|
func (c *ApiController) UpdateSession() {
|
|
var session object.Session
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &session)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = wrapActionResponse(object.UpdateSession(util.GetIdFromOwnerAndName(session.Owner, session.Name), &session))
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// AddSession
|
|
// @Title AddSession
|
|
// @Tag Session API
|
|
// @Description Add session for one user in one application. If there are other existing sessions, join the session into the list.
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
// @Param sessionId query string true "sessionId to be added"
|
|
// @Success 200 {array} string The Response object
|
|
// @router /add-session [post]
|
|
func (c *ApiController) AddSession() {
|
|
var session object.Session
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &session)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = wrapActionResponse(object.AddSession(&session))
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// DeleteSession
|
|
// @Title DeleteSession
|
|
// @Tag Session API
|
|
// @Description Delete session for one user in one application.
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
// @Success 200 {array} string The Response object
|
|
// @router /delete-session [post]
|
|
func (c *ApiController) DeleteSession() {
|
|
var session object.Session
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &session)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
if len(session.SessionId) == 0 {
|
|
c.ResponseError(c.T("controllers:No sessions to delete"))
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = wrapActionResponse(object.DeleteSession(util.GetIdFromOwnerAndName(session.Owner, session.Name)))
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// IsSessionDuplicated
|
|
// @Title IsSessionDuplicated
|
|
// @Tag Session API
|
|
// @Description Check if there are other different sessions for one user in one application.
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
// @Param sessionId query string true "sessionId to be checked"
|
|
// @Success 200 {array} string The Response object
|
|
// @router /is-session-duplicated [get]
|
|
func (c *ApiController) IsSessionDuplicated() {
|
|
id := c.Input().Get("sessionPkId")
|
|
sessionId := c.Input().Get("sessionId")
|
|
|
|
isUserSessionDuplicated, err := object.IsSessionDuplicated(id, sessionId)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(isUserSessionDuplicated)
|
|
}
|