42 lines
614 B
Go
42 lines
614 B
Go
package acl
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Service struct {
|
|
initialized bool
|
|
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewService(db *gorm.DB) (*Service, error) {
|
|
if db == nil {
|
|
return nil, fmt.Errorf("db is required")
|
|
}
|
|
return &Service{
|
|
db: db,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) isInitialized() bool {
|
|
return s.initialized
|
|
}
|
|
|
|
func (s *Service) Init() error {
|
|
if s.isInitialized() {
|
|
return nil
|
|
}
|
|
|
|
// AutoMigrate models
|
|
err := s.db.AutoMigrate(&UserRole{}, &Resource{}, &Role{}, &RoleResource{})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to migrate ACL models: %w", err)
|
|
}
|
|
|
|
s.initialized = true
|
|
return nil
|
|
}
|