Ejemplo n.º 1
0
// DeleteEmail deletes email address for current user
//
// DELETE /user/emails
//
func (c *UsersController) DeleteEmail(ctx *gin.Context) {
	var json bindings.UpdateEmail
	err := ctx.Bind(&json)
	if err != nil {
		helpers.JSONResponseValidationFailed(ctx, err)
	}

	user := getCurrentUser(ctx)
	accountService, err := services.NewAccountService(user)
	if err != nil {
		if c.WriteTrace {
			c.Logger.Printf("Failed to create AccountService for user: %v", user)
		}
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	err = accountService.RemoveEmailAddress(json)
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseOK(ctx, user.ListOfEmails())
}
Ejemplo n.º 2
0
// ChangePassword updates the password for authenticated user
//
// POST /change/password
//
// Params:
//
// type ChangePassword struct {
// 	OldPassword string `json:"old_password" binding:"required"`
// 	NewPassword string `json:"new_password" binding:"required"`
// }
//
func (c *UsersController) ChangePassword(ctx *gin.Context) {

	var json bindings.ChangePassword
	if err := ctx.Bind(&json); err != nil {
		helpers.JSONResponseValidationFailed(ctx, err)
		return
	}

	user := getCurrentUser(ctx)
	accountService, err := services.NewAccountService(user)
	if err != nil {
		if c.WriteTrace {
			c.Logger.Printf("Failed to create AccountService for user: %v", user)
		}
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	ok, err := accountService.ChangePassword(json)
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	if !ok {
		helpers.JSONErrorf(ctx, http.StatusBadRequest, "Failed to change password for user: %s", user.Username)
		return
	}

	helpers.JSONResponseOK(ctx)

}
Ejemplo n.º 3
0
// AuthorizeUser performs the authentication for the API user
//
// GET /authorize
//
func (c *SessionController) AuthorizeUser(ctx *gin.Context) {
	var json bindings.AuthorizeUser
	err := ctx.Bind(&json)
	if err != nil {
		// Missing authentication credentials
		helpers.JSONResponseValidationFailed(ctx, err)
		return
	}

	user, err := services.Auth.AuthorizeUser(json)
	if err != nil {
		helpers.JSONForbidden(ctx, "Invalid authentication credentials")
		return
	}

	tokenFields, err := services.Auth.GenerateToken(user)
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseOK(ctx, gin.H{
		"user":   presenters.UserPresenter(user),
		"tokens": tokenFields,
	})

}
Ejemplo n.º 4
0
// ListMyEmails lists email addresses for current user
//
// GET /user/emails
//
func (c *UsersController) ListMyEmails(ctx *gin.Context) {
	user := getCurrentUser(ctx)
	helpers.JSONResponseOK(ctx, user.ListOfEmails())
}