105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
package gonexapp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// Groups reads Users and Groups from Provisioning OCS.
|
|
type Groups struct {
|
|
Cred Credentials
|
|
Client *http.Client
|
|
OCS OCSClient
|
|
}
|
|
|
|
// NewGroups returns a directory reader using cred for AppAPI auth.
|
|
func NewGroups(cred Credentials) Groups {
|
|
return Groups{
|
|
Cred: cred,
|
|
OCS: OCSClient{Cred: cred},
|
|
}
|
|
}
|
|
|
|
func (g Groups) ocsClient() OCSClient {
|
|
c := g.OCS
|
|
if c.Cred.BaseURL == "" {
|
|
c.Cred = g.Cred
|
|
}
|
|
if c.Client == nil {
|
|
c.Client = g.Client
|
|
}
|
|
return c
|
|
}
|
|
|
|
// UserGroups returns the Nextcloud group ids of userID.
|
|
// The OCS call is authenticated as userID.
|
|
// It returns an error when the OCS call fails or the body cannot be decoded.
|
|
func (g Groups) UserGroups(userID string) ([]string, error) {
|
|
ocs := g.ocsClient()
|
|
ocs.Cred = ocs.Cred.WithUser(userID)
|
|
path := "cloud/users/" + url.PathEscape(userID) + "/groups"
|
|
raw, err := ocs.Call(http.MethodGet, path, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeUserGroups(raw)
|
|
}
|
|
|
|
// GroupMembers returns the user ids in groupID.
|
|
// The OCS call uses Cred as-is. Nextcloud requires that user to be an admin
|
|
// or a subadmin of the group.
|
|
// It returns an error when the OCS call fails or the body cannot be decoded.
|
|
// There is no search or paging.
|
|
func (g Groups) GroupMembers(groupID string) ([]string, error) {
|
|
ocs := g.ocsClient()
|
|
path := "cloud/groups/" + url.PathEscape(groupID)
|
|
raw, err := ocs.Call(http.MethodGet, path, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeGroupMembers(raw)
|
|
}
|
|
|
|
func decodeGroupMembers(raw []byte) ([]string, error) {
|
|
var parsed struct {
|
|
OCS struct {
|
|
Data struct {
|
|
Users []string `json:"users"`
|
|
} `json:"data"`
|
|
} `json:"ocs"`
|
|
}
|
|
if err := json.Unmarshal(raw, &parsed); err != nil {
|
|
return nil, fmt.Errorf("group members decode: %w", err)
|
|
}
|
|
return parsed.OCS.Data.Users, nil
|
|
}
|
|
|
|
// ListGroups returns instance group ids.
|
|
// The OCS call uses Cred as-is and needs an admin or subadmin.
|
|
// It returns an error when the OCS call fails or the body cannot be decoded.
|
|
// There is no search or paging.
|
|
func (g Groups) ListGroups() ([]string, error) {
|
|
ocs := g.ocsClient()
|
|
raw, err := ocs.Call(http.MethodGet, "cloud/groups", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeUserGroups(raw)
|
|
}
|
|
|
|
func decodeUserGroups(raw []byte) ([]string, error) {
|
|
var parsed struct {
|
|
OCS struct {
|
|
Data struct {
|
|
Groups []string `json:"groups"`
|
|
} `json:"data"`
|
|
} `json:"ocs"`
|
|
}
|
|
if err := json.Unmarshal(raw, &parsed); err != nil {
|
|
return nil, fmt.Errorf("user groups decode: %w", err)
|
|
}
|
|
return parsed.OCS.Data.Groups, nil
|
|
}
|