This commit is contained in:
veypi
2021-11-05 23:46:21 +08:00
parent d7aea82ced
commit bc3f5e0b0c
43 changed files with 1096 additions and 327 deletions
+21 -6
View File
@@ -8,7 +8,10 @@ import (
"gorm.io/gorm"
)
func AddUser(tx *gorm.DB, appID uint, userID uint, roleID uint) error {
func AddUser(tx *gorm.DB, appID uint, userID uint, roleID uint, status models.AUStatus) error {
if appID == 0 || userID == 0 {
return oerr.FuncArgsError
}
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
@@ -17,19 +20,25 @@ func AddUser(tx *gorm.DB, appID uint, userID uint, roleID uint) error {
return oerr.ResourceDuplicated
}
if errors.Is(err, gorm.ErrRecordNotFound) {
au.Status = status
err = tx.Create(au).Error
if err != nil {
return err
}
err = auth.BindUserRole(tx, userID, roleID)
if err != nil {
return err
if roleID > 0 {
err = auth.BindUserRole(tx, userID, roleID)
if err != nil {
return err
}
}
return tx.Model(&models.App{}).Where("id = ?", appID).Update("user_count", gorm.Expr("user_count + ?", 1)).Error
}
return err
}
func EnableUser(tx *gorm.DB, appID uint, userID uint) error {
if appID == 0 || userID == 0 {
return oerr.FuncArgsError
}
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
@@ -37,12 +46,18 @@ func EnableUser(tx *gorm.DB, appID uint, userID uint) error {
if err != nil {
return err
}
return tx.Where(au).Update("disabled", false).Error
if au.Status != models.AUOK {
return tx.Where(au).Update("status", models.AUOK).Error
}
return nil
}
func DisableUser(tx *gorm.DB, appID uint, userID uint) error {
if appID == 0 || userID == 0 {
return oerr.FuncArgsError
}
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
return tx.Where(au).Update("disabled", true).Error
return tx.Where(au).Update("status", models.AUDisable).Error
}