app/auth/service.go

78 lines
1.9 KiB
Go

package auth
import (
"github.com/spf13/viper"
"gorm.io/gorm"
"kumoly.io/kumoly/app/server"
"kumoly.io/kumoly/app/store"
"kumoly.io/kumoly/app/system"
"kumoly.io/kumoly/app/util"
)
var DB *gorm.DB
func SetDB(db *gorm.DB) {
DB = db
}
type Service struct {
system.BaseService
}
func New() *Service {
return &Service{}
}
func (srv Service) GetName() string { return "auth.Service" }
func (srv Service) GetDependencies() []string { return []string{"server.Service", "auth.Auth"} }
func (srv Service) IsService() bool { return true }
func (srv Service) Init() error {
l = util.Klog.With().Str("mod", "auth").Logger()
l.Debug().Msg("Migrating database for auth.Service ...")
if err := store.Migrate(&User{}, &Profile{}, &Group{}); err != nil {
l.Error().Err(err).Msg("Migrating database")
return err
}
l.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 {
server.API.POST("/login", ApiLogin)
server.API.POST("/logout", ApiLogout)
server.API.GET("/whoami", ApiMe)
usrAPI := 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 := 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
}