2021-11-05 09:37:30 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2021-11-05 09:37:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const pageSize = 100
|
|
|
|
|
|
|
|
func (ms *MemorySearcher) FetchUsers() []api.User {
|
|
|
|
fetchUsersOffset := func(page int) (*api.PaginatedUserList, error) {
|
|
|
|
users, _, err := ms.si.GetAPIClient().CoreApi.CoreUsersList(context.TODO()).Page(int32(page)).PageSize(pageSize).Execute()
|
|
|
|
if err != nil {
|
|
|
|
ms.log.WithError(err).Warning("failed to update users")
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-11 22:18:32 +00:00
|
|
|
ms.log.WithField("page", page).WithField("count", len(users.Results)).Debug("fetched users")
|
2021-11-05 09:37:30 +00:00
|
|
|
return &users, nil
|
|
|
|
}
|
|
|
|
page := 1
|
|
|
|
users := make([]api.User, 0)
|
|
|
|
for {
|
|
|
|
apiUsers, err := fetchUsersOffset(page)
|
|
|
|
if err != nil {
|
|
|
|
return users
|
|
|
|
}
|
2021-11-11 22:18:32 +00:00
|
|
|
users = append(users, apiUsers.Results...)
|
2021-11-05 09:37:30 +00:00
|
|
|
if apiUsers.Pagination.Next > 0 {
|
|
|
|
page += 1
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MemorySearcher) FetchGroups() []api.Group {
|
|
|
|
fetchGroupsOffset := func(page int) (*api.PaginatedGroupList, error) {
|
|
|
|
groups, _, err := ms.si.GetAPIClient().CoreApi.CoreGroupsList(context.TODO()).Page(int32(page)).PageSize(pageSize).Execute()
|
|
|
|
if err != nil {
|
|
|
|
ms.log.WithError(err).Warning("failed to update groups")
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-11 22:18:32 +00:00
|
|
|
ms.log.WithField("page", page).WithField("count", len(groups.Results)).Debug("fetched groups")
|
2021-11-05 09:37:30 +00:00
|
|
|
return &groups, nil
|
|
|
|
}
|
|
|
|
page := 1
|
|
|
|
groups := make([]api.Group, 0)
|
|
|
|
for {
|
|
|
|
apiGroups, err := fetchGroupsOffset(page)
|
|
|
|
if err != nil {
|
|
|
|
return groups
|
|
|
|
}
|
2021-11-11 22:18:32 +00:00
|
|
|
groups = append(groups, apiGroups.Results...)
|
2021-11-05 09:37:30 +00:00
|
|
|
if apiGroups.Pagination.Next > 0 {
|
|
|
|
page += 1
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return groups
|
|
|
|
}
|