mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 01:47:33 +00:00
fix: Remove project (#538)
* Remove project added * Gemeriert * corrections * corrections * Delete*sByProjectID added * Correct typos
This commit is contained in:
@@ -163,6 +163,35 @@ func (es *ProjectEventstore) ReactivateProject(ctx context.Context, id string) (
|
||||
return model.ProjectToModel(repoExisting), nil
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) RemoveProject(ctx context.Context, proj *proj_model.Project) error {
|
||||
project, aggregate, err := es.PrepareRemoveProject(ctx, proj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = es_sdk.PushAggregates(ctx, es.PushAggregates, project.AppendEvents, aggregate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
es.projectCache.cacheProject(project)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) PrepareRemoveProject(ctx context.Context, proj *proj_model.Project) (*model.Project, *es_models.Aggregate, error) {
|
||||
if proj.AggregateID == "" {
|
||||
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-Akbov", "Errors.ProjectInvalid")
|
||||
}
|
||||
existing, err := es.ProjectByID(ctx, proj.AggregateID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
repoProject := model.ProjectFromModel(existing)
|
||||
projectAggregate, err := ProjectRemovedAggregate(ctx, es.Eventstore.AggregateCreator(), repoProject)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return repoProject, projectAggregate, nil
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) ProjectMemberByIDs(ctx context.Context, member *proj_model.ProjectMember) (*proj_model.ProjectMember, error) {
|
||||
if member.UserID == "" {
|
||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-ld93d", "Errors.Project.UserIDMissing")
|
||||
@@ -202,7 +231,7 @@ func (es *ProjectEventstore) AddProjectMember(ctx context.Context, member *proj_
|
||||
if _, m := model.GetProjectMember(repoProject.Members, member.UserID); m != nil {
|
||||
return model.ProjectMemberToModel(m), nil
|
||||
}
|
||||
return nil, caos_errs.ThrowInternal(nil, "EVENT-3udjs", "Errors.Internal")
|
||||
return nil, caos_errs.ThrowInternal(nil, "EVENT-rfQWv", "Errors.Internal")
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) ChangeProjectMember(ctx context.Context, member *proj_model.ProjectMember) (*proj_model.ProjectMember, error) {
|
||||
@@ -229,7 +258,7 @@ func (es *ProjectEventstore) ChangeProjectMember(ctx context.Context, member *pr
|
||||
if _, m := model.GetProjectMember(repoProject.Members, member.UserID); m != nil {
|
||||
return model.ProjectMemberToModel(m), nil
|
||||
}
|
||||
return nil, caos_errs.ThrowInternal(nil, "EVENT-3udjs", "Errors.Internal")
|
||||
return nil, caos_errs.ThrowInternal(nil, "EVENT-pLyzi", "Errors.Internal")
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) RemoveProjectMember(ctx context.Context, member *proj_model.ProjectMember) error {
|
||||
@@ -255,6 +284,29 @@ func (es *ProjectEventstore) RemoveProjectMember(ctx context.Context, member *pr
|
||||
return err
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) PrepareRemoveProjectMember(ctx context.Context, member *proj_model.ProjectMember) (*model.ProjectMember, *es_models.Aggregate, error) {
|
||||
if member.UserID == "" {
|
||||
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-tCXHE", "Errors.Project.MemberInvalid")
|
||||
}
|
||||
existing, err := es.ProjectByID(ctx, member.AggregateID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if _, m := existing.GetMember(member.UserID); m == nil {
|
||||
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-wPcg5", "Errors.Project.MemberNotExisting")
|
||||
}
|
||||
repoProject := model.ProjectFromModel(existing)
|
||||
repoMember := model.ProjectMemberFromModel(member)
|
||||
|
||||
projectAggregate := ProjectMemberRemovedAggregate(es.Eventstore.AggregateCreator(), repoProject, repoMember)
|
||||
agg, err := projectAggregate(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return repoMember, agg, err
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) AddProjectRoles(ctx context.Context, roles ...*proj_model.ProjectRole) (*proj_model.ProjectRole, error) {
|
||||
if roles == nil || len(roles) == 0 {
|
||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-uOJAs", "Errors.Project.MinimumOneRoleNeeded")
|
||||
@@ -487,7 +539,7 @@ func (es *ProjectEventstore) AddApplication(ctx context.Context, app *proj_model
|
||||
converted.OIDCConfig.ClientSecretString = stringPw
|
||||
return converted, nil
|
||||
}
|
||||
return nil, caos_errs.ThrowInternal(nil, "EVENT-3udjs", "Errors.Internal")
|
||||
return nil, caos_errs.ThrowInternal(nil, "EVENT-GvPct", "Errors.Internal")
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) ChangeApplication(ctx context.Context, app *proj_model.Application) (*proj_model.Application, error) {
|
||||
@@ -538,6 +590,27 @@ func (es *ProjectEventstore) RemoveApplication(ctx context.Context, app *proj_mo
|
||||
return nil
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) PrepareRemoveApplication(ctx context.Context, app *proj_model.Application) (*model.Application, *es_models.Aggregate, error) {
|
||||
if app.AppID == "" {
|
||||
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-xu0Wy", "Errors.Project.IDMissing")
|
||||
}
|
||||
existing, err := es.ProjectByID(ctx, app.AggregateID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if _, app := existing.GetApp(app.AppID); app == nil {
|
||||
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-gaOD2", "Errors.Project.AppNotExisting")
|
||||
}
|
||||
repoProject := model.ProjectFromModel(existing)
|
||||
appRepo := model.AppFromModel(app)
|
||||
projectAggregate := ApplicationRemovedAggregate(es.Eventstore.AggregateCreator(), repoProject, appRepo)
|
||||
agg, err := projectAggregate(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return appRepo, agg, nil
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) ApplicationChanges(ctx context.Context, projectID string, appID string, lastSequence uint64, limit uint64, sortAscending bool) (*proj_model.ApplicationChanges, error) {
|
||||
query := ChangesQuery(projectID, lastSequence, limit, sortAscending)
|
||||
|
||||
@@ -927,7 +1000,7 @@ func (es *ProjectEventstore) ProjectGrantMemberByIDs(ctx context.Context, member
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
return nil, caos_errs.ThrowNotFound(nil, "EVENT-3udjs", "Errors.Project.MemberNotFound")
|
||||
return nil, caos_errs.ThrowNotFound(nil, "EVENT-LxiBI", "Errors.Project.MemberNotFound")
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) AddProjectGrantMember(ctx context.Context, member *proj_model.ProjectGrantMember) (*proj_model.ProjectGrantMember, error) {
|
||||
@@ -955,7 +1028,7 @@ func (es *ProjectEventstore) AddProjectGrantMember(ctx context.Context, member *
|
||||
return model.GrantMemberToModel(m), nil
|
||||
}
|
||||
}
|
||||
return nil, caos_errs.ThrowInternal(nil, "EVENT-3udjs", "Errors.Internal")
|
||||
return nil, caos_errs.ThrowInternal(nil, "EVENT-BBcGD", "Errors.Internal")
|
||||
}
|
||||
|
||||
func (es *ProjectEventstore) ChangeProjectGrantMember(ctx context.Context, member *proj_model.ProjectGrantMember) (*proj_model.ProjectGrantMember, error) {
|
||||
|
@@ -345,6 +345,93 @@ func TestReactivateProject(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveProject(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
type args struct {
|
||||
es *ProjectEventstore
|
||||
ctx context.Context
|
||||
existing *model.Project
|
||||
}
|
||||
type res struct {
|
||||
result *model.Project
|
||||
wantErr bool
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "remove project, ok",
|
||||
args: args{
|
||||
es: GetMockManipulateProject(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
existing: &model.Project{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 1},
|
||||
Name: "Name",
|
||||
Members: []*model.ProjectMember{&model.ProjectMember{UserID: "UserID", Roles: []string{"Roles"}}},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
result: &model.Project{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no projectid",
|
||||
args: args{
|
||||
es: GetMockManipulateProject(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
existing: &model.Project{
|
||||
ObjectRoot: es_models.ObjectRoot{Sequence: 1},
|
||||
Name: "Name",
|
||||
Members: []*model.ProjectMember{&model.ProjectMember{UserID: "UserID", Roles: []string{"Roles"}}},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsPreconditionFailed,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "project not existing",
|
||||
args: args{
|
||||
es: GetMockManipulateProject(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
existing: &model.Project{},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsPreconditionFailed,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "existing not found",
|
||||
args: args{
|
||||
es: GetMockManipulateProjectNoEvents(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
existing: &model.Project{ObjectRoot: es_models.ObjectRoot{AggregateID: "OtherAggregateID", Sequence: 1}},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsNotFound,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.es.RemoveProject(tt.args.ctx, tt.args.existing)
|
||||
|
||||
if !tt.res.wantErr && err != nil {
|
||||
t.Errorf("should not get err")
|
||||
}
|
||||
if tt.res.wantErr && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectMemberByIDs(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
type args struct {
|
||||
|
@@ -2,6 +2,7 @@ package eventsourcing
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/caos/zitadel/internal/crypto"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore/models"
|
||||
@@ -99,6 +100,17 @@ func projectStateAggregate(aggCreator *es_models.AggregateCreator, project *mode
|
||||
}
|
||||
}
|
||||
|
||||
func ProjectRemovedAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, existing *model.Project) (*es_models.Aggregate, error) {
|
||||
if existing == nil {
|
||||
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-Cj7lb", "Errors.Internal")
|
||||
}
|
||||
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return agg.AppendEvent(model.ProjectRemoved, existing)
|
||||
}
|
||||
|
||||
func ProjectMemberAddedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, member *model.ProjectMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
||||
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
||||
if member == nil {
|
||||
|
@@ -19,6 +19,19 @@ func ApplicationByID(db *gorm.DB, table, appID string) (*model.ApplicationView,
|
||||
return app, err
|
||||
}
|
||||
|
||||
func ApplicationsByProjectID(db *gorm.DB, table, projectID string) ([]*model.ApplicationView, error) {
|
||||
applications := make([]*model.ApplicationView, 0)
|
||||
queries := []*proj_model.ApplicationSearchQuery{
|
||||
&proj_model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.ApplicationSearchRequest{Queries: queries})
|
||||
_, err := query(db, &applications)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return applications, nil
|
||||
}
|
||||
|
||||
func ApplicationByOIDCClientID(db *gorm.DB, table, clientID string) (*model.ApplicationView, error) {
|
||||
app := new(model.ApplicationView)
|
||||
clientIDQuery := model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyOIDCClientID, Value: clientID, Method: global_model.SearchMethodEquals}
|
||||
@@ -58,3 +71,8 @@ func DeleteApplication(db *gorm.DB, table, appID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.ApplicationSearchKey(proj_model.AppSearchKeyAppID), appID)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteApplicationsByProjectID(db *gorm.DB, table, projectID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.ApplicationSearchKey(proj_model.AppSearchKeyProjectID), projectID)
|
||||
return delete(db)
|
||||
}
|
||||
|
@@ -2,18 +2,20 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/caos/logging"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore/models"
|
||||
"github.com/caos/zitadel/internal/project/model"
|
||||
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
||||
"github.com/lib/pq"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ProjectGrantMemberKeyUserID = "user_id"
|
||||
ProjectGrantMemberKeyGrantID = "grant_id"
|
||||
ProjectGrantMemberKeyProjectID = "project_id"
|
||||
ProjectGrantMemberKeyUserName = "user_name"
|
||||
ProjectGrantMemberKeyEmail = "email"
|
||||
ProjectGrantMemberKeyFirstName = "first_name"
|
||||
|
@@ -63,6 +63,8 @@ func (key ProjectGrantMemberSearchKey) ToColumnName() string {
|
||||
return ProjectGrantMemberKeyUserID
|
||||
case proj_model.ProjectGrantMemberSearchKeyGrantID:
|
||||
return ProjectGrantMemberKeyGrantID
|
||||
case proj_model.ProjectGrantMemberSearchKeyProjectID:
|
||||
return ProjectGrantMemberKeyProjectID
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
|
@@ -22,6 +22,19 @@ func ProjectGrantMemberByIDs(db *gorm.DB, table, grantID, userID string) (*model
|
||||
return role, err
|
||||
}
|
||||
|
||||
func ProjectGrantMembersByProjectID(db *gorm.DB, table, projectID string) ([]*model.ProjectGrantMemberView, error) {
|
||||
members := make([]*model.ProjectGrantMemberView, 0)
|
||||
queries := []*proj_model.ProjectGrantMemberSearchQuery{
|
||||
&proj_model.ProjectGrantMemberSearchQuery{Key: proj_model.ProjectGrantMemberSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectGrantMemberSearchRequest{Queries: queries})
|
||||
_, err := query(db, &members)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return members, nil
|
||||
}
|
||||
|
||||
func SearchProjectGrantMembers(db *gorm.DB, table string, req *proj_model.ProjectGrantMemberSearchRequest) ([]*model.ProjectGrantMemberView, uint64, error) {
|
||||
roles := make([]*model.ProjectGrantMemberView, 0)
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectGrantMemberSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
||||
@@ -58,3 +71,8 @@ func DeleteProjectGrantMember(db *gorm.DB, table, grantID, userID string) error
|
||||
delete := repository.PrepareDeleteByObject(table, role)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteProjectGrantMembersByProjectID(db *gorm.DB, table, projectID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.ProjectGrantMemberSearchKey(proj_model.ProjectGrantMemberSearchKeyProjectID), projectID)
|
||||
return delete(db)
|
||||
}
|
||||
|
@@ -72,3 +72,8 @@ func DeleteProjectGrant(db *gorm.DB, table, grantID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.ProjectGrantSearchKey(proj_model.GrantedProjectSearchKeyGrantID), grantID)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteProjectGrantsByProjectID(db *gorm.DB, table, projectID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.ProjectGrantSearchKey(proj_model.GrantedProjectSearchKeyProjectID), projectID)
|
||||
return delete(db)
|
||||
}
|
||||
|
@@ -22,6 +22,19 @@ func ProjectMemberByIDs(db *gorm.DB, table, projectID, userID string) (*model.Pr
|
||||
return role, err
|
||||
}
|
||||
|
||||
func ProjectMembersByProjectID(db *gorm.DB, table, projectID string) ([]*model.ProjectMemberView, error) {
|
||||
members := make([]*model.ProjectMemberView, 0)
|
||||
queries := []*proj_model.ProjectMemberSearchQuery{
|
||||
&proj_model.ProjectMemberSearchQuery{Key: proj_model.ProjectMemberSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectMemberSearchRequest{Queries: queries})
|
||||
_, err := query(db, &members)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return members, nil
|
||||
}
|
||||
|
||||
func SearchProjectMembers(db *gorm.DB, table string, req *proj_model.ProjectMemberSearchRequest) ([]*model.ProjectMemberView, uint64, error) {
|
||||
roles := make([]*model.ProjectMemberView, 0)
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectMemberSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
||||
@@ -57,3 +70,8 @@ func DeleteProjectMember(db *gorm.DB, table, projectID, userID string) error {
|
||||
delete := repository.PrepareDeleteByObject(table, role)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteProjectMembersByProjectID(db *gorm.DB, table, projectID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.ProjectMemberSearchKey(proj_model.ProjectMemberSearchKeyProjectID), projectID)
|
||||
return delete(db)
|
||||
}
|
||||
|
@@ -23,6 +23,19 @@ func ProjectRoleByIDs(db *gorm.DB, table, projectID, orgID, key string) (*model.
|
||||
return role, err
|
||||
}
|
||||
|
||||
func ProjectRolesByProjectID(db *gorm.DB, table, projectID string) ([]*model.ProjectRoleView, error) {
|
||||
roles := make([]*model.ProjectRoleView, 0)
|
||||
queries := []*proj_model.ProjectRoleSearchQuery{
|
||||
&proj_model.ProjectRoleSearchQuery{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Queries: queries})
|
||||
_, err := query(db, &roles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
func ResourceOwnerProjectRolesByKey(db *gorm.DB, table, projectID, resourceOwner, key string) ([]*model.ProjectRoleView, error) {
|
||||
roles := make([]*model.ProjectRoleView, 0)
|
||||
queries := []*proj_model.ProjectRoleSearchQuery{
|
||||
@@ -75,3 +88,9 @@ func DeleteProjectRole(db *gorm.DB, table, projectID, orgID, key string) error {
|
||||
delete := repository.PrepareDeleteByObject(table, role)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteProjectRolesByProjectID(db *gorm.DB, table, projectID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.ProjectRoleSearchKey(proj_model.ProjectRoleSearchKeyProjectID), projectID)
|
||||
return delete(db)
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user