Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: delete subject template group when delete subject #286

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/database/dao/group_resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func (m *groupResourcePolicyManager) BulkDeleteByGroupPKsWithTx(
tx *sqlx.Tx,
groupPKs []int64,
) error {
if len(groupPKs) == 0 {
return nil
}

sql := `DELETE FROM rbac_group_resource_policy WHERE group_pk IN (?)`
return database.SqlxDeleteWithTx(tx, sql, groupPKs)
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/database/dao/mock/subject_template_group.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pkg/database/dao/subject_template_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type SubjectTemplateGroup struct {

type SubjectTemplateGroupManager interface {
GetTemplateGroupMemberCount(groupPK, templateID int64) (int64, error)
GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64, excludeTemplateID int64) (int64, error)
ListPagingTemplateGroupMember(
groupPK, templateID int64,
limit, offset int64,
Expand All @@ -46,7 +47,7 @@ type SubjectTemplateGroupManager interface {
BulkUpdateExpiredAtWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error
BulkUpdateExpiredAtByRelationWithTx(tx *sqlx.Tx, relations []SubjectRelation) error
BulkDeleteWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error
GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64, excludeTemplateID int64) (int64, error)
BulkDeleteBySubjectPKs(tx *sqlx.Tx, subjectPKs []int64) error
zhu327 marked this conversation as resolved.
Show resolved Hide resolved
}

type subjectTemplateGroupManager struct {
Expand Down Expand Up @@ -235,3 +236,9 @@ func (m *subjectTemplateGroupManager) ListThinRelationWithMaxExpiredAtByGroupPK(

return relations, err
}

func (m *subjectTemplateGroupManager) BulkDeleteBySubjectPKs(tx *sqlx.Tx, subjectPKs []int64) error {
sql := `DELETE FROM subject_template_group
WHERE subject_pk in (?)`
return database.SqlxDeleteWithTx(tx, sql, subjectPKs)
}
10 changes: 10 additions & 0 deletions pkg/service/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,16 @@ func (l *groupService) BulkDeleteBySubjectPKsWithTx(tx *sqlx.Tx, subjectPKs []in
err, "manager.BulkDeleteBySubjectPKs subject_pks=`%+v` fail", subjectPKs)
}

// 批量删除subject template group
err = l.subjectTemplateGroupManager.BulkDeleteBySubjectPKs(tx, subjectPKs)
if err != nil {
return errorWrapf(
err,
"subjectTemplateGroupManager.BulkDeleteBySubjectPKsWithTx subjectPKs=`%+v` fail",
subjectPKs,
)
}

// 批量删除用户的subject system group
err = l.subjectSystemGroupManager.DeleteBySubjectPKsWithTx(tx, subjectPKs)
if err != nil {
Expand Down
42 changes: 38 additions & 4 deletions pkg/service/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,49 @@ var _ = Describe("GroupService", func() {
assert.Contains(GinkgoT(), err.Error(), "BulkDeleteBySubjectPKs")
})

It("manager.BulkDeleteBySubjectPKs fail", func() {
mockSubjectService := mock.NewMockSubjectGroupManager(ctl)

mockSubjectService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl)
mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
errors.New("error"),
).AnyTimes()

manager := &groupService{
manager: mockSubjectService,
subjectTemplateGroupManager: mockSubjectTemplateGroupService,
}

err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2})
assert.Error(GinkgoT(), err)
assert.Contains(GinkgoT(), err.Error(), "subjectTemplateGroupManager.BulkDeleteBySubjectPKsWithTx")
})

It("subjectSystemGroupManager.DeleteBySubjectPKsWithTx fail", func() {
mockSubjectService := mock.NewMockSubjectGroupManager(ctl)

mockSubjectService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl)
mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

mockSubjectSystemGroupService := mock.NewMockSubjectSystemGroupManager(ctl)
mockSubjectSystemGroupService.EXPECT().DeleteBySubjectPKsWithTx(gomock.Any(), []int64{1, 2}).Return(
errors.New("error"),
).AnyTimes()

manager := &groupService{
manager: mockSubjectService,
subjectSystemGroupManager: mockSubjectSystemGroupService,
manager: mockSubjectService,
subjectSystemGroupManager: mockSubjectSystemGroupService,
subjectTemplateGroupManager: mockSubjectTemplateGroupService,
}

err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2})
Expand All @@ -123,14 +151,20 @@ var _ = Describe("GroupService", func() {
nil,
).AnyTimes()

mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl)
mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

mockSubjectSystemGroupService := mock.NewMockSubjectSystemGroupManager(ctl)
mockSubjectSystemGroupService.EXPECT().DeleteBySubjectPKsWithTx(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

manager := &groupService{
manager: mockSubjectService,
subjectSystemGroupManager: mockSubjectSystemGroupService,
manager: mockSubjectService,
subjectSystemGroupManager: mockSubjectSystemGroupService,
subjectTemplateGroupManager: mockSubjectTemplateGroupService,
}

err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2})
Expand Down