zitadel/internal/query/project.go
Silvan f3e6f3b23b
feat: remove org (#4148)
* feat(command): remove org

* refactor: imports, unused code, error handling

* reduce org removed in action

* add org deletion to projections

* add org removal to projections

* add org removal to projections

* org removed projection

* lint import

* projections

* fix: table names in tests

* fix: table names in tests

* logging

* add org state

* fix(domain): add Owner removed to object details

* feat(ListQuery): add with owner removed

* fix(org-delete): add bool to functions to select with owner removed

* fix(org-delete): add bools to user grants with events to determine if dependencies lost owner

* fix(org-delete): add unit tests for owner removed and org removed events

* fix(org-delete): add handling of org remove for grants and members

* fix(org-delete): correction of unit tests for owner removed

* fix(org-delete): update projections, unit tests and get functions

* fix(org-delete): add change date to authnkeys and owner removed to org metadata

* fix(org-delete): include owner removed for login names

* fix(org-delete): some column fixes in projections and build for queries with owner removed

* indexes

* fix(org-delete): include review changes

* fix(org-delete): change user projection name after merge

* fix(org-delete): include review changes for project grant where no project owner is necessary

* fix(org-delete): include auth and adminapi tables with owner removed information

* fix(org-delete): cleanup username and orgdomain uniqueconstraints when org is removed

* fix(org-delete): add permissions for org.remove

* remove unnecessary unique constraints

* fix column order in primary keys

* fix(org-delete): include review changes

* fix(org-delete): add owner removed indexes and chang setup step to create tables

* fix(org-delete): move PK order of instance_id and change added user_grant from review

* fix(org-delete): no params for prepareUserQuery

* change to step 6

* merge main

* fix(org-delete): OldUserName rename to private

* fix linting

* cleanup

* fix: remove org test

* create prerelease

* chore: delete org-delete as prerelease

Co-authored-by: Stefan Benz <stefan@caos.ch>
Co-authored-by: Livio Spring <livio.a@gmail.com>
Co-authored-by: Fabi <38692350+hifabienne@users.noreply.github.com>
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2022-11-30 17:01:17 +01:00

283 lines
7.7 KiB
Go

package query
import (
"context"
"database/sql"
errs "errors"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/errors"
)
var (
projectsTable = table{
name: projection.ProjectProjectionTable,
instanceIDCol: projection.ProjectColumnInstanceID,
}
ProjectColumnID = Column{
name: projection.ProjectColumnID,
table: projectsTable,
}
ProjectColumnName = Column{
name: projection.ProjectColumnName,
table: projectsTable,
}
ProjectColumnProjectRoleAssertion = Column{
name: projection.ProjectColumnProjectRoleAssertion,
table: projectsTable,
}
ProjectColumnProjectRoleCheck = Column{
name: projection.ProjectColumnProjectRoleCheck,
table: projectsTable,
}
ProjectColumnHasProjectCheck = Column{
name: projection.ProjectColumnHasProjectCheck,
table: projectsTable,
}
ProjectColumnPrivateLabelingSetting = Column{
name: projection.ProjectColumnPrivateLabelingSetting,
table: projectsTable,
}
ProjectColumnCreationDate = Column{
name: projection.ProjectColumnCreationDate,
table: projectsTable,
}
ProjectColumnChangeDate = Column{
name: projection.ProjectColumnChangeDate,
table: projectsTable,
}
ProjectColumnResourceOwner = Column{
name: projection.ProjectColumnResourceOwner,
table: projectsTable,
}
ProjectColumnInstanceID = Column{
name: projection.ProjectColumnInstanceID,
table: projectsTable,
}
ProjectColumnSequence = Column{
name: projection.ProjectColumnSequence,
table: projectsTable,
}
ProjectColumnState = Column{
name: projection.ProjectColumnState,
table: projectsTable,
}
ProjectColumnOwnerRemoved = Column{
name: projection.ProjectColumnOwnerRemoved,
table: projectsTable,
}
)
type Projects struct {
SearchResponse
Projects []*Project
}
type Project struct {
ID string
CreationDate time.Time
ChangeDate time.Time
ResourceOwner string
State domain.ProjectState
Sequence uint64
Name string
ProjectRoleAssertion bool
ProjectRoleCheck bool
HasProjectCheck bool
PrivateLabelingSetting domain.PrivateLabelingSetting
}
type ProjectSearchQueries struct {
SearchRequest
Queries []SearchQuery
}
func (q *Queries) ProjectByID(ctx context.Context, shouldTriggerBulk bool, id string, withOwnerRemoved bool) (*Project, error) {
if shouldTriggerBulk {
projection.ProjectProjection.Trigger(ctx)
}
stmt, scan := prepareProjectQuery()
eq := sq.Eq{
ProjectColumnID.identifier(): id,
ProjectColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
}
if !withOwnerRemoved {
eq[ProjectColumnOwnerRemoved.identifier()] = false
}
query, args, err := stmt.Where(eq).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-2m00Q", "Errors.Query.SQLStatment")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func (q *Queries) SearchProjects(ctx context.Context, queries *ProjectSearchQueries, withOwnerRemoved bool) (projects *Projects, err error) {
query, scan := prepareProjectsQuery()
eq := sq.Eq{ProjectColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID()}
if !withOwnerRemoved {
eq[ProjectColumnOwnerRemoved.identifier()] = false
}
stmt, args, err := queries.toQuery(query).Where(eq).ToSql()
if err != nil {
return nil, errors.ThrowInvalidArgument(err, "QUERY-fn9ew", "Errors.Query.InvalidRequest")
}
rows, err := q.client.QueryContext(ctx, stmt, args...)
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-2j00f", "Errors.Internal")
}
projects, err = scan(rows)
if err != nil {
return nil, err
}
projects.LatestSequence, err = q.latestSequence(ctx, projectsTable)
return projects, err
}
func NewProjectNameSearchQuery(method TextComparison, value string) (SearchQuery, error) {
return NewTextQuery(ProjectColumnName, value, method)
}
func NewProjectIDSearchQuery(ids []string) (SearchQuery, error) {
list := make([]interface{}, len(ids))
for i, value := range ids {
list[i] = value
}
return NewListQuery(ProjectColumnID, list, ListIn)
}
func NewProjectResourceOwnerSearchQuery(value string) (SearchQuery, error) {
return NewTextQuery(ProjectColumnResourceOwner, value, TextEquals)
}
func (r *ProjectSearchQueries) AppendMyResourceOwnerQuery(orgID string) error {
query, err := NewProjectResourceOwnerSearchQuery(orgID)
if err != nil {
return err
}
r.Queries = append(r.Queries, query)
return nil
}
func (r *ProjectSearchQueries) AppendPermissionQueries(permissions []string) error {
if !authz.HasGlobalPermission(permissions) {
ids := authz.GetAllPermissionCtxIDs(permissions)
query, err := NewProjectIDSearchQuery(ids)
if err != nil {
return err
}
r.Queries = append(r.Queries, query)
}
return nil
}
func (q *ProjectSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
query = q.SearchRequest.toQuery(query)
for _, q := range q.Queries {
query = q.toQuery(query)
}
return query
}
func prepareProjectQuery() (sq.SelectBuilder, func(*sql.Row) (*Project, error)) {
return sq.Select(
ProjectColumnID.identifier(),
ProjectColumnCreationDate.identifier(),
ProjectColumnChangeDate.identifier(),
ProjectColumnResourceOwner.identifier(),
ProjectColumnState.identifier(),
ProjectColumnSequence.identifier(),
ProjectColumnName.identifier(),
ProjectColumnProjectRoleAssertion.identifier(),
ProjectColumnProjectRoleCheck.identifier(),
ProjectColumnHasProjectCheck.identifier(),
ProjectColumnPrivateLabelingSetting.identifier()).
From(projectsTable.identifier()).PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*Project, error) {
p := new(Project)
err := row.Scan(
&p.ID,
&p.CreationDate,
&p.ChangeDate,
&p.ResourceOwner,
&p.State,
&p.Sequence,
&p.Name,
&p.ProjectRoleAssertion,
&p.ProjectRoleCheck,
&p.HasProjectCheck,
&p.PrivateLabelingSetting,
)
if err != nil {
if errs.Is(err, sql.ErrNoRows) {
return nil, errors.ThrowNotFound(err, "QUERY-fk2fs", "Errors.Project.NotFound")
}
return nil, errors.ThrowInternal(err, "QUERY-dj2FF", "Errors.Internal")
}
return p, nil
}
}
func prepareProjectsQuery() (sq.SelectBuilder, func(*sql.Rows) (*Projects, error)) {
return sq.Select(
ProjectColumnID.identifier(),
ProjectColumnCreationDate.identifier(),
ProjectColumnChangeDate.identifier(),
ProjectColumnResourceOwner.identifier(),
ProjectColumnState.identifier(),
ProjectColumnSequence.identifier(),
ProjectColumnName.identifier(),
ProjectColumnProjectRoleAssertion.identifier(),
ProjectColumnProjectRoleCheck.identifier(),
ProjectColumnHasProjectCheck.identifier(),
ProjectColumnPrivateLabelingSetting.identifier(),
countColumn.identifier()).
From(projectsTable.identifier()).PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) (*Projects, error) {
projects := make([]*Project, 0)
var count uint64
for rows.Next() {
project := new(Project)
err := rows.Scan(
&project.ID,
&project.CreationDate,
&project.ChangeDate,
&project.ResourceOwner,
&project.State,
&project.Sequence,
&project.Name,
&project.ProjectRoleAssertion,
&project.ProjectRoleCheck,
&project.HasProjectCheck,
&project.PrivateLabelingSetting,
&count,
)
if err != nil {
return nil, err
}
projects = append(projects, project)
}
if err := rows.Close(); err != nil {
return nil, errors.ThrowInternal(err, "QUERY-QMXJv", "Errors.Query.CloseRows")
}
return &Projects{
Projects: projects,
SearchResponse: SearchResponse{
Count: count,
},
}, nil
}
}