Ejemplo n.º 1
0
func RecordsQueryHandler(c echo.Context) error {
	session := session.NewSession(c)

	if !session.AuthFailed {
		endpoint := session.GetEndpoint()
		request := session.GetParam("request").(map[string]interface{})

		bypass_plugin := false

		if session.User.SuperUser && request["where"].(map[string]interface{})["$bypass_plugin"] != nil {
			bypass_plugin = true
			delete(request["where"].(map[string]interface{}), "$bypass_plugin")
		}

		if !bypass_plugin && plugin.CheckPlugin("query", endpoint) {
			plugin.RunPlugin("query", endpoint, session)
		} else {
			records, count := endpoint.FindReadRecordsWhere(request["where"].(map[string]interface{}), session.User)
			session.SetRecordsAndCalculateCapabilitiesForUser(endpoint.Name, records, session.User)
			session.SetRecordCount(count)
		}
	}
	session.Write()

	return nil
}
Ejemplo n.º 2
0
func RecordsTriggerHandler(c echo.Context) error {
	session := session.NewSession(c)

	if !session.AuthFailed {
		endpoint := session.GetEndpoint()
		request := session.GetParam("request").(map[string]interface{})

		bypass_plugin := false

		if session.User.SuperUser && request["content"] != nil && request["content"].(map[string]interface{})["$bypass_plugin"] != nil {
			bypass_plugin = true
			delete(request["content"].(map[string]interface{}), "$bypass_plugin")
		}

		if !bypass_plugin && plugin.CheckPlugin("trigger", endpoint) {
			plugin.RunPlugin("trigger", endpoint, session)
		} else {
			id := bson.ObjectIdHex(session.GetParam("record_id").(string))
			record := endpoint.FindWriteRecordById(id, session.User)
			record.UpdateReferenceMaps()
			record.SaveAndTriggerCreate(endpoint.Name, false)

			go record.FindAndExecuteTriggerForEvent(endpoint.Name + ":update")
		}
	}

	session.Write()

	return nil
}
Ejemplo n.º 3
0
func RecordsDeleteHandler(c echo.Context) error {
	session := session.NewSession(c)

	if !session.AuthFailed {
		endpoint := session.GetEndpoint()
		request := session.GetParam("request").(map[string]interface{})

		bypass_plugin := false

		if session.User.SuperUser && request["content"] != nil && request["content"].(map[string]interface{})["$bypass_plugin"] != nil {
			bypass_plugin = true
			delete(request["content"].(map[string]interface{}), "$bypass_plugin")
		}

		if !bypass_plugin && plugin.CheckPlugin("delete", endpoint) {
			plugin.RunPlugin("delete", endpoint, session)
		} else {
			id := bson.ObjectIdHex(session.GetParam("record_id").(string))
			record := endpoint.FindDestroyRecordById(id, session.User)
			record.DeleteAndTrigger(endpoint.Name, !session.TriggerSession)
		}
	}

	session.Write()

	return nil
}
Ejemplo n.º 4
0
func RecordsUpdateHandler(c echo.Context) error {
	session := session.NewSession(c)

	if !session.AuthFailed {
		endpoint := session.GetEndpoint()
		request := session.GetParam("request").(map[string]interface{})

		bypass_plugin := false

		if session.User.SuperUser && request["content"].(map[string]interface{})["$bypass_plugin"] != nil {
			bypass_plugin = true
			delete(request["content"].(map[string]interface{}), "$bypass_plugin")
		}

		if !bypass_plugin && plugin.CheckPlugin("update", endpoint) {
			plugin.RunPlugin("update", endpoint, session)
		} else {
			id := bson.ObjectIdHex(session.GetParam("record_id").(string))
			record := endpoint.FindWriteRecordById(id, session.User)

			if request != nil && request["content"] != nil {
				request_content := request["content"].(map[string]interface{})
				request_permissions := request["permissions"].(map[string]interface{})

				var trigger = false

				if request_content["$trigger"] != nil {
					trigger = request_content["$trigger"].(bool)
					delete(request_content, "$trigger")
				} else {
					trigger = !session.TriggerSession
				}

				if session.User.SuperUser != true {
					if request_content["protected"] != nil {
						request_content["protected"] = record.Content["protected"]
					}
				}

				record.Content = request_content
				record.Permissions.UpdatePermissions(request_permissions)

				record.UpdateReferenceMaps()
				record.StripReferenceData()
				record.SaveAndTrigger(endpoint.Name, trigger)

				session.SetRecordsAndCalculateCapabilitiesForUser(session.GetEndpoint().Name, []model.Record{record}, session.User)
			}

			session.SetRecordsAndCalculateCapabilitiesForUser(session.GetEndpoint().Name, []model.Record{record}, session.User)
		}
	}

	session.Write()

	return nil
}
Ejemplo n.º 5
0
func FilesGetHandler(c echo.Context) error {

	// Create the User Session
	session := session.NewSession(c)

	// Validate that the request signature is valid
	session.ValidateFileRequest()

	redirect := false

	if !session.AuthFailed {
		// Get the endpoint for that record
		endpoint := session.GetEndpoint()

		if plugin.CheckPlugin("get_file", endpoint) {
			plugin.RunPlugin("get_file", endpoint, session)
		} else {
			// Get the ID of the record that owns the file
			record_id := bson.ObjectIdHex(session.GetParam("record_id").(string))

			// Get the record from the endpoint collection
			record := endpoint.FindReadRecordById(record_id, session.User)

			// Check if the record exists
			if record.Id != bson.ObjectId("") {

				// Get the ID of the file
				file_id := bson.ObjectIdHex(session.GetParam("file_id").(string))

				// Get the file from the database
				file := record.FindFileById(file_id)

				// Validate that the file exists
				if file.Id != bson.ObjectId("") {

					// Redirect to the file path
					c.Redirect(301, file.DownloadURL())
					redirect = true

					// Log the request
					session.LogRequest()
				}
			}

			if !redirect {
				session.Write()
			}
		}
	}

	return nil
}
Ejemplo n.º 6
0
func RecordsBulkQueryHandler(c echo.Context) error {
	s := session.NewSession(c)

	if !s.AuthFailed {

		request := s.GetParam("request").(map[string]interface{})
		requests := request["requests"].([]interface{})

		c := make(chan bool, len(requests))

		for _, request := range requests {
			go func(request interface{}, session *session.Session) {
				endpoint := session.GetApplication().FindEndpointByName(request.(map[string]interface{})["endpoint"].(string))
				guid := request.(map[string]interface{})["guid"].(string)

				t := time.Now()

				if plugin.CheckPlugin("query", endpoint) {
					s := session.CopySessionForBulkRequest(guid)
					plugin.RunPlugin("query", endpoint, s)
					session.CopyResponseFromSessionForBulkRequest(s, guid, endpoint.Name)
				} else {
					r := request.(map[string]interface{})["request"].(map[string]interface{})
					records, count := endpoint.FindReadRecordsWhere(r["where"].(map[string]interface{}), session.User)
					session.SetRecordsAndCalculateCapabilitiesForUserForGuid(endpoint.Name, records, session.User, guid)
					session.SetRecordCountForGuid(count, guid)
				}

				logger.Log("Bulk Loaded:", "/"+endpoint.ApplicationId.Hex()+"/"+endpoint.Name+"/query", time.Now().Sub(t).String(), session.User.Id.Hex())
				c <- true
			}(request, &s)
		}

		i := 0

		for {
			<-c
			i++
			if i >= len(requests) {
				break
			}
		}
	}

	s.Write()

	return nil
}
Ejemplo n.º 7
0
func RecordsCreateHandler(c echo.Context) error {
	session := session.NewSession(c)

	if !session.AuthFailed {
		endpoint := session.GetEndpoint()
		request := session.GetParam("request").(map[string]interface{})

		bypass_plugin := false

		if session.User.SuperUser && request["content"].(map[string]interface{})["$bypass_plugin"] != nil {
			bypass_plugin = true
			delete(request["content"].(map[string]interface{}), "$bypass_plugin")
		}

		if !bypass_plugin && plugin.CheckPlugin("create", endpoint) {
			plugin.RunPlugin("create", endpoint, session)
		} else {
			if endpoint.CanCreate(session.User) {
				record := endpoint.NewRecord()

				if session.User.Id != bson.ObjectId("") {
					record.CreatedBy = session.User.Id
				}

				if request != nil && request["content"] != nil {

					var trigger = false

					if request["content"].(map[string]interface{})["$trigger"] != nil {
						trigger = request["content"].(map[string]interface{})["$trigger"].(bool)
						delete(request["content"].(map[string]interface{}), "$trigger")
					} else {
						trigger = !session.TriggerSession
					}

					if request["content"].(map[string]interface{})["$created_by"] != nil &&
						session.User.SuperUser == true {
						record.CreatedBy = bson.ObjectIdHex(request["content"].(map[string]interface{})["$created_by"].(string))
						delete(request["content"].(map[string]interface{}), "$created_by")
					}

					request_content := request["content"].(map[string]interface{})
					request_permissions := request["permissions"].(map[string]interface{})

					record.Content = request_content
					record.Permissions.UpdatePermissions(request_permissions)

					record.SaveAndTrigger("", false)
					record.UpdateReferenceMaps()
					record.SaveAndTriggerCreate(endpoint.Name, trigger)

					session.SetRecordsAndCalculateCapabilitiesForUser(session.GetEndpoint().Name, []model.Record{record}, session.User)
				}
			}
		}
	}

	session.Write()

	return nil
}