Skip to content

Commit

Permalink
fix: update delete relation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu327 committed Oct 26, 2023
1 parent f336a4f commit 496d06d
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 355 deletions.
17 changes: 11 additions & 6 deletions pkg/abac/pap/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,18 +582,23 @@ func (c *groupController) BulkDeleteSubjectTemplateGroup(subjectTemplateGroups [
return errorWrapf(err, "convertToSubjectTemplateGroups subjectTemplateGroups=`%+v` fail", subjectTemplateGroups)
}

// 查询是否有其它的关系
now := time.Now().Unix()
for i := range relations {
relation := &relations[i]

exist, err := c.service.HasRelationExceptTemplate(*relation)
if err != nil {
return errorWrapf(err, "service.HasRelationExceptTemplate relation=`%+v` fail", relation)
expiredAt, err := c.service.GetMaxExpiredAtBySubjectGroup(relation.SubjectPK, relation.GroupPK)
if err != nil && !errors.Is(err, service.ErrGroupMemberNotFound) {
return errorWrapf(
err, "GetMaxExpiredAtBySubjectGroup subjectPK=`%d`, groupPK=`%d` fail",
)
}

if !exist {
relation.NeedUpdate = true
// 如果有其它的关系, 并且过期时间大于当前时间, 不需要删除
if err == nil && expiredAt > now {
continue
}

relation.NeedUpdate = true
}

// 如果没有其他关系了需要删除subject system group数据
Expand Down
25 changes: 5 additions & 20 deletions pkg/abac/pap/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,14 +1076,9 @@ var _ = Describe("GroupController", func() {
assert.Contains(GinkgoT(), err.Error(), "convertToSubjectTemplateGroups")
})

It("HasRelationExceptTemplate fail", func() {
It("GetMaxExpiredAtBySubjectGroup fail", func() {
mockService := mock.NewMockGroupService(ctl)
mockService.EXPECT().HasRelationExceptTemplate(types.SubjectTemplateGroup{
SubjectPK: 1,
TemplateID: 1,
GroupPK: 2,
ExpiredAt: 3,
}).Return(false, errors.New("err"))
mockService.EXPECT().GetMaxExpiredAtBySubjectGroup(int64(1), int64(2)).Return(int64(0), errors.New("err"))

manager := &groupController{
service: mockService,
Expand All @@ -1099,17 +1094,12 @@ var _ = Describe("GroupController", func() {
},
})
assert.Error(GinkgoT(), err)
assert.Contains(GinkgoT(), err.Error(), "HasRelationExceptTemplate")
assert.Contains(GinkgoT(), err.Error(), "GetMaxExpiredAtBySubjectGroup")
})

It("BulkDeleteSubjectTemplateGroupWithTx fail", func() {
mockService := mock.NewMockGroupService(ctl)
mockService.EXPECT().HasRelationExceptTemplate(types.SubjectTemplateGroup{
SubjectPK: 1,
TemplateID: 1,
GroupPK: 2,
ExpiredAt: 3,
}).Return(true, nil)
mockService.EXPECT().GetMaxExpiredAtBySubjectGroup(int64(1), int64(2)).Return(time.Now().Unix()+10, nil)
mockService.EXPECT().
BulkDeleteSubjectTemplateGroupWithTx(gomock.Any(), gomock.Any()).
Return(errors.New("err"))
Expand Down Expand Up @@ -1142,12 +1132,7 @@ var _ = Describe("GroupController", func() {

It("ok", func() {
mockService := mock.NewMockGroupService(ctl)
mockService.EXPECT().HasRelationExceptTemplate(types.SubjectTemplateGroup{
SubjectPK: 1,
TemplateID: 1,
GroupPK: 2,
ExpiredAt: 3,
}).Return(true, nil)
mockService.EXPECT().GetMaxExpiredAtBySubjectGroup(int64(1), int64(2)).Return(time.Now().Unix()+10, nil)
mockService.EXPECT().BulkDeleteSubjectTemplateGroupWithTx(gomock.Any(), gomock.Any()).Return(nil)

db, mock := database.NewMockSqlxDB()
Expand Down
15 changes: 0 additions & 15 deletions pkg/database/dao/mock/subject_group.go

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

27 changes: 6 additions & 21 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.

19 changes: 0 additions & 19 deletions pkg/database/dao/subject_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ type SubjectGroupManager interface {
limit, offset int64,
) (members []ThinSubjectRelation, err error)
ListRelationBySubjectPKGroupPKs(subjectPK int64, groupPKs []int64) ([]SubjectRelation, error)
HasRelation(subjectPK, groupPK int64) (bool, error)

FilterGroupPKsHasMemberBeforeExpiredAt(groupPKs []int64, expiredAt int64) ([]int64, error)

Expand Down Expand Up @@ -108,24 +107,6 @@ func (m *subjectGroupManager) GetSubjectGroupCount(subjectPK int64) (int64, erro
return count, err
}

func (m *subjectGroupManager) HasRelation(subjectPK, groupPK int64) (bool, error) {
var pk int64
query := `SELECT
pk
FROM subject_relation
WHERE subject_pk = ?
AND parent_pk = ?
LIMIT 1`
err := database.SqlxGet(m.DB, &pk, query, subjectPK, groupPK)
if err == sql.ErrNoRows {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}

// GetSubjectSystemGroupCount ...
func (m *subjectGroupManager) GetSubjectSystemGroupCount(subjectPK int64, systemID string) (int64, error) {
var count int64
Expand Down
18 changes: 0 additions & 18 deletions pkg/database/dao/subject_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,3 @@ func Test_subjectRelationManager_BulkUpdateExpiredAtWithTx(t *testing.T) {
assert.NoError(t, err, "query from db fail.")
})
}

func Test_subjectRelationManager_HasRelation(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT pk FROM subject_relation WHERE subject_pk = (.*) AND parent_pk = (.*) LIMIT 1`
mockRows := sqlmock.NewRows(
[]string{
"pk",
},
).AddRow(int64(1))
mock.ExpectQuery(mockQuery).WithArgs(int64(1), int64(2)).WillReturnRows(mockRows)

manager := &subjectGroupManager{DB: db}
exist, err := manager.HasRelation(int64(1), int64(2))

assert.NoError(t, err, "query from db fail.")
assert.True(t, exist)
})
}
30 changes: 4 additions & 26 deletions pkg/database/dao/subject_template_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package dao
//go:generate mockgen -source=$GOFILE -destination=./mock/$GOFILE -package=mock

import (
"database/sql"
"time"

"github.com/jmoiron/sqlx"
Expand All @@ -35,8 +34,7 @@ type SubjectTemplateGroupManager interface {
BulkCreateWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error
BulkUpdateExpiredAtWithTx(tx *sqlx.Tx, relations []SubjectRelation) error
BulkDeleteWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error
HasRelationExceptTemplate(subjectPK, groupPK, templateID int64) (bool, error)
GetExpiredAtBySubjectGroup(subjectPK, groupPK int64) (int64, error)
GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64) (int64, error)
}

type subjectTemplateGroupManager struct {
Expand Down Expand Up @@ -92,33 +90,13 @@ func (m *subjectTemplateGroupManager) BulkDeleteWithTx(tx *sqlx.Tx, relations []
return database.SqlxBulkUpdateWithTx(tx, sql, relations)
}

func (m *subjectTemplateGroupManager) HasRelationExceptTemplate(subjectPK, groupPK, templateID int64) (bool, error) {
var pk int64
query := `SELECT
pk
FROM subject_template_group
WHERE subject_pk = ?
AND group_pk = ?
AND template_id != ?
LIMIT 1`
err := database.SqlxGet(m.DB, &pk, query, subjectPK, groupPK, templateID)
if err == sql.ErrNoRows {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}

func (m *subjectTemplateGroupManager) GetExpiredAtBySubjectGroup(subjectPK, groupPK int64) (int64, error) {
func (m *subjectTemplateGroupManager) GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64) (int64, error) {
var expiredAt int64
query := `SELECT
expired_at
MAX(expired_at)
FROM subject_template_group
WHERE subject_pk = ?
AND group_pk = ?
LIMIT 1`
AND group_pk = ?`
err := database.SqlxGet(m.DB, &expiredAt, query, subjectPK, groupPK)
return expiredAt, err
}
22 changes: 2 additions & 20 deletions pkg/database/dao/subject_template_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,14 @@ func Test_subjectTemplateGroupManager_BulkDeleteWithTx(t *testing.T) {
})
}

func Test_subjectTemplateGroupManager_HasRelationExceptTemplate(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT pk FROM subject_template_group WHERE subject_pk =`
mockRows := sqlmock.NewRows(
[]string{
"pk",
},
).AddRow(int64(1))
mock.ExpectQuery(mockQuery).WithArgs(int64(1), int64(2), int64(3)).WillReturnRows(mockRows)

manager := &subjectTemplateGroupManager{DB: db}
exist, err := manager.HasRelationExceptTemplate(int64(1), int64(2), int64(3))

assert.NoError(t, err, "query from db fail.")
assert.True(t, exist)
})
}

func Test_subjectTemplateGroupManager_GetExpiredAtBySubjectGroup(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT expired_at FROM subject_template_group WHERE subject_pk =`
mockQuery := `^SELECT (.*) FROM subject_template_group WHERE subject_pk =`
mockRows := sqlmock.NewRows([]string{"policy_expired_at"}).AddRow(int64(1))
mock.ExpectQuery(mockQuery).WithArgs(int64(1), int64(10)).WillReturnRows(mockRows)

manager := &subjectTemplateGroupManager{DB: db}
expiredAt, err := manager.GetExpiredAtBySubjectGroup(int64(1), int64(10))
expiredAt, err := manager.GetMaxExpiredAtBySubjectGroup(int64(1), int64(10))

assert.NoError(t, err, "query from db fail.")
assert.Equal(t, expiredAt, int64(1))
Expand Down
Loading

0 comments on commit 496d06d

Please sign in to comment.