mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-03-01 01:20:49 +03:00

* [refactor] api controller * [fix] access log path better to not hardcode the access log path, maybe some ppl dont want to use the default ./access.log * [fix] set select options from logs paths in xray settings * [update] .gitignore * [lint] all .go files * [update] use status code for jsonMsg and 401 to unauthorize * [update] handle response status code via axios * [fix] set correct value if log paths is set to 'none' we also use the default value for the paths if its set to none * [fix] iplimit - only warning access log if f2b is installed
102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"x-ui/database"
|
|
"x-ui/database/model"
|
|
"x-ui/logger"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UserService struct{}
|
|
|
|
func (s *UserService) GetFirstUser() (*model.User, error) {
|
|
db := database.GetDB()
|
|
|
|
user := &model.User{}
|
|
err := db.Model(model.User{}).
|
|
First(user).
|
|
Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func (s *UserService) CheckUser(username string, password string, secret string) *model.User {
|
|
db := database.GetDB()
|
|
|
|
user := &model.User{}
|
|
err := db.Model(model.User{}).
|
|
Where("username = ? and password = ? and login_secret = ?", username, password, secret).
|
|
First(user).
|
|
Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil
|
|
} else if err != nil {
|
|
logger.Warning("check user err:", err)
|
|
return nil
|
|
}
|
|
return user
|
|
}
|
|
|
|
func (s *UserService) UpdateUser(id int, username string, password string) error {
|
|
db := database.GetDB()
|
|
return db.Model(model.User{}).
|
|
Where("id = ?", id).
|
|
Updates(map[string]interface{}{"username": username, "password": password}).
|
|
Error
|
|
}
|
|
|
|
func (s *UserService) UpdateUserSecret(id int, secret string) error {
|
|
db := database.GetDB()
|
|
return db.Model(model.User{}).
|
|
Where("id = ?", id).
|
|
Update("login_secret", secret).
|
|
Error
|
|
}
|
|
|
|
func (s *UserService) RemoveUserSecret() error {
|
|
db := database.GetDB()
|
|
return db.Model(model.User{}).
|
|
Where("1 = 1").
|
|
Update("login_secret", "").
|
|
Error
|
|
}
|
|
|
|
func (s *UserService) GetUserSecret(id int) *model.User {
|
|
db := database.GetDB()
|
|
user := &model.User{}
|
|
err := db.Model(model.User{}).
|
|
Where("id = ?", id).
|
|
First(user).
|
|
Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil
|
|
}
|
|
return user
|
|
}
|
|
|
|
func (s *UserService) UpdateFirstUser(username string, password string) error {
|
|
if username == "" {
|
|
return errors.New("username can not be empty")
|
|
} else if password == "" {
|
|
return errors.New("password can not be empty")
|
|
}
|
|
db := database.GetDB()
|
|
user := &model.User{}
|
|
err := db.Model(model.User{}).First(user).Error
|
|
if database.IsNotFound(err) {
|
|
user.Username = username
|
|
user.Password = password
|
|
return db.Model(model.User{}).Create(user).Error
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
user.Username = username
|
|
user.Password = password
|
|
return db.Save(user).Error
|
|
}
|