@@ -13,6 +13,7 @@ import (
1313type Fetcher struct {
1414 kcc * kc.KeycloakClient
1515 Groups bool
16+ Roles bool
1617}
1718
1819func New (client * kc.KeycloakClient ) (* Fetcher , error ) {
@@ -26,6 +27,11 @@ func (f *Fetcher) WithGroups(groups bool) *Fetcher {
2627 return f
2728}
2829
30+ func (f * Fetcher ) WithRoles (roles bool ) * Fetcher {
31+ f .Roles = roles
32+ return f
33+ }
34+
2935func (f * Fetcher ) Fetch (ctx context.Context , outputWriter io.Writer , errorWriter common.ErrorWriter ) error {
3036 writer := js .NewJSONArrayWriter (outputWriter )
3137 defer writer .Close ()
@@ -63,10 +69,16 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter
6369 }
6470 }
6571
72+ if f .Roles {
73+ if err := f .fetchRoles (ctx , writer , errorWriter ); err != nil {
74+ return err
75+ }
76+ }
77+
6678 return nil
6779}
6880
69- func (f * Fetcher ) fetchGroups (ctx context.Context ,
81+ func (f * Fetcher ) fetchGroups (ctx context.Context , //nolint:dupl
7082 writer * js.JSONArrayWriter ,
7183 errorWriter common.ErrorWriter ,
7284) error {
@@ -106,3 +118,44 @@ func (f *Fetcher) fetchGroups(ctx context.Context,
106118
107119 return nil
108120}
121+
122+ func (f * Fetcher ) fetchRoles (ctx context.Context , //nolint:dupl
123+ writer * js.JSONArrayWriter ,
124+ errorWriter common.ErrorWriter ,
125+ ) error {
126+ roles , err := f .kcc .ListRoles (ctx )
127+ if err != nil {
128+ errorWriter .Error (err )
129+ return err
130+ }
131+
132+ for _ , role := range roles {
133+ role .Type = "role"
134+ roleBytes , err := json .Marshal (role )
135+ errorWriter .Error (err )
136+
137+ var obj map [string ]any
138+ if err := json .Unmarshal (roleBytes , & obj ); err != nil {
139+ errorWriter .Error (err )
140+ continue
141+ }
142+
143+ usersInRole , err := f .kcc .GetUsersOfRole (ctx , role .Name )
144+ errorWriter .Error (err )
145+
146+ usersInRoleBytes , err := json .Marshal (usersInRole )
147+ errorWriter .Error (err )
148+
149+ var users []map [string ]any
150+ if err := json .Unmarshal (usersInRoleBytes , & users ); err != nil {
151+ errorWriter .Error (err )
152+ }
153+
154+ obj ["users" ] = users
155+
156+ err = writer .Write (obj )
157+ errorWriter .Error (err )
158+ }
159+
160+ return nil
161+ }
0 commit comments