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

Refactor: Updated api schema and removed redundant types #4097

Merged
merged 7 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions chaoscenter/authentication/api/handlers/grpc/grpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *ServerGrpc) GetProjectById(ctx context.Context,
for _, member := range project.Members {
var projectMember protos.ProjectMembers
projectMember.Email = memberMap[member.UserID].Email
projectMember.UserName = memberMap[member.UserID].UserName
projectMember.Username = memberMap[member.UserID].Username
projectMember.Invitation = string(member.Invitation)
projectMember.Uid = member.UserID
projectMember.JoinedAt = member.JoinedAt
Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *ServerGrpc) GetUserById(ctx context.Context,
return &protos.GetUserByIdResponse{
Id: user.ID,
Name: user.Name,
Username: user.UserName,
Username: user.Username,
CreatedAt: strconv.FormatInt(user.CreatedAt, 10),
UpdatedAt: strconv.FormatInt(user.UpdatedAt, 10),
DeactivatedAt: deactivatedAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func DexCallback(userService services.ApplicationService) gin.HandlerFunc {
var userData = entities.User{
Name: claims.Name,
Email: claims.Email,
UserName: claims.Email,
Username: claims.Email,
Role: entities.RoleUser,
Audit: entities.Audit{
CreatedAt: createdAt,
Expand Down
147 changes: 34 additions & 113 deletions chaoscenter/authentication/api/handlers/rest/project_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/litmuschaos/litmus/chaoscenter/authentication/api/presenter"
"github.com/litmuschaos/litmus/chaoscenter/authentication/api/types"
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/entities"
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/services"
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/utils"
Expand Down Expand Up @@ -75,53 +74,7 @@ func GetProject(service services.ApplicationService) gin.HandlerFunc {
return
}

// Fetching user ids of all the members in the project
var uids []string
for _, member := range project.Members {
uids = append(uids, member.UserID)
}

authUsers, err := service.FindUsersByUID(uids)
if err != nil {
log.Error(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}

memberMap := make(map[string]entities.User)
for _, authUser := range *authUsers {
memberMap[authUser.ID] = authUser
}

var members []*types.Member

// Adding additional details of project members
for _, member := range project.Members {
members = append(members, &types.Member{
UserID: memberMap[member.UserID].ID,
UserName: memberMap[member.UserID].UserName,
Name: memberMap[member.UserID].Name,
Role: member.Role,
Email: memberMap[member.UserID].Email,
Invitation: member.Invitation,
JoinedAt: member.JoinedAt,
DeactivatedAt: memberMap[member.UserID].DeactivatedAt,
})
}

c.JSON(200, gin.H{"data": types.Project{
ID: project.ID,
Name: project.Name,
State: project.State,
Audit: entities.Audit{
IsRemoved: project.IsRemoved,
CreatedAt: project.CreatedAt,
CreatedBy: project.UpdatedBy,
UpdatedAt: project.UpdatedAt,
UpdatedBy: project.UpdatedBy,
},
Members: members,
}})
c.JSON(200, gin.H{"data": project})
}
}

Expand All @@ -141,59 +94,7 @@ func GetProjectsByUserID(service services.ApplicationService) gin.HandlerFunc {
return
}

var uids []string

// Fetching user ids of all members from all user's projects
for _, project := range projects {
for _, member := range project.Members {
uids = append(uids, member.UserID)
}
}
authUsers, err := service.FindUsersByUID(uids)
if err != nil || authUsers == nil {
log.Error(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}
memberMap := make(map[string]entities.User)

for _, authUser := range *authUsers {
memberMap[authUser.ID] = authUser
}

var outputProjects []*types.Project

// Adding additional details of project members
for _, project := range projects {
var members []*types.Member
for _, member := range project.Members {
members = append(members, &types.Member{
UserID: memberMap[member.UserID].ID,
UserName: memberMap[member.UserID].UserName,
Name: memberMap[member.UserID].Name,
Role: member.Role,
Email: memberMap[member.UserID].Email,
Invitation: member.Invitation,
JoinedAt: member.JoinedAt,
DeactivatedAt: memberMap[member.UserID].DeactivatedAt,
})
}
outputProjects = append(outputProjects, &types.Project{
ID: project.ID,
Name: project.Name,
Members: members,
State: project.State,
Audit: entities.Audit{
IsRemoved: project.IsRemoved,
CreatedAt: project.CreatedAt,
CreatedBy: project.UpdatedBy,
UpdatedAt: project.UpdatedAt,
UpdatedBy: project.UpdatedBy,
},
})
}

c.JSON(200, gin.H{"data": outputProjects})
c.JSON(200, gin.H{"data": projects})
}
}

Expand Down Expand Up @@ -221,6 +122,19 @@ func GetProjectStats(service services.ApplicationService) gin.HandlerFunc {
}
}

func GetActiveProjectMembers(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
projectID := c.Param("project_id")
state := c.Param("state")
members, err := service.GetProjectMembers(projectID, state)
if err != nil {
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}
c.JSON(200, gin.H{"data": members})
}
}

// getInvitation returns the Invitation status
func getInvitation(service services.ApplicationService, member entities.MemberInput) (entities.Invitation, error) {
project, err := service.GetProjectByProjectID(member.ProjectID)
Expand Down Expand Up @@ -291,9 +205,17 @@ func CreateProject(service services.ApplicationService) gin.HandlerFunc {
Audit: entities.Audit{
IsRemoved: false,
CreatedAt: time.Now().Unix(),
CreatedBy: user.ID,
CreatedBy: entities.UserDetailResponse{
Username: user.Username,
UserID: user.ID,
Email: user.Email,
},
UpdatedAt: time.Now().Unix(),
UpdatedBy: user.ID,
UpdatedBy: entities.UserDetailResponse{
Username: user.Username,
UserID: user.ID,
Email: user.Email,
},
},
}

Expand Down Expand Up @@ -385,15 +307,14 @@ func SendInvitation(service services.ApplicationService) gin.HandlerFunc {
return
}

c.JSON(200, gin.H{"data": types.Member{
UserID: user.ID,
UserName: user.UserName,
Name: user.Name,
Role: entities.MemberRole(newMember.Role),
Email: user.Email,
Invitation: entities.Invitation(newMember.Invitation),
JoinedAt: newMember.JoinedAt,
DeactivatedAt: user.DeactivatedAt,
c.JSON(200, gin.H{"data": entities.Member{
UserID: user.ID,
Username: user.Username,
Name: user.Name,
Role: newMember.Role,
Email: user.Email,
Invitation: newMember.Invitation,
JoinedAt: newMember.JoinedAt,
}})
}
}
Expand Down Expand Up @@ -610,7 +531,7 @@ func UpdateProjectName(service services.ApplicationService) gin.HandlerFunc {
}
}

// GetOwnerProject returns an array of project IDs in which user is an owner
// GetOwnerProjectIDs returns an array of project IDs in which user is an owner
func GetOwnerProjectIDs(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
uid := c.MustGet("uid").(string)
Expand Down
51 changes: 41 additions & 10 deletions chaoscenter/authentication/api/handlers/rest/user_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func CreateUser(service services.ApplicationService) gin.HandlerFunc {
return
}

userRequest.UserName = utils.SanitizeString(userRequest.UserName)
if userRequest.Role == "" || userRequest.UserName == "" || userRequest.Password == "" {
userRequest.Username = utils.SanitizeString(userRequest.Username)
if userRequest.Role == "" || userRequest.Username == "" || userRequest.Password == "" {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}
Expand Down Expand Up @@ -138,6 +138,29 @@ func FetchUsers(service services.ApplicationService) gin.HandlerFunc {
}
}

func InviteUsers(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
projectID := c.Param("project_id")
if projectID == "" {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}
projectMembers, err := service.GetProjectMembers(projectID, "all")

var uids []string
for _, k := range projectMembers {
uids = append(uids, k.UserID)
}
users, err := service.InviteUsers(uids)
if err != nil {
log.Error(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}
c.JSON(200, users)
}
}

func LoginUser(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
var userRequest entities.User
Expand All @@ -147,14 +170,14 @@ func LoginUser(service services.ApplicationService) gin.HandlerFunc {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}
userRequest.UserName = utils.SanitizeString(userRequest.UserName)
if userRequest.UserName == "" || userRequest.Password == "" {
userRequest.Username = utils.SanitizeString(userRequest.Username)
if userRequest.Username == "" || userRequest.Password == "" {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}

// Checking if user exists
user, err := service.FindUserByUsername(userRequest.UserName)
user, err := service.FindUserByUsername(userRequest.Username)
if err != nil {
log.Error(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrUserNotFound], presenter.CreateErrorResponse(utils.ErrUserNotFound))
Expand Down Expand Up @@ -201,15 +224,23 @@ func LoginUser(service services.ApplicationService) gin.HandlerFunc {
state := "active"
newProject := &entities.Project{
ID: uuid.Must(uuid.NewRandom()).String(),
Name: user.UserName + "'s project",
Name: user.Username + "'s project",
Members: members,
State: &state,
Audit: entities.Audit{
IsRemoved: false,
CreatedAt: time.Now().Unix(),
CreatedBy: user.ID,
CreatedBy: entities.UserDetailResponse{
Username: user.Username,
UserID: user.ID,
Email: user.Email,
},
UpdatedAt: time.Now().Unix(),
UpdatedBy: user.ID,
UpdatedBy: entities.UserDetailResponse{
Username: user.Username,
UserID: user.ID,
Email: user.Email,
},
},
}
err := service.CreateProject(newProject)
Expand Down Expand Up @@ -274,7 +305,7 @@ func ResetPassword(service services.ApplicationService) gin.HandlerFunc {
}
uid := c.MustGet("uid").(string)
var adminUser entities.User
adminUser.UserName = c.MustGet("username").(string)
adminUser.Username = c.MustGet("username").(string)
adminUser.ID = uid
if utils.StrictPasswordPolicy {
err := utils.ValidateStrictPassword(userPasswordRequest.NewPassword)
Expand Down Expand Up @@ -321,7 +352,7 @@ func UpdateUserState(service services.ApplicationService) gin.HandlerFunc {
}

var adminUser entities.User
adminUser.UserName = c.MustGet("username").(string)
adminUser.Username = c.MustGet("username").(string)
adminUser.ID = c.MustGet("uid").(string)

// Checking if loggedIn user is admin
Expand Down
2 changes: 1 addition & 1 deletion chaoscenter/authentication/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func validatedAdminSetup(service services.ApplicationService) {

adminUser := entities.User{
ID: uID,
UserName: utils.AdminName,
Username: utils.AdminName,
Password: password,
Role: entities.RoleAdmin,
Audit: entities.Audit{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion chaoscenter/authentication/api/routes/project_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func ProjectRouter(router *gin.Engine, service services.ApplicationService) {
router.Use(middleware.JwtMiddleware())
router.GET("/get_project/:project_id", rest.GetProject(service))
router.GET("/get_project_members/:project_id/:state", rest.GetActiveProjectMembers(service))
router.GET("/get_user_with_project/:username", rest.GetUserWithProject(service))
router.GET("/get_owner_projects", rest.GetOwnerProjectIDs(service))
router.GET("/get_project_role/:project_id", rest.GetProjectRole(service))
Expand All @@ -23,5 +24,5 @@ func ProjectRouter(router *gin.Engine, service services.ApplicationService) {
router.POST("/decline_invitation", rest.DeclineInvitation(service))
router.POST("/remove_invitation", rest.RemoveInvitation(service))
router.POST("/leave_project", rest.LeaveProject(service))
router.POST("/update_projectname", rest.UpdateProjectName(service))
router.POST("/update_project_name", rest.UpdateProjectName(service))
}
1 change: 1 addition & 0 deletions chaoscenter/authentication/api/routes/user_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ func UserRouter(router *gin.Engine, service services.ApplicationService) {
router.POST("/update/details", rest.UpdateUser(service))
router.GET("/getUser/:uid", rest.GetUser(service))
router.GET("/users", rest.FetchUsers(service))
router.GET("/invite_users/:project_id", rest.InviteUsers(service))
router.POST("/update/state", rest.UpdateUserState(service))
}
Loading
Loading