fix(session): real getMyProfiles shape {myprofiles:[...]} + active-profile detection [#767]

https://projects.knownelement.com/issues/767#note-4191
This commit is contained in:
2026-09-04 08:56:38 -05:00
parent 5da1eee08f
commit 9d71c690b3
7 changed files with 199 additions and 29 deletions
+29 -6
View File
@@ -7,21 +7,44 @@ import (
)
// Profile is one GLPI profile of the logged-in user (GET /getMyProfiles,
// GET /Profile/<id>). IsActive reflects the session's active profile.
// GET /Profile/<id>). The myprofiles rows carry NO is_active on the
// real wire — the active profile is detected via GetActiveProfile.
type Profile struct {
ID int `json:"id"`
Name string `json:"name"`
IsActive bool `json:"is_active"`
}
// GetMyProfiles lists the profiles of the logged-in user. GLPI answers
// with a bare JSON array (not a wrapped object).
// GetMyProfiles lists the profiles of the logged-in user. GLPI's REAL
// wire shape (verified against prod cmdb 2026-09-03) is a WRAPPED
// object — {"myprofiles":[{"id":N,"name":"...","entities":[...]},...]} —
// NOT a bare array; rows may carry extra fields (entities, comments)
// which are ignored here. IsActive is left false by this call.
func (c *Client) GetMyProfiles(ctx context.Context) ([]Profile, error) {
var profiles []Profile
if err := c.call(ctx, http.MethodGet, "/getMyProfiles", nil, nil, &profiles); err != nil {
var body struct {
MyProfiles []Profile `json:"myprofiles"`
}
if err := c.call(ctx, http.MethodGet, "/getMyProfiles", nil, nil, &body); err != nil {
return nil, err
}
return profiles, nil
return body.MyProfiles, nil
}
// GetActiveProfile returns the id of the session's active profile
// (GET /getActiveProfile → {"active_profile":{...,"id":N,...}}).
func (c *Client) GetActiveProfile(ctx context.Context) (int, error) {
var body struct {
ActiveProfile struct {
ID int `json:"id"`
} `json:"active_profile"`
}
if err := c.call(ctx, http.MethodGet, "/getActiveProfile", nil, nil, &body); err != nil {
return 0, err
}
if body.ActiveProfile.ID == 0 {
return 0, fmt.Errorf("%w: getActiveProfile returned no id", ErrMalformedResponse)
}
return body.ActiveProfile.ID, nil
}
// ChangeActiveProfile switches the session's active profile