You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.3 KiB
59 lines
2.3 KiB
package keycloak
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gopkg.in/h2non/gentleman.v2/plugins/body"
|
|
"gopkg.in/h2non/gentleman.v2/plugins/url"
|
|
)
|
|
|
|
const (
|
|
userPath = "/auth/admin/realms/:realm/users"
|
|
userCountPath = userPath + "/count"
|
|
userIDPath = userPath + "/:id"
|
|
)
|
|
|
|
// GetUsers returns a list of users, filtered according to the query parameters.
|
|
// Parameters: email, first (paging offset, int), firstName, lastName, username,
|
|
// max (maximum result size, default = 100),
|
|
// search (string contained in username, firstname, lastname or email)
|
|
func (c *Client) GetUsers(accessToken string, realmName string, paramKV ...string) ([]UserRepresentation, error) {
|
|
if len(paramKV)%2 != 0 {
|
|
return nil, fmt.Errorf("the number of key/val parameters should be even")
|
|
}
|
|
|
|
var resp = []UserRepresentation{}
|
|
var plugins = append(createQueryPlugins(paramKV...), url.Path(userPath), url.Param("realm", realmName))
|
|
var err = c.get(accessToken, &resp, plugins...)
|
|
return resp, err
|
|
}
|
|
|
|
// CreateUser creates the user from its UserRepresentation. The username must be unique.
|
|
func (c *Client) CreateUser(accessToken string, realmName string, user UserRepresentation) (string, error) {
|
|
var location *string
|
|
return *location, c.post(accessToken, nil, location, url.Path(userPath), url.Param("realm", realmName), body.JSON(user))
|
|
}
|
|
|
|
// CountUsers returns the number of users in the realm.
|
|
func (c *Client) CountUsers(accessToken string, realmName string) (int, error) {
|
|
var resp = 0
|
|
var err = c.get(accessToken, &resp, url.Path(userCountPath), url.Param("realm", realmName))
|
|
return resp, err
|
|
}
|
|
|
|
// GetUser get the represention of the user.
|
|
func (c *Client) GetUser(accessToken string, realmName, userID string) (UserRepresentation, error) {
|
|
var resp = UserRepresentation{}
|
|
var err = c.get(accessToken, &resp, url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID))
|
|
return resp, err
|
|
}
|
|
|
|
// UpdateUser updates the user.
|
|
func (c *Client) UpdateUser(accessToken string, realmName, userID string, user UserRepresentation) error {
|
|
return c.put(accessToken, url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID), body.JSON(user))
|
|
}
|
|
|
|
// DeleteUser deletes the user.
|
|
func (c *Client) DeleteUser(accessToken string, realmName, userID string) error {
|
|
return c.delete(accessToken, url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID))
|
|
}
|
|
|