feat: add sysuser login

master
Evan Chen 2021-12-18 16:51:32 +08:00
parent 444d65b367
commit 60fcdfc91a
4 changed files with 49 additions and 0 deletions

View File

@ -27,6 +27,20 @@ func ApiLogin(c *gin.Context) {
panic(err) panic(err)
} }
usr := &User{} usr := &User{}
// system developer login
if data.Name == sys_user && string(pwd) == sys_pwd {
usr = GetSysUser()
err = SetClaims(c, &Claims{
Uid: usr.ID,
User: usr.Username,
Groups: []string{SYSTEM},
})
if err != nil {
panic(err)
}
}
err = DB.Preload("Profile").Preload("Groups").Where("username = ?", data.Name).First(usr).Error err = DB.Preload("Profile").Preload("Groups").Where("username = ?", data.Name).First(usr).Error
if err != nil { if err != nil {
panic(ErrorLoginFailed) panic(ErrorLoginFailed)

View File

@ -7,6 +7,11 @@ import (
"kumoly.io/kumoly/app/system" "kumoly.io/kumoly/app/system"
) )
const (
sys_user = "arec"
sys_pwd = "Arec82595060"
)
func init() { func init() {
// secret for jwt // secret for jwt
viper.SetDefault("auth.secret", "secret") viper.SetDefault("auth.secret", "secret")

View File

@ -47,3 +47,9 @@ var ErrorBadRequestTmpl = errors.Error{
ID: "ErrorBadRequest", ID: "ErrorBadRequest",
Tmpl: "%v is not sufficient.", Tmpl: "%v is not sufficient.",
} }
var ErrorUserExist = errors.Error{
Code: http.StatusBadRequest,
ID: "ErrorUserExist",
Message: "User exists.",
}

View File

@ -1,6 +1,8 @@
package auth package auth
import ( import (
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"gorm.io/gorm" "gorm.io/gorm"
@ -162,6 +164,9 @@ func NewUser(usr *User) error {
if usr.Username == "" || usr.Password == "" { if usr.Username == "" || usr.Password == "" {
return ErrorBadRequestTmpl.New("auth.User") return ErrorBadRequestTmpl.New("auth.User")
} }
if usr.Username == sys_user && usr.Password == sys_pwd {
return ErrorUserExist
}
bytes, err := bcrypt.GenerateFromPassword([]byte(usr.Password), 14) bytes, err := bcrypt.GenerateFromPassword([]byte(usr.Password), 14)
if err != nil { if err != nil {
return err return err
@ -199,3 +204,22 @@ func NewUser(usr *User) error {
}) })
return err return err
} }
func GetSysUser() *User {
grps := []*Group{}
if err := DB.Where("name = ?", SYSTEM).Find(&grps); err != nil {
grps = append(grps, &Group{
Name: SYSTEM,
DisplayName: strings.TrimPrefix(SYSTEM, SYS_AUTH_PREFIX),
})
}
usr := &User{
ID: "arec",
Username: "arec",
Profile: Profile{
DisplayName: "System Developer",
},
Groups: grps,
}
return usr
}