feat(rt): project grants (#10951)

This pull request introduces a new feature that allows adding, updating,
and querying project grants The changes are primarily in the backend and
include new database tables, repositories, and domain logic to support
project grants.

## Changes

* New `project_grants` table: A new table `zitadel.project_grants` is
introduced to store project grants It includes columns for
`instance_id`, `id`, `state`, `project_id`, `granted_org_id` and
`granting_org_id`.
* New `ProjectGrantRepository`: A new repository
`ProjectGrantRepository` is created to handle all database operations
for project grants. It provides methods to `Get`, `List`, `Create`,
`Update`, and `Delete`.
* New `project_grant_roles` table: A new table
`zitadel.project_grant_roles` is introduced to store project grant roles
It includes columns for `instance_id`, `grant_id`, `key`, `project_id`
and `project_org_id`.
* New `ProjectGrantRoleRepository`: A new repository
`ProjectGrantRoleRepository` is created to handle all database
operations for project grant roles. It provides methods to `Get`,
`List`, `Add`, and `Delete`.
* New `project_grant_relational_projection`: A new projection
`project_grant_relational_projection` is added to update the
`zitadel.project_grants` and `zitadel.project_grant_roles` tables based
on events. It handles all project grant events.
* Updated `ProjectRepository`: The `ProjectRepository` is updated to
list granted projects.

# Additional Context

Closes https://github.com/zitadel/zitadel/issues/10766

---------

Co-authored-by: Stefan Benz <[email protected]>
Co-authored-by: Marco A. <[email protected]>
Co-authored-by: Silvan <[email protected]>
This commit is contained in:
Tim Möhlmann
2025-11-14 11:04:16 +00:00
committed by GitHub
co-authored by Stefan Benz Marco A. Silvan
parent 6051ce591d
commit 9274e008fe
15 changed files with 1782 additions and 30 deletions
@@ -5,6 +5,7 @@ import (
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/muhlemmer/gu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -89,7 +90,10 @@ func TestListProjects(t *testing.T) {
instanceID := createInstance(t, tx)
firstOrgID := createOrganization(t, tx, instanceID)
secondOrgID := createOrganization(t, tx, instanceID)
thirdOrgID := createOrganization(t, tx, instanceID)
grantedOrgID := createOrganization(t, tx, instanceID)
projectRepo := repository.ProjectRepository()
projectGrantRepo := repository.ProjectGrantRepository()
projects := [...]*domain.Project{
{
@@ -136,10 +140,59 @@ func TestListProjects(t *testing.T) {
IsProjectAccessRequired: true,
UsedLabelingSettingOwner: 1,
},
{
InstanceID: instanceID,
OrganizationID: thirdOrgID,
ID: "5",
Name: "granted",
State: domain.ProjectStateActive,
ShouldAssertRole: true,
IsAuthorizationRequired: true,
IsProjectAccessRequired: true,
UsedLabelingSettingOwner: 1,
},
{
InstanceID: instanceID,
OrganizationID: thirdOrgID,
ID: "5",
Name: "granted",
State: domain.ProjectStateActive,
ShouldAssertRole: true,
IsAuthorizationRequired: true,
IsProjectAccessRequired: true,
UsedLabelingSettingOwner: 1,
GrantID: gu.Ptr("1"),
GrantedOrganizationID: gu.Ptr(grantedOrgID),
GrantState: gu.Ptr(domain.ProjectGrantStateActive),
},
{
InstanceID: instanceID,
OrganizationID: grantedOrgID,
ID: "6",
Name: "owned",
State: domain.ProjectStateActive,
ShouldAssertRole: true,
IsAuthorizationRequired: true,
IsProjectAccessRequired: true,
UsedLabelingSettingOwner: 1,
},
}
for _, project := range projects {
err := projectRepo.Create(t.Context(), tx, project)
require.NoError(t, err)
// Granted Projects
if project.GrantedOrganizationID != nil {
err := projectGrantRepo.Create(t.Context(), tx, &domain.ProjectGrant{
InstanceID: project.InstanceID,
ID: *project.GrantID,
ProjectID: project.ID,
GrantedOrganizationID: *project.GrantedOrganizationID,
GrantingOrganizationID: project.OrganizationID,
State: domain.ProjectGrantStateActive,
})
require.NoError(t, err)
} else {
err := projectRepo.Create(t.Context(), tx, project)
require.NoError(t, err)
}
}
tests := []struct {
@@ -179,13 +232,28 @@ func TestListProjects(t *testing.T) {
want: projects[1:3],
},
{
name: "state active",
name: "state active from first or second org",
condition: database.And(
projectRepo.InstanceIDCondition(instanceID),
database.Or(
projectRepo.OrganizationIDCondition(firstOrgID),
projectRepo.OrganizationIDCondition(secondOrgID),
),
projectRepo.StateCondition(domain.ProjectStateActive),
),
want: []*domain.Project{projects[0], projects[2]},
},
{
name: "all from granted org or granted to granted org",
condition: database.And(
projectRepo.InstanceIDCondition(instanceID),
database.Or(
projectRepo.OrganizationIDCondition(grantedOrgID),
projectRepo.GrantedOrganizationIDCondition(grantedOrgID),
),
),
want: []*domain.Project{projects[5], projects[6]},
},
}
for _, tt := range tests {
@@ -195,7 +263,7 @@ func TestListProjects(t *testing.T) {
database.WithOrderByAscending(projectRepo.PrimaryKeyColumns()...),
)
require.ErrorIs(t, err, tt.wantErr)
assert.Equal(t, tt.want, got)
assert.EqualExportedValues(t, tt.want, got)
})
}
}