Search users by username instead of by email
As some setups don't have email addresses setup or reuse email addresses, the keycloak search has to be done by username as that is guaranteed to always be unique and defined. This PR changes that.
This commit is contained in:
@@ -90,34 +90,46 @@ func (c *ConcreteClient) SendActionsMail(ctx context.Context, realm, userID stri
|
||||
return c.keycloak.ExecuteActionsEmail(ctx, token.AccessToken, realm, params)
|
||||
}
|
||||
|
||||
// GetUserByEmail looks up a user by email.
|
||||
func (c *ConcreteClient) GetUserByEmail(ctx context.Context, realm, mail string) (*libregraph.User, error) {
|
||||
// GetUserByParams looks up a user by the given parameters.
|
||||
func (c *ConcreteClient) GetUserByParams(ctx context.Context, realm string, params gocloak.GetUsersParams) (*libregraph.User, error) {
|
||||
token, err := c.getToken(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
users, err := c.keycloak.GetUsers(ctx, token.AccessToken, realm, gocloak.GetUsersParams{
|
||||
Email: &mail,
|
||||
})
|
||||
users, err := c.keycloak.GetUsers(ctx, token.AccessToken, realm, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(users) == 0 {
|
||||
return nil, fmt.Errorf("no users found with mail address %s", mail)
|
||||
return nil, fmt.Errorf("no users found")
|
||||
}
|
||||
|
||||
if len(users) > 1 {
|
||||
return nil, fmt.Errorf("%d users found with mail address %s, expected 1", len(users), mail)
|
||||
return nil, fmt.Errorf("%d users found", len(users))
|
||||
}
|
||||
|
||||
return c.keycloakUserToLibregraph(users[0]), nil
|
||||
}
|
||||
|
||||
// GetUserByEmail looks up a user by email.
|
||||
func (c *ConcreteClient) GetUserByEmail(ctx context.Context, realm, mail string) (*libregraph.User, error) {
|
||||
return c.GetUserByParams(ctx, realm, gocloak.GetUsersParams{
|
||||
Email: &mail,
|
||||
})
|
||||
}
|
||||
|
||||
// GetUserByUsername looks up a user by username.
|
||||
func (c *ConcreteClient) GetUserByUsername(ctx context.Context, realm, username string) (*libregraph.User, error) {
|
||||
return c.GetUserByParams(ctx, realm, gocloak.GetUsersParams{
|
||||
Username: &username,
|
||||
})
|
||||
}
|
||||
|
||||
// GetPIIReport returns a structure with all the PII for the user.
|
||||
func (c *ConcreteClient) GetPIIReport(ctx context.Context, realm string, email string) (*PIIReport, error) {
|
||||
u, err := c.GetUserByEmail(ctx, realm, email)
|
||||
func (c *ConcreteClient) GetPIIReport(ctx context.Context, realm, username string) (*PIIReport, error) {
|
||||
u, err := c.GetUserByUsername(ctx, realm, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ type PIIReport struct {
|
||||
type Client interface {
|
||||
CreateUser(ctx context.Context, realm string, user *libregraph.User, userActions []UserAction) (string, error)
|
||||
SendActionsMail(ctx context.Context, realm, userID string, userActions []UserAction) error
|
||||
GetUserByParams(ctx context.Context, realm string, params gocloak.GetUsersParams) (*libregraph.User, error)
|
||||
GetUserByEmail(ctx context.Context, realm, email string) (*libregraph.User, error)
|
||||
GetPIIReport(ctx context.Context, realm string, email string) (*PIIReport, error)
|
||||
GetUserByUsername(ctx context.Context, realm, username string) (*libregraph.User, error)
|
||||
GetPIIReport(ctx context.Context, realm, username string) (*PIIReport, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user