From 630fe07554afa059a69b27a4422e88abd4ed33cd Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Tue, 7 Jul 2015 12:59:11 -0800 Subject: [PATCH] Species editable permissions --- characteristic_types.go | 2 +- characteristics.go | 2 +- entities.go | 2 +- handlers.go | 4 +++- helpers.go | 15 +++++++++++++ measurements.go | 2 +- species.go | 50 +++++++++++++++++++++++++++++++++++++---- strains.go | 2 +- users.go | 2 +- 9 files changed, 70 insertions(+), 11 deletions(-) diff --git a/characteristic_types.go b/characteristic_types.go index e40816a..3ab8083 100644 --- a/characteristic_types.go +++ b/characteristic_types.go @@ -95,7 +95,7 @@ func (c CharacteristicTypeService) list(val *url.Values, claims Claims) (entity, return &characteristic_types, nil } -func (c CharacteristicTypeService) get(id int64, dummy string) (entity, *appError) { +func (c CharacteristicTypeService) get(id int64, dummy string, claims Claims) (entity, *appError) { var characteristic_type CharacteristicType q := `SELECT ct.*, array_agg(c.id) AS characteristics, 0 AS sort_order FROM characteristic_types ct diff --git a/characteristics.go b/characteristics.go index 19a39ed..700dba0 100644 --- a/characteristics.go +++ b/characteristics.go @@ -97,7 +97,7 @@ func (c CharacteristicService) list(val *url.Values, claims Claims) (entity, *ap return &characteristics, nil } -func (c CharacteristicService) get(id int64, dummy string) (entity, *appError) { +func (c CharacteristicService) get(id int64, dummy string, claims Claims) (entity, *appError) { var characteristic Characteristic q := `SELECT c.*, array_agg(m.id) AS measurements, array_agg(st.id) AS strains FROM characteristics c diff --git a/entities.go b/entities.go index 562939f..405bf30 100644 --- a/entities.go +++ b/entities.go @@ -7,7 +7,7 @@ type entity interface { } type getter interface { - get(int64, string) (entity, *appError) + get(int64, string, Claims) (entity, *appError) } type lister interface { diff --git a/handlers.go b/handlers.go index a01b2bc..895cf5b 100644 --- a/handlers.go +++ b/handlers.go @@ -127,7 +127,9 @@ func handleGetter(g getter) errorHandler { return newJSONError(err, http.StatusInternalServerError) } - e, appErr := g.get(id, mux.Vars(r)["genus"]) + claims := getClaims(r) + + e, appErr := g.get(id, mux.Vars(r)["genus"], claims) if appErr != nil { return appErr } diff --git a/helpers.go b/helpers.go index a5471f6..a93eec6 100644 --- a/helpers.go +++ b/helpers.go @@ -118,3 +118,18 @@ func getClaims(r *http.Request) Claims { func canAdd(claims Claims) bool { return claims.Role == "A" || claims.Role == "W" } + +func canEdit(claims Claims, id_list map[int64]int64) []int64 { + id := claims.Sub + role := claims.Role + + out := make([]int64, 0) + + for k, v := range id_list { + if (role == "W" && id == v) || role == "A" { + out = append(out, k) + } + } + + return out +} diff --git a/measurements.go b/measurements.go index a454feb..43532a1 100644 --- a/measurements.go +++ b/measurements.go @@ -138,7 +138,7 @@ func (m MeasurementService) list(val *url.Values, claims Claims) (entity, *appEr return &measurements, nil } -func (m MeasurementService) get(id int64, genus string) (entity, *appError) { +func (m MeasurementService) get(id int64, genus string, claims Claims) (entity, *appError) { var measurement Measurement q := `SELECT m.*, t.text_measurement_name AS text_measurement_type_name, u.symbol AS unit_type_name, te.name AS test_method_name diff --git a/species.go b/species.go index d97117b..2a99e3d 100644 --- a/species.go +++ b/species.go @@ -51,7 +51,14 @@ type Species struct { type ManySpecies []*Species type SpeciesMeta struct { - CanAdd bool `json:"canAdd"` + CanAdd bool `json:"canAdd"` + CanEdit []int64 `json:"canEdit"` +} + +type SpeciesPayload struct { + Species *Species `json:"species"` + Strains *Strains `json:"strains"` + Meta *SpeciesMeta `json:"meta"` } type ManySpeciesPayload struct { @@ -68,6 +75,10 @@ func (s *Species) marshal() ([]byte, error) { return json.Marshal(&SpeciesJSON{Species: s}) } +func (s *SpeciesPayload) marshal() ([]byte, error) { + return json.Marshal(s) +} + func (s *ManySpeciesPayload) marshal() ([]byte, error) { return json.Marshal(s) } @@ -126,10 +137,19 @@ func (s SpeciesService) list(val *url.Values, claims Claims) (entity, *appError) return nil, newJSONError(err, http.StatusInternalServerError) } + edit_list := make(map[int64]int64) + + for _, v := range species { + edit_list[v.Id] = v.CreatedBy + } + payload := ManySpeciesPayload{ Species: &species, Strains: strains, - Meta: &SpeciesMeta{CanAdd: canAdd(claims)}, + Meta: &SpeciesMeta{ + CanAdd: canAdd(claims), + CanEdit: canEdit(claims, edit_list), + }, } return &payload, nil @@ -159,7 +179,7 @@ func strainOptsFromSpecies(opt ListOptions) (*ListOptions, error) { return &ListOptions{Genus: opt.Genus, Ids: relatedStrainIds}, nil } -func (s SpeciesService) get(id int64, genus string) (entity, *appError) { +func (s SpeciesService) get(id int64, genus string, claims Claims) (entity, *appError) { var species Species q := `SELECT sp.*, g.genus_name, array_agg(st.id) AS strains, COUNT(st) AS total_strains, 0 AS sort_order @@ -174,7 +194,29 @@ func (s SpeciesService) get(id int64, genus string) (entity, *appError) { } return nil, newJSONError(err, http.StatusInternalServerError) } - return &species, nil + + opt := ListOptions{Genus: genus, Ids: []int64{id}} + + strains_opt, err := strainOptsFromSpecies(opt) + if err != nil { + return nil, newJSONError(err, http.StatusInternalServerError) + } + + strains, err := listStrains(*strains_opt) + if err != nil { + return nil, newJSONError(err, http.StatusInternalServerError) + } + + payload := SpeciesPayload{ + Species: &species, + Strains: strains, + Meta: &SpeciesMeta{ + CanAdd: canAdd(claims), + CanEdit: canEdit(claims, map[int64]int64{species.Id: species.CreatedBy}), + }, + } + + return &payload, nil } func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError { diff --git a/strains.go b/strains.go index 4e2ea88..f3eb3a7 100644 --- a/strains.go +++ b/strains.go @@ -125,7 +125,7 @@ func listStrains(opt ListOptions) (*Strains, error) { return &strains, nil } -func (s StrainService) get(id int64, genus string) (entity, *appError) { +func (s StrainService) get(id int64, genus string, claims Claims) (entity, *appError) { var strain Strain q := `SELECT st.*, array_agg(m.id) AS measurements, COUNT(m) AS total_measurements, 0 AS sort_order diff --git a/users.go b/users.go index 52693ec..1e6e7b9 100644 --- a/users.go +++ b/users.go @@ -145,7 +145,7 @@ func (u UserService) list(val *url.Values, claims Claims) (entity, *appError) { return &users, nil } -func (u UserService) get(id int64, genus string) (entity, *appError) { +func (u UserService) get(id int64, genus string, claims Claims) (entity, *appError) { var user User q := `SELECT id, email, 'password' AS password, name, role, created_at, updated_at, deleted_at