Esempio n. 1
0
//GetSchema returns the schema filtered and trimmed for a specific user or nil when the user shouldn't see it at all
func GetSchema(s *schema.Schema, authorization schema.Authorization) (result *schema.Resource, err error) {
	manager := schema.GetManager()
	metaschema, _ := manager.Schema("schema")
	policy, _ := manager.PolicyValidate("read", s.GetPluralURL(), authorization)
	if policy == nil {
		return
	}
	if s.IsAbstract() {
		return
	}
	rawSchema := s.JSON()
	filteredSchema := util.ExtendMap(nil, s.JSONSchema)
	rawSchema["schema"] = filteredSchema
	schemaProperties, schemaPropertiesOrder, schemaRequired := policy.FilterSchema(
		util.MaybeMap(s.JSONSchema["properties"]),
		util.MaybeStringList(s.JSONSchema["propertiesOrder"]),
		util.MaybeStringList(s.JSONSchema["required"]))

	filteredSchema["properties"] = schemaProperties
	filteredSchema["propertiesOrder"] = schemaPropertiesOrder
	filteredSchema["required"] = schemaRequired

	result, err = schema.NewResource(metaschema, rawSchema)
	if err != nil {
		log.Warning("%s %s", result, err)
		return
	}
	return
}
Esempio n. 2
0
func (tl *transactionEventLogger) logEvent(eventType string, resource *schema.Resource, version int64) error {
	schemaManager := schema.GetManager()
	eventSchema, ok := schemaManager.Schema("event")
	if !ok {
		return fmt.Errorf("event schema not found")
	}

	if resource.Schema().Metadata["nosync"] == true {
		log.Debug("skipping event logging for schema: %s", resource.Schema().ID)
		return nil
	}
	body, err := resource.JSONString()
	if err != nil {
		return fmt.Errorf("Error during event resource deserialisation: %s", err.Error())
	}
	eventResource, err := schema.NewResource(eventSchema, map[string]interface{}{
		"type":      eventType,
		"path":      resource.Path(),
		"version":   version,
		"body":      body,
		"timestamp": int64(time.Now().Unix()),
	})
	tl.eventLogged = true
	return tl.Transaction.Create(eventResource)
}
Esempio n. 3
0
//GetSchema returns the schema filtered and trimmed for a specific user or nil when the user shouldn't see it at all
func GetSchema(s *schema.Schema, authorization schema.Authorization) (result *schema.Resource, err error) {
	manager := schema.GetManager()
	metaschema, _ := manager.Schema("schema")
	policy, _ := manager.PolicyValidate("read", s.GetPluralURL(), authorization)
	if policy == nil {
		return
	}
	originalRawSchema := s.RawData.(map[string]interface{})
	rawSchema := map[string]interface{}{}
	for key, value := range originalRawSchema {
		rawSchema[key] = value
	}
	originalSchema := originalRawSchema["schema"].(map[string]interface{})
	schemaSchema := map[string]interface{}{}
	for key, value := range originalSchema {
		schemaSchema[key] = value
	}
	rawSchema["schema"] = schemaSchema
	originalProperties := originalSchema["properties"].(map[string]interface{})
	schemaProperties := map[string]interface{}{}
	for key, value := range originalProperties {
		schemaProperties[key] = value
	}
	var schemaPropertiesOrder []interface{}
	if _, ok := originalSchema["propertiesOrder"]; ok {
		originalPropertiesOrder := originalSchema["propertiesOrder"].([]interface{})
		for _, value := range originalPropertiesOrder {
			schemaPropertiesOrder = append(schemaPropertiesOrder, value)
		}
	}
	var schemaRequired []interface{}
	if _, ok := originalSchema["required"]; ok {
		originalRequired := originalSchema["required"].([]interface{})
		for _, value := range originalRequired {
			schemaRequired = append(schemaRequired, value)
		}
	}
	schemaProperties, schemaPropertiesOrder, schemaRequired = policy.MetaFilter(schemaProperties, schemaPropertiesOrder, schemaRequired)
	schemaSchema["properties"] = schemaProperties
	schemaSchema["propertiesOrder"] = schemaPropertiesOrder
	schemaSchema["required"] = schemaRequired
	result, err = schema.NewResource(metaschema, rawSchema)
	if err != nil {
		log.Warning("%s %s", result, err)
		return
	}
	return
}
Esempio n. 4
0
File: sql.go Progetto: vozhyk-/gohan
func (tx *Transaction) decodeRows(s *schema.Schema, rows *sqlx.Rows, list []*schema.Resource) ([]*schema.Resource, error) {
	for rows.Next() {
		resourceData := map[string]interface{}{}
		data := map[string]interface{}{}
		rows.MapScan(data)

		var resource *schema.Resource
		tx.decode(s, s.GetDbTableName(), data, resourceData)
		resource, err := schema.NewResource(s, resourceData)
		if err != nil {
			return nil, fmt.Errorf("Failed to decode rows")
		}
		list = append(list, resource)
	}
	return list, nil
}
Esempio n. 5
0
		r0, r1        *schema.Resource
	)

	var ()

	BeforeEach(func() {
		manager = schema.GetManager()
		s, ok = manager.Schema("test")
		Expect(ok).To(BeTrue())

		fakeResources = []map[string]interface{}{
			map[string]interface{}{"tenant_id": "t0", "test_string": "str0", "test_bool": false},
			map[string]interface{}{"tenant_id": "t1", "test_string": "str1", "test_bool": true},
		}

		r0, err = schema.NewResource(s, fakeResources[0])
		Expect(err).ToNot(HaveOccurred())
		r1, err = schema.NewResource(s, fakeResources[1])
		Expect(err).ToNot(HaveOccurred())

	})

	Describe("gohan_db_list", func() {
		Context("When valid minimum parameters are given", func() {
			It("returns the list ordered by id", func() {
				extension, err := schema.NewExtension(map[string]interface{}{
					"id": "test_extension",
					"code": `
					  gohan_register_handler("test_event", function(context){
					    var tx = context.transaction;
					    context.resp = gohan_db_list(
Esempio n. 6
0
//List resources in the db
func (tx *Transaction) List(s *schema.Schema, filter map[string]interface{}, pg *pagination.Paginator) (list []*schema.Resource, total uint64, err error) {
	db := tx.db
	db.load()
	table := db.getTable(s)
	for _, rawData := range table {
		data := rawData.(map[string]interface{})
		var resource *schema.Resource
		resource, err = schema.NewResource(s, data)
		if err != nil {
			log.Warning("%s %s", resource, err)
			return
		}
		valid := true
		if filter != nil {
			for key, value := range filter {
				if data[key] == nil {
					continue
				}
				property, err := s.GetPropertyByID(key)
				if err != nil {
					continue
				}
				switch value.(type) {
				case string:
					if property.Type == "boolean" {
						dataBool, err1 := strconv.ParseBool(data[key].(string))
						valueBool, err2 := strconv.ParseBool(value.(string))
						if err1 != nil || err2 != nil || dataBool != valueBool {
							valid = false
						}
					} else if data[key] != value {
						valid = false
					}
				case []string:
					if property.Type == "boolean" {
						v, _ := strconv.ParseBool(data[key].(string))
						if !boolInSlice(v, value.([]string)) {
							valid = false
						}
					}
					if !stringInSlice(fmt.Sprintf("%v", data[key]), value.([]string)) {
						valid = false
					}
				default:
					if data[key] != value {
						valid = false
					}
				}
			}
		}
		if valid {
			list = append(list, resource)
		}

		if pg != nil {
			sort.Sort(byPaginator{list, pg})
			if pg.Limit > 0 {
				list = list[:pg.Limit]
			}
		}
	}
	total = uint64(len(list))
	return
}