예제 #1
0
파일: user.go 프로젝트: messagedb/messagedb
// EditMyOrganizationMembership edit your organization membership for the authenticated user
//
// PATCH /user/memberships/orgs/:org
//
func (c *UsersController) EditMyOrganizationMembership(ctx *gin.Context) {
	var json bindings.EditMyMembership
	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
	}

	org := getOrganizationFromContext(ctx)

	member, err := accountService.EditMyMembership(org.ID, json)
	if err != nil {
		if err == ErrNotFound {
			helpers.JSONErrorf(ctx, http.StatusNotFound, "No membership found for this organization")
			return
		}
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseObject(ctx, presenters.MemberPresenter(member))
}
예제 #2
0
파일: user.go 프로젝트: messagedb/messagedb
// 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())
}
예제 #3
0
파일: user.go 프로젝트: messagedb/messagedb
// 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)

}
예제 #4
0
파일: user.go 프로젝트: messagedb/messagedb
// ListMyOrganizations list the active organizations for the authenticated user
//
// GET /user/orgs
//
func (c *UsersController) ListMyOrganizations(ctx *gin.Context) {
	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
	}

	orgs, err := accountService.ListMyOrganizations()
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseObject(ctx, presenters.OrganizationCollectionPresenter(orgs))
}