You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/libs/app/user.go

68 lines
1.5 KiB
Go

package app
import (
"errors"
"github.com/veypi/OneAuth/libs/auth"
"github.com/veypi/OneAuth/libs/oerr"
"github.com/veypi/OneAuth/models"
"gorm.io/gorm"
)
func AddUser(tx *gorm.DB, uuid string, userID uint, roleID uint, status models.AUStatus) (*models.AppUser, error) {
if uuid == "" || userID == 0 {
return nil, oerr.FuncArgsError
}
au := &models.AppUser{
AppUUID: uuid,
3 years ago
}
au.UserID = userID
err := tx.Where(au).First(au).Error
if err == nil {
return nil, oerr.ResourceDuplicated
}
if errors.Is(err, gorm.ErrRecordNotFound) {
3 years ago
au.Status = status
err = tx.Create(au).Error
if err != nil {
return nil, err
}
3 years ago
if roleID > 0 {
err = auth.BindUserRole(tx, userID, roleID)
if err != nil {
return nil, err
3 years ago
}
}
err = tx.Model(&models.App{}).Where("UUID = ?", uuid).Update("UserCount", gorm.Expr("UserCount + ?", 1)).Error
return au, err
}
return nil, err
}
func EnableUser(tx *gorm.DB, uuid string, userID uint) error {
if uuid == "" || userID == 0 {
3 years ago
return oerr.FuncArgsError
}
au := &models.AppUser{
AppUUID: uuid,
}
au.UserID = userID
err := tx.Where(au).First(au).Error
if err != nil {
return err
}
3 years ago
if au.Status != models.AUOK {
return tx.Where(au).Update("Status", models.AUOK).Error
3 years ago
}
return nil
}
func DisableUser(tx *gorm.DB, uuid string, userID uint) error {
if uuid == "" || userID == 0 {
3 years ago
return oerr.FuncArgsError
}
au := &models.AppUser{
AppUUID: uuid,
}
au.UserID = userID
return tx.Where(au).Update("Status", models.AUDisable).Error
}