sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This commit is contained in:
Martin Geno 2016-12-19 12:52:43 +01:00
parent 48cab176df
commit cfbb6a619f
11 changed files with 42 additions and 39 deletions

View File

@ -27,7 +27,7 @@ const (
) )
type FileDriver struct { type FileDriver struct {
config Config config *Config
db *gorm.DB db *gorm.DB
Perm ftpd.Perm Perm ftpd.Perm
login system.Login login system.Login
@ -98,18 +98,18 @@ func (driver *FileDriver) realPath(path string) (string, bool) {
if len(paths) > 1 && driver.login.ID > 0 { if len(paths) > 1 && driver.login.ID > 0 {
switch paths[1] { switch paths[1] {
case DriverFolderOwn: case DriverFolderOwn:
root = fmt.Sprintf(driver.Own, driver.login.ID) root = fmt.Sprintf(driver.config.Own, driver.login.ID)
paths = append([]string{paths[0]}, paths[2:]...) paths = append([]string{paths[0]}, paths[2:]...)
real = true real = true
case DriverFolderDomain: case DriverFolderDomain:
if len(paths) > 2 { if len(paths) > 2 {
root = fmt.Sprintf(driver.Host, driver.login.ID) root = fmt.Sprintf(driver.config.Host, driver.login.ID)
paths = append([]string{paths[0]}, paths[3:]...) paths = append([]string{paths[0]}, paths[3:]...)
real = true real = true
} }
case DriverFolderWeb: case DriverFolderWeb:
if len(paths) > 2 { if len(paths) > 2 {
root = fmt.Sprintf(driver.Web, driver.login.ID) root = fmt.Sprintf(driver.config.Web, driver.login.ID)
paths = append([]string{paths[0]}, paths[3:]...) paths = append([]string{paths[0]}, paths[3:]...)
real = true real = true
} }
@ -215,7 +215,7 @@ func (driver *FileDriver) ListDir(path string, callback func(ftpd.FileInfo) erro
driver.db.Preload("Domain.Profil.Login").Order("length(subdomain) asc").Find(&list) driver.db.Preload("Domain.Profil.Login").Order("length(subdomain) asc").Find(&list)
for _, i := range list { for _, i := range list {
domain := i.Domain.FQDN domain := i.Domain.FQDN
if len(item.Subdomain) > 0 { if len(i.Subdomain) > 0 {
domain = fmt.Sprintf("%s.%s", i.Subdomain, domain) domain = fmt.Sprintf("%s.%s", i.Subdomain, domain)
} }
err := callback(&FackFileInfo{name: domain}) err := callback(&FackFileInfo{name: domain})
@ -228,7 +228,7 @@ func (driver *FileDriver) ListDir(path string, callback func(ftpd.FileInfo) erro
var list []*web.Website var list []*web.Website
driver.db.Find(&list) driver.db.Find(&list)
for _, i := range list { for _, i := range list {
err := callback(&FackFileInfo{name: strconv.Itoa(i.ID)}) err := callback(&FackFileInfo{name: fmt.Sprintf("%d", i.ID)})
if err != nil { if err != nil {
return err return err
} }
@ -370,7 +370,7 @@ func (driver *FileDriver) PutFile(destPath string, data io.Reader, appendData bo
} }
type FileDriverFactory struct { type FileDriverFactory struct {
config Config config *Config
db *gorm.DB db *gorm.DB
Perm ftpd.Perm Perm ftpd.Perm
} }

View File

@ -46,7 +46,7 @@ func main() {
opt := &ftpd.ServerOpts{ opt := &ftpd.ServerOpts{
Name: "", Name: "",
Factory: &FileDriverFactory{Config: config, db: dbconnection, Perm: ftpd.NewSimplePerm("warehost", "http")}, Factory: &FileDriverFactory{config: config, db: dbconnection, Perm: ftpd.NewSimplePerm("warehost", "http")},
Port: config.Port, Port: config.Port,
Auth: WarehostAuth{db: dbconnection}, Auth: WarehostAuth{db: dbconnection},
} }

View File

@ -11,7 +11,6 @@ import (
"github.com/rs/cors" "github.com/rs/cors"
"goji.io" "goji.io"
"goji.io/pat" "goji.io/pat"
"golang.org/x/net/context"
config "dev.sum7.eu/sum7/warehost/config" config "dev.sum7.eu/sum7/warehost/config"
libapi "dev.sum7.eu/sum7/warehost/lib/api" libapi "dev.sum7.eu/sum7/warehost/lib/api"
@ -75,10 +74,9 @@ func main() {
// END Module deklations // END Module deklations
// Make Modules debugable // Make Modules debugable
router.HandleFuncC(pat.Get("/modules"), libapi.SessionHandler( router.HandleFunc(pat.Get("/modules"), libapi.SessionHandler(
func(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { func(w http.ResponseWriter, r *http.Request) {
returndata = modules libapi.JSONWrite(w, r, modules, nil)
return
})) }))
liblog.Log.Info("load modul: ", modules) liblog.Log.Info("load modul: ", modules)

View File

@ -58,6 +58,7 @@ func domainShow(w http.ResponseWriter, r *http.Request) {
domain, returnerr := getDomain(w, r) domain, returnerr := getDomain(w, r)
if returnerr != nil { if returnerr != nil {
logger.Info("not found") logger.Info("not found")
libapi.JSONWrite(w, r, false, returnerr)
return return
} }
logger = logger.WithField("dID", domain.ID) logger = logger.WithField("dID", domain.ID)
@ -107,6 +108,7 @@ func domainEdit(w http.ResponseWriter, r *http.Request) {
domain, returnerr := getDomain(w, r) domain, returnerr := getDomain(w, r)
if returnerr != nil { if returnerr != nil {
logger.Info("not found") logger.Info("not found")
libapi.JSONWrite(w, r, false, returnerr)
return return
} }
logger = logger.WithField("dID", domain.ID) logger = logger.WithField("dID", domain.ID)
@ -142,6 +144,7 @@ func domainDelete(w http.ResponseWriter, r *http.Request) {
domain, returnerr := getDomain(w, r) domain, returnerr := getDomain(w, r)
if returnerr != nil { if returnerr != nil {
logger.Info("not found") logger.Info("not found")
libapi.JSONWrite(w, r, false, returnerr)
return return
} }
logger = logger.WithField("dID", domain.ID) logger = logger.WithField("dID", domain.ID)

View File

@ -28,7 +28,6 @@ func TestAPIDomain(t *testing.T) {
result, w := session.JSONRequest("DELETE", "/host/delete", nil) result, w := session.JSONRequest("DELETE", "/host/delete", nil)
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
// Need a Profile for Next tests // Need a Profile for Next tests
result, w = session.JSONRequest("POST", "/host/signup", nil) result, w = session.JSONRequest("POST", "/host/signup", nil)
@ -78,7 +77,7 @@ func TestAPIDomain(t *testing.T) {
result, w = session.JSONRequest("POST", "/host/domain", Domain{ result, w = session.JSONRequest("POST", "/host/domain", Domain{
FQDN: "example.de", FQDN: "example.de",
}) })
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusInternalServerError, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
/* /*

View File

@ -23,12 +23,12 @@ func getMail(w http.ResponseWriter, r *http.Request) (mail Mail, returnerr *liba
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
mail = Mail{ID: id} mail = Mail{}
if login.Superadmin { if login.Superadmin {
dbconnection.Preload("Forwards").Find(&mail) dbconnection.Where("ID = ?", id).Preload("Forwards").Find(&mail)
} else { } else {
dbconnection.Where(map[string]int64{"domain.profil": profil.ID}).Preload("Forwards").Find(&mail) dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Preload("Forwards").Find(&mail)
} }
if mail.ID <= 0 { if mail.ID <= 0 {
returnerr = &libapi.ErrorResult{Fields: []string{"mail"}, Message: "not found"} returnerr = &libapi.ErrorResult{Fields: []string{"mail"}, Message: "not found"}
@ -43,6 +43,7 @@ func mailList(w http.ResponseWriter, r *http.Request) {
domain, returnerr := getDomain(w, r) domain, returnerr := getDomain(w, r)
if returnerr != nil { if returnerr != nil {
logger.Info("not found") logger.Info("not found")
libapi.JSONWrite(w, r, false, returnerr)
return return
} }
logger = logger.WithField("dID", domain.ID) logger = logger.WithField("dID", domain.ID)
@ -98,6 +99,7 @@ func mailEdit(w http.ResponseWriter, r *http.Request) {
mail, returnerr := getMail(w, r) mail, returnerr := getMail(w, r)
if returnerr != nil { if returnerr != nil {
logger.Info("not found") logger.Info("not found")
libapi.JSONWrite(w, r, false, returnerr)
return return
} }
logger = logger.WithField("mID", mail.ID) logger = logger.WithField("mID", mail.ID)

View File

@ -59,7 +59,7 @@ func TestAPIMail(t *testing.T) {
session.Clean() session.Clean()
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/mail", nil) result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/mail", nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)
@ -80,13 +80,13 @@ func TestAPIMail(t *testing.T) {
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", Mail{ result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", Mail{
Name: "test-bug", Name: "test-bug",
}) })
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", []byte{2, 3}) result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", []byte{2, 3})
assertion.Equal(w.StatusCode, http.StatusBadRequest) assertion.Equal(http.StatusBadRequest, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(-1)+"/mail", Mail{ result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(-1)+"/mail", Mail{
@ -104,7 +104,7 @@ func TestAPIMail(t *testing.T) {
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", Mail{ result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", Mail{
Name: "test", Name: "test",
}) })
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusInternalServerError, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
/* /*
@ -126,13 +126,13 @@ func TestAPIMail(t *testing.T) {
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), Mail{ result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), Mail{
Name: "test-bug-auth", Name: "test-bug-auth",
}) })
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), []byte{2, 3}) result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), []byte{2, 3})
assertion.Equal(w.StatusCode, http.StatusBadRequest) assertion.Equal(http.StatusBadRequest, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(-1), Mail{ result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(-1), Mail{
@ -153,7 +153,7 @@ func TestAPIMail(t *testing.T) {
session.Clean() session.Clean()
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), nil) result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)

View File

@ -39,7 +39,7 @@ func TestAPIProfil(t *testing.T) {
*/ */
session.Clean() session.Clean()
result, w = session.JSONRequest("GET", "/host/profils", nil) result, w = session.JSONRequest("GET", "/host/profils", nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)
@ -63,7 +63,7 @@ func TestAPIProfil(t *testing.T) {
session.Clean() session.Clean()
result, w = session.JSONRequest("PATCH", "/host/profil/"+strconv.Itoa(id), nil) result, w = session.JSONRequest("PATCH", "/host/profil/"+strconv.Itoa(id), nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)

View File

@ -36,11 +36,11 @@ func getWeb(w http.ResponseWriter, r *http.Request) (web Web, returnerr *libapi.
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
web = Web{ID: id} web = Web{}
if login.Superadmin { if login.Superadmin {
dbconnection.Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web) dbconnection.Where("ID = ?", id).Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web)
} else { } else {
dbconnection.Where(map[string]int64{"domain.profil": profil.ID}).Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web) dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web)
} }
if web.ID <= 0 { if web.ID <= 0 {
returnerr = &libapi.ErrorResult{Fields: []string{"web"}, Message: "not found"} returnerr = &libapi.ErrorResult{Fields: []string{"web"}, Message: "not found"}
@ -55,6 +55,7 @@ func webList(w http.ResponseWriter, r *http.Request) {
domain, returnerr := getDomain(w, r) domain, returnerr := getDomain(w, r)
if returnerr != nil { if returnerr != nil {
logger.Info("not found") logger.Info("not found")
libapi.JSONWrite(w, r, false, returnerr)
return return
} }
logger = logger.WithField("dID", domain.ID) logger = logger.WithField("dID", domain.ID)
@ -211,6 +212,7 @@ func webDelete(w http.ResponseWriter, r *http.Request) {
web, returnerr := getWeb(w, r) web, returnerr := getWeb(w, r)
if returnerr != nil { if returnerr != nil {
logger.Info("not found") logger.Info("not found")
libapi.JSONWrite(w, r, false, returnerr)
return return
} }
logger = logger.WithField("wID", web.ID) logger = logger.WithField("wID", web.ID)

View File

@ -59,7 +59,7 @@ func TestAPIWeb(t *testing.T) {
session.Clean() session.Clean()
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/web", nil) result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/web", nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)
@ -80,13 +80,13 @@ func TestAPIWeb(t *testing.T) {
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{ result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{
Subdomain: "", Subdomain: "",
}) })
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", []byte{2, 3}) result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", []byte{2, 3})
assertion.Equal(w.StatusCode, http.StatusBadRequest) assertion.Equal(http.StatusBadRequest, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(-1)+"/web", Web{ result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(-1)+"/web", Web{
@ -104,7 +104,7 @@ func TestAPIWeb(t *testing.T) {
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{ result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{
Subdomain: "", Subdomain: "",
}) })
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusInternalServerError, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
/* /*
@ -126,13 +126,13 @@ func TestAPIWeb(t *testing.T) {
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), Web{ result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), Web{
Subdomain: "test-bug-auth", Subdomain: "test-bug-auth",
}) })
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), []byte{2, 3}) result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), []byte{2, 3})
assertion.Equal(w.StatusCode, http.StatusBadRequest) assertion.Equal(http.StatusBadRequest, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(-1), Web{ result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(-1), Web{
@ -153,7 +153,7 @@ func TestAPIWeb(t *testing.T) {
session.Clean() session.Clean()
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), nil) result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized) assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data) assertion.Equal(false, result.Data)
loginTest(session, assertion) loginTest(session, assertion)

View File

@ -1,10 +1,9 @@
package system package system
import ( import (
"context"
"net/http" "net/http"
"golang.org/x/net/context"
libapi "dev.sum7.eu/sum7/warehost/lib/api" libapi "dev.sum7.eu/sum7/warehost/lib/api"
liblog "dev.sum7.eu/sum7/warehost/lib/log" liblog "dev.sum7.eu/sum7/warehost/lib/log"
libsession "dev.sum7.eu/sum7/warehost/lib/session" libsession "dev.sum7.eu/sum7/warehost/lib/session"