mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 06:32:28 +08:00
141 lines
3.8 KiB
Go
141 lines
3.8 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
|
|
|
claims "github.com/grafana/authlib/types"
|
|
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
|
iamv0 "github.com/grafana/grafana/pkg/apis/iam/v0alpha1"
|
|
"github.com/grafana/grafana/pkg/registry/apis/iam/common"
|
|
"github.com/grafana/grafana/pkg/registry/apis/iam/legacy"
|
|
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
|
"github.com/grafana/grafana/pkg/services/user"
|
|
)
|
|
|
|
var (
|
|
_ rest.Scoper = (*LegacyStore)(nil)
|
|
_ rest.SingularNameProvider = (*LegacyStore)(nil)
|
|
_ rest.Getter = (*LegacyStore)(nil)
|
|
_ rest.Lister = (*LegacyStore)(nil)
|
|
_ rest.Storage = (*LegacyStore)(nil)
|
|
)
|
|
|
|
var resource = iamv0.UserResourceInfo
|
|
|
|
func NewLegacyStore(store legacy.LegacyIdentityStore, ac claims.AccessClient) *LegacyStore {
|
|
return &LegacyStore{store, ac}
|
|
}
|
|
|
|
type LegacyStore struct {
|
|
store legacy.LegacyIdentityStore
|
|
ac claims.AccessClient
|
|
}
|
|
|
|
func (s *LegacyStore) New() runtime.Object {
|
|
return resource.NewFunc()
|
|
}
|
|
|
|
func (s *LegacyStore) Destroy() {}
|
|
|
|
func (s *LegacyStore) NamespaceScoped() bool {
|
|
return true // namespace == org
|
|
}
|
|
|
|
func (s *LegacyStore) GetSingularName() string {
|
|
return resource.GetSingularName()
|
|
}
|
|
|
|
func (s *LegacyStore) NewList() runtime.Object {
|
|
return resource.NewListFunc()
|
|
}
|
|
|
|
func (s *LegacyStore) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
|
return resource.TableConverter().ConvertToTable(ctx, object, tableOptions)
|
|
}
|
|
|
|
func (s *LegacyStore) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
|
|
res, err := common.List(
|
|
ctx, resource.GetName(), s.ac, common.PaginationFromListOptions(options),
|
|
func(ctx context.Context, ns claims.NamespaceInfo, p common.Pagination) (*common.ListResponse[iamv0.User], error) {
|
|
found, err := s.store.ListUsers(ctx, ns, legacy.ListUserQuery{
|
|
Pagination: p,
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
users := make([]iamv0.User, 0, len(found.Users))
|
|
for _, u := range found.Users {
|
|
users = append(users, toUserItem(&u, ns.Value))
|
|
}
|
|
|
|
return &common.ListResponse[iamv0.User]{
|
|
Items: users,
|
|
RV: found.RV,
|
|
Continue: found.Continue,
|
|
}, nil
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
obj := &iamv0.UserList{Items: res.Items}
|
|
obj.Continue = common.OptionalFormatInt(res.Continue)
|
|
obj.ResourceVersion = common.OptionalFormatInt(res.RV)
|
|
return obj, nil
|
|
}
|
|
|
|
func (s *LegacyStore) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
|
ns, err := request.NamespaceInfoFrom(ctx, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
found, err := s.store.ListUsers(ctx, ns, legacy.ListUserQuery{
|
|
OrgID: ns.OrgID,
|
|
UID: name,
|
|
Pagination: common.Pagination{Limit: 1},
|
|
})
|
|
if found == nil || err != nil {
|
|
return nil, resource.NewNotFound(name)
|
|
}
|
|
if len(found.Users) < 1 {
|
|
return nil, resource.NewNotFound(name)
|
|
}
|
|
|
|
obj := toUserItem(&found.Users[0], ns.Value)
|
|
return &obj, nil
|
|
}
|
|
|
|
func toUserItem(u *user.User, ns string) iamv0.User {
|
|
item := &iamv0.User{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: u.UID,
|
|
Namespace: ns,
|
|
ResourceVersion: fmt.Sprintf("%d", u.Updated.UnixMilli()),
|
|
CreationTimestamp: metav1.NewTime(u.Created),
|
|
},
|
|
Spec: iamv0.UserSpec{
|
|
Name: u.Name,
|
|
Login: u.Login,
|
|
Email: u.Email,
|
|
EmailVerified: u.EmailVerified,
|
|
Disabled: u.IsDisabled,
|
|
InternalID: u.ID,
|
|
},
|
|
}
|
|
obj, _ := utils.MetaAccessor(item)
|
|
obj.SetUpdatedTimestamp(&u.Updated)
|
|
obj.SetDeprecatedInternalID(u.ID) // nolint:staticcheck
|
|
return *item
|
|
}
|