Exemple #1
0
// RemoveMembership removes the user's membership from the organization. The authenticated user must be an organization owner.
//
// DELETE /orgs/:org/memberships/:username
//
func (c *OrganizationsController) RemoveMembership(ctx *gin.Context) {

	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)

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

	ctx.JSON(http.StatusNoContent, nil)
}
Exemple #2
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))
}
Exemple #3
0
// CheckPublicMembership checks if a user is a public member of the organization
//
// GET /orgs/:org/public_members/:username
//
func (c *OrganizationsController) CheckPublicMembership(ctx *gin.Context) {

	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)

	check, err := orgService.CheckPublicMembership(user)
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	if check {
		ctx.JSON(http.StatusNoContent, nil)
	} else {
		ctx.JSON(http.StatusNotFound, nil)
	}
}
Exemple #4
0
// EditOrganization modifes an organization record
//
// PATCH /orgs/:org
//
func (c *OrganizationsController) EditOrganization(ctx *gin.Context) {

	var json bindings.UpdateOrganization
	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
	}

	org, err = orgService.UpdateOrganization(json)
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseObject(ctx, presenters.OrganizationPresenter(org))
}
Exemple #5
0
// ListPublicMembers retusn the list of all public members. Each user can control the visibility of their membership. This will
// only includes the members that have chosen to do so.
//
// GET /orgs/:org/public_members
//
func (c *OrganizationsController) ListPublicMembers(ctx *gin.Context) {

	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
	}

	members, err := orgService.GetPublicMembers()
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseCollection(ctx, presenters.MemberCollectionPresenter(members))
}