package system import ( "time" log "dev.sum7.de/sum7/warehost/lib/log" libpassword "dev.sum7.de/sum7/warehost/lib/password" "github.com/jinzhu/gorm" ) // MINPASSWORDLENTH to validate password const MINPASSWORDLENTH = 3 /* * API Model */ // RequestLogin for api request to log in type RequestLogin struct { Username string `json:"username"` Password string `json:"password"` } // ChangePasswordRequest for api request of a new password type ChangePasswordRequest struct { CurrentPassword string `json:"currentpassword"` NewPassword string `json:"newpassword"` } /* * Database Model */ // Login found type Login struct { ID uint Username string `gorm:"type:varchar(255);unique;column:mail" json:"username"` Password string `gorm:"type:varchar(255);column:password" json:"-"` Active bool `gorm:"default:'false';column:active" json:"active"` Code string `gorm:"type:varchar(255);column:code" json:"-"` Superadmin bool `gorm:"default:'false';column:superadmin" json:"superadmin"` CreateAt time.Time `sql:"default:current_timestamp" gorm:"column:createat" json:"createat"` LastLoginAt time.Time `gorm:"column:lastloginat" json:"lastloginat"` Invites []Invite `gorm:"foreignkey:Login" json:"invites"` } // Login found type Invite struct { LoginID uint `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login;primary_key"` Login Login `gorm:"column:login" json:"login"` InvitedID uint `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:invited;primary_key"` Invited Login `gorm:"column:invited" json:"invited"` Admin bool `sql:"default:'false'" json:"admin"` } func (l *Login) GetInvitedby(dbconnection *gorm.DB) (invited Invite) { invited = Invite{InvitedID: l.ID} dbconnection.Where("invited = ?", invited.InvitedID).First(&invited) return } // SyncModels to verify the database schema func SyncModels(dbconnection *gorm.DB) { dbconnection.AutoMigrate(&Login{}, &Invite{}) var result int64 dbconnection.Model(&Login{}).Count(&result) if result <= 0 { login := &Login{ Username: "root", Active: true, Superadmin: true, Password: libpassword.NewHash("root"), } dbconnection.Create(login) log.Log.Error("have to create \"login\"") } else { log.Log.Info("Connection to \"login\" works") } }