Ejemplo n.º 1
0
func (a *ApplicationController) CreateApplicationHandler(c *gin.Context) {
	body := &ApplicationModel{}

	if err := c.Bind(body); err != nil {
		RestError(c, err)
		return
	}

	if body.Name == "" {
		RestErrorInvalidBody(c)
		return
	}

	d := db.NewAppsDbService(c.MustGet("user").(string))

	username := c.MustGet("user").(string)
	doc := JSON{
		"name":      body.Name,
		"owner":     username,
		"types":     []string{"users"},
		"createdAt": time.Now(),
		"masterKey": utils.GetCleanUUID(),
	}

	if err := d.Insert(doc); err != nil {
		RestError(c, err)
		return
	}

	RespondId(doc["_id"], c)
}
Ejemplo n.º 2
0
func (d *dbService) Insert(doc bson.M) error {
	if _, ok := doc["_id"]; !ok {
		doc["_id"] = utils.GetCleanUUID()
	}

	session, collection := d.GetCollection()

	defer session.Close()
	return collection.Insert(doc)
}
Ejemplo n.º 3
0
func (a *ApplicationController) CreateApplicationHandler(w rest.ResponseWriter, r *rest.Request) {
	body := &ApplicationModel{}

	if err := r.DecodeJsonPayload(body); err != nil {
		RestError(w, err)
		return
	}

	if body.Name == "" {
		RestErrorInvalidBody(w)
		return
	}

	db := neutrino.NewAppsDbService(r.Env["user"].(string))

	username := r.Env["user"].(string)
	doc := bson.M{
		"name":      body.Name,
		"owner":     username,
		"types":     []string{"users"},
		"createdAt": time.Now(),
		"keys": bson.M{ //TODO:
			"Master Key": bson.M{
				"key":  utils.GetCleanUUID(),
				"name": "Master Key",
				"permissions": bson.M{
					"types": bson.M{
						"read":  true,
						"write": true,
					},
				},
			},
		},
	}

	if err := db.Insert(doc); err != nil {
		RestError(w, err)
		return
	}

	RespondId(doc["_id"], w)
}