Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Authentication API issues #3467

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions litmus-portal/authentication/api/handlers/rest/project_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"litmus/litmus-portal/authentication/pkg/services"
"litmus/litmus-portal/authentication/pkg/utils"
"litmus/litmus-portal/authentication/pkg/validations"
"net/http"
"strconv"
"time"

Expand Down Expand Up @@ -49,6 +50,16 @@ func GetUserWithProject(service services.ApplicationService) gin.HandlerFunc {
func GetProject(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
projectID := c.Param("project_id")

err := validations.RbacValidator(c.MustGet("uid").(string), projectID,
validations.MutationRbacRules["getProject"], string(entities.AcceptedInvitation), service)
if err != nil {
log.Warn(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized],
presenter.CreateErrorResponse(utils.ErrUnauthorized))
return
}

project, err := service.GetProjectByProjectID(projectID)
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -220,9 +231,9 @@ func CreateProject(service services.ApplicationService) gin.HandlerFunc {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}

// checking if project name is empty
if userRequest.ProjectName == "" {
c.JSON(400, gin.H{"message": "project name can't be empty"})
c.JSON(utils.ErrorStatusCodes[utils.ErrEmptyProjectName], presenter.CreateErrorResponse(utils.ErrEmptyProjectName))
return
}

Expand Down Expand Up @@ -324,12 +335,12 @@ func SendInvitation(service services.ApplicationService) gin.HandlerFunc {
return
}
if member.Role == nil {
c.JSON(400, gin.H{"message": "Enter a vaild role"})
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRole], presenter.CreateErrorResponse(utils.ErrInvalidRole))
return
}

if *member.Role != entities.RoleEditor && *member.Role != entities.RoleViewer {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRole], presenter.CreateErrorResponse(utils.ErrInvalidRole))
return
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code block from 337 to 345 can be replaced with this:

Suggested change
// Validating member role
if mem.Role == nil || ( *mem.Role != entities.RoleProjectEditor && *mem.Role != entities.RoleProjectViewer) {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRole], presenter.CreateErrorResponse(utils.ErrInvalidRole))
return
}

Expand All @@ -355,7 +366,7 @@ func SendInvitation(service services.ApplicationService) gin.HandlerFunc {
}

if invitation == entities.AcceptedInvitation {
c.JSON(400, gin.H{"message": "user is already a member of this project"})
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], gin.H{"message": "user is already a member of this project"})
return
} else if invitation == entities.PendingInvitation || invitation == entities.DeclinedInvitation || invitation == entities.ExitedProject {
err = service.UpdateInvite(member.ProjectID, member.UserID, entities.PendingInvitation, member.Role)
Expand All @@ -364,7 +375,7 @@ func SendInvitation(service services.ApplicationService) gin.HandlerFunc {
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}
c.JSON(200, gin.H{"message": "Invitation sent successfully"})
c.JSON(http.StatusOK, gin.H{"message": "Invitation sent successfully"})
return
}

Expand Down Expand Up @@ -510,6 +521,11 @@ func RemoveInvitation(service services.ApplicationService) gin.HandlerFunc {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}
if member.UserID == "" {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}

err = validations.RbacValidator(c.MustGet("uid").(string), member.ProjectID,
validations.MutationRbacRules["removeInvitation"],
string(entities.AcceptedInvitation),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ func UpdateUserState(service services.ApplicationService) gin.HandlerFunc {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}
if userRequest.IsDeactivate == nil {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}

var adminUser entities.User
adminUser.UserName = c.MustGet("username").(string)
Expand Down
2 changes: 1 addition & 1 deletion litmus-portal/authentication/pkg/entities/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type UserPassword struct {
// UpdateUserState defines structure to deactivate or reactivate user
type UpdateUserState struct {
Username string `json:"username"`
IsDeactivate bool `json:"is_deactivate"`
IsDeactivate *bool `json:"is_deactivate"`
}

// APIStatus defines structure for APIroute status
Expand Down
9 changes: 7 additions & 2 deletions litmus-portal/authentication/pkg/project/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ func (r repository) GetProjectsByUserID(userID string, isOwner bool) ([]*entitie
{"members", bson.D{
{"$elemMatch", bson.D{
{"user_id", userID},
{"invitation", bson.D{
{"$ne", entities.DeclinedInvitation},
{"$and", bson.A{
bson.D{{"invitation", bson.D{
{"$ne", entities.DeclinedInvitation},
}}},
bson.D{{"invitation", bson.D{
{"$ne", entities.ExitedProject},
}}},
}},
}},
}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (a applicationService) UpdateStateTransaction(userRequest entities.UpdateUs

var deactivateTime string

if userRequest.IsDeactivate {
if *userRequest.IsDeactivate {
deactivateTime = strconv.FormatInt(time.Now().Unix(), 10)

// Checking if user is already deactivated
Expand All @@ -48,7 +48,7 @@ func (a applicationService) UpdateStateTransaction(userRequest entities.UpdateUs
}

// Updating details in user collection
err = a.UpdateUserState(userRequest.Username, userRequest.IsDeactivate, deactivateTime)
err = a.UpdateUserState(userRequest.Username, *userRequest.IsDeactivate, deactivateTime)
if err != nil {
log.Info(err)
return utils.ErrServerError
Expand Down
6 changes: 6 additions & 0 deletions litmus-portal/authentication/pkg/utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var (
ErrUpdatingAdmin AppError = errors.New("cannot remove admin")
ErrUserDeactivated AppError = errors.New("your account has been deactivated")
ErrUserAlreadyDeactivated AppError = errors.New("user already deactivated")
ErrEmptyProjectName AppError = errors.New("invalid project name")
ErrInvalidRole AppError = errors.New("invalid role")
)

// ErrorStatusCodes holds the http status codes for every AppError
Expand All @@ -33,6 +35,8 @@ var ErrorStatusCodes = map[AppError]int{
ErrUpdatingAdmin: 400,
ErrUserDeactivated: 400,
ErrUserAlreadyDeactivated: 400,
ErrEmptyProjectName: 400,
ErrInvalidRole: 400,
}

// ErrorDescriptions holds detailed error description for every AppError
Expand All @@ -43,4 +47,6 @@ var ErrorDescriptions = map[AppError]string{
ErrUnauthorized: "The user does not have requested authorization to access this resource",
ErrUserExists: "This username is already assigned to another user",
ErrStrictPasswordPolicyViolation: "Please ensure the password is 8 characters long and has 1 digit, 1 lowercase alphabet, 1 uppercase alphabet and 1 special character",
ErrEmptyProjectName: "Project name can't be empty",
ErrInvalidRole: "Role is invalid",
}
1 change: 1 addition & 0 deletions litmus-portal/authentication/pkg/validations/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var MutationRbacRules = map[string][]string{
"removeInvitation": {string(entities.RoleOwner)},
"leaveProject": {string(entities.RoleViewer), string(entities.RoleEditor)},
"updateProjectName": {string(entities.RoleOwner)},
"getProject": {string(entities.RoleOwner), string(entities.RoleViewer), string(entities.RoleEditor)},
}