mirror of
https://github.com/SigNoz/signoz.git
synced 2026-04-29 23:20:27 +01:00
Some checks failed
build-staging / prepare (push) Has been cancelled
build-staging / js-build (push) Has been cancelled
build-staging / go-build (push) Has been cancelled
build-staging / staging (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
* feat: user v2 apis * fix: openapi specs * chore: address review comments * fix: proper handling if invalid roles are passed * chore: address review comments * refactor: frontend to use deprecated apis after id rename * feat: separate apis for adding and deleting user role * fix: invalidate token when roles are updated * fix: openapi specs and frontend test * fix: openapi schema * fix: openapi spec and move to snakecasing for json
99 lines
2.3 KiB
Go
99 lines
2.3 KiB
Go
package impluser
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/SigNoz/signoz/pkg/factory"
|
|
"github.com/SigNoz/signoz/pkg/sqlstore"
|
|
"github.com/SigNoz/signoz/pkg/types/authtypes"
|
|
"github.com/SigNoz/signoz/pkg/valuer"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type userRoleStore struct {
|
|
sqlstore sqlstore.SQLStore
|
|
settings factory.ProviderSettings
|
|
}
|
|
|
|
func NewUserRoleStore(sqlstore sqlstore.SQLStore, settings factory.ProviderSettings) authtypes.UserRoleStore {
|
|
return &userRoleStore{sqlstore: sqlstore, settings: settings}
|
|
}
|
|
|
|
func (store *userRoleStore) ListUserRolesByOrgIDAndUserIDs(ctx context.Context, orgID valuer.UUID, userIDs []valuer.UUID) ([]*authtypes.UserRole, error) {
|
|
userRoles := make([]*authtypes.UserRole, 0)
|
|
|
|
err := store.sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewSelect().
|
|
Model(&userRoles).
|
|
Join("JOIN users").
|
|
JoinOn("users.id = user_role.user_id").
|
|
Where("users.org_id = ?", orgID).
|
|
Where("users.id IN (?)", bun.In(userIDs)).
|
|
Relation("Role").
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return userRoles, nil
|
|
}
|
|
|
|
func (store *userRoleStore) CreateUserRoles(ctx context.Context, userRoles []*authtypes.UserRole) error {
|
|
_, err := store.sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewInsert().
|
|
Model(&userRoles).
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return store.sqlstore.WrapAlreadyExistsErrf(err, authtypes.ErrCodeUserRoleAlreadyExists, "duplicate role assignments for user")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *userRoleStore) DeleteUserRoles(ctx context.Context, userID valuer.UUID) error {
|
|
_, err := store.sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewDelete().
|
|
Model(new(authtypes.UserRole)).
|
|
Where("user_id = ?", userID).
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (store *userRoleStore) DeleteUserRoleByUserIDAndRoleID(ctx context.Context, userID valuer.UUID, roleID valuer.UUID) error {
|
|
_, err := store.sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewDelete().
|
|
Model(new(authtypes.UserRole)).
|
|
Where("user_id = ?", userID).
|
|
Where("role_id = ?", roleID).
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (store *userRoleStore) GetUserRolesByUserID(ctx context.Context, userID valuer.UUID) ([]*authtypes.UserRole, error) {
|
|
userRoles := make([]*authtypes.UserRole, 0)
|
|
|
|
err := store.sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewSelect().
|
|
Model(&userRoles).
|
|
Where("user_id = ?", userID).
|
|
Relation("Role").
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return userRoles, nil
|
|
}
|