Beispiel #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)
}
Beispiel #2
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)
	}
}
Beispiel #3
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))
}
Beispiel #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))
}
Beispiel #5
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))
}
Beispiel #6
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())
}
Beispiel #7
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)

}
Beispiel #8
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,
	})

}
Beispiel #9
0
// ListAllUsers returns all the users
//
// GET /users
//
func (c *UsersController) ListAllUsers(ctx *gin.Context) {
	users, err := services.ListAllUsers()
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}
	helpers.JSONResponseCollection(ctx, presenters.UserCollectionPresenter(users))
}
Beispiel #10
0
// ListPublicConversations returns the list of all public conversations
//
// GET /conversations
//
func (c *ConversationsController) ListPublicConversations(ctx *gin.Context) {

	conversations, err := services.ListPublicConversations(nil)
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseObject(ctx, presenters.ConversationCollectionPresenter(conversations))
}
Beispiel #11
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))
}
Beispiel #12
0
// ListUserOrganizations lists all organizations for a specific user
//
// GET /users/:username/orgs
//
func (c *UsersController) ListUserOrganizations(ctx *gin.Context) {
	user := getCurrentUser(ctx)

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

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

	helpers.JSONResponseObject(ctx, presenters.OrganizationCollectionPresenter(orgs))
}
Beispiel #13
0
// GetConversation returns a specific conversation
//
// GET /conversations/:id
//
func (c *ConversationsController) GetConversation(ctx *gin.Context) {

	ctxConversation := getConversationFromContext(ctx)

	conversationService, err := services.NewConversationService(ctxConversation, getCurrentUser(ctx))
	if err != nil {
		if c.WriteTrace {
			c.Logger.Printf("Failed to create ConversationService for org: %v", ctxConversation)
		}
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	conversation, err := conversationService.GetConversation()
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseObject(ctx, presenters.ConversationPresenter(conversation))
}
Beispiel #14
0
// ListMyOrganizationMemberships list your organization memberships for the authenticated users
//
// GET /user/memberships/orgs
//
func (c *UsersController) ListMyOrganizationMemberships(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
	}

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

	helpers.JSONResponseObject(ctx, presenters.MemberCollectionPresenter(members))
}
Beispiel #15
0
// RegisterNewUser creates a new user account
//
// POST /users
//
func (c *UsersController) RegisterNewUser(ctx *gin.Context) {
	var json bindings.RegisterNewUser
	err := ctx.Bind(&json)
	if err != nil {
		helpers.JSONResponseValidationFailed(ctx, err)
		return
	}

	user, err := services.RegisterNewUser(json)
	if err != nil {
		if err == services.ErrNamespaceAlreadyExists {
			helpers.JSONErrorf(ctx, http.StatusBadRequest, "Username already exists")
		} else {
			helpers.JSONResponseInternalServerError(ctx, err)
		}
		return
	}

	helpers.JSONResponseObject(ctx, presenters.UserPresenter(user))
}
Beispiel #16
0
// CreateOrganization creates a new Organization and makes the current authenticated user the owner
//
// POST /orgs
//
func (c *OrganizationsController) CreateOrganization(ctx *gin.Context) {
	var json bindings.CreateOrganization
	err := ctx.Bind(&json)
	if err != nil {
		helpers.JSONResponseValidationFailed(ctx, err)
		return
	}

	// creates the organization and set the current user as part of Owners Team
	org, err := services.CreateOrganization(json, getCurrentUser(ctx))
	if err != nil {
		if err == services.ErrNamespaceAlreadyExists {
			helpers.JSONErrorf(ctx, http.StatusBadRequest, "Organization name already exists")
		} else {
			helpers.JSONResponseInternalServerError(ctx, err)
		}
		return
	}

	helpers.JSONResponseObject(ctx, presenters.OrganizationPresenter(org))
}