fix: allow users to open projects with accessible environments #3395
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is this PR about?
This PR fixes a bug where users with restricted environment access (e.g., only
developmentbut notproduction) couldn't open projects because the application always tried to navigate to the default environment. The fix ensures that when opening a project, we first try the default environment if accessible, otherwise fall back to the first accessible environment. This makes the permission system work as intended and allows users with restricted access to still work with projects they have permission to view.Checklist
Before submitting this PR, please make sure that:
canarybranch.Issues related (if applicable)
Fixes #3394
Changes Made
Core Fix
Updated the environment selection logic in three locations to handle users without access to the default environment:
Search Command (
apps/dokploy/components/dashboard/search-command.tsx):Projects List (
apps/dokploy/components/dashboard/projects/show.tsx):productionEnvironmentlookup withaccessibleEnvironmentthat uses fallback logicServer-Side Redirect (
apps/dokploy/pages/dashboard/project/[projectId]/environment/[environmentId].tsx):Technical Details
The backend already filters environments correctly - the
project.allquery returns only environments the user has access to for members. The issue was that the frontend code didn't account for this filtering and still tried to find the default from what it assumed was the full list.The fix is straightforward: since the environments array is already filtered for members, we select from that filtered list. We try the default first (if it exists in the accessible list), otherwise we grab the first one available.
Testing
Added comprehensive test coverage in
apps/dokploy/__test__/env/environment-access-fallback.test.ts:Backward Compatibility
This change is fully backward compatible. Users who have access to the default environment will see no change in behavior. The fix only improves the experience for users with restricted access.
Testing Instructions
To verify the fix:
production(default) anddevelopmentenvironmentsdevelopmentenvironment