Skip to content

Commit

Permalink
update: belong api add source (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu327 authored Dec 20, 2023
1 parent 076e1c2 commit d01af93
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
14 changes: 8 additions & 6 deletions pkg/abac/pap/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,20 @@ func (c *groupController) CheckSubjectEffectGroups(
}

groupIDBelong[groupID] = map[string]interface{}{
"belong": true,
"expired_at": group.ExpiredAt,
"created_at": group.CreatedAt,
"belong": true,
"expired_at": group.ExpiredAt,
"created_at": group.CreatedAt,
"is_direct_added": group.IsDirectAdded,
}
}

for _, groupID := range groupIDs {
if _, ok := groupIDBelong[groupID]; !ok {
groupIDBelong[groupID] = map[string]interface{}{
"belong": false,
"expired_at": 0,
"created_at": time.Time{},
"belong": false,
"expired_at": 0,
"created_at": time.Time{},
"is_direct_added": false,
}
}
}
Expand Down
39 changes: 22 additions & 17 deletions pkg/abac/pap/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ var _ = Describe("GroupController", func() {
It("ok, all groupID valid", func() {
mockGroupService := mock.NewMockGroupService(ctl)
mockGroupService.EXPECT().ListEffectSubjectGroupsBySubjectPKGroupPKs(gomock.Any(), gomock.Any()).Return(
[]types.SubjectGroup{{
[]types.SubjectGroupWithSource{{
GroupPK: 10,
ExpiredAt: 1,
CreatedAt: time.Time{},
Expand All @@ -435,21 +435,23 @@ var _ = Describe("GroupController", func() {
assert.NoError(GinkgoT(), err)
assert.Len(GinkgoT(), groupIDBelong, 2)
assert.Equal(GinkgoT(), map[string]interface{}{
"belong": true,
"expired_at": int64(1),
"created_at": time.Time{},
"belong": true,
"expired_at": int64(1),
"created_at": time.Time{},
"is_direct_added": false,
}, groupIDBelong["10"])
assert.Equal(GinkgoT(), map[string]interface{}{
"belong": false,
"expired_at": 0,
"created_at": time.Time{},
"belong": false,
"expired_at": 0,
"created_at": time.Time{},
"is_direct_added": false,
}, groupIDBelong["20"])
})

It("ok, has invalid groupID", func() {
mockGroupService := mock.NewMockGroupService(ctl)
mockGroupService.EXPECT().ListEffectSubjectGroupsBySubjectPKGroupPKs(gomock.Any(), gomock.Any()).Return(
[]types.SubjectGroup{{
[]types.SubjectGroupWithSource{{
GroupPK: 10,
ExpiredAt: 1,
}, {
Expand All @@ -466,19 +468,22 @@ var _ = Describe("GroupController", func() {
assert.NoError(GinkgoT(), err)
assert.Len(GinkgoT(), groupIDBelong, 3)
assert.Equal(GinkgoT(), map[string]interface{}{
"belong": true,
"expired_at": int64(1),
"created_at": time.Time{},
"belong": true,
"expired_at": int64(1),
"created_at": time.Time{},
"is_direct_added": false,
}, groupIDBelong["10"])
assert.Equal(GinkgoT(), map[string]interface{}{
"belong": false,
"expired_at": 0,
"created_at": time.Time{},
"belong": false,
"expired_at": 0,
"created_at": time.Time{},
"is_direct_added": false,
}, groupIDBelong["20"])
assert.Equal(GinkgoT(), map[string]interface{}{
"belong": false,
"expired_at": 0,
"created_at": time.Time{},
"belong": false,
"expired_at": 0,
"created_at": time.Time{},
"is_direct_added": false,
}, groupIDBelong["invalid"])
})
})
Expand Down
19 changes: 14 additions & 5 deletions pkg/service/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ type GroupService interface {
ListPagingSubjectSystemGroups(
subjectPK int64, systemID string, beforeExpiredAt, limit, offset int64,
) ([]types.SubjectGroup, error)
ListEffectSubjectGroupsBySubjectPKGroupPKs(subjectPK int64, groupPKs []int64) ([]types.SubjectGroup, error)
ListEffectSubjectGroupsBySubjectPKGroupPKs(
subjectPK int64,
groupPKs []int64,
) ([]types.SubjectGroupWithSource, error)
FilterGroupPKsHasMemberBeforeExpiredAt(groupPKs []int64, expiredAt int64) ([]int64, error)

BulkDeleteBySubjectPKsWithTx(tx *sqlx.Tx, subjectPKs []int64) error
Expand Down Expand Up @@ -280,7 +283,7 @@ func (l *groupService) FilterGroupPKsHasMemberBeforeExpiredAt(
func (l *groupService) ListEffectSubjectGroupsBySubjectPKGroupPKs(
subjectPK int64,
groupPKs []int64,
) (subjectGroups []types.SubjectGroup, err error) {
) (subjectGroups []types.SubjectGroupWithSource, err error) {
errorWrapf := errorx.NewLayerFunctionErrorWrapf(GroupSVC, "ListEffectSubjectGroupsBySubjectPKGroupPKs")

relations, err := l.manager.ListRelationBySubjectPKGroupPKs(subjectPK, groupPKs)
Expand All @@ -303,9 +306,15 @@ func (l *groupService) ListEffectSubjectGroupsBySubjectPKGroupPKs(

groupPKset := set.NewInt64Set()

subjectGroups = make([]types.SubjectGroup, 0, len(relations)+len(templateRelations))
subjectGroups = make([]types.SubjectGroupWithSource, 0, len(relations)+len(templateRelations))
for _, r := range relations {
subjectGroups = append(subjectGroups, convertToSubjectGroup(r))
subjectGroups = append(subjectGroups, types.SubjectGroupWithSource{
PK: r.PK,
GroupPK: r.GroupPK,
ExpiredAt: r.ExpiredAt,
CreatedAt: r.CreatedAt,
IsDirectAdded: true,
})

groupPKset.Add(r.GroupPK)
}
Expand All @@ -315,7 +324,7 @@ func (l *groupService) ListEffectSubjectGroupsBySubjectPKGroupPKs(
continue
}

subjectGroups = append(subjectGroups, types.SubjectGroup{
subjectGroups = append(subjectGroups, types.SubjectGroupWithSource{
PK: r.PK,
GroupPK: r.GroupPK,
ExpiredAt: r.ExpiredAt,
Expand Down
7 changes: 4 additions & 3 deletions pkg/service/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,10 @@ var _ = Describe("GroupService", func() {

subjectGroups, err := manager.ListEffectSubjectGroupsBySubjectPKGroupPKs(int64(123), []int64{1})
assert.NoError(GinkgoT(), err)
assert.ElementsMatch(GinkgoT(), []types.SubjectGroup{{
GroupPK: 1,
ExpiredAt: 1,
assert.ElementsMatch(GinkgoT(), []types.SubjectGroupWithSource{{
GroupPK: 1,
ExpiredAt: 1,
IsDirectAdded: true,
}, {
GroupPK: 2,
ExpiredAt: 1,
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/mock/group.go

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

9 changes: 9 additions & 0 deletions pkg/service/types/subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ type SubjectGroup struct {
CreatedAt time.Time `json:"created_at"`
}

// SubjectGroup subject关联的组
type SubjectGroupWithSource struct {
PK int64 `json:"pk"`
GroupPK int64 `json:"group_pk"`
ExpiredAt int64 `json:"expired_at"`
CreatedAt time.Time `json:"created_at"`
IsDirectAdded bool `json:"is_direct_added"`
}

// GroupSubject 关系数据
type GroupSubject struct {
SubjectPK int64 `json:"subject_pk"`
Expand Down

0 comments on commit d01af93

Please sign in to comment.