From 0625d8f5ec07460b77a5258511c0d78806057021 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Wed, 24 Jun 2020 17:21:49 +0200 Subject: [PATCH] storage: add GetSourceType --- api/sources.go | 18 +++++++++++++++--- storage/interface.go | 1 + storage/leveldb/source.go | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/api/sources.go b/api/sources.go index 2566959..71c3b50 100644 --- a/api/sources.go +++ b/api/sources.go @@ -52,7 +52,7 @@ func init() { router.GET("/api/sources/:sid", apiAuthHandler(sourceHandler(getSource))) router.PUT("/api/sources/:sid", apiAuthHandler(sourceHandler(updateSource))) - router.DELETE("/api/sources/:sid", apiAuthHandler(sourceHandler(deleteSource))) + router.DELETE("/api/sources/:sid", apiAuthHandler(sourceTypeHandler(deleteSource))) router.GET("/api/sources/:sid/domains", apiAuthHandler(sourceHandler(getDomainsHostedBySource))) } @@ -73,6 +73,18 @@ func getSources(_ *config.Options, u *happydns.User, p httprouter.Params, body i } } +func sourceTypeHandler(f func(*config.Options, *happydns.SourceType, *happydns.User, 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 { + if sid, err := strconv.ParseInt(string(ps.ByName("sid")), 10, 64); err != nil { + return APIErrorResponse{err: err} + } else if srcType, err := storage.MainStore.GetSourceType(u, sid); err != nil { + return APIErrorResponse{err: err} + } else { + return f(opts, srcType, u, body) + } + } +} + func sourceHandler(f func(*config.Options, *happydns.SourceCombined, *happydns.User, 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 { if sid, err := strconv.ParseInt(string(ps.ByName("sid")), 10, 64); err != nil { @@ -167,8 +179,8 @@ func updateSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.Use } } -func deleteSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.User, body io.Reader) Response { - if err := storage.MainStore.DeleteSource(&s.SourceType); err != nil { +func deleteSource(_ *config.Options, st *happydns.SourceType, u *happydns.User, body io.Reader) Response { + if err := storage.MainStore.DeleteSource(st); err != nil { return APIErrorResponse{ err: err, } diff --git a/storage/interface.go b/storage/interface.go index 5d87900..09eb685 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -58,6 +58,7 @@ type Storage interface { GetSourceTypes(u *happydns.User) ([]happydns.SourceType, error) GetSource(u *happydns.User, id int64) (*happydns.SourceCombined, error) + GetSourceType(u *happydns.User, id int64) (*happydns.SourceType, error) CreateSource(u *happydns.User, s happydns.Source, comment string) (*happydns.SourceCombined, error) UpdateSource(s *happydns.SourceCombined) error UpdateSourceOwner(s *happydns.SourceCombined, newOwner *happydns.User) error diff --git a/storage/leveldb/source.go b/storage/leveldb/source.go index 6750c9c..3f03186 100644 --- a/storage/leveldb/source.go +++ b/storage/leveldb/source.go @@ -70,6 +70,27 @@ func (s *LevelDBStorage) GetSourceTypes(u *happydns.User) (srcs []happydns.Sourc return } +func (s *LevelDBStorage) GetSourceType(u *happydns.User, id int64) (srcType *happydns.SourceType, err error) { + var v []byte + v, err = s.db.Get([]byte(fmt.Sprintf("source-%d", id)), nil) + if err != nil { + return + } + + srcType = new(happydns.SourceType) + err = decodeData(v, &srcType) + if err != nil { + return + } + + if srcType.OwnerId != u.Id { + srcType = nil + err = leveldb.ErrNotFound + } + + return +} + func (s *LevelDBStorage) GetSource(u *happydns.User, id int64) (src *happydns.SourceCombined, err error) { var v []byte v, err = s.db.Get([]byte(fmt.Sprintf("source-%d", id)), nil)