refactorying to easyier binding
This commit is contained in:
parent
8eb3aedc62
commit
82cc1f67f9
|
@ -8,9 +8,9 @@ import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
|
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
libpassword "dev.sum7.eu/sum7/warehost/lib/password"
|
||||||
system "dev.sum7.de/sum7/warehost/system"
|
system "dev.sum7.eu/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -27,12 +27,12 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
config = ReadConfigFile(configFile)
|
config = ReadConfigFile(configFile)
|
||||||
log.NewSilenceLogger(config.Log.Path)
|
liblog.NewSilenceLogger(config.Log.Path)
|
||||||
|
|
||||||
// Main Databaseconnection
|
// Main Databaseconnection
|
||||||
dbconnection, err = gorm.Open("postgres", config.Database)
|
dbconnection, err = gorm.Open("postgres", config.Database)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log.Fatal("database connection: ", err)
|
liblog.Log.Fatal("database connection: ", err)
|
||||||
}
|
}
|
||||||
defer dbconnection.Close()
|
defer dbconnection.Close()
|
||||||
dbconnection.SingularTable(true)
|
dbconnection.SingularTable(true)
|
||||||
|
@ -40,7 +40,7 @@ func main() {
|
||||||
|
|
||||||
username := os.Getenv("AUTHD_ACCOUNT")
|
username := os.Getenv("AUTHD_ACCOUNT")
|
||||||
password := os.Getenv("AUTHD_PASSWORD")
|
password := os.Getenv("AUTHD_PASSWORD")
|
||||||
logger := log.Log.WithField("user", username)
|
logger := liblog.Log.WithField("user", username)
|
||||||
login := system.Login{Username: username}
|
login := system.Login{Username: username}
|
||||||
dbconnection.Where("mail = ?", username).First(&login)
|
dbconnection.Where("mail = ?", username).First(&login)
|
||||||
if login.ID <= 0 {
|
if login.ID <= 0 {
|
||||||
|
|
|
@ -9,15 +9,15 @@ import (
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
"github.com/russross/blackfriday"
|
"github.com/russross/blackfriday"
|
||||||
|
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
modul "dev.sum7.de/sum7/warehost/modul/web"
|
web "dev.sum7.eu/sum7/warehost/modul/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
//ProxyHost if this server is behind a proxy
|
//ProxyHost if this server is behind a proxy
|
||||||
const ProxyHost = "X-Real-Host"
|
const ProxyHost = "X-Real-Host"
|
||||||
|
|
||||||
func getWebsite(host string) *modul.Website {
|
func getWebsite(host string) *web.Website {
|
||||||
website := &modul.Website{}
|
website := &web.Website{}
|
||||||
dbconnection.Model(website).Joins("JOIN web_domain ON web_domain.website = web_website.id").Where("web_domain.name = ?", host).First(website)
|
dbconnection.Model(website).Joins("JOIN web_domain ON web_domain.website = web_website.id").Where("web_domain.name = ?", host).First(website)
|
||||||
return website
|
return website
|
||||||
}
|
}
|
||||||
|
@ -59,13 +59,13 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
||||||
host = r.Host
|
host = r.Host
|
||||||
}
|
}
|
||||||
url := r.URL.Path[1:]
|
url := r.URL.Path[1:]
|
||||||
logger := log.NewModulLog(host).GetLog(r, url)
|
logger := liblog.NewModulLog(host).GetLog(r, url)
|
||||||
website := getWebsite(host)
|
website := getWebsite(host)
|
||||||
logger = logger.WithField("hID", website.ID)
|
logger = logger.WithField("hID", website.ID)
|
||||||
|
|
||||||
var menus []*modul.Menu
|
var menus []*web.Menu
|
||||||
dbconnection.Where("website = ?", website.ID).Preload("Menu").Order("position").Find(&menus)
|
dbconnection.Where("website = ?", website.ID).Preload("Menu").Order("position").Find(&menus)
|
||||||
var menu *modul.Menu
|
var menu *web.Menu
|
||||||
for _, item := range menus {
|
for _, item := range menus {
|
||||||
if item.Shorturl == "" && menu == nil {
|
if item.Shorturl == "" && menu == nil {
|
||||||
menu = item
|
menu = item
|
||||||
|
@ -74,8 +74,8 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
||||||
menu = item
|
menu = item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
menus = modul.BuildMenuTree(menus)
|
menus = web.BuildMenuTree(menus)
|
||||||
page := &modul.Page{WebsiteID: website.ID, MenuID: menu.ID}
|
page := &web.Page{WebsiteID: website.ID, MenuID: menu.ID}
|
||||||
dbconnection.Where(page).Find(page)
|
dbconnection.Where(page).Find(page)
|
||||||
|
|
||||||
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
|
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
|
||||||
|
|
|
@ -9,9 +9,9 @@ import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
|
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
modul "dev.sum7.de/sum7/warehost/modul/web"
|
web "dev.sum7.eu/sum7/warehost/modul/web"
|
||||||
system "dev.sum7.de/sum7/warehost/system"
|
system "dev.sum7.eu/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -25,12 +25,12 @@ func main() {
|
||||||
flag.StringVar(&configFile, "c", "config.yml", "path of configuration file")
|
flag.StringVar(&configFile, "c", "config.yml", "path of configuration file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
config = ReadConfigFile(configFile)
|
config = ReadConfigFile(configFile)
|
||||||
log.NewLogger(config.Log.Path)
|
liblog.NewLogger(config.Log.Path)
|
||||||
|
|
||||||
// Main Databaseconnection
|
// Main Databaseconnection
|
||||||
dbconnection, err = gorm.Open("postgres", config.Database)
|
dbconnection, err = gorm.Open("postgres", config.Database)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log.Fatal("database connection: ", err)
|
liblog.Log.Fatal("database connection: ", err)
|
||||||
}
|
}
|
||||||
defer dbconnection.Close()
|
defer dbconnection.Close()
|
||||||
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
||||||
|
@ -40,7 +40,7 @@ func main() {
|
||||||
|
|
||||||
//load system Models to database
|
//load system Models to database
|
||||||
system.SyncModels(dbconnection)
|
system.SyncModels(dbconnection)
|
||||||
modul.SyncModels(dbconnection)
|
web.SyncModels(dbconnection)
|
||||||
|
|
||||||
// http handler
|
// http handler
|
||||||
http.Handle("/static/", gziphandler.GzipHandler(http.StripPrefix("/static/", http.HandlerFunc(handlerstatic))))
|
http.Handle("/static/", gziphandler.GzipHandler(http.StripPrefix("/static/", http.HandlerFunc(handlerstatic))))
|
||||||
|
@ -49,7 +49,7 @@ func main() {
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
address := net.JoinHostPort(config.Address, config.Port)
|
address := net.JoinHostPort(config.Address, config.Port)
|
||||||
log.Log.Info("starting warehost web on ", address)
|
liblog.Log.Info("starting warehost web on ", address)
|
||||||
// TODO bad
|
// TODO bad
|
||||||
log.Log.Fatal(http.ListenAndServe(address, nil))
|
liblog.Log.Fatal(http.ListenAndServe(address, nil))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import modul "dev.sum7.de/sum7/warehost/modul/web"
|
import modul "dev.sum7.eu/sum7/warehost/modul/web"
|
||||||
|
|
||||||
//TemplateInfo Information send to Template
|
//TemplateInfo Information send to Template
|
||||||
type TemplateInfo struct {
|
type TemplateInfo struct {
|
||||||
|
|
|
@ -6,111 +6,112 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/NYTimes/gziphandler"
|
"github.com/NYTimes/gziphandler"
|
||||||
"github.com/astaxie/session"
|
|
||||||
_ "github.com/astaxie/session/providers/memory"
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
|
"goji.io"
|
||||||
|
"goji.io/pat"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libconfig "dev.sum7.de/sum7/warehost/config"
|
config "dev.sum7.eu/sum7/warehost/config"
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
modulhost "dev.sum7.de/sum7/warehost/modul/host"
|
libsession "dev.sum7.eu/sum7/warehost/lib/session"
|
||||||
modulweb "dev.sum7.de/sum7/warehost/modul/web"
|
modulhost "dev.sum7.eu/sum7/warehost/modul/host"
|
||||||
"dev.sum7.de/sum7/warehost/system"
|
modulweb "dev.sum7.eu/sum7/warehost/modul/web"
|
||||||
|
"dev.sum7.eu/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configFile string
|
configFile string
|
||||||
config *libconfig.Config
|
|
||||||
dbconnection *gorm.DB
|
dbconnection *gorm.DB
|
||||||
sessions *session.Manager
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
flag.StringVar(&configFile, "c", "config.yml", "path of configuration file")
|
flag.StringVar(&configFile, "c", "config.yml", "path of configuration file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
config = libconfig.ReadConfigFile(configFile)
|
config.ReadConfigFile(configFile)
|
||||||
log.NewLogger(config.Log.Path)
|
liblog.NewLogger(config.Data.Log.Path)
|
||||||
|
|
||||||
// Session mgmt
|
// Session mgmt
|
||||||
sessions, _ = session.NewManager("memory", "session", 3600)
|
libsession.Init()
|
||||||
go sessions.GC()
|
go libsession.Stop()
|
||||||
|
|
||||||
// Main Databaseconnection
|
// Main Databaseconnection
|
||||||
dbconnection, err = gorm.Open("postgres", config.Database)
|
dbconnection, err = gorm.Open("postgres", config.Data.Database)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log.Fatal("database connection: ", err)
|
liblog.Log.Fatal("database connection: ", err)
|
||||||
}
|
}
|
||||||
defer dbconnection.Close()
|
defer dbconnection.Close()
|
||||||
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
||||||
dbconnection.Callback().Update().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.Data.DatabaseDebug)
|
||||||
//load system Models to database
|
//load system Models to database
|
||||||
system.SyncModels(dbconnection)
|
system.SyncModels(dbconnection)
|
||||||
|
|
||||||
// API routes
|
// API routes
|
||||||
router := httprouter.New()
|
router := goji.NewMux()
|
||||||
system.NewAPI(config, sessions, dbconnection, router, "")
|
system.BindAPI(dbconnection, router, "")
|
||||||
|
|
||||||
// START Module deklations
|
// START Module deklations
|
||||||
var modules []string
|
var modules []string
|
||||||
// load modul web (first)
|
// load modul web (first)
|
||||||
modulname := "web"
|
modulname := "web"
|
||||||
if modul := config.Modules[modulname]; MODULWEB && modul != nil && modul.Enabled {
|
if modul := config.Data.Modules[modulname]; MODULWEB && modul != nil && modul.Enabled {
|
||||||
dbmodulconnection := modulconnection(dbconnection, modul, modulname)
|
dbmodulconnection := modulconnection(modul, modulname)
|
||||||
modulweb.SyncModels(dbmodulconnection)
|
modulweb.SyncModels(dbmodulconnection)
|
||||||
modulweb.NewAPI(config, sessions, dbmodulconnection, router, "/web")
|
modulweb.BindAPI(dbmodulconnection, router, "/web")
|
||||||
modules = append(modules, modulname)
|
modules = append(modules, modulname)
|
||||||
}
|
}
|
||||||
modulname = "host"
|
modulname = "host"
|
||||||
if modul := config.Modules[modulname]; MODULHOST && modul != nil && modul.Enabled {
|
if modul := config.Data.Modules[modulname]; MODULHOST && modul != nil && modul.Enabled {
|
||||||
dbmodulconnection := modulconnection(dbconnection, modul, modulname)
|
dbmodulconnection := modulconnection(modul, modulname)
|
||||||
modulhost.SyncModels(dbmodulconnection)
|
modulhost.SyncModels(dbmodulconnection)
|
||||||
modulhost.NewAPI(config, sessions, dbmodulconnection, router, "/host")
|
modulhost.BindAPI(dbmodulconnection, router, "/host")
|
||||||
modules = append(modules, modulname)
|
modules = append(modules, modulname)
|
||||||
}
|
}
|
||||||
// END Module deklations
|
// END Module deklations
|
||||||
|
|
||||||
// Make Modules debugable
|
// Make Modules debugable
|
||||||
router.GET("/modules", libapi.SessionHandler(
|
router.HandleFuncC(pat.Get("/modules"), libapi.SessionHandler(
|
||||||
func(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = modules
|
returndata = modules
|
||||||
return
|
return
|
||||||
},
|
}))
|
||||||
sessions,
|
liblog.Log.Info("load modul: ", modules)
|
||||||
))
|
|
||||||
log.Log.Info("load modul: ", modules)
|
|
||||||
|
|
||||||
// Fallback filesystem
|
// Fallback filesystem
|
||||||
if config.Webroot != "" {
|
if config.Data.Webroot != "" {
|
||||||
router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot)))
|
router.Handle(pat.Get(""), gziphandler.GzipHandler(http.FileServer(http.Dir(config.Data.Webroot))))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manage CORS (JSONOutput allow requested -> lib/api)
|
// Manage CORS (JSONOutput allow requested -> lib/api)
|
||||||
c := cors.New(cors.Options{
|
c := cors.New(cors.Options{
|
||||||
AllowedOrigins: []string{config.API.AllowedOrigins},
|
AllowedOrigins: []string{config.Data.API.AllowedOrigins},
|
||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
|
||||||
})
|
})
|
||||||
handler := c.Handler(router)
|
handler := c.Handler(router)
|
||||||
// Start server
|
// Start server
|
||||||
address := net.JoinHostPort(config.API.Address, config.API.Port)
|
address := net.JoinHostPort(config.Data.API.Address, config.Data.API.Port)
|
||||||
log.Log.Info("starting api on ", address)
|
liblog.Log.Info("starting api on ", address)
|
||||||
// TODO bad
|
// TODO bad
|
||||||
log.Log.Fatal(http.ListenAndServe(address, handler))
|
liblog.Log.Fatal(http.ListenAndServe(address, handler))
|
||||||
}
|
}
|
||||||
|
|
||||||
func modulconnection(dbconnection *gorm.DB, modul *libconfig.ModulConfig, name string) *gorm.DB {
|
func modulconnection(modul *config.ModulConfig, name string) *gorm.DB {
|
||||||
if len(modul.Database) > 0 {
|
if len(modul.Database) > 0 {
|
||||||
dbmodulconnection, err := gorm.Open("postgres", modul.Database)
|
dbmodulconnection, err := gorm.Open("postgres", modul.Database)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log.WithField("modul", name).Fatal("database modul connection: ", err)
|
liblog.Log.WithField("modul", name).Fatal("database modul connection: ", err)
|
||||||
}
|
}
|
||||||
defer dbmodulconnection.Close()
|
defer dbmodulconnection.Close()
|
||||||
|
dbmodulconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
||||||
|
dbmodulconnection.Callback().Update().Remove("gorm:update_time_stamp")
|
||||||
|
dbmodulconnection.SingularTable(true)
|
||||||
|
dbmodulconnection.LogMode(config.Data.DatabaseDebug)
|
||||||
return dbmodulconnection
|
return dbmodulconnection
|
||||||
}
|
}
|
||||||
return dbconnection
|
return dbconnection
|
||||||
|
|
|
@ -6,6 +6,9 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Data of readed configuration
|
||||||
|
var Data *Config
|
||||||
|
|
||||||
// Config is the struct of the api
|
// Config is the struct of the api
|
||||||
type Config struct {
|
type Config struct {
|
||||||
API struct {
|
API struct {
|
||||||
|
@ -29,12 +32,12 @@ type ModulConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadConfigFile reads a config models by path to a yml file
|
// ReadConfigFile reads a config models by path to a yml file
|
||||||
func ReadConfigFile(path string) *Config {
|
func ReadConfigFile(path string) {
|
||||||
config := &Config{}
|
config := &Config{}
|
||||||
file, _ := ioutil.ReadFile(path)
|
file, _ := ioutil.ReadFile(path)
|
||||||
err := yaml.Unmarshal(file, &config)
|
err := yaml.Unmarshal(file, &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
return config
|
Data = config
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,14 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"goji.io"
|
||||||
"github.com/julienschmidt/httprouter"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
libsession "dev.sum7.eu/sum7/warehost/lib/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handle for session
|
// Handle with response
|
||||||
type Handle func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session) (interface{}, *ErrorResult)
|
type Handle func(ctx context.Context, w http.ResponseWriter, r *http.Request) (interface{}, *ErrorResult)
|
||||||
|
|
||||||
// ErrorResult struct for api error answer
|
// ErrorResult struct for api error answer
|
||||||
type ErrorResult struct {
|
type ErrorResult struct {
|
||||||
|
@ -28,7 +30,8 @@ type JSONResult struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSONOutput generate default json answer
|
// JSONOutput generate default json answer
|
||||||
func JSONOutput(w http.ResponseWriter, r *http.Request, sess session.Session, data interface{}, errorresult *ErrorResult) {
|
func JSONOutput(ctx context.Context, w http.ResponseWriter, r *http.Request, data interface{}, errorresult *ErrorResult) {
|
||||||
|
sess := ctx.Value("session").(libsession.Session)
|
||||||
result := JSONResult{Data: data, Error: errorresult}
|
result := JSONResult{Data: data, Error: errorresult}
|
||||||
result.Session.Login = sess.Get("login")
|
result.Session.Login = sess.Get("login")
|
||||||
js, err := json.Marshal(result)
|
js, err := json.Marshal(result)
|
||||||
|
@ -48,10 +51,11 @@ func JSONOutput(w http.ResponseWriter, r *http.Request, sess session.Session, da
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionHandler Handler to manage session of api request
|
// SessionHandler Handler to manage session of api request
|
||||||
func SessionHandler(h Handle, sessions *session.Manager) httprouter.Handle {
|
func SessionHandler(h Handle) goji.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||||
sess := sessions.SessionStart(w, r)
|
sess := libsession.SessionStart(w, r)
|
||||||
data, err := h(w, r, ps, sess)
|
ctx = context.WithValue(ctx, "session", sess)
|
||||||
JSONOutput(w, r, sess, data, err)
|
data, err := h(ctx, w, r)
|
||||||
|
JSONOutput(ctx, w, r, data, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
assession "github.com/astaxie/session"
|
||||||
|
_ "github.com/astaxie/session/providers/memory"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Session a Session object with all data
|
||||||
|
type Session struct {
|
||||||
|
assession.Session
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data of readed configuration
|
||||||
|
var data *assession.Manager
|
||||||
|
|
||||||
|
// Init session manager
|
||||||
|
func Init() {
|
||||||
|
data, _ = assession.NewManager("memory", "session", 3600)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop cleanup session manager
|
||||||
|
func Stop() {
|
||||||
|
data.GC()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionStart init a session on a request
|
||||||
|
func SessionStart(w http.ResponseWriter, r *http.Request) assession.Session {
|
||||||
|
return data.SessionStart(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionDestroy destroy a session on a request
|
||||||
|
func SessionDestroy(w http.ResponseWriter, r *http.Request) assession.Session {
|
||||||
|
return SessionDestroy(w, r)
|
||||||
|
}
|
|
@ -3,41 +3,33 @@ package host
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/julienschmidt/httprouter"
|
"goji.io"
|
||||||
|
"goji.io/pat"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libconfig "dev.sum7.de/sum7/warehost/config"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//MODULNAME to get global name for the modul
|
//MODULNAME to get global name for the modul
|
||||||
const MODULNAME = "host"
|
const MODULNAME = "host"
|
||||||
|
|
||||||
//API keep data in module global
|
var dbconnection *gorm.DB
|
||||||
type API struct {
|
var log *liblog.ModulLog
|
||||||
config *libconfig.Config
|
|
||||||
sessions *session.Manager
|
|
||||||
dbconnection *gorm.DB
|
|
||||||
log *log.ModulLog
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAPI sets the routes to the api functions
|
// BindAPI sets the routes to the api functions
|
||||||
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router, prefix string) {
|
func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
||||||
api := &API{
|
dbconnection = db
|
||||||
config: config,
|
log = liblog.NewModulLog(MODULNAME)
|
||||||
sessions: sessions,
|
|
||||||
dbconnection: dbconnection,
|
router.HandleFuncC(pat.Get(prefix+"/status"), libapi.SessionHandler(status))
|
||||||
log: log.NewModulLog(MODULNAME),
|
|
||||||
}
|
|
||||||
router.GET(prefix+"/status", libapi.SessionHandler(api.Status, sessions))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status to get Login and Server status
|
// Status to get Login and Server status
|
||||||
func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func status(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = true
|
returndata = true
|
||||||
logger := api.log.GetLog(r, "status")
|
logger := log.GetLog(r, "status")
|
||||||
logger.Info("status")
|
logger.Info("status")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
103
modul/web/api.go
103
modul/web/api.go
|
@ -4,72 +4,67 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/julienschmidt/httprouter"
|
"goji.io"
|
||||||
|
"goji.io/pat"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libconfig "dev.sum7.de/sum7/warehost/config"
|
//libconfig "dev.sum7.eu/sum7/warehost/config"
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
libsystem "dev.sum7.de/sum7/warehost/system"
|
libsystem "dev.sum7.eu/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
//MODULNAME to get global name for the modul
|
//MODULNAME to get global name for the modul
|
||||||
const MODULNAME = "web"
|
const MODULNAME = "web"
|
||||||
|
|
||||||
//API keep data in module global
|
var dbconnection *gorm.DB
|
||||||
type API struct {
|
var log *liblog.ModulLog
|
||||||
config *libconfig.Config
|
|
||||||
sessions *session.Manager
|
|
||||||
dbconnection *gorm.DB
|
|
||||||
log *log.ModulLog
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAPI sets the routes to the api functions
|
// BindAPI sets the routes to the api functions
|
||||||
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router, prefix string) {
|
func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
||||||
api := &API{
|
dbconnection = db
|
||||||
config: config,
|
log = liblog.NewModulLog(MODULNAME)
|
||||||
sessions: sessions,
|
|
||||||
dbconnection: dbconnection,
|
router.HandleFuncC(pat.Get(prefix+"/involve"), libapi.SessionHandler(libsystem.LoginHandler(involve)))
|
||||||
log: log.NewModulLog(MODULNAME),
|
router.HandleFuncC(pat.Post(prefix+"/website"), libapi.SessionHandler(libsystem.LoginHandler(websiteAdd)))
|
||||||
}
|
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteEdit))))
|
||||||
router.GET(prefix+"/involve", libsystem.LoginHandler(api.Involve, sessions))
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteDelete))))
|
||||||
router.POST(prefix+"/website", libsystem.LoginHandler(api.WebsiteAdd, sessions))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/permission"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionList))))
|
||||||
router.PUT(prefix+"/website/:websiteid", InvolveWebsiteHandler(api.WebsiteEdit, sessions, dbconnection))
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionAdd))))
|
||||||
router.DELETE(prefix+"/website/:websiteid", InvolveWebsiteHandler(api.WebsiteDelete, sessions, dbconnection))
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionDelete))))
|
||||||
router.GET(prefix+"/website/:websiteid/permission", InvolveWebsiteHandler(api.PermissionList, sessions, dbconnection))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainList))))
|
||||||
router.POST(prefix+"/website/:websiteid/permission/:loginid", InvolveWebsiteHandler(api.PermissionAdd, sessions, dbconnection))
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/domain/:domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainAdd))))
|
||||||
router.DELETE(prefix+"/website/:websiteid/permission/:loginid", InvolveWebsiteHandler(api.PermissionDelete, sessions, dbconnection))
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/domain/:domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainDelete))))
|
||||||
router.GET(prefix+"/website/:websiteid/domain", InvolveWebsiteHandler(api.DomainList, sessions, dbconnection))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuTree))))
|
||||||
router.POST(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainAdd, sessions, dbconnection))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu/list"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuList))))
|
||||||
router.DELETE(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainDelete, sessions, dbconnection))
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuAdd))))
|
||||||
router.GET(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuTree, sessions, dbconnection))
|
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuEdit))))
|
||||||
router.GET(prefix+"/website/:websiteid/menu/list", InvolveWebsiteHandler(api.MenuList, sessions, dbconnection))
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuDelete))))
|
||||||
router.POST(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuAdd, sessions, dbconnection))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageList))))
|
||||||
router.PUT(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuEdit, sessions, dbconnection))
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageAdd))))
|
||||||
router.DELETE(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuDelete, sessions, dbconnection))
|
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageEdit))))
|
||||||
router.GET(prefix+"/website/:websiteid/page", InvolveWebsiteHandler(api.PageList, sessions, dbconnection))
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageDelete))))
|
||||||
router.POST(prefix+"/website/:websiteid/page", InvolveWebsiteHandler(api.PageAdd, sessions, dbconnection))
|
|
||||||
router.PUT(prefix+"/website/:websiteid/page/:pageid", InvolveWebsiteHandler(api.PageEdit, sessions, dbconnection))
|
|
||||||
router.DELETE(prefix+"/website/:websiteid/page/:pageid", InvolveWebsiteHandler(api.PageDelete, sessions, dbconnection))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Involve to get Website where loggend in user has privilegs
|
// Involve to get Website where loggend in user has privilegs
|
||||||
func (api *API) Involve(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func involve(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*libsystem.Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "involve")
|
logger := log.GetLog(r, "involve")
|
||||||
var involved []*Manager
|
var involved []*Manager
|
||||||
api.dbconnection.Where("login = ?", login.ID).Preload("Website").Find(&involved)
|
dbconnection.Where("login = ?", login.ID).Preload("Website").Find(&involved)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
returndata = involved
|
returndata = involved
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebsiteAdd to add a new website
|
// WebsiteAdd to add a new website
|
||||||
func (api *API) WebsiteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func websiteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*libsystem.Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "websiteadd")
|
logger := log.GetLog(r, "websiteadd")
|
||||||
tx := api.dbconnection.Begin()
|
tx := dbconnection.Begin()
|
||||||
var websiteRequest Website
|
var websiteRequest Website
|
||||||
err := json.NewDecoder(r.Body).Decode(&websiteRequest)
|
err := json.NewDecoder(r.Body).Decode(&websiteRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -101,9 +96,9 @@ func (api *API) WebsiteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebsiteEdit to edit website
|
// WebsiteEdit to edit website
|
||||||
func (api *API) WebsiteEdit(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func websiteEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "websiteedit")
|
logger := log.GetLog(r, "websiteedit")
|
||||||
var websiteRequest Website
|
var websiteRequest Website
|
||||||
err := json.NewDecoder(r.Body).Decode(&websiteRequest)
|
err := json.NewDecoder(r.Body).Decode(&websiteRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -112,8 +107,8 @@ func (api *API) WebsiteEdit(w http.ResponseWriter, r *http.Request, _ httprouter
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
websiteRequest.ID = websiteid
|
websiteRequest.ID = ctx.Value("websiteid").(int64)
|
||||||
if err := api.dbconnection.Save(websiteRequest).Error; err != nil {
|
if err := dbconnection.Save(websiteRequest).Error; err != nil {
|
||||||
logger.Error("Database: during edit Website")
|
logger.Error("Database: during edit Website")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
@ -124,13 +119,13 @@ func (api *API) WebsiteEdit(w http.ResponseWriter, r *http.Request, _ httprouter
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebsiteDelete to delete website
|
// WebsiteDelete to delete website
|
||||||
func (api *API) WebsiteDelete(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func websiteDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "websitedelete")
|
logger := log.GetLog(r, "websitedelete")
|
||||||
website := &Website{
|
website := &Website{
|
||||||
ID: websiteid,
|
ID: ctx.Value("websiteid").(int64),
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Unscoped().Delete(website).Error; err != nil {
|
if err := dbconnection.Unscoped().Delete(website).Error; err != nil {
|
||||||
logger.Error("database: during delete website")
|
logger.Error("database: during delete website")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
|
|
@ -3,33 +3,32 @@ package web
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"goji.io/pat"
|
||||||
"github.com/julienschmidt/httprouter"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
libsystem "dev.sum7.de/sum7/warehost/system"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DomainList to list domains
|
// DomainList to list domains
|
||||||
func (api *API) DomainList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "domainlist")
|
logger := log.GetLog(r, "domainlist")
|
||||||
var domain []*Domain
|
var domain []*Domain
|
||||||
api.dbconnection.Where("website = ?", websiteid).Preload("Domains").Find(&domain)
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Domains").Find(&domain)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
returndata = domain
|
returndata = domain
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// DomainAdd to add domain
|
// DomainAdd to add domain
|
||||||
func (api *API) DomainAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func domainAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "domainadd")
|
logger := log.GetLog(r, "domainadd")
|
||||||
domain := &Domain{
|
domain := &Domain{
|
||||||
WebsiteID: websiteid,
|
WebsiteID: ctx.Value("websiteid").(int64),
|
||||||
Name: ps.ByName("domain"),
|
Name: pat.Param(ctx, "domain"),
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Create(domain).Error; err != nil {
|
if err := dbconnection.Create(domain).Error; err != nil {
|
||||||
logger.Error("database: during create website domain: ", err)
|
logger.Error("database: during create website domain: ", err)
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
@ -40,14 +39,14 @@ func (api *API) DomainAdd(w http.ResponseWriter, r *http.Request, ps httprouter.
|
||||||
}
|
}
|
||||||
|
|
||||||
// DomainDelete to delete domain
|
// DomainDelete to delete domain
|
||||||
func (api *API) DomainDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func domainDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "domaindelete")
|
logger := log.GetLog(r, "domaindelete")
|
||||||
domain := &Domain{
|
domain := &Domain{
|
||||||
WebsiteID: websiteid,
|
WebsiteID: ctx.Value("websiteid").(int64),
|
||||||
Name: ps.ByName("domain"),
|
Name: pat.Param(ctx, "domain"),
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Unscoped().Delete(domain).Error; err != nil {
|
if err := dbconnection.Unscoped().Delete(domain).Error; err != nil {
|
||||||
logger.Error("database: during delete website Domain")
|
logger.Error("database: during delete website Domain")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,39 +5,38 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"goji.io/pat"
|
||||||
"github.com/julienschmidt/httprouter"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
libsystem "dev.sum7.de/sum7/warehost/system"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MenuTree to give the tree of a menu back
|
// MenuTree to give the tree of a menu back
|
||||||
func (api *API) MenuTree(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func menuTree(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menutree")
|
logger := log.GetLog(r, "menutree")
|
||||||
var menus []*Menu
|
var menus []*Menu
|
||||||
api.dbconnection.Where("website = ?", websiteid).Order("position").Find(&menus)
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Order("position").Find(&menus)
|
||||||
returndata = BuildMenuTree(menus)
|
returndata = BuildMenuTree(menus)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuList give all menu entries of a website
|
// MenuList give all menu entries of a website
|
||||||
func (api *API) MenuList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func menuList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menulist")
|
logger := log.GetLog(r, "menulist")
|
||||||
var menus []*Menu
|
var menus []*Menu
|
||||||
api.dbconnection.Where("website = ?", websiteid).Order("position").Find(&menus)
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Order("position").Find(&menus)
|
||||||
returndata = menus
|
returndata = menus
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuAdd to add a new menu entry
|
// MenuAdd to add a new menu entry
|
||||||
func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func menuAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menuadd")
|
logger := log.GetLog(r, "menuadd")
|
||||||
var menuEntry Menu
|
var menuEntry Menu
|
||||||
err := json.NewDecoder(r.Body).Decode(&menuEntry)
|
err := json.NewDecoder(r.Body).Decode(&menuEntry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -49,9 +48,9 @@ func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
menuEntry.WebsiteID = websiteid
|
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
|
||||||
|
|
||||||
if err := api.dbconnection.Create(&menuEntry).Error; err != nil {
|
if err := dbconnection.Create(&menuEntry).Error; err != nil {
|
||||||
logger.Error("database: during create menu")
|
logger.Error("database: during create menu")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
@ -62,11 +61,11 @@ func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuEdit to edit menu
|
// MenuEdit to edit menu
|
||||||
func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func menuEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menuedit")
|
logger := log.GetLog(r, "menuedit")
|
||||||
var menuEntry Menu
|
var menuEntry Menu
|
||||||
menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64)
|
menuid, err := strconv.ParseInt(pat.Param(ctx, "menuid"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
|
||||||
logger.Warn("invalid loginid, no integer")
|
logger.Warn("invalid loginid, no integer")
|
||||||
|
@ -82,10 +81,10 @@ func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
menuEntry.WebsiteID = websiteid
|
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
|
||||||
menuEntry.ID = menuid
|
menuEntry.ID = menuid
|
||||||
|
|
||||||
if err := api.dbconnection.Save(menuEntry).Error; err != nil {
|
if err := dbconnection.Save(menuEntry).Error; err != nil {
|
||||||
logger.Error("database: during delete website menu entry")
|
logger.Error("database: during delete website menu entry")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
@ -96,10 +95,10 @@ func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuDelete to delete menu entry
|
// MenuDelete to delete menu entry
|
||||||
func (api *API) MenuDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func menuDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menudelete")
|
logger := log.GetLog(r, "menudelete")
|
||||||
menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64)
|
menuid, err := strconv.ParseInt(pat.Param(ctx, "menuid"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
|
||||||
logger.Warn("invalid menuid, no integer")
|
logger.Warn("invalid menuid, no integer")
|
||||||
|
@ -107,10 +106,10 @@ func (api *API) MenuDelete(w http.ResponseWriter, r *http.Request, ps httprouter
|
||||||
}
|
}
|
||||||
logger = logger.WithField("id", menuid)
|
logger = logger.WithField("id", menuid)
|
||||||
menu := &Menu{
|
menu := &Menu{
|
||||||
WebsiteID: websiteid,
|
WebsiteID: ctx.Value("websiteid").(int64),
|
||||||
ID: menuid,
|
ID: menuid,
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Unscoped().Delete(menu).Error; err != nil {
|
if err := dbconnection.Unscoped().Delete(menu).Error; err != nil {
|
||||||
logger.Error("database: during delete website menu entry")
|
logger.Error("database: during delete website menu entry")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,28 +5,27 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"goji.io/pat"
|
||||||
"github.com/julienschmidt/httprouter"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
libsystem "dev.sum7.de/sum7/warehost/system"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PageList give all pages of a website
|
// PageList give all pages of a website
|
||||||
func (api *API) PageList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func pageList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "pagelist")
|
logger := log.GetLog(r, "pagelist")
|
||||||
var pages []*Page
|
var pages []*Page
|
||||||
api.dbconnection.Where("website = ?", websiteid).Preload("Menu").Find(&pages)
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Menu").Find(&pages)
|
||||||
returndata = pages
|
returndata = pages
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// PageAdd to add a new page
|
// PageAdd to add a new page
|
||||||
func (api *API) PageAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func pageAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "pageadd")
|
logger := log.GetLog(r, "pageadd")
|
||||||
var page Page
|
var page Page
|
||||||
err := json.NewDecoder(r.Body).Decode(&page)
|
err := json.NewDecoder(r.Body).Decode(&page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -38,13 +37,13 @@ func (api *API) PageAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
page.WebsiteID = websiteid
|
page.WebsiteID = ctx.Value("websiteid").(int64)
|
||||||
if menu := page.Menu; menu != nil {
|
if menu := page.Menu; menu != nil {
|
||||||
page.MenuID = page.Menu.ID
|
page.MenuID = page.Menu.ID
|
||||||
page.Menu = nil
|
page.Menu = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := api.dbconnection.Create(&page).Error; err != nil {
|
if err := dbconnection.Create(&page).Error; err != nil {
|
||||||
logger.Error("database: during create page")
|
logger.Error("database: during create page")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
@ -55,11 +54,11 @@ func (api *API) PageAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
|
||||||
}
|
}
|
||||||
|
|
||||||
// PageEdit to edit page
|
// PageEdit to edit page
|
||||||
func (api *API) PageEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func pageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "pageedit")
|
logger := log.GetLog(r, "pageedit")
|
||||||
var page Page
|
var page Page
|
||||||
pageid, err := strconv.ParseInt(ps.ByName("pageid"), 10, 64)
|
pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"}
|
||||||
logger.Warn("invalid pageid, no integer")
|
logger.Warn("invalid pageid, no integer")
|
||||||
|
@ -75,14 +74,14 @@ func (api *API) PageEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
page.WebsiteID = websiteid
|
page.WebsiteID = ctx.Value("websiteid").(int64)
|
||||||
page.ID = pageid
|
page.ID = pageid
|
||||||
if menu := page.Menu; menu != nil {
|
if menu := page.Menu; menu != nil {
|
||||||
page.MenuID = page.Menu.ID
|
page.MenuID = page.Menu.ID
|
||||||
page.Menu = nil
|
page.Menu = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := api.dbconnection.Save(page).Error; err != nil {
|
if err := dbconnection.Save(page).Error; err != nil {
|
||||||
logger.Error("database: during delete website page")
|
logger.Error("database: during delete website page")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
@ -93,10 +92,10 @@ func (api *API) PageEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
}
|
}
|
||||||
|
|
||||||
// PageDelete to delete page
|
// PageDelete to delete page
|
||||||
func (api *API) PageDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func pageDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "pagedelete")
|
logger := log.GetLog(r, "pagedelete")
|
||||||
pageid, err := strconv.ParseInt(ps.ByName("pageid"), 10, 64)
|
pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"}
|
||||||
logger.Warn("invalid pageid, no integer")
|
logger.Warn("invalid pageid, no integer")
|
||||||
|
@ -104,10 +103,10 @@ func (api *API) PageDelete(w http.ResponseWriter, r *http.Request, ps httprouter
|
||||||
}
|
}
|
||||||
logger = logger.WithField("id", pageid)
|
logger = logger.WithField("id", pageid)
|
||||||
page := &Page{
|
page := &Page{
|
||||||
WebsiteID: websiteid,
|
WebsiteID: ctx.Value("websiteid").(int64),
|
||||||
ID: pageid,
|
ID: pageid,
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Unscoped().Delete(page).Error; err != nil {
|
if err := dbconnection.Unscoped().Delete(page).Error; err != nil {
|
||||||
logger.Error("database: during delete website page")
|
logger.Error("database: during delete website page")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
|
|
@ -4,39 +4,38 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"goji.io/pat"
|
||||||
"github.com/julienschmidt/httprouter"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
libsystem "dev.sum7.de/sum7/warehost/system"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PermissionList to add permissions
|
// PermissionList to add permissions
|
||||||
func (api *API) PermissionList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func permissionList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "permissionlist")
|
logger := log.GetLog(r, "permissionlist")
|
||||||
var involved []*Manager
|
var involved []*Manager
|
||||||
api.dbconnection.Where("website = ?", websiteid).Preload("Login").Find(&involved)
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Login").Find(&involved)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
returndata = involved
|
returndata = involved
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// PermissionAdd to add permissions
|
// PermissionAdd to add permissions
|
||||||
func (api *API) PermissionAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func permissionAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "permissionadd")
|
logger := log.GetLog(r, "permissionadd")
|
||||||
loginid, err := strconv.ParseInt(ps.ByName("loginid"), 10, 64)
|
loginid, err := strconv.ParseInt(pat.Param(ctx, "loginid"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"}
|
||||||
logger.Warn("invalid loginid, no integer")
|
logger.Warn("invalid loginid, no integer")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
manager := &Manager{
|
manager := &Manager{
|
||||||
WebsiteID: websiteid,
|
WebsiteID: ctx.Value("websiteid").(int64),
|
||||||
LoginID: loginid,
|
LoginID: loginid,
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Create(manager).Error; err != nil {
|
if err := dbconnection.Create(manager).Error; err != nil {
|
||||||
logger.Error("database: during create website permission")
|
logger.Error("database: during create website permission")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
@ -47,20 +46,20 @@ func (api *API) PermissionAdd(w http.ResponseWriter, r *http.Request, ps httprou
|
||||||
}
|
}
|
||||||
|
|
||||||
// PermissionDelete to delete permissions
|
// PermissionDelete to delete permissions
|
||||||
func (api *API) PermissionDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func permissionDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "permissiondelete")
|
logger := log.GetLog(r, "permissiondelete")
|
||||||
loginid, err := strconv.ParseInt(ps.ByName("loginid"), 10, 64)
|
loginid, err := strconv.ParseInt(pat.Param(ctx, "loginid"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"}
|
||||||
logger.Warn("invalid loginid, no integer")
|
logger.Warn("invalid loginid, no integer")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
manager := &Manager{
|
manager := &Manager{
|
||||||
WebsiteID: websiteid,
|
WebsiteID: ctx.Value("websiteid").(int64),
|
||||||
LoginID: loginid,
|
LoginID: loginid,
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Unscoped().Delete(manager).Error; err != nil {
|
if err := dbconnection.Unscoped().Delete(manager).Error; err != nil {
|
||||||
logger.Error("database: during delete website permission")
|
logger.Error("database: during delete website permission")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,46 +5,43 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"github.com/astaxie/session"
|
||||||
"github.com/jinzhu/gorm"
|
"goji.io/pat"
|
||||||
"github.com/julienschmidt/httprouter"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
libsystem "dev.sum7.de/sum7/warehost/system"
|
libsystem "dev.sum7.eu/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handle to handle request with session and current logged in user
|
//InvolveWebsiteHandler for api function to Verifie User ist libloggedin
|
||||||
type Handle func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, id int64) (interface{}, *libapi.ErrorResult)
|
func InvolveWebsiteHandler(h libapi.Handle) libapi.Handle {
|
||||||
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
//InvolveWebsiteHandler for api function to Verifie User ist loggedin
|
sess := ctx.Value("session").(session.Session)
|
||||||
func InvolveWebsiteHandler(h Handle, sessions *session.Manager, dbconnection *gorm.DB) httprouter.Handle {
|
returnerr = &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not liblogged in"}
|
||||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
returndata = false
|
||||||
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 login := sess.Get("login"); login != nil {
|
||||||
if loginObj := login.(libsystem.Login); loginObj.Active {
|
if loginObj := login.(libsystem.Login); loginObj.Active {
|
||||||
id, errI := strconv.ParseInt(ps.ByName("websiteid"), 10, 64)
|
id, err := strconv.ParseInt(pat.Param(ctx, "websiteid"), 10, 64)
|
||||||
if errI != nil {
|
if err == 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{})
|
res := dbconnection.Where(map[string]int64{"website": id, "login": loginObj.ID}).Find(&Manager{})
|
||||||
if !res.RecordNotFound() {
|
if !res.RecordNotFound() {
|
||||||
data, err = h(w, r, ps, sess, &loginObj, id)
|
ctx = context.WithValue(ctx, "websiteid", id)
|
||||||
} else {
|
returndata, returnerr = h(ctx, w, r)
|
||||||
err = &libapi.ErrorResult{Fields: []string{"permission"}, Message: "No permission"}
|
return
|
||||||
log.Log.Info("no Permissions")
|
|
||||||
}
|
}
|
||||||
|
returnerr = &libapi.ErrorResult{Fields: []string{"permission"}, Message: "No permission"}
|
||||||
|
liblog.Log.Info("no Permissions")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
returnerr = &libapi.ErrorResult{Fields: []string{"websiteid"}, Message: "Not a valid websiteid"}
|
||||||
log.Log.Warn("user not active")
|
liblog.Log.Warn("invalid websiteid, no integer")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
liblog.Log.Warn("user not active")
|
||||||
log.Log.Warn("not loggedin")
|
return
|
||||||
}
|
}
|
||||||
libapi.JSONOutput(w, r, sess, data, err)
|
liblog.Log.Warn("not libloggedin")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
|
||||||
system "dev.sum7.de/sum7/warehost/system"
|
system "dev.sum7.eu/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Website struct
|
// Website struct
|
||||||
|
|
169
system/api.go
169
system/api.go
|
@ -7,56 +7,49 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/julienschmidt/httprouter"
|
"goji.io"
|
||||||
|
"goji.io/pat"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libconfig "dev.sum7.de/sum7/warehost/config"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
libpassword "dev.sum7.eu/sum7/warehost/lib/password"
|
||||||
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
libsession "dev.sum7.eu/sum7/warehost/lib/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
//MODULNAME to get global name for the modul
|
//MODULNAME to get global name for the modul
|
||||||
const MODULNAME = "system"
|
const MODULNAME = "system"
|
||||||
|
|
||||||
//API keep data in module global
|
var dbconnection *gorm.DB
|
||||||
type API struct {
|
var log *liblog.ModulLog
|
||||||
config *libconfig.Config
|
|
||||||
sessions *session.Manager
|
|
||||||
dbconnection *gorm.DB
|
|
||||||
log *log.ModulLog
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAPI sets the routes to the api functions
|
// BindAPI sets the routes to the api functions
|
||||||
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router, prefix string) {
|
func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
||||||
api := &API{
|
dbconnection = db
|
||||||
config: config,
|
log = liblog.NewModulLog(MODULNAME)
|
||||||
sessions: sessions,
|
|
||||||
dbconnection: dbconnection,
|
router.HandleFuncC(pat.Get(prefix+"/status"), libapi.SessionHandler(status))
|
||||||
log: log.NewModulLog(MODULNAME),
|
router.HandleFuncC(pat.Post(prefix+"/login"), libapi.SessionHandler(login))
|
||||||
}
|
router.HandleFuncC(pat.Get(prefix+"/logout"), libapi.SessionHandler(LoginHandler(logout)))
|
||||||
router.GET(prefix+"/status", libapi.SessionHandler(api.Status, sessions))
|
router.HandleFuncC(pat.Post(prefix+"/password"), libapi.SessionHandler(LoginHandler(password)))
|
||||||
router.POST(prefix+"/login", libapi.SessionHandler(api.Login, sessions))
|
router.HandleFuncC(pat.Get(prefix+"/delete"), libapi.SessionHandler(LoginHandler(delete)))
|
||||||
router.GET(prefix+"/logout", LoginHandler(api.Logout, sessions))
|
router.HandleFuncC(pat.Get(prefix+"/invite"), libapi.SessionHandler(LoginHandler(inviteList)))
|
||||||
router.POST(prefix+"/password", LoginHandler(api.Password, sessions))
|
router.HandleFuncC(pat.Post(prefix+"/invite"), libapi.SessionHandler(LoginHandler(inviteAdd)))
|
||||||
router.GET(prefix+"/delete", LoginHandler(api.Delete, sessions))
|
router.HandleFuncC(pat.Get(prefix+"/user"), libapi.SessionHandler(LoginHandler(loginList)))
|
||||||
router.GET(prefix+"/invite", LoginHandler(api.InviteList, sessions))
|
router.HandleFuncC(pat.Post(prefix+"/user"), libapi.SessionHandler(LoginHandler(loginAdd)))
|
||||||
router.POST(prefix+"/invite", LoginHandler(api.InviteAdd, sessions))
|
router.HandleFuncC(pat.Put(prefix+"/user/:id"), libapi.SessionHandler(LoginHandler(loginEdit)))
|
||||||
router.GET(prefix+"/user", LoginHandler(api.LoginList, sessions))
|
router.HandleFuncC(pat.Delete(prefix+"/user/:id"), libapi.SessionHandler(LoginHandler(loginDelete)))
|
||||||
router.POST(prefix+"/user", LoginHandler(api.LoginAdd, sessions))
|
router.HandleFuncC(pat.Get(prefix+"/invitor"), libapi.SessionHandler(LoginHandler(invitor)))
|
||||||
router.PUT(prefix+"/user/:id", LoginHandler(api.LoginEdit, sessions))
|
router.HandleFuncC(pat.Put(prefix+"/invitor"), libapi.SessionHandler(LoginHandler(invitorAdminToggle)))
|
||||||
router.DELETE(prefix+"/user/:id", LoginHandler(api.LoginDelete, sessions))
|
|
||||||
router.GET(prefix+"/invitor", LoginHandler(api.Invitor, sessions))
|
|
||||||
router.PUT(prefix+"/invitor", LoginHandler(api.InvitorAdminToggle, sessions))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status to get Login and Server status
|
// Status to get Login and Server status
|
||||||
func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func status(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "status")
|
logger := log.GetLog(r, "status")
|
||||||
var result int64
|
var result int64
|
||||||
api.dbconnection.Model(&Login{}).Count(&result)
|
dbconnection.Model(&Login{}).Count(&result)
|
||||||
if result > 0 {
|
if result > 0 {
|
||||||
returndata = true
|
returndata = true
|
||||||
}
|
}
|
||||||
|
@ -65,9 +58,10 @@ func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logout current user
|
// Logout current user
|
||||||
func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, _ *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func logout(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
api.sessions.SessionDestroy(w, r)
|
sess := ctx.Value("session").(libsession.Session)
|
||||||
logger := api.log.GetLog(r, "logout")
|
libsession.SessionDestroy(w, r)
|
||||||
|
logger := log.GetLog(r, "logout")
|
||||||
if login := sess.Get("login"); login != nil {
|
if login := sess.Get("login"); login != nil {
|
||||||
logger = logger.WithField("user", login.(Login).Username)
|
logger = logger.WithField("user", login.(Login).Username)
|
||||||
}
|
}
|
||||||
|
@ -80,9 +74,10 @@ func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login of system
|
// Login of system
|
||||||
func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func login(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
sess := ctx.Value("session").(libsession.Session)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "login")
|
logger := log.GetLog(r, "login")
|
||||||
var requestlogin RequestLogin
|
var requestlogin RequestLogin
|
||||||
err := json.NewDecoder(r.Body).Decode(&requestlogin)
|
err := json.NewDecoder(r.Body).Decode(&requestlogin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -92,7 +87,7 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
|
||||||
}
|
}
|
||||||
logger = logger.WithField("user", requestlogin.Username)
|
logger = logger.WithField("user", requestlogin.Username)
|
||||||
var login = Login{Username: requestlogin.Username}
|
var login = Login{Username: requestlogin.Username}
|
||||||
api.dbconnection.Where("mail = ?", requestlogin.Username).First(&login)
|
dbconnection.Where("mail = ?", requestlogin.Username).First(&login)
|
||||||
if login.ID <= 0 {
|
if login.ID <= 0 {
|
||||||
logger.Warn("user not found")
|
logger.Warn("user not found")
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"username"}, Message: "User not Found"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"username"}, Message: "User not Found"}
|
||||||
|
@ -102,7 +97,7 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
|
||||||
output, _ := libpassword.Validate(login.Password, requestlogin.Password)
|
output, _ := libpassword.Validate(login.Password, requestlogin.Password)
|
||||||
if output {
|
if output {
|
||||||
returndata = true
|
returndata = true
|
||||||
api.dbconnection.Model(&login).Update("LastLoginAt", time.Now())
|
dbconnection.Model(&login).Update("LastLoginAt", time.Now())
|
||||||
sess.Set("login", login)
|
sess.Set("login", login)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
} else {
|
} else {
|
||||||
|
@ -117,9 +112,11 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
|
||||||
}
|
}
|
||||||
|
|
||||||
//Password to change the password
|
//Password to change the password
|
||||||
func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func password(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
sess := ctx.Value("session").(libsession.Session)
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "password")
|
logger := log.GetLog(r, "password")
|
||||||
var changePasswordRequest ChangePasswordRequest
|
var changePasswordRequest ChangePasswordRequest
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&changePasswordRequest)
|
err := json.NewDecoder(r.Body).Decode(&changePasswordRequest)
|
||||||
|
@ -142,7 +139,7 @@ func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
login.Password = libpassword.NewHash(changePasswordRequest.NewPassword)
|
login.Password = libpassword.NewHash(changePasswordRequest.NewPassword)
|
||||||
if err := api.dbconnection.Save(login).Error; err != nil {
|
if err := dbconnection.Save(login).Error; err != nil {
|
||||||
logger.Warn("error save new password to database")
|
logger.Warn("error save new password to database")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error save new password"}
|
returnerr = &libapi.ErrorResult{Message: "Error save new password"}
|
||||||
return
|
return
|
||||||
|
@ -154,11 +151,13 @@ func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
|
||||||
}
|
}
|
||||||
|
|
||||||
//Delete of login on warehost
|
//Delete of login on warehost
|
||||||
func (api *API) Delete(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func delete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
sess := ctx.Value("session").(libsession.Session)
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "delete")
|
logger := log.GetLog(r, "delete")
|
||||||
sess.Delete("login")
|
sess.Delete("login")
|
||||||
if err := api.dbconnection.Unscoped().Delete(login).Error; err != nil {
|
if err := dbconnection.Unscoped().Delete(login).Error; err != nil {
|
||||||
logger.Warn("error detete login")
|
logger.Warn("error detete login")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error delete login"}
|
returnerr = &libapi.ErrorResult{Message: "Error delete login"}
|
||||||
return
|
return
|
||||||
|
@ -169,10 +168,11 @@ func (api *API) Delete(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
||||||
}
|
}
|
||||||
|
|
||||||
// InviteList list all of your invites
|
// 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 inviteList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "invitelist")
|
logger := log.GetLog(r, "invitelist")
|
||||||
if err := api.dbconnection.Model(login).Preload("Invites.Invited").First(login).Error; err != nil {
|
if err := dbconnection.Model(login).Preload("Invites.Invited").First(login).Error; err != nil {
|
||||||
logger.Warn("error load own invites")
|
logger.Warn("error load own invites")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Could not load invites!"}
|
returnerr = &libapi.ErrorResult{Message: "Could not load invites!"}
|
||||||
return
|
return
|
||||||
|
@ -183,9 +183,10 @@ func (api *API) InviteList(w http.ResponseWriter, r *http.Request, _ httprouter.
|
||||||
}
|
}
|
||||||
|
|
||||||
// InviteAdd invite a new user to warehost
|
// 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 inviteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "inviteadd")
|
logger := log.GetLog(r, "inviteadd")
|
||||||
var newLogin RequestLogin
|
var newLogin RequestLogin
|
||||||
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -202,7 +203,7 @@ func (api *API) InviteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.P
|
||||||
Active: true,
|
Active: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Create(invite).Error; err != nil {
|
if err := dbconnection.Create(invite).Error; err != nil {
|
||||||
logger.Warn("error create invite")
|
logger.Warn("error create invite")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Username exists already"}
|
returnerr = &libapi.ErrorResult{Message: "Username exists already"}
|
||||||
return
|
return
|
||||||
|
@ -213,15 +214,16 @@ func (api *API) InviteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.P
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoginList list all users in system
|
// LoginList list all users in system
|
||||||
func (api *API) LoginList(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func loginList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "loginlist")
|
logger := log.GetLog(r, "loginlist")
|
||||||
var logins []Login
|
var logins []Login
|
||||||
selectfield := "ID, mail"
|
selectfield := "ID, mail"
|
||||||
if login.Superadmin {
|
if login.Superadmin {
|
||||||
selectfield = "ID, mail, superadmin"
|
selectfield = "ID, mail, superadmin"
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Select(selectfield).Find(&logins).Error; err != nil {
|
if err := dbconnection.Select(selectfield).Find(&logins).Error; err != nil {
|
||||||
logger.Warn("sql edit login")
|
logger.Warn("sql edit login")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error during edit login"}
|
returnerr = &libapi.ErrorResult{Message: "Error during edit login"}
|
||||||
return
|
return
|
||||||
|
@ -232,9 +234,10 @@ func (api *API) LoginList(w http.ResponseWriter, r *http.Request, ps httprouter.
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoginAdd add a new Login
|
// LoginAdd add a new Login
|
||||||
func (api *API) LoginAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func loginAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "loginadd")
|
logger := log.GetLog(r, "loginadd")
|
||||||
if !login.Superadmin {
|
if !login.Superadmin {
|
||||||
logger.Error("no superadmin")
|
logger.Error("no superadmin")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error no permission to edit this invite"}
|
returnerr = &libapi.ErrorResult{Message: "Error no permission to edit this invite"}
|
||||||
|
@ -253,7 +256,7 @@ func (api *API) LoginAdd(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
Password: libpassword.NewHash(newLogin.Password),
|
Password: libpassword.NewHash(newLogin.Password),
|
||||||
Active: true,
|
Active: true,
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Create(loginObj).Error; err != nil {
|
if err := dbconnection.Create(loginObj).Error; err != nil {
|
||||||
logger.Warn("error create login")
|
logger.Warn("error create login")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Username exists already"}
|
returnerr = &libapi.ErrorResult{Message: "Username exists already"}
|
||||||
return
|
return
|
||||||
|
@ -264,10 +267,11 @@ func (api *API) LoginAdd(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoginEdit edit a login by invite or superadmin
|
// LoginEdit edit a login by invite or superadmin
|
||||||
func (api *API) LoginEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func loginEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "loginedit")
|
logger := log.GetLog(r, "loginedit")
|
||||||
id, err := strconv.ParseInt(ps.ByName("id"), 10, 64)
|
id, err := strconv.ParseInt(pat.Param(ctx, "id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error invalid input"}
|
returnerr = &libapi.ErrorResult{Message: "Error invalid input"}
|
||||||
logger.Warn("invalid userinput, no integer")
|
logger.Warn("invalid userinput, no integer")
|
||||||
|
@ -284,8 +288,8 @@ func (api *API) LoginEdit(w http.ResponseWriter, r *http.Request, ps httprouter.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
api.dbconnection.Where("id = ?", invitedLogin.ID).First(&invitedLogin)
|
dbconnection.Where("id = ?", invitedLogin.ID).First(&invitedLogin)
|
||||||
invite := invitedLogin.GetInvitedby(api.dbconnection)
|
invite := invitedLogin.GetInvitedby(dbconnection)
|
||||||
if !login.Superadmin && !invite.Admin && invitedLogin.CreateAt.Before(invitedLogin.LastLoginAt) {
|
if !login.Superadmin && !invite.Admin && invitedLogin.CreateAt.Before(invitedLogin.LastLoginAt) {
|
||||||
logger.Warn("no permission")
|
logger.Warn("no permission")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error no permission to edit this login"}
|
returnerr = &libapi.ErrorResult{Message: "Error no permission to edit this login"}
|
||||||
|
@ -298,7 +302,7 @@ func (api *API) LoginEdit(w http.ResponseWriter, r *http.Request, ps httprouter.
|
||||||
invitedLogin.Username = changeLogin.Username
|
invitedLogin.Username = changeLogin.Username
|
||||||
invitedLogin.Superadmin = changeLogin.Superadmin
|
invitedLogin.Superadmin = changeLogin.Superadmin
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Save(invitedLogin).Error; err != nil {
|
if err := dbconnection.Save(invitedLogin).Error; err != nil {
|
||||||
logger.Warn("sql edit login")
|
logger.Warn("sql edit login")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error during edit login"}
|
returnerr = &libapi.ErrorResult{Message: "Error during edit login"}
|
||||||
return
|
return
|
||||||
|
@ -309,10 +313,11 @@ func (api *API) LoginEdit(w http.ResponseWriter, r *http.Request, ps httprouter.
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoginDelete delete a login by invite or superadmin
|
// LoginDelete delete a login by invite or superadmin
|
||||||
func (api *API) LoginDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func loginDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "logindelete")
|
logger := log.GetLog(r, "logindelete")
|
||||||
id, err := strconv.ParseInt(ps.ByName("id"), 10, 64)
|
id, err := strconv.ParseInt(pat.Param(ctx, "id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error invalid input"}
|
returnerr = &libapi.ErrorResult{Message: "Error invalid input"}
|
||||||
logger.Warn("invalid userinput, no integer")
|
logger.Warn("invalid userinput, no integer")
|
||||||
|
@ -320,14 +325,14 @@ func (api *API) LoginDelete(w http.ResponseWriter, r *http.Request, ps httproute
|
||||||
}
|
}
|
||||||
logger = logger.WithField("id", id)
|
logger = logger.WithField("id", id)
|
||||||
var invitedLogin = Login{ID: id}
|
var invitedLogin = Login{ID: id}
|
||||||
api.dbconnection.Where("id = ?", invitedLogin.ID).First(&invitedLogin)
|
dbconnection.Where("id = ?", invitedLogin.ID).First(&invitedLogin)
|
||||||
invite := invitedLogin.GetInvitedby(api.dbconnection)
|
invite := invitedLogin.GetInvitedby(dbconnection)
|
||||||
if !login.Superadmin && !invite.Admin && invitedLogin.CreateAt.Before(invitedLogin.LastLoginAt) {
|
if !login.Superadmin && !invite.Admin && invitedLogin.CreateAt.Before(invitedLogin.LastLoginAt) {
|
||||||
logger.Warn("no permission")
|
logger.Warn("no permission")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error no permission to delete this login"}
|
returnerr = &libapi.ErrorResult{Message: "Error no permission to delete this login"}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := api.dbconnection.Unscoped().Delete(invitedLogin).Error; err != nil {
|
if err := dbconnection.Unscoped().Delete(invitedLogin).Error; err != nil {
|
||||||
logger.Warn("sql detete login")
|
logger.Warn("sql detete login")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error during delete login"}
|
returnerr = &libapi.ErrorResult{Message: "Error during delete login"}
|
||||||
return
|
return
|
||||||
|
@ -338,22 +343,24 @@ func (api *API) LoginDelete(w http.ResponseWriter, r *http.Request, ps httproute
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invitor get Invite of current login
|
// Invitor get Invite of current login
|
||||||
func (api *API) Invitor(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func invitor(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "invitor")
|
logger := log.GetLog(r, "invitor")
|
||||||
invite := login.GetInvitedby(api.dbconnection)
|
invite := login.GetInvitedby(dbconnection)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
returndata = invite
|
returndata = invite
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// InvitorAdminToggle toggle admin of current login
|
// InvitorAdminToggle toggle admin of current login
|
||||||
func (api *API) InvitorAdminToggle(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func invitorAdminToggle(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
login := ctx.Value("login").(*Login)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "invitoradmintoggle")
|
logger := log.GetLog(r, "invitoradmintoggle")
|
||||||
invite := login.GetInvitedby(api.dbconnection)
|
invite := login.GetInvitedby(dbconnection)
|
||||||
invite.Admin = !invite.Admin
|
invite.Admin = !invite.Admin
|
||||||
api.dbconnection.Model(invite).Save(&invite)
|
dbconnection.Model(invite).Save(&invite)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
returndata = true
|
returndata = true
|
||||||
return
|
return
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"dev.sum7.de/sum7/warehost/test"
|
"dev.sum7.eu/sum7/warehost/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAPI(t *testing.T) {
|
func TestAPI(t *testing.T) {
|
||||||
|
@ -16,7 +16,7 @@ func TestAPI(t *testing.T) {
|
||||||
SyncModels(db)
|
SyncModels(db)
|
||||||
|
|
||||||
// Bind API
|
// Bind API
|
||||||
NewAPI(config, sessions, db, router, "")
|
BindAPI(db, router, "")
|
||||||
session := test.NewSession(router)
|
session := test.NewSession(router)
|
||||||
|
|
||||||
// Test status
|
// Test status
|
||||||
|
|
|
@ -4,32 +4,29 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"github.com/astaxie/session"
|
||||||
"github.com/julienschmidt/httprouter"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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 *Login) (interface{}, *libapi.ErrorResult)
|
|
||||||
|
|
||||||
//LoginHandler for api function to Verifie User ist loggedin
|
//LoginHandler for api function to Verifie User ist loggedin
|
||||||
func LoginHandler(h Handle, sessions *session.Manager) httprouter.Handle {
|
func LoginHandler(h libapi.Handle) libapi.Handle {
|
||||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
sess := sessions.SessionStart(w, r)
|
sess := ctx.Value("session").(session.Session)
|
||||||
err := &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not logged in"}
|
returndata = false
|
||||||
var data interface{}
|
|
||||||
data = false
|
|
||||||
|
|
||||||
if login := sess.Get("login"); login != nil {
|
if login := sess.Get("login"); login != nil {
|
||||||
if loginObj := login.(Login); loginObj.Active {
|
if loginObj := login.(Login); loginObj.Active {
|
||||||
data, err = h(w, r, ps, sess, &loginObj)
|
ctx = context.WithValue(ctx, "login", &loginObj)
|
||||||
} else {
|
returndata, returnerr = h(ctx, w, r)
|
||||||
log.Log.Warn("user not active")
|
|
||||||
}
|
}
|
||||||
} else {
|
returnerr = &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not active user"}
|
||||||
log.Log.Warn("not loggedin")
|
liblog.Log.Warn("user not active")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
libapi.JSONOutput(w, r, sess, data, err)
|
returnerr = &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not logged in"}
|
||||||
|
liblog.Log.Warn("not loggedin")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,10 @@ package system
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
|
||||||
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
|
libpassword "dev.sum7.eu/sum7/warehost/lib/password"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MINPASSWORDLENTH to validate password
|
// MINPASSWORDLENTH to validate password
|
||||||
|
@ -77,8 +77,8 @@ func SyncModels(dbconnection *gorm.DB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
dbconnection.Create(login)
|
dbconnection.Create(login)
|
||||||
log.Log.Error("have to create \"login\"")
|
liblog.Log.Error("have to create \"login\"")
|
||||||
} else {
|
} else {
|
||||||
log.Log.Info("Connection to \"login\" works")
|
liblog.Log.Info("Connection to \"login\" works")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
16
test/main.go
16
test/main.go
|
@ -11,16 +11,16 @@ import (
|
||||||
_ "github.com/astaxie/session/providers/memory"
|
_ "github.com/astaxie/session/providers/memory"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"goji.io"
|
||||||
|
|
||||||
libconfig "dev.sum7.de/sum7/warehost/config"
|
libconfig "dev.sum7.eu/sum7/warehost/config"
|
||||||
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
log "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Init to initialisieren a API
|
//Init to initialisieren a API
|
||||||
func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router) {
|
func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *goji.Mux) {
|
||||||
assertion = assert.New(t)
|
assertion = assert.New(t)
|
||||||
config = libconfig.ReadConfigFile("../cmd/warehost/config.yml.example")
|
config = libconfig.ReadConfigFile("../cmd/warehost/config.yml.example")
|
||||||
log.NewLogger(config.Log.Path)
|
log.NewLogger(config.Log.Path)
|
||||||
|
@ -39,7 +39,7 @@ func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config,
|
||||||
dbconnection.SingularTable(true)
|
dbconnection.SingularTable(true)
|
||||||
dbconnection.LogMode(config.DatabaseDebug)
|
dbconnection.LogMode(config.DatabaseDebug)
|
||||||
|
|
||||||
router = httprouter.New()
|
router = goji.NewMux()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,11 +47,11 @@ func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config,
|
||||||
type Request struct {
|
type Request struct {
|
||||||
req *http.Request
|
req *http.Request
|
||||||
cookies []*http.Cookie
|
cookies []*http.Cookie
|
||||||
router *httprouter.Router
|
router *goji.Mux
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSession to get a new easy manager
|
// NewSession to get a new easy manager
|
||||||
func NewSession(router *httprouter.Router) *Request {
|
func NewSession(router *goji.Mux) *Request {
|
||||||
return &Request{router: router}
|
return &Request{router: router}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue