示例#1
0
// AddOrUpdateMembership adds the user as member to the organization. The authenticated user must be an organization owner.
// If the user is not yet a member of the organization, the membership will be pending until the user accepts the invitation. Otherwise, if
// the user is already a member of the organization, this method will update the role of the member within the organization.
//
// PUT /orgs/:org/memberships/:username
//
func (c *OrganizationsController) AddOrUpdateMembership(ctx *gin.Context) {

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

	org := getOrganizationFromContext(ctx)
	orgService, err := services.NewOrganizationService(org, getCurrentUser(ctx))
	if err != nil {
		if c.WriteTrace {
			c.Logger.Printf("Failed to create OrganizationService for org: %v", org)
		}
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	// retrieves the user that was found based on username parameter
	user := getUserFromContext(ctx)

	member, err := orgService.AddOrUpdateMembership(user, json)
	if err != nil {
		if err == services.ErrNotAnOrganizationOwner {
			helpers.JSONForbidden(ctx, err.Error())
		} else {
			helpers.JSONResponseInternalServerError(ctx, err)
		}
		return
	}

	helpers.JSONResponseObject(ctx, presenters.MemberPresenter(member))
}
示例#2
0
// 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))
}