2021-09-08 09:52:54 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddUserCap adds the capabilities for a user.
|
2021-10-01 15:35:37 +00:00
|
|
|
//
|
2021-09-08 09:52:54 +00:00
|
|
|
// On Success, it returns the updated list of UserCaps for the user.
|
|
|
|
func (api *API) AddUserCap(ctx context.Context, uid, userCap string) ([]UserCapSpec, error) {
|
|
|
|
if uid == "" {
|
|
|
|
return nil, errMissingUserID
|
|
|
|
}
|
|
|
|
if userCap == "" {
|
|
|
|
return nil, errMissingUserCap
|
|
|
|
}
|
|
|
|
|
|
|
|
user := User{ID: uid, UserCaps: userCap}
|
2022-02-11 13:18:34 +00:00
|
|
|
body, err := api.call(ctx, http.MethodPut, "/user?caps", valueToURLParams(user, []string{"uid", "user-caps"}))
|
2021-09-08 09:52:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ref []UserCapSpec
|
|
|
|
err = json.Unmarshal(body, &ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUserCap removes the capabilities from a user.
|
2021-10-01 15:35:37 +00:00
|
|
|
//
|
2021-09-08 09:52:54 +00:00
|
|
|
// On Success, it returns the updated list of UserCaps for the user.
|
|
|
|
func (api *API) RemoveUserCap(ctx context.Context, uid, userCap string) ([]UserCapSpec, error) {
|
|
|
|
if uid == "" {
|
|
|
|
return nil, errMissingUserID
|
|
|
|
}
|
|
|
|
if userCap == "" {
|
|
|
|
return nil, errMissingUserCap
|
|
|
|
}
|
|
|
|
|
|
|
|
user := User{ID: uid, UserCaps: userCap}
|
2022-02-11 13:18:34 +00:00
|
|
|
body, err := api.call(ctx, http.MethodDelete, "/user?caps", valueToURLParams(user, []string{"uid", "user-caps"}))
|
2021-09-08 09:52:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ref []UserCapSpec
|
|
|
|
err = json.Unmarshal(body, &ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref, nil
|
|
|
|
}
|