-
Notifications
You must be signed in to change notification settings - Fork 14
/
realmRoles.go
99 lines (81 loc) · 2.46 KB
/
realmRoles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package keycloak
import (
"context"
"fmt"
"net/http"
)
// Role representation
type Role struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Composite *bool `json:"composite,omitempty"`
ClientRole *bool `json:"clientRole,omitempty"`
ContainerID *string `json:"containerId,omitempty"`
Attributes *map[string][]string `json:"attributes,omitempty"`
}
// RealmRolesService ...
type RealmRolesService service
// Create a new role.
func (s *RealmRolesService) Create(ctx context.Context, realm string, role *Role) (*http.Response, error) {
u := fmt.Sprintf("admin/realms/%s/roles", realm)
req, err := s.keycloak.NewRequest(http.MethodPost, u, role)
if err != nil {
return nil, err
}
return s.keycloak.Do(ctx, req, nil)
}
// RolesListOptions ...
type RolesListOptions struct {
BriefRepresentation bool `url:"briefRepresentation,omitempty"`
Search bool `url:"search,omitempty"`
Options
}
// List roles.
func (s *RealmRolesService) List(ctx context.Context, realm string, opts *RolesListOptions) ([]*Role, *http.Response, error) {
u := fmt.Sprintf("admin/realms/%s/roles", realm)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.keycloak.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var roles []*Role
res, err := s.keycloak.Do(ctx, req, &roles)
if err != nil {
return nil, nil, err
}
return roles, res, nil
}
// GetByName gets role by name.
func (s *RealmRolesService) GetByName(ctx context.Context, realm, name string) (*Role, *http.Response, error) {
u := fmt.Sprintf("admin/realms/%s/roles/%s", realm, name)
req, err := s.keycloak.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var role Role
res, err := s.keycloak.Do(ctx, req, &role)
if err != nil {
return nil, nil, err
}
return &role, res, nil
}
// GetByID gets role by id.
func (s *RealmRolesService) GetByID(ctx context.Context, realm, roleID string) (*Role, *http.Response, error) {
u := fmt.Sprintf("admin/realms/%s/roles-by-id/%s", realm, roleID)
req, err := s.keycloak.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var role Role
res, err := s.keycloak.Do(ctx, req, &role)
if err != nil {
return nil, nil, err
}
return &role, res, nil
}
// delete role
// delete client role