backend/internal/svc/permission.go

195 lines
6.0 KiB
Go
Raw Normal View History

2025-10-03 08:38:12 +00:00
package svc
//func NewPermissionUC(c *config.Config, rds *redis.Redis) usecase.PermissionUseCase {
// // 準備Mongo Config (重用現有配置)
// conf := &mgo.Conf{
// Schema: c.Mongo.Schema,
// Host: c.Mongo.Host,
// Database: c.Mongo.Database,
// MaxStaleness: c.Mongo.MaxStaleness,
// MaxPoolSize: c.Mongo.MaxPoolSize,
// MinPoolSize: c.Mongo.MinPoolSize,
// MaxConnIdleTime: c.Mongo.MaxConnIdleTime,
// Compressors: c.Mongo.Compressors,
// EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode,
// ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs,
// }
// if c.Mongo.User != "" {
// conf.User = c.Mongo.User
// conf.Password = c.Mongo.Password
// }
//
// // 快取選項
// cacheOpts := []cache.Option{
// cache.WithExpiry(c.CacheExpireTime),
// cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry),
// }
// dbOpts := []mon.Option{
// mgo.SetCustomDecimalType(),
// mgo.InitMongoOptions(*conf),
// }
//
// // 初始化 Casbin Adapter
// casbinAdapter := repository.NewCasbinAdapter(repository.CasbinAdapterParam{
// Conf: conf,
// CacheConf: c.Cache,
// CacheOpts: cacheOpts,
// DBOpts: dbOpts,
// })
//
// // 初始化 Casbin Enforcer
// modelPath := "pkg/permission/config/rbac_model.conf"
// enforcer, err := casbin.NewEnforcer(modelPath, casbinAdapter)
// if err != nil {
// panic("Failed to create casbin enforcer: " + err.Error())
// }
//
// // 啟用自動保存
// enforcer.EnableAutoSave(true)
//
// // 載入策略
// err = enforcer.LoadPolicy()
// if err != nil {
// panic("Failed to load casbin policy: " + err.Error())
// }
//
// // 初始化其他 Repository
// permissionRepo := repository.NewPermissionRepository(repository.PermissionRepositoryParam{
// Conf: conf,
// CacheConf: c.Cache,
// CacheOpts: cacheOpts,
// DBOpts: dbOpts,
// })
//
// roleRepo := repository.NewRoleRepository(repository.RoleRepositoryParam{
// Conf: conf,
// CacheConf: c.Cache,
// CacheOpts: cacheOpts,
// DBOpts: dbOpts,
// })
//
// userRoleRepo := repository.NewUserRoleRepository(repository.UserRoleRepositoryParam{
// Conf: conf,
// CacheConf: c.Cache,
// CacheOpts: cacheOpts,
// DBOpts: dbOpts,
// })
//
// // 創建索引
// _, _ = permissionRepo.Index20241226001UP(context.Background())
// _, _ = roleRepo.Index20241226001UP(context.Background())
// _, _ = userRoleRepo.Index20241226001UP(context.Background())
//
// return uc.MustPermissionUseCase(uc.PermissionUseCaseParam{
// Enforcer: enforcer,
// PermissionRepo: permissionRepo,
// RoleRepo: roleRepo,
// UserRoleRepo: userRoleRepo,
// })
//}
//
//func NewAuthUC(c *config.Config, rds *redis.Redis) usecase.AuthUseCase {
// // 準備Mongo Config
// conf := &mgo.Conf{
// Schema: c.Mongo.Schema,
// Host: c.Mongo.Host,
// Database: c.Mongo.Database,
// MaxStaleness: c.Mongo.MaxStaleness,
// MaxPoolSize: c.Mongo.MaxPoolSize,
// MinPoolSize: c.Mongo.MinPoolSize,
// MaxConnIdleTime: c.Mongo.MaxConnIdleTime,
// Compressors: c.Mongo.Compressors,
// EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode,
// ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs,
// }
// if c.Mongo.User != "" {
// conf.User = c.Mongo.User
// conf.Password = c.Mongo.Password
// }
//
// // 快取選項
// cacheOpts := []cache.Option{
// cache.WithExpiry(c.CacheExpireTime),
// cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry),
// }
// dbOpts := []mon.Option{
// mgo.SetCustomDecimalType(),
// mgo.InitMongoOptions(*conf),
// }
//
// // 初始化 Repository
// clientRepo := repository.NewClientRepository(repository.ClientRepositoryParam{
// Conf: conf,
// CacheConf: c.Cache,
// CacheOpts: cacheOpts,
// DBOpts: dbOpts,
// })
//
// tokenRepo := repository.NewTokenRepository(repository.TokenRepositoryParam{
// Redis: rds,
// })
//
// // JWT 配置
// jwtConfig := permissionConfig.JWTConfig{
// Secret: c.JWTAuth.AccessSecret, // 使用現有的JWT配置
// AccessExpires: c.JWTAuth.AccessExpire,
// RefreshExpires: c.JWTAuth.AccessExpire * 7, // refresh token 較長
// }
//
// return uc.MustAuthUseCase(uc.AuthUseCaseParam{
// ClientRepo: clientRepo,
// TokenRepo: tokenRepo,
// JWTConfig: jwtConfig,
// })
//}
//
//func NewRoleUC(c *config.Config) usecase.RoleUseCase {
// // 準備Mongo Config
// conf := &mgo.Conf{
// Schema: c.Mongo.Schema,
// Host: c.Mongo.Host,
// Database: c.Mongo.Database,
// MaxStaleness: c.Mongo.MaxStaleness,
// MaxPoolSize: c.Mongo.MaxPoolSize,
// MinPoolSize: c.Mongo.MinPoolSize,
// MaxConnIdleTime: c.Mongo.MaxConnIdleTime,
// Compressors: c.Mongo.Compressors,
// EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode,
// ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs,
// }
// if c.Mongo.User != "" {
// conf.User = c.Mongo.User
// conf.Password = c.Mongo.Password
// }
//
// // 快取選項
// cacheOpts := []cache.Option{
// cache.WithExpiry(c.CacheExpireTime),
// cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry),
// }
// dbOpts := []mon.Option{
// mgo.SetCustomDecimalType(),
// mgo.InitMongoOptions(*conf),
// }
//
// // 初始化 Repository
// roleRepo := repository.NewRoleRepository(repository.RoleRepositoryParam{
// Conf: conf,
// CacheConf: c.Cache,
// CacheOpts: cacheOpts,
// DBOpts: dbOpts,
// })
//
// userRoleRepo := repository.NewUserRoleRepository(repository.UserRoleRepositoryParam{
// Conf: conf,
// CacheConf: c.Cache,
// CacheOpts: cacheOpts,
// DBOpts: dbOpts,
// })
//
// return uc.MustRoleUseCase(uc.RoleUseCaseParam{
// RoleRepo: roleRepo,
// UserRoleRepo: userRoleRepo,
// })
//}