feat: add permission
This commit is contained in:
parent
d31b44d434
commit
25075f3d7a
|
|
@ -7,14 +7,14 @@ import (
|
||||||
|
|
||||||
// PermissionRepository 權限 Repository 介面
|
// PermissionRepository 權限 Repository 介面
|
||||||
type PermissionRepository interface {
|
type PermissionRepository interface {
|
||||||
// Get 取得單一權限
|
// FindOne 取得單一權限
|
||||||
FindOne(ctx context.Context, id string) (*entity.Permission, error)
|
FindOne(ctx context.Context, id string) (*entity.Permission, error)
|
||||||
// GetByName 根據名稱取得權限
|
// FindByName 根據名稱取得權限
|
||||||
GetByName(ctx context.Context, name string) (*entity.Permission, error)
|
FindByName(ctx context.Context, name string) (*entity.Permission, error)
|
||||||
// GetByNames 批量根據名稱取得權限
|
// GetByNames 批量根據名稱取得權限
|
||||||
GetByNames(ctx context.Context, names []string) ([]*entity.Permission, error)
|
GetByNames(ctx context.Context, names []string) ([]*entity.Permission, error)
|
||||||
// GetByHTTP 根據 HTTP Path 和 Method 取得權限
|
// FindByHTTP 根據 HTTP Path 和 Method 取得權限
|
||||||
GetByHTTP(ctx context.Context, path, method string) (*entity.Permission, error)
|
FindByHTTP(ctx context.Context, path, method string) (*entity.Permission, error)
|
||||||
// List 列出所有權限
|
// List 列出所有權限
|
||||||
List(ctx context.Context, filter PermissionFilter) ([]*entity.Permission, error)
|
List(ctx context.Context, filter PermissionFilter) ([]*entity.Permission, error)
|
||||||
// ListActive 列出所有啟用的權限 (常用,可快取)
|
// ListActive 列出所有啟用的權限 (常用,可快取)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||||
"github.com/zeromicro/go-zero/core/stores/mon"
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PermissionRepositoryParam struct {
|
type PermissionRepositoryParam struct {
|
||||||
|
|
@ -61,19 +62,57 @@ func (repo *PermissionRepository) FindOne(ctx context.Context, id string) (*enti
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *PermissionRepository) GetByName(ctx context.Context, name string) (*entity.Permission, error) {
|
func (repo *PermissionRepository) FindByName(ctx context.Context, name string) (*entity.Permission, error) {
|
||||||
//TODO implement me
|
var data entity.Permission
|
||||||
panic("implement me")
|
rk := domain.GetPermissionNameRedisKey(name)
|
||||||
|
|
||||||
|
err := repo.DB.FindOne(ctx, rk, &data, bson.M{"name": name})
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
return &data, nil
|
||||||
|
case errors.Is(err, mon.ErrNotFound):
|
||||||
|
return nil, ErrNotFound
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *PermissionRepository) GetByNames(ctx context.Context, names []string) ([]*entity.Permission, error) {
|
func (repo *PermissionRepository) GetByNames(ctx context.Context, names []string) ([]*entity.Permission, error) {
|
||||||
//TODO implement me
|
var data []*entity.Permission
|
||||||
panic("implement me")
|
|
||||||
|
filter := bson.M{
|
||||||
|
"name": bson.M{"$in": names},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := repo.DB.GetClient().Find(ctx, &data, filter)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, mon.ErrNotFound) {
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *PermissionRepository) GetByHTTP(ctx context.Context, path, method string) (*entity.Permission, error) {
|
func (repo *PermissionRepository) FindByHTTP(ctx context.Context, path, method string) (*entity.Permission, error) {
|
||||||
//TODO implement me
|
var perm entity.Permission
|
||||||
panic("implement me")
|
|
||||||
|
filter := bson.M{
|
||||||
|
"path": path,
|
||||||
|
"method": strings.ToUpper(method), // 確保大小寫一致
|
||||||
|
}
|
||||||
|
|
||||||
|
err := repo.DB.GetClient().FindOne(ctx, &perm, filter)
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
return &perm, nil
|
||||||
|
case errors.Is(err, mon.ErrNotFound):
|
||||||
|
return nil, ErrNotFound
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *PermissionRepository) List(ctx context.Context, filter repository.PermissionFilter) ([]*entity.Permission, error) {
|
func (repo *PermissionRepository) List(ctx context.Context, filter repository.PermissionFilter) ([]*entity.Permission, error) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue