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

83 lines
2.3 KiB
Go
Raw Normal View History

2016-08-13 11:03:03 +02:00
package system
import (
"time"
2016-08-14 18:29:25 +02:00
log "dev.sum7.de/sum7/warehost/lib/log"
2016-08-13 11:03:03 +02:00
libpassword "dev.sum7.de/sum7/warehost/lib/password"
2016-08-20 01:17:08 +02:00
"github.com/jinzhu/gorm"
2016-08-13 11:03:03 +02:00
)
// MINPASSWORDLENTH to validate password
const MINPASSWORDLENTH = 3
/*
* API Model
*/
// RequestLogin for api request to log in
2016-08-13 11:03:03 +02:00
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
*/
2016-08-13 11:03:03 +02:00
// Login found
type Login struct {
2016-08-23 22:56:12 +02:00
ID uint
2016-08-20 01:17:08 +02:00
Username string `gorm:"type:varchar(255);unique;column:mail" json:"username"`
Password string `gorm:"type:varchar(255);column:password" json:"-"`
2016-08-23 22:56:12 +02:00
Active bool `gorm:"default:'false';column:active" json:"active"`
2016-08-20 01:17:08 +02:00
Code string `gorm:"type:varchar(255);column:code" json:"-"`
2016-08-23 22:56:12 +02:00
Superadmin bool `gorm:"default:'false';column:superadmin" json:"superadmin"`
CreateAt time.Time `sql:"default:current_timestamp" gorm:"column:createat" json:"createat"`
2016-08-20 01:17:08 +02:00
LastLoginAt time.Time `gorm:"column:lastloginat" json:"lastloginat"`
2016-08-24 23:02:25 +02:00
Invites []Invite `gorm:"foreignkey:Login" json:"invites"`
2016-08-20 01:17:08 +02:00
}
// Login found
type Invite struct {
2016-08-24 23:02:25 +02:00
LoginID uint `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login;primary_key"`
2016-08-20 01:17:08 +02:00
Login Login `gorm:"column:login" json:"login"`
2016-08-24 23:02:25 +02:00
InvitedID uint `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:invited;primary_key"`
2016-08-20 01:17:08 +02:00
Invited Login `gorm:"column:invited" json:"invited"`
2016-08-23 22:56:12 +02:00
Admin bool `sql:"default:'false'" json:"admin"`
2016-08-13 11:03:03 +02:00
}
2016-08-24 23:02:25 +02:00
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
2016-08-20 01:17:08 +02:00
func SyncModels(dbconnection *gorm.DB) {
dbconnection.AutoMigrate(&Login{}, &Invite{})
var result int64
dbconnection.Model(&Login{}).Count(&result)
2016-08-13 11:03:03 +02:00
if result <= 0 {
2016-08-20 01:17:08 +02:00
login := &Login{
Username: "root",
Active: true,
Superadmin: true,
2016-08-24 23:02:25 +02:00
Password: libpassword.NewHash("root"),
2016-08-13 11:03:03 +02:00
}
2016-08-20 01:17:08 +02:00
dbconnection.Create(login)
log.Log.Error("have to create \"login\"")
} else {
2016-08-24 23:02:25 +02:00
log.Log.Info("Connection to \"login\" works")
2016-08-13 11:03:03 +02:00
}
}