sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/system/models.go

54 lines
1.5 KiB
Go

package system
import (
"log"
"time"
libpassword "dev.sum7.de/sum7/warehost/lib/password"
"github.com/go-xorm/xorm"
)
// Login object for request
type RequestLogin struct {
Username string `json:"username"`
Password string `json:"password"`
}
// Login found
type Login struct {
Id int64 `json:"id"`
Username string `xorm:"varchar(255) not null unique 'mail'" json:"username"`
Password string `xorm:"varchar(255) not null 'password'" json:"-"`
Active bool `xorm:"boolean default false 'active'" json:"active"`
Code string `xorm:"varchar(255) 'code'" json:"-"`
Superadmin bool `xorm:"boolean default false 'superadmin'" json:"superadmin"`
CreateAt time.Time `xorm:"timestampz 'createat'" json:"createat"`
LastLoginAt time.Time `xorm:"timestampz 'lastloginat'" json:"lastloginat"`
}
func SyncModels(dbconnection *xorm.Engine) {
err := dbconnection.Sync(new(Login))
if err != nil {
log.Print("[system] Error create table \"login\": ", err)
}
result, err := dbconnection.Count(new(Login))
if err != nil {
log.Print("[system] Error get \"login\" count: ", err)
}
if result <= 0 {
login := new(Login)
login.Username = "root"
login.Password = libpassword.NewHesh("root")
login.CreateAt = time.Now()
login.Active = true
login.Superadmin = true
_, err := dbconnection.Insert(login)
if err == nil {
log.Print("[system] Create user \"root\"")
} else {
log.Print("[system] Error cound not first user: ", err)
}
}
}