Ejemplo n.º 1
0
// CloseSession takes a digits auth and converts it into internal auth
func CloseSession(c *echo.Context) (int, interface{}) {
	hauth, ok := c.Get("habloAuth").(*models.HabloAuth)
	if !ok {
		return msg.Forbidden("must include an authorization")
	}

	log.Printf("Awaiting for [HabloAuth = %s] deletion", hauth.ID.Hex())
	err := mangos.Execute(collection, func(c *mgo.Collection) error {
		return c.RemoveId(hauth.ID)
	})
	if err != nil {
		log.Printf("Cannot remove [HabloAuth = %s]: %s", hauth.ID.Hex(), err)
		return msg.InternalError(err)
	}

	log.Printf("Removed [HabloAuth = %s]", hauth.ID.Hex())
	return msg.Ok("see you")
}
Ejemplo n.º 2
0
// Update merges remote object state into database database, overwritting values.
// ID must be the same as session.
func Update(c *echo.Context) (int, interface{}) {
	reqid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	attid, _ := c.Get("digitsID").(int64)

	if reqid == attid {
		var profile models.ProfileUpdate
		var err error

		// Bind body to profile data
		err = c.Bind(&profile)
		if err != nil {
			return msg.BadRequest(err)
		}

		// Valdate profile data
		err = validator.Validate(profile)
		if err != nil {
			return msg.BadRequest(err)
		}

		// If email is written, require confirmation again
		if profile.Email != "" {
			profile.RequiresConfirm = true
		}

		// Execute update
		log.Printf("Awaiting for [Profile = %d] update", reqid)
		err = mangos.Execute(collection, func(c *mgo.Collection) error {
			return c.Update(bson.M{"_id": reqid}, bson.M{"$set": profile})
		})
		if err != nil {
			log.Printf("Cannot update [Profile = %d]: %s", reqid, err)
			return msg.InternalError(err)
		}

		log.Printf("Updated Profile ->\n%s", msg.Detail(profile))
		return msg.Ok("updated")
	}

	return msg.Forbidden("the id does not belongs to user")
}
Ejemplo n.º 3
0
// Delete removes the indicated object from database.
// ID must be the same as session.
func Delete(c *echo.Context) (int, interface{}) {
	var err error
	reqid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	attid, _ := c.Get("digitsID").(int64)

	if reqid == attid {
		// Execute delete
		log.Printf("Awaiting for [Profile = %d] delete", reqid)
		err = mangos.Execute(collection, func(c *mgo.Collection) error {
			return c.RemoveId(reqid)
		})
		if err != nil {
			log.Printf("Cannot delete [Profile = %d]: %s", reqid, err)
			return msg.InternalError(err)
		}

		log.Printf("Deleted [Profile = %d]", reqid)
		return msg.Ok("deleted")
	}

	return msg.Forbidden("the id does not belongs to user")
}