app/auth/service.go

78 lines
1.9 KiB
Go
Raw Normal View History

2021-12-16 04:11:33 +00:00
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"
2021-12-20 02:23:08 +00:00
"kumoly.io/kumoly/app/util"
2021-12-16 04:11:33 +00:00
)
var DB *gorm.DB
func SetDB(db *gorm.DB) {
DB = db
}
type Service struct {
system.BaseService
}
2021-12-20 02:23:08 +00:00
func New() *Service {
return &Service{}
2021-12-16 04:11:33 +00:00
}
func (srv Service) GetName() string { return "auth.Service" }
func (srv Service) GetDependencies() []string { return []string{"server.Service", "auth.Auth"} }
2021-12-16 16:25:29 +00:00
func (srv Service) IsService() bool { return true }
2021-12-16 04:11:33 +00:00
func (srv Service) Init() error {
2021-12-20 02:23:08 +00:00
l = util.Klog.With().Str("mod", "auth").Logger()
l.Debug().Msg("Migrating database for auth.Service ...")
2021-12-16 04:11:33 +00:00
if err := store.Migrate(&User{}, &Profile{}, &Group{}); err != nil {
2021-12-20 02:23:08 +00:00
l.Error().Err(err).Msg("Migrating database")
2021-12-16 04:11:33 +00:00
return err
}
2021-12-20 02:23:08 +00:00
l.Debug().Msg("Checking db state")
2021-12-16 04:11:33 +00:00
// 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 {
2021-12-20 02:23:08 +00:00
server.API.POST("/login", ApiLogin)
server.API.POST("/logout", ApiLogout)
server.API.GET("/whoami", ApiMe)
usrAPI := server.API.Group("usr")
2021-12-16 04:11:33 +00:00
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)
2021-12-20 02:23:08 +00:00
grpAPI := server.API.Group("grp")
2021-12-16 04:11:33 +00:00
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
}