Esempio n. 1
0
func (handler APIHandler) FeatureCreate(w http.ResponseWriter, r *http.Request) {
	var feature m.FeatureFlag

	if err := json.NewDecoder(r.Body).Decode(&feature); err != nil {
		writeUnprocessableEntity(err, w)
		return
	}

	if err := feature.Validate(); err != nil {
		writeMessage(400, "invalid_feature", err.Error(), w)
		return
	}

	err := handler.FeatureService.AddFeature(feature)
	if err != nil && err.Error() == "Feature already exists" {
		writeMessage(400, "invalid_feature", err.Error(), w)
		return
	}

	w.Header().Set("Content-Type", getJsonHeader())
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(feature); err != nil {
		panic(err)
	}
}
Esempio n. 2
0
func hasAccessToFeature(feature m.FeatureFlag, ar AccessRequest) bool {
	// Handle trivial case
	if feature.IsEnabled() {
		return true
	}

	// Access thanks to a group?
	if len(ar.Groups) > 0 {
		for _, group := range ar.Groups {
			if feature.GroupHasAccess(group) {
				return true
			}
		}
	}

	// Access thanks to the user?
	if ar.User > 0 {
		return feature.UserHasAccess(ar.User)
	}

	return false
}