diff --git a/characteristic_types.go b/characteristic_types.go index 43d9684..e40816a 100644 --- a/characteristic_types.go +++ b/characteristic_types.go @@ -57,7 +57,7 @@ func (c *CharacteristicTypes) marshal() ([]byte, error) { return json.Marshal(&CharacteristicTypesJSON{CharacteristicTypes: c}) } -func (c CharacteristicTypeService) list(val *url.Values) (entity, *appError) { +func (c CharacteristicTypeService) list(val *url.Values, claims Claims) (entity, *appError) { if val == nil { return nil, ErrMustProvideOptionsJSON } diff --git a/characteristics.go b/characteristics.go index 419ea5e..19a39ed 100644 --- a/characteristics.go +++ b/characteristics.go @@ -59,7 +59,7 @@ func (c *Characteristics) marshal() ([]byte, error) { return json.Marshal(&CharacteristicsJSON{Characteristics: c}) } -func (c CharacteristicService) list(val *url.Values) (entity, *appError) { +func (c CharacteristicService) list(val *url.Values, claims Claims) (entity, *appError) { if val == nil { return nil, ErrMustProvideOptionsJSON } diff --git a/entities.go b/entities.go index 55eeb45..562939f 100644 --- a/entities.go +++ b/entities.go @@ -11,7 +11,7 @@ type getter interface { } type lister interface { - list(*url.Values) (entity, *appError) + list(*url.Values, Claims) (entity, *appError) } type updater interface { diff --git a/handlers.go b/handlers.go index d1cfb12..a01b2bc 100644 --- a/handlers.go +++ b/handlers.go @@ -146,7 +146,9 @@ func handleLister(l lister) errorHandler { opt := r.URL.Query() opt.Add("Genus", mux.Vars(r)["genus"]) - es, appErr := l.list(&opt) + claims := getClaims(r) + + es, appErr := l.list(&opt, claims) if appErr != nil { return appErr } diff --git a/helpers.go b/helpers.go index 1c0d048..a5471f6 100644 --- a/helpers.go +++ b/helpers.go @@ -114,3 +114,7 @@ func getClaims(r *http.Request) Claims { } return claims } + +func canAdd(claims Claims) bool { + return claims.Role == "A" || claims.Role == "W" +} diff --git a/measurements.go b/measurements.go index 6b235eb..a454feb 100644 --- a/measurements.go +++ b/measurements.go @@ -66,7 +66,7 @@ func (m *Measurements) marshal() ([]byte, error) { return json.Marshal(&MeasurementsJSON{Measurements: m}) } -func (m MeasurementService) list(val *url.Values) (entity, *appError) { +func (m MeasurementService) list(val *url.Values, claims Claims) (entity, *appError) { if val == nil { return nil, ErrMustProvideOptionsJSON } diff --git a/species.go b/species.go index 47305bd..da63a0c 100644 --- a/species.go +++ b/species.go @@ -50,20 +50,25 @@ type Species struct { type ManySpecies []*Species -type SpeciesJSON struct { - Species *Species `json:"species"` +type SpeciesMeta struct { + CanAdd bool `json:"canAdd"` } -type ManySpeciesJSON struct { - ManySpecies *ManySpecies `json:"species"` +type ManySpeciesPayload struct { + Species *ManySpecies `json:"species"` + Meta *SpeciesMeta `json:"meta"` +} + +type SpeciesJSON struct { + Species *Species `json:"species"` } func (s *Species) marshal() ([]byte, error) { return json.Marshal(&SpeciesJSON{Species: s}) } -func (s *ManySpecies) marshal() ([]byte, error) { - return json.Marshal(&ManySpeciesJSON{ManySpecies: s}) +func (s *ManySpeciesPayload) marshal() ([]byte, error) { + return json.Marshal(s) } func (s SpeciesService) unmarshal(b []byte) (entity, error) { @@ -72,7 +77,7 @@ func (s SpeciesService) unmarshal(b []byte) (entity, error) { return sj.Species, err } -func (s SpeciesService) list(val *url.Values) (entity, *appError) { +func (s SpeciesService) list(val *url.Values, claims Claims) (entity, *appError) { if val == nil { return nil, ErrMustProvideOptionsJSON } @@ -109,7 +114,10 @@ func (s SpeciesService) list(val *url.Values) (entity, *appError) { if err != nil { return nil, newJSONError(err, http.StatusInternalServerError) } - return &species, nil + + meta := SpeciesMeta{CanAdd: canAdd(claims)} + payload := ManySpeciesPayload{Species: &species, Meta: &meta} + return &payload, nil } func (s SpeciesService) get(id int64, genus string) (entity, *appError) { diff --git a/strains.go b/strains.go index 7f98cd9..b556bfc 100644 --- a/strains.go +++ b/strains.go @@ -74,7 +74,7 @@ func (s StrainService) unmarshal(b []byte) (entity, error) { return sj.Strain, err } -func (s StrainService) list(val *url.Values) (entity, *appError) { +func (s StrainService) list(val *url.Values, claims Claims) (entity, *appError) { if val == nil { return nil, ErrMustProvideOptionsJSON } diff --git a/users.go b/users.go index 814a375..52693ec 100644 --- a/users.go +++ b/users.go @@ -124,7 +124,7 @@ func (u *User) validate() error { return nil } -func (u UserService) list(val *url.Values) (entity, *appError) { +func (u UserService) list(val *url.Values, claims Claims) (entity, *appError) { if val == nil { return nil, ErrMustProvideOptionsJSON }