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/modul/web/lib.go

51 lines
1.7 KiB
Go
Raw Normal View History

2016-09-01 22:19:39 +02:00
package web
2016-09-03 16:30:48 +02:00
import (
"net/http"
"strconv"
"github.com/astaxie/session"
"github.com/jinzhu/gorm"
"github.com/julienschmidt/httprouter"
libapi "dev.sum7.de/sum7/warehost/lib/api"
log "dev.sum7.de/sum7/warehost/lib/log"
libsystem "dev.sum7.de/sum7/warehost/system"
)
// Handle to handle request with session and current logged in user
type Handle func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, id int64) (interface{}, *libapi.ErrorResult)
//InvolveWebsiteHandler for api function to Verifie User ist loggedin
func InvolveWebsiteHandler(h Handle, sessions *session.Manager, dbconnection *gorm.DB) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
sess := sessions.SessionStart(w, r)
err := &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not logged in"}
var data interface{}
data = false
if login := sess.Get("login"); login != nil {
if loginObj := login.(libsystem.Login); loginObj.Active {
id, errI := strconv.ParseInt(ps.ByName("websiteid"), 10, 64)
if errI != nil {
err = &libapi.ErrorResult{Fields: []string{"websiteid"}, Message: "Not a valid websiteid"}
log.Log.Warn("invalid websiteid, no integer")
} else {
res := dbconnection.Where(map[string]int64{"website": id, "login": loginObj.ID}).Find(&Manager{})
if !res.RecordNotFound() {
data, err = h(w, r, ps, sess, &loginObj, id)
} else {
err = &libapi.ErrorResult{Fields: []string{"permission"}, Message: "No permission"}
log.Log.Info("no Permissions")
}
}
} else {
log.Log.Warn("user not active")
}
} else {
log.Log.Warn("not loggedin")
}
libapi.JSONOutput(w, r, sess, data, err)
}
}