initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"s-ui/database"
|
||||
"s-ui/database/model"
|
||||
"s-ui/logger"
|
||||
"s-ui/util/common"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
}
|
||||
|
||||
func (s *UserService) Login(username string, password string, remoteIP string) (string, error) {
|
||||
user := s.CheckUser(username, password, remoteIP)
|
||||
if user == nil {
|
||||
return "", common.NewError("wrong user or password! IP: ", remoteIP)
|
||||
}
|
||||
return user.Username, nil
|
||||
}
|
||||
|
||||
func (s *UserService) CheckUser(username string, password string, remoteIP string) *model.User {
|
||||
db := database.GetDB()
|
||||
|
||||
user := &model.User{}
|
||||
err := db.Model(model.User{}).
|
||||
Where("username = ? and password = ?", username, password).
|
||||
First(user).
|
||||
Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
logger.Warning("check user err:", err, " IP: ", remoteIP)
|
||||
return nil
|
||||
}
|
||||
|
||||
lastLoginTxt := time.Now().Format("2006-01-02 15:04:05") + "-" + remoteIP
|
||||
err = db.Model(model.User{}).
|
||||
Where("username = ?", username).
|
||||
Update("last_logins", &lastLoginTxt).Error
|
||||
if err != nil {
|
||||
logger.Warning("unable to log login data", err)
|
||||
}
|
||||
return user
|
||||
}
|
||||
Reference in New Issue
Block a user