Species editable permissions
This commit is contained in:
parent
0b029f9098
commit
630fe07554
9 changed files with 70 additions and 11 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -7,7 +7,7 @@ type entity interface {
|
|||
}
|
||||
|
||||
type getter interface {
|
||||
get(int64, string) (entity, *appError)
|
||||
get(int64, string, Claims) (entity, *appError)
|
||||
}
|
||||
|
||||
type lister interface {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
15
helpers.go
15
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
48
species.go
48
species.go
|
@ -52,6 +52,13 @@ type ManySpecies []*Species
|
|||
|
||||
type SpeciesMeta struct {
|
||||
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 {
|
||||
|
|
|
@ -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
|
||||
|
|
2
users.go
2
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
|
||||
|
|
Reference in a new issue