An open-source, faster Devin alternative
TypeScript
Fix org switch bypassing WorkOS membership check when user has no workos_user_id (#97)
Mohamed Habib0c92e8b5c2ee
diff --git a/internal/api/dashboard_org.go b/internal/api/dashboard_org.goindex e6a7eefd..9cb68c04 100644--- a/internal/api/dashboard_org.go+++ b/internal/api/dashboard_org.go@@ -323,28 +323,33 @@ func (s *Server) dashboardSwitchOrg(c echo.Context) error {}// Validate the user has access to the target org via WorkOS- if s.workos != nil && s.workos.OrgMgr() != nil && user.WorkOSUserID != nil {- targetOrg, err := s.store.GetOrg(c.Request().Context(), req.OrgID)- if err != nil {- return c.JSON(http.StatusNotFound, map[string]string{"error": "target org not found"})- }- if targetOrg.WorkOSOrgID != nil {- memberships, err := s.workos.OrgMgr().ListUserMemberships(c.Request().Context(), *user.WorkOSUserID)- if err != nil {- return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})- }- found := false- for _, m := range memberships {- if m.OrganizationID == *targetOrg.WorkOSOrgID {- found = true- break- }- }- if !found {- return c.JSON(http.StatusForbidden, map[string]string{"error": "not a member of this org"})- }+ if s.workos == nil || s.workos.OrgMgr() == nil {+ return c.JSON(http.StatusServiceUnavailable, map[string]string{"error": "identity provider not configured"})+ }+ if user.WorkOSUserID == nil || *user.WorkOSUserID == "" {+ return c.JSON(http.StatusForbidden, map[string]string{"error": "user account not linked to identity provider"})+ }+ targetOrg, err := s.store.GetOrg(c.Request().Context(), req.OrgID)+ if err != nil {+ return c.JSON(http.StatusNotFound, map[string]string{"error": "target org not found"})+ }+ if targetOrg.WorkOSOrgID == nil || *targetOrg.WorkOSOrgID == "" {+ return c.JSON(http.StatusForbidden, map[string]string{"error": "target org not linked to identity provider"})+ }+ memberships, err := s.workos.OrgMgr().ListUserMemberships(c.Request().Context(), *user.WorkOSUserID)+ if err != nil {+ return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})+ }+ found := false+ for _, m := range memberships {+ if m.OrganizationID == *targetOrg.WorkOSOrgID {+ found = true+ break}}+ if !found {+ return c.JSON(http.StatusForbidden, map[string]string{"error": "not a member of this org"})+ }if err := s.store.SetActiveOrg(c.Request().Context(), user.ID, req.OrgID); err != nil {return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})