Пример #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
// 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)
}
Пример #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,
	})

}