Skip to content

Commit cf2205f

Browse files
committed
fixed some endpoint consistency bugs, now works properly
1 parent 6e9ab68 commit cf2205f

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

controllers/admin_controller.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,11 @@ func (controller *AdminController) AddAdminToOrg(ctx shared.Context) error {
9494
orgID := ctx.Param("orgID")
9595
parsedOrgID, err := uuid.Parse(orgID)
9696
if err != nil {
97-
return echo.NewHTTPError(400, "missing or invalid user id")
97+
return echo.NewHTTPError(400, "missing or invalid org id")
9898
}
9999

100-
user := ctx.Param("userID")
101-
102-
if !utils.IsEmail(user) {
100+
user, err := extractMailFromRequest(ctx)
101+
if err != nil {
103102
return echo.NewHTTPError(400, "user is not a valid mail address")
104103
}
105104

@@ -124,43 +123,32 @@ func (controller *AdminController) AddAdminToOrg(ctx shared.Context) error {
124123
if err != nil {
125124
return echo.NewHTTPError(500, "could not add admin to organization").WithInternal(err)
126125
}
127-
return ctx.JSON(201, nil)
126+
return ctx.NoContent(201)
128127
}
129128

130129
func (controller *AdminController) RevokeAdmin(ctx shared.Context) error {
131130
orgID := ctx.Param("orgID")
132131
parsedOrgID, err := uuid.Parse(orgID)
133132
if err != nil {
134-
return echo.NewHTTPError(400, "missing or invalid user id")
133+
return echo.NewHTTPError(400, "missing or invalid org id")
135134
}
136135

137-
user := ctx.Param("userID")
138-
139-
if !utils.IsEmail(user) {
140-
return echo.NewHTTPError(400, "user is not a valid mail address")
136+
userID := ctx.Param("userID")
137+
parsedUserID, err := uuid.Parse(userID)
138+
if err != nil {
139+
return echo.NewHTTPError(400, "missing or invalid user id")
141140
}
142141

143142
authAdminClient := shared.GetAuthAdminClient(ctx)
144143
if authAdminClient == nil {
145144
return echo.NewHTTPError(500, "could not get auth client")
146145
}
147-
userID, err := controller.adminService.GetUserIDFromMail(context.Background(), authAdminClient, user)
148-
if err != nil {
149-
switch err.Error() {
150-
case dtos.CouldNotFindUserWithMail:
151-
return echo.NewHTTPError(404, "could not find a user associated with this email")
152-
case dtos.CouldNotFindDefinitiveUserWithMail:
153-
return echo.NewHTTPError(400, "could not find a definitive user associated with this email")
154-
default:
155-
return echo.NewHTTPError(500, "could not determine user based on email")
156-
}
157-
}
158146

159-
err = controller.adminService.RevokeAdminFromOrg(context.Background(), parsedOrgID, userID)
147+
err = controller.adminService.RevokeAdminFromOrg(context.Background(), parsedOrgID, parsedUserID)
160148
if err != nil {
161149
return echo.NewHTTPError(500, "could not revoke admin role from user")
162150
}
163-
return ctx.JSON(204, nil)
151+
return ctx.NoContent(204)
164152
}
165153

166154
// checkCooldown reads the config DB for the last trigger time and returns an
@@ -420,3 +408,15 @@ func (controller *AdminController) runDaemonSSE(
420408
// Return nil so Echo does not try to write again.
421409
return nil
422410
}
411+
412+
func extractMailFromRequest(ctx shared.Context) (string, error) {
413+
userID, err := shared.GetURLDecodedParam(ctx, "userMail")
414+
if err != nil {
415+
return "", err
416+
}
417+
418+
if !utils.IsEmail(userID) {
419+
return "", fmt.Errorf("mail is invalid")
420+
}
421+
return userID, nil
422+
}

router/admin_router.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func NewAdminRouter(apiV1Router APIV1Router, adminController *controllers.AdminC
3535
return ctx.JSON(200, map[string]string{"status": "ok"})
3636
})
3737

38-
adminRouter.GET("/external-orgs", adminController.GetAdminsForExternalOrgs)
39-
adminRouter.PUT("/external-orgs/:orgID/admins/:userID", adminController.AddAdminToOrg)
40-
adminRouter.DELETE("/external-orgs/:orgID/admins/:userID", adminController.RevokeAdmin)
38+
adminRouter.GET("/external-orgs/", adminController.GetAdminsForExternalOrgs)
39+
adminRouter.PUT("/external-orgs/:orgID/admins/:userMail/", adminController.AddAdminToOrg)
40+
adminRouter.DELETE("/external-orgs/:orgID/admins/:userID/", adminController.RevokeAdmin)
4141

4242
// Daemon trigger endpoints – each daemon has its own SSE trigger route
4343
daemonGroup := adminRouter.Group("/daemons")

0 commit comments

Comments
 (0)