mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-30 07:58:51 +00:00
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:
co-authored by
Stefan Benz
Marco A.
Silvan
parent
6051ce591d
commit
9274e008fe
@@ -24,7 +24,7 @@ func (project) Role() domain.ProjectRoleRepository {
|
||||
}
|
||||
|
||||
func (p project) Get(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.Project, error) {
|
||||
builder, err := p.prepareQuery(opts)
|
||||
builder, err := p.prepareGetQuery(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func (p project) Get(ctx context.Context, client database.QueryExecutor, opts ..
|
||||
}
|
||||
|
||||
func (p project) List(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) ([]*domain.Project, error) {
|
||||
builder, err := p.prepareQuery(opts)
|
||||
builder, err := p.prepareListQuery(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -62,31 +62,11 @@ func (project) Create(ctx context.Context, client database.QueryExecutor, projec
|
||||
}
|
||||
|
||||
func (p project) Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) (int64, error) {
|
||||
if len(changes) == 0 {
|
||||
return 0, database.ErrNoChanges
|
||||
}
|
||||
if err := checkPKCondition(p, condition); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if !database.Changes(changes).IsOnColumn(p.UpdatedAtColumn()) {
|
||||
changes = append(changes, database.NewChange(p.UpdatedAtColumn(), database.NullInstruction))
|
||||
}
|
||||
builder := database.NewStatementBuilder(`UPDATE zitadel.projects SET `)
|
||||
database.Changes(changes).Write(builder)
|
||||
writeCondition(builder, condition)
|
||||
|
||||
return client.Exec(ctx, builder.String(), builder.Args()...)
|
||||
return updateOne(ctx, client, p, condition, changes...)
|
||||
}
|
||||
|
||||
func (p project) Delete(ctx context.Context, client database.QueryExecutor, condition database.Condition) (int64, error) {
|
||||
if err := checkPKCondition(p, condition); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
builder := database.NewStatementBuilder(`DELETE FROM zitadel.projects `)
|
||||
writeCondition(builder, condition)
|
||||
|
||||
return client.Exec(ctx, builder.String(), builder.Args()...)
|
||||
return deleteOne(ctx, client, p, condition)
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
@@ -140,6 +120,10 @@ func (p project) OrganizationIDCondition(organizationID string) database.Conditi
|
||||
return database.NewTextCondition(p.OrganizationIDColumn(), database.TextOperationEqual, organizationID)
|
||||
}
|
||||
|
||||
func (p project) GrantedOrganizationIDCondition(organizationID string) database.Condition {
|
||||
return database.NewTextCondition(p.GrantedOrganizationIDColumn(), database.TextOperationEqual, organizationID)
|
||||
}
|
||||
|
||||
func (p project) IDCondition(projectID string) database.Condition {
|
||||
return database.NewTextCondition(p.IDColumn(), database.TextOperationEqual, projectID)
|
||||
}
|
||||
@@ -156,6 +140,10 @@ func (p project) StateCondition(state domain.ProjectState) database.Condition {
|
||||
// columns
|
||||
// -------------------------------------------------------------
|
||||
|
||||
func (p project) qualifiedTableName() string {
|
||||
return "zitadel." + p.unqualifiedTableName()
|
||||
}
|
||||
|
||||
func (project) unqualifiedTableName() string {
|
||||
return "projects"
|
||||
}
|
||||
@@ -176,6 +164,10 @@ func (p project) OrganizationIDColumn() database.Column {
|
||||
return database.NewColumn(p.unqualifiedTableName(), "organization_id")
|
||||
}
|
||||
|
||||
func (p project) GrantedOrganizationIDColumn() database.Column {
|
||||
return database.NewColumn(p.unqualifiedTableName(), "granted_organization_id")
|
||||
}
|
||||
|
||||
func (p project) IDColumn() database.Column {
|
||||
return database.NewColumn(p.unqualifiedTableName(), "id")
|
||||
}
|
||||
@@ -227,10 +219,13 @@ const queryProjectStmt = `SELECT
|
||||
projects.should_assert_role,
|
||||
projects.is_authorization_required,
|
||||
projects.is_project_access_required,
|
||||
projects.used_labeling_setting_owner
|
||||
projects.used_labeling_setting_owner,
|
||||
NULL::TEXT AS grant_id,
|
||||
NULL::TEXT AS granted_organization_id,
|
||||
NULL::zitadel.project_grant_state AS grant_state
|
||||
FROM zitadel.projects`
|
||||
|
||||
func (p project) prepareQuery(opts []database.QueryOption) (*database.StatementBuilder, error) {
|
||||
func (p project) prepareGetQuery(opts []database.QueryOption) (*database.StatementBuilder, error) {
|
||||
options := new(database.QueryOpts)
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
@@ -243,3 +238,61 @@ func (p project) prepareQuery(opts []database.QueryOption) (*database.StatementB
|
||||
|
||||
return builder, nil
|
||||
}
|
||||
|
||||
const queryGrantedProjectStmt = `SELECT
|
||||
projects.instance_id,
|
||||
projects.organization_id,
|
||||
projects.id as id,
|
||||
grants.created_at,
|
||||
grants.updated_at,
|
||||
projects.name,
|
||||
projects.state,
|
||||
projects.should_assert_role,
|
||||
projects.is_authorization_required,
|
||||
projects.is_project_access_required,
|
||||
projects.used_labeling_setting_owner,
|
||||
grants.id AS grant_id,
|
||||
grants.granted_organization_id,
|
||||
grants.state as grant_state
|
||||
FROM zitadel.projects
|
||||
INNER JOIN zitadel.project_grants as grants
|
||||
ON grants.instance_id = projects.instance_id
|
||||
AND grants.granting_organization_id = projects.organization_id
|
||||
AND grants.project_id = projects.id`
|
||||
|
||||
const queryProjectsUnionStmt = `SELECT
|
||||
projects.instance_id,
|
||||
projects.organization_id,
|
||||
projects.id as id,
|
||||
projects.created_at,
|
||||
projects.updated_at,
|
||||
projects.name,
|
||||
projects.state,
|
||||
projects.should_assert_role,
|
||||
projects.is_authorization_required,
|
||||
projects.is_project_access_required,
|
||||
projects.used_labeling_setting_owner,
|
||||
projects.grant_id,
|
||||
projects.granted_organization_id,
|
||||
projects.grant_state
|
||||
FROM (` +
|
||||
queryProjectStmt +
|
||||
` UNION ALL ` +
|
||||
queryGrantedProjectStmt +
|
||||
`) AS projects`
|
||||
|
||||
func (p project) prepareListQuery(opts []database.QueryOption) (*database.StatementBuilder, error) {
|
||||
options := new(database.QueryOpts)
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
if err := checkRestrictingColumns(options.Condition, p.InstanceIDColumn()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
builder := database.NewStatementBuilder(
|
||||
queryProjectsUnionStmt,
|
||||
)
|
||||
options.Write(builder)
|
||||
|
||||
return builder, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user