2022-06-25 13:36:35 +00:00
|
|
|
package db
|
2022-06-16 08:06:10 +00:00
|
|
|
|
|
|
|
import (
|
2023-08-14 14:54:38 +00:00
|
|
|
"encoding/base64"
|
|
|
|
|
2022-06-16 08:06:10 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2023-08-14 14:54:38 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
|
|
|
"github.com/go-webauthn/webauthn/webauthn"
|
2022-06-16 08:06:10 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-12-18 11:51:20 +00:00
|
|
|
func GetUserByRole(role int) (*model.User, error) {
|
|
|
|
user := model.User{Role: role}
|
2022-06-25 14:05:02 +00:00
|
|
|
if err := db.Where(user).Take(&user).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user, nil
|
2022-06-16 08:06:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 13:34:44 +00:00
|
|
|
func GetUserByName(username string) (*model.User, error) {
|
2022-12-18 11:51:20 +00:00
|
|
|
user := model.User{Username: username}
|
|
|
|
if err := db.Where(user).First(&user).Error; err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed find user")
|
2022-06-16 08:06:10 +00:00
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
return &user, nil
|
2022-06-16 08:06:10 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 09:55:33 +00:00
|
|
|
func GetUserBySSOID(ssoID string) (*model.User, error) {
|
|
|
|
user := model.User{SsoID: ssoID}
|
2022-12-27 14:11:22 +00:00
|
|
|
if err := db.Where(user).First(&user).Error; err != nil {
|
2023-03-02 09:55:33 +00:00
|
|
|
return nil, errors.Wrapf(err, "The single sign on platform is not bound to any users")
|
2022-12-27 14:11:22 +00:00
|
|
|
}
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
2022-06-16 08:06:10 +00:00
|
|
|
func GetUserById(id uint) (*model.User, error) {
|
|
|
|
var u model.User
|
|
|
|
if err := db.First(&u, id).Error; err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed get old user")
|
|
|
|
}
|
|
|
|
return &u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateUser(u *model.User) error {
|
|
|
|
return errors.WithStack(db.Create(u).Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateUser(u *model.User) error {
|
|
|
|
return errors.WithStack(db.Save(u).Error)
|
|
|
|
}
|
|
|
|
|
2022-12-18 11:51:20 +00:00
|
|
|
func GetUsers(pageIndex, pageSize int) (users []model.User, count int64, err error) {
|
2022-06-16 13:59:17 +00:00
|
|
|
userDB := db.Model(&model.User{})
|
|
|
|
if err := userDB.Count(&count).Error; err != nil {
|
2022-06-16 08:06:10 +00:00
|
|
|
return nil, 0, errors.Wrapf(err, "failed get users count")
|
|
|
|
}
|
2024-08-03 05:11:09 +00:00
|
|
|
if err := userDB.Order(columnName("id")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
|
2022-06-16 08:06:10 +00:00
|
|
|
return nil, 0, errors.Wrapf(err, "failed get find users")
|
|
|
|
}
|
|
|
|
return users, count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUserById(id uint) error {
|
|
|
|
return errors.WithStack(db.Delete(&model.User{}, id).Error)
|
|
|
|
}
|
2023-08-14 14:54:38 +00:00
|
|
|
|
|
|
|
func UpdateAuthn(userID uint, authn string) error {
|
|
|
|
return db.Model(&model.User{ID: userID}).Update("authn", authn).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterAuthn(u *model.User, credential *webauthn.Credential) error {
|
|
|
|
if u == nil {
|
|
|
|
return errors.New("user is nil")
|
|
|
|
}
|
|
|
|
exists := u.WebAuthnCredentials()
|
|
|
|
if credential != nil {
|
|
|
|
exists = append(exists, *credential)
|
|
|
|
}
|
|
|
|
res, err := utils.Json.Marshal(exists)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return UpdateAuthn(u.ID, string(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveAuthn(u *model.User, id string) error {
|
|
|
|
exists := u.WebAuthnCredentials()
|
|
|
|
for i := 0; i < len(exists); i++ {
|
|
|
|
idEncoded := base64.StdEncoding.EncodeToString(exists[i].ID)
|
|
|
|
if idEncoded == id {
|
|
|
|
exists[len(exists)-1], exists[i] = exists[i], exists[len(exists)-1]
|
|
|
|
exists = exists[:len(exists)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := utils.Json.Marshal(exists)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return UpdateAuthn(u.ID, string(res))
|
|
|
|
}
|