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)
|
log.Log.Fatal("database connection: ", err)
|
||||||
}
|
}
|
||||||
defer dbconnection.Close()
|
defer dbconnection.Close()
|
||||||
|
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
||||||
|
dbconnection.Callback().Update().Remove("gorm:update_time_stamp")
|
||||||
dbconnection.SingularTable(true)
|
dbconnection.SingularTable(true)
|
||||||
dbconnection.LogMode(config.DatabaseDebug)
|
dbconnection.LogMode(config.DatabaseDebug)
|
||||||
//load system Models to database
|
//load system Models to database
|
||||||
|
|
|
@ -3,6 +3,7 @@ package system
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"github.com/astaxie/session"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
@ -139,7 +140,12 @@ func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
login.Password = libpassword.NewHesh(changePasswordRequest.NewPassword)
|
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)
|
sess.Set("login", *login)
|
||||||
returndata = true
|
returndata = true
|
||||||
logger.Info("works")
|
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 := api.log.GetLog(r, "delete")
|
||||||
logger.Warn("login delete")
|
logger.Warn("login delete")
|
||||||
sess.Delete("login")
|
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
|
returndata = true
|
||||||
return
|
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) {
|
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 := api.log.GetLog(r, "invitelist")
|
||||||
logger.Warn("not implemented")
|
logger.Info("list invites")
|
||||||
api.dbconnection.Model(login).Preload("Invites.Invited").First(login)
|
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
|
returndata = login.Invites
|
||||||
return
|
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) {
|
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 := api.log.GetLog(r, "inviteadd")
|
||||||
logger.Warn("not implemented")
|
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
|
returndata = false
|
||||||
return
|
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) {
|
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")
|
logger.Warn("not implemented")
|
||||||
returndata = false
|
returndata = false
|
||||||
return
|
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) {
|
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")
|
logger.Warn("not implemented")
|
||||||
returndata = false
|
returndata = false
|
||||||
return
|
return
|
||||||
|
|
|
@ -34,25 +34,24 @@ type ChangePasswordRequest struct {
|
||||||
|
|
||||||
// Login found
|
// Login found
|
||||||
type Login struct {
|
type Login struct {
|
||||||
gorm.Model
|
ID uint
|
||||||
Username string `gorm:"type:varchar(255);unique;column:mail" json:"username"`
|
Username string `gorm:"type:varchar(255);unique;column:mail" json:"username"`
|
||||||
Password string `gorm:"type:varchar(255);column:password" json:"-"`
|
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:"-"`
|
Code string `gorm:"type:varchar(255);column:code" json:"-"`
|
||||||
Superadmin bool `gorm:"column:superadmin" json:"superadmin"`
|
Superadmin bool `gorm:"default:'false';column:superadmin" json:"superadmin"`
|
||||||
CreateAt time.Time `gorm:"column:createat" json:"createat"`
|
CreateAt time.Time `sql:"default:current_timestamp" gorm:"column:createat" json:"createat"`
|
||||||
LastLoginAt time.Time `gorm:"column:lastloginat" json:"lastloginat"`
|
LastLoginAt time.Time `gorm:"column:lastloginat" json:"lastloginat"`
|
||||||
Invites []Invite `gorm:"foreignkey:Login"`
|
Invites []Invite `gorm:"foreignkey:Login"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login found
|
// Login found
|
||||||
type Invite struct {
|
type Invite struct {
|
||||||
gorm.Model
|
LoginID uint `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login"`
|
||||||
LoginID uint `sql:"type:bigint REFERENCES login(id);column:login"`
|
|
||||||
Login Login `gorm:"column:login" json:"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"`
|
Invited Login `gorm:"column:invited" json:"invited"`
|
||||||
Admin bool `json:"admin"`
|
Admin bool `sql:"default:'false'" json:"admin"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncModels to verify the database schema
|
// SyncModels to verify the database schema
|
||||||
|
@ -67,7 +66,6 @@ func SyncModels(dbconnection *gorm.DB) {
|
||||||
Active: true,
|
Active: true,
|
||||||
Superadmin: true,
|
Superadmin: true,
|
||||||
Password: libpassword.NewHesh("root"),
|
Password: libpassword.NewHesh("root"),
|
||||||
CreateAt: time.Now(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbconnection.Create(login)
|
dbconnection.Create(login)
|
||||||
|
|
Reference in New Issue