用户加密机制设计初步完成

This commit is contained in:
veypi
2021-10-21 18:09:40 +08:00
parent cd7029c298
commit 82b64a4bb2
40 changed files with 1027 additions and 322 deletions
+48
View File
@@ -0,0 +1,48 @@
package app
import (
"OneAuth/libs/auth"
"OneAuth/libs/oerr"
"OneAuth/models"
"errors"
"gorm.io/gorm"
)
func AddUser(tx *gorm.DB, appID uint, userID uint, roleID uint) error {
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
err := tx.Where(au).First(au).Error
if err == nil {
return oerr.ResourceDuplicated
}
if errors.Is(err, gorm.ErrRecordNotFound) {
err = tx.Create(au).Error
if err != nil {
return err
}
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 {
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
err := tx.Where(au).First(au).Error
if err != nil {
return err
}
return tx.Where(au).Update("disabled", false).Error
}
func DisableUser(tx *gorm.DB, appID uint, userID uint) error {
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
return tx.Where(au).Update("disabled", true).Error
}