system invite + improve schema
This commit is contained in:
parent
8c3b114428
commit
cd8bff38ab
2
main.go
2
main.go
|
@ -42,6 +42,8 @@ func main() {
|
|||
log.Log.Fatal("database connection: ", err)
|
||||
}
|
||||
defer dbconnection.Close()
|
||||
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
||||
dbconnection.Callback().Update().Remove("gorm:update_time_stamp")
|
||||
dbconnection.SingularTable(true)
|
||||
dbconnection.LogMode(config.DatabaseDebug)
|
||||
//load system Models to database
|
||||
|
|
|
@ -3,6 +3,7 @@ package system
|
|||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/session"
|
||||
"github.com/jinzhu/gorm"
|
||||
|
@ -139,7 +140,12 @@ func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
|
|||
return
|
||||
}
|
||||
login.Password = libpassword.NewHesh(changePasswordRequest.NewPassword)
|
||||
api.dbconnection.Save(login)
|
||||
if err := api.dbconnection.Save(login).Error; err != nil {
|
||||
logger.Warn("error save new password to database")
|
||||
returnerr = &libapi.ErrorResult{Message: "Error save new password"}
|
||||
returndata = false
|
||||
return
|
||||
}
|
||||
sess.Set("login", *login)
|
||||
returndata = true
|
||||
logger.Info("works")
|
||||
|
@ -151,35 +157,72 @@ func (api *API) Delete(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
|||
logger := api.log.GetLog(r, "delete")
|
||||
logger.Warn("login delete")
|
||||
sess.Delete("login")
|
||||
api.dbconnection.Delete(login)
|
||||
if err := api.dbconnection.Unscoped().Delete(login).Error; err != nil {
|
||||
logger.Warn("error detete login")
|
||||
returnerr = &libapi.ErrorResult{Message: "Error delete login"}
|
||||
returndata = false
|
||||
return
|
||||
}
|
||||
returndata = true
|
||||
return
|
||||
}
|
||||
|
||||
// InviteList list all of your invites
|
||||
func (api *API) InviteList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||
logger := api.log.GetLog(r, "invitelist")
|
||||
logger.Warn("not implemented")
|
||||
api.dbconnection.Model(login).Preload("Invites.Invited").First(login)
|
||||
logger.Info("list invites")
|
||||
if err := api.dbconnection.Model(login).Preload("Invites.Invited").First(login).Error; err != nil {
|
||||
logger.Warn("error load own invites")
|
||||
returnerr = &libapi.ErrorResult{Message: "Could not load invites!"}
|
||||
returndata = false
|
||||
return
|
||||
}
|
||||
returndata = login.Invites
|
||||
return
|
||||
}
|
||||
|
||||
// InviteAdd invite a new user to warehost
|
||||
func (api *API) InviteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||
logger := api.log.GetLog(r, "invitelist")
|
||||
logger.Warn("not implemented")
|
||||
logger := api.log.GetLog(r, "inviteadd")
|
||||
returndata = false
|
||||
var newLogin RequestLogin
|
||||
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
||||
if err != nil {
|
||||
logger.Error("fetch request")
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
||||
return
|
||||
}
|
||||
invite := &Invite{
|
||||
Login: *login,
|
||||
Invited: Login{
|
||||
Username: strings.ToLower(newLogin.Username),
|
||||
Password: libpassword.NewHesh(newLogin.Password),
|
||||
Active: true,
|
||||
},
|
||||
}
|
||||
if err := api.dbconnection.Create(invite).Error; err != nil {
|
||||
logger.Warn("error create invite")
|
||||
returnerr = &libapi.ErrorResult{Message: "Username exists already"}
|
||||
returndata = false
|
||||
return
|
||||
}
|
||||
logger.Info("invite")
|
||||
returndata = true
|
||||
return
|
||||
}
|
||||
|
||||
// InviteEdit edit a invite
|
||||
func (api *API) InviteEdit(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||
logger := api.log.GetLog(r, "invitelist")
|
||||
logger := api.log.GetLog(r, "inviteedit")
|
||||
logger.Warn("not implemented")
|
||||
returndata = false
|
||||
return
|
||||
}
|
||||
|
||||
// InviteDelete delete a invite
|
||||
func (api *API) InviteDelete(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||
logger := api.log.GetLog(r, "invitelist")
|
||||
logger := api.log.GetLog(r, "invitedelete")
|
||||
logger.Warn("not implemented")
|
||||
returndata = false
|
||||
return
|
||||
|
|
|
@ -34,25 +34,24 @@ type ChangePasswordRequest struct {
|
|||
|
||||
// Login found
|
||||
type Login struct {
|
||||
gorm.Model
|
||||
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:"column:active" json:"active"`
|
||||
Active bool `gorm:"default:'false';column:active" json:"active"`
|
||||
Code string `gorm:"type:varchar(255);column:code" json:"-"`
|
||||
Superadmin bool `gorm:"column:superadmin" json:"superadmin"`
|
||||
CreateAt time.Time `gorm:"column:createat" json:"createat"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// Login found
|
||||
type Invite struct {
|
||||
gorm.Model
|
||||
LoginID uint `sql:"type:bigint REFERENCES login(id);column:login"`
|
||||
LoginID uint `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login"`
|
||||
Login Login `gorm:"column:login" json:"login"`
|
||||
InvitedID uint `sql:"type:bigint REFERENCES login(id);column:invited"`
|
||||
InvitedID uint `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:invited"`
|
||||
Invited Login `gorm:"column:invited" json:"invited"`
|
||||
Admin bool `json:"admin"`
|
||||
Admin bool `sql:"default:'false'" json:"admin"`
|
||||
}
|
||||
|
||||
// SyncModels to verify the database schema
|
||||
|
@ -67,7 +66,6 @@ func SyncModels(dbconnection *gorm.DB) {
|
|||
Active: true,
|
||||
Superadmin: true,
|
||||
Password: libpassword.NewHesh("root"),
|
||||
CreateAt: time.Now(),
|
||||
}
|
||||
|
||||
dbconnection.Create(login)
|
||||
|
|
Reference in New Issue