package auth import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/viper" "gorm.io/gorm" "kumoly.io/kumoly/app/server" "kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/system" ) var DB *gorm.DB func SetDB(db *gorm.DB) { DB = db } type Service struct { system.BaseService Logger zerolog.Logger server *server.Service } func New(s *server.Service) *Service { return &Service{server: s} } func (srv Service) GetName() string { return "auth.Service" } func (srv Service) GetDependencies() []string { return []string{"server.Service", "auth.Auth"} } func (srv Service) Init() error { srv.Logger = log.With().Str("mod", "auth").Str("service", "auth.Service").Logger() srv.Logger.Debug().Msg("Migrating database for auth.Service ...") if err := store.Migrate(&User{}, &Profile{}, &Group{}); err != nil { srv.Logger.Error().Err(err).Msg("Migrating database") return err } srv.Logger.Debug().Msg("Checking db state") // add default group if err := srv.SetDefaultGroups(); err != nil { return err } // add default admin account if err := srv.SetDefaultAdmin( viper.GetString("admin.user"), viper.GetString("admin.passwd"), ); err != nil { return err } return nil } func (srv Service) Load() error { srv.server.API.POST("/login", ApiLogin) srv.server.API.POST("/logout", ApiLogout) srv.server.API.GET("/whoami", ApiMe) usrAPI := srv.server.API.Group("usr") usrAPI.POST("/", ACAdmin(), ApiUserNew) usrAPI.GET("/", ACAdmin(), ApiUsers) usrAPI.DELETE("/:id", ACAdmin(), ApiUserDelete) usrAPI.PUT("/passwd", ApiUserChangePasswd) usrAPI.PUT("/activate/:id", ACAdmin(), ApiUserActivate) usrAPI.PUT("/deactivate/:id", ACAdmin(), ApiUserDeactivate) grpAPI := srv.server.API.Group("grp") grpAPI.GET("/", ACAdmin(), ApiGrps) grpAPI.POST("/", ACAdmin(), ApiGrpNew) grpAPI.PUT("/", ApiGrpUpdate) grpAPI.DELETE("/:id", ACAdmin(), ApiGrpDel) grpAPI.POST("/add/:uid/:gid", ApiGrpAssign) grpAPI.POST("/remove/:uid/:gid", ACAdmin(), ApiGrpRemove) return nil }