diff --git a/admin/db-zone.go b/admin/db-zone.go index 175951d..b908241 100644 --- a/admin/db-zone.go +++ b/admin/db-zone.go @@ -52,17 +52,17 @@ func init() { router.PUT("/api/users/:userid/domains/:domain/zones", api.ApiHandler(userHandler(domainHandler(updateUserDomainZones)))) router.POST("/api/users/:userid/domains/:domain/zones", api.ApiHandler(userHandler(domainHandler(newUserDomainZone)))) - router.GET("/api/users/:userid/domains/:domain/zones/:zoneid", api.ApiHandler(zoneHandler(getZone))) - router.PUT("/api/users/:userid/domains/:domain/zones/:zoneid", api.ApiHandler(zoneHandler(updateZone))) + router.GET("/api/users/:userid/domains/:domain/zones/:zoneid", api.ApiHandler(userHandler(zoneHandler(getZone)))) + router.PUT("/api/users/:userid/domains/:domain/zones/:zoneid", api.ApiHandler(userHandler(zoneHandler(updateZone)))) router.DELETE("/api/users/:userid/domains/:domain/zones/:zoneid", api.ApiHandler(deleteZone)) - router.GET("/api/zones/:zoneid", api.ApiHandler(zoneHandler(getZone))) - router.PUT("/api/zones/:zoneid", api.ApiHandler(zoneHandler(updateZone))) + router.GET("/api/zones/:zoneid", api.ApiHandler(userHandler(zoneHandler(getZone)))) + router.PUT("/api/zones/:zoneid", api.ApiHandler(userHandler(zoneHandler(updateZone)))) router.DELETE("/api/zones/:zoneid", api.ApiHandler(deleteZone)) - router.GET("/api/users/:userid/domains/:domain/zones/:zoneid/:serviceid", api.ApiHandler(zoneHandler(getZoneService))) - router.PUT("/api/users/:userid/domains/:domain/zones/:zoneid/:serviceid", api.ApiHandler(zoneHandler(updateZoneService))) - router.PATCH("/api/users/:userid/domains/:domain/zones/:zoneid", api.ApiHandler(zoneHandler(patchZoneService))) + router.GET("/api/users/:userid/domains/:domain/zones/:zoneid/*serviceid", api.ApiHandler(userHandler(zoneHandler(getZoneService)))) + router.PUT("/api/users/:userid/domains/:domain/zones/:zoneid/*serviceid", api.ApiHandler(userHandler(zoneHandler(updateZoneService)))) + router.PATCH("/api/users/:userid/domains/:domain/zones/:zoneid", api.ApiHandler(userHandler(zoneHandler(patchZoneService)))) } func getUserDomainZones(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, _ io.Reader) api.Response { @@ -89,27 +89,41 @@ func newUserDomainZone(_ *config.Options, domain *happydns.Domain, _ httprouter. return api.NewAPIResponse(uz, storage.MainStore.CreateZone(uz)) } -func zoneHandler(f func(*config.Options, *happydns.Zone, httprouter.Params, io.Reader) api.Response) func(*config.Options, httprouter.Params, io.Reader) api.Response { - return func(opts *config.Options, ps httprouter.Params, body io.Reader) api.Response { - zoneid, err := strconv.ParseInt(ps.ByName("zoneid"), 10, 64) - if err != nil { - return api.NewAPIErrorResponse(http.StatusNotFound, err) - } else { +func zoneHandler(f func(*config.Options, *happydns.Domain, *happydns.Zone, httprouter.Params, io.Reader) api.Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) api.Response { + return func(opts *config.Options, user *happydns.User, ps httprouter.Params, body io.Reader) api.Response { + return domainHandler(func(opts *config.Options, domain *happydns.Domain, ps httprouter.Params, body io.Reader) api.Response { + zoneid, err := strconv.ParseInt(ps.ByName("zoneid"), 10, 64) + if err != nil { + return api.NewAPIErrorResponse(http.StatusNotFound, err) + } + + // Check that the zoneid exists in the domain history + found := false + for _, v := range domain.ZoneHistory { + if v.Id == zoneid { + found = true + break + } + } + if !found { + return api.NewAPIErrorResponse(http.StatusNotFound, fmt.Errorf("Zone not found")) + } + zone, err := storage.MainStore.GetZone(zoneid) if err != nil { return api.NewAPIErrorResponse(http.StatusNotFound, err) } else { - return f(opts, zone, ps, body) + return f(opts, domain, zone, ps, body) } - } + })(opts, user, ps, body) } } -func getZone(_ *config.Options, zone *happydns.Zone, _ httprouter.Params, _ io.Reader) api.Response { +func getZone(_ *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, _ io.Reader) api.Response { return api.NewAPIResponse(zone, nil) } -func updateZone(_ *config.Options, zone *happydns.Zone, _ httprouter.Params, body io.Reader) api.Response { +func updateZone(_ *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, body io.Reader) api.Response { uz := &happydns.Zone{} err := json.NewDecoder(body).Decode(&uz) if err != nil { @@ -120,17 +134,19 @@ func updateZone(_ *config.Options, zone *happydns.Zone, _ httprouter.Params, bod return api.NewAPIResponse(uz, storage.MainStore.UpdateZone(uz)) } -func getZoneService(_ *config.Options, zone *happydns.Zone, ps httprouter.Params, body io.Reader) api.Response { - serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")) +func getZoneService(_ *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) api.Response { + serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")[1:]) if err != nil { return api.NewAPIErrorResponse(http.StatusBadRequest, err) } - return api.NewAPIResponse(zone.FindService(serviceid), nil) + _, svc := zone.FindService(serviceid) + + return api.NewAPIResponse(svc, nil) } -func updateZoneService(_ *config.Options, zone *happydns.Zone, ps httprouter.Params, body io.Reader) api.Response { - serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")) +func updateZoneService(_ *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) api.Response { + serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")[1:]) if err != nil { return api.NewAPIErrorResponse(http.StatusBadRequest, err) } @@ -141,7 +157,7 @@ func updateZoneService(_ *config.Options, zone *happydns.Zone, ps httprouter.Par return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err)) } - err = zone.EraseService(usc.Domain, serviceid, usc) + err = zone.EraseService(usc.Domain, domain.DomainName, serviceid, usc) if err != nil { return api.NewAPIErrorResponse(http.StatusBadRequest, err) } @@ -149,14 +165,14 @@ func updateZoneService(_ *config.Options, zone *happydns.Zone, ps httprouter.Par return api.NewAPIResponse(zone.Services, storage.MainStore.UpdateZone(zone)) } -func patchZoneService(_ *config.Options, zone *happydns.Zone, _ httprouter.Params, body io.Reader) api.Response { +func patchZoneService(_ *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, body io.Reader) api.Response { usc := &happydns.ServiceCombined{} err := json.NewDecoder(body).Decode(&usc) if err != nil { return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err)) } - err = zone.EraseService(usc.Domain, usc.Id, usc) + err = zone.EraseService(usc.Domain, domain.DomainName, usc.Id, usc) if err != nil { return api.NewAPIErrorResponse(http.StatusBadRequest, err) } diff --git a/api/zones.go b/api/zones.go index 6416255..541ae3b 100644 --- a/api/zones.go +++ b/api/zones.go @@ -32,6 +32,7 @@ package api import ( + "encoding/base64" "encoding/json" "errors" "fmt" @@ -50,11 +51,14 @@ import ( func init() { router.GET("/api/domains/:domain/zone/:zoneid", apiAuthHandler(zoneHandler(getZone))) router.PATCH("/api/domains/:domain/zone/:zoneid", apiAuthHandler(zoneHandler(updateZoneService))) + router.GET("/api/domains/:domain/zone/:zoneid/:subdomain", apiAuthHandler(zoneHandler(getZoneSubdomain))) + router.GET("/api/domains/:domain/zone/:zoneid/:subdomain/*serviceid", apiAuthHandler(zoneHandler(getZoneService))) + router.DELETE("/api/domains/:domain/zone/:zoneid/:subdomain/*serviceid", apiAuthHandler(zoneHandler(deleteZoneService))) router.POST("/api/domains/:domain/import_zone", apiAuthHandler(domainHandler(importZone))) } -func zoneHandler(f func(*config.Options, *happydns.Domain, *happydns.Zone, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response { +func zoneHandler(f func(*config.Options, *happydns.Domain, *happydns.Zone, httprouter.Params, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response { return func(opts *config.Options, u *happydns.User, ps httprouter.Params, body io.Reader) Response { zoneid, err := strconv.ParseInt(ps.ByName("zoneid"), 10, 64) if err != nil { @@ -85,18 +89,40 @@ func zoneHandler(f func(*config.Options, *happydns.Domain, *happydns.Zone, io.Re err: errors.New("Zone not found"), } } else { - return f(opts, domain, zone, body) + return f(opts, domain, zone, ps, body) } })(opts, u, ps, body) } } -func getZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, body io.Reader) Response { +func getZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, body io.Reader) Response { return APIResponse{ response: zone, } } +func getZoneSubdomain(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) Response { + return APIResponse{ + response: map[string]interface{}{ + "aliases": zone.Aliases[ps.ByName("subdomain")], + "services": zone.Services[ps.ByName("subdomain")], + }, + } +} + +func getZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) Response { + serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")[1:]) + if err != nil { + return APIErrorResponse{ + err: err, + } + } + + return APIResponse{ + response: zone.FindSubdomainService(ps.ByName("subdomain"), serviceid), + } +} + func importZone(opts *config.Options, domain *happydns.Domain, body io.Reader) Response { source, err := storage.MainStore.GetSource(&happydns.User{Id: domain.IdUser}, domain.IdSource) if err != nil { @@ -150,7 +176,7 @@ func importZone(opts *config.Options, domain *happydns.Domain, body io.Reader) R } } -func updateZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, body io.Reader) Response { +func updateZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, body io.Reader) Response { usc := &happydns.ServiceCombined{} err := json.NewDecoder(body).Decode(&usc) if err != nil { @@ -159,7 +185,34 @@ func updateZoneService(opts *config.Options, domain *happydns.Domain, zone *happ } } - err = zone.EraseService(usc.Domain, usc.Id, usc) + err = zone.EraseService(usc.Domain, domain.DomainName, usc.Id, usc) + if err != nil { + return APIErrorResponse{ + err: err, + } + } + + err = storage.MainStore.UpdateZone(zone) + if err != nil { + return APIErrorResponse{ + err: err, + } + } + + return APIResponse{ + response: zone, + } +} + +func deleteZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) Response { + serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")[1:]) + if err != nil { + return APIErrorResponse{ + err: err, + } + } + + err = zone.EraseService(ps.ByName("subdomain"), domain.DomainName, serviceid, nil) if err != nil { return APIErrorResponse{ err: err, diff --git a/htdocs/src/components/hDomainService.vue b/htdocs/src/components/hDomainService.vue index 10194f6..e3291d7 100644 --- a/htdocs/src/components/hDomainService.vue +++ b/htdocs/src/components/hDomainService.vue @@ -44,7 +44,7 @@ - + @@ -96,29 +96,52 @@ export default { }, methods: { - saveService (service, cbSuccess, cbFail) { - ZoneApi.updateZoneService(this.origin, this.zoneMeta.id, service) + deleteService (service) { + this.showDetails = false + ZoneApi.deleteZoneService(this.origin, this.zoneMeta.id, service) .then( (response) => { this.$emit('updateMyServices', response.data) - if (cbSuccess != null) { - cbSuccess() - } }, (error) => { this.$bvToast.toast( error.response.data.errmsg, { - title: 'An error occurs when updating the service!', + title: 'An error occurs when deleting the service!', autoHideDelay: 5000, variant: 'danger', toaster: 'b-toaster-content-right' } ) - if (cbFail != null) { - cbFail(error) + }) + }, + + saveService (service, cbSuccess, cbFail) { + if (service.Service === undefined) { + this.deleteService(service) + } else { + ZoneApi.updateZoneService(this.origin, this.zoneMeta.id, service) + .then( + (response) => { + this.$emit('updateMyServices', response.data) + if (cbSuccess != null) { + cbSuccess() + } + }, + (error) => { + this.$bvToast.toast( + error.response.data.errmsg, { + title: 'An error occurs when updating the service!', + autoHideDelay: 5000, + variant: 'danger', + toaster: 'b-toaster-content-right' + } + ) + if (cbFail != null) { + cbFail(error) + } } - } - ) + ) + } }, toogleShowDetails () { diff --git a/htdocs/src/components/hResourceValue.vue b/htdocs/src/components/hResourceValue.vue index 7996c56..27c0598 100644 --- a/htdocs/src/components/hResourceValue.vue +++ b/htdocs/src/components/hResourceValue.vue @@ -32,7 +32,7 @@ -->