Skip to content

Commit

Permalink
fix: fix permissions checking
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmatrhd committed Jan 5, 2023
1 parent fac63d5 commit 4f7bffb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion core/grant/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func reduceGrantsByProviderRole(rc *domain.ResourceConfig, grants []*domain.Gran
sort.Strings(allGrantPermissions)

for roleID, permissionsByRole := range rolePermissionsMap {
if containing, headIndex := utils.ContainsOrdered(allGrantPermissions, permissionsByRole); containing {
if containing, headIndex := utils.SubsliceExists(allGrantPermissions, permissionsByRole); containing {
sampleGrant := permissionGrantsMap[allGrantPermissions[0]]
sampleGrant.Role = roleID
sampleGrant.Permissions = permissionsByRole
Expand Down
44 changes: 23 additions & 21 deletions utils/slices.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package utils

func ContainsOrdered(ss []string, lookingFor []string) (bool, int) {
var headFound bool
var headFoundIndex int
func SubsliceExists(slice, subslice []string) (bool, int) {
// check for empty subslice slice
if len(subslice) == 0 {
return true, 0
}

if len(lookingFor) <= 0 {
return true, headFoundIndex
// check if subslice slice is longer than slice
if len(subslice) > len(slice) {
return false, 0
}
head := lookingFor[0]

for i := 0; i < len(ss); i++ {
if !headFound && ss[i] != head {
// skip head mismatch
continue
} else if !headFound {
// mark position in ss when found matching head
headFound = true
headFoundIndex = i
continue
} else if ss[i] != lookingFor[i-headFoundIndex] {
// return false on first mismatch after head
return false, headFoundIndex
} else if len(lookingFor)-1 == i-headFoundIndex {
return true, headFoundIndex
// looking for a subslice
for i := 0; i < len(slice); i++ {
if slice[i] == subslice[0] {
found := true
for j := 1; j < len(subslice); j++ {
if slice[i+j] != subslice[j] {
found = false
break
}
}
if found {
return true, i
}
}
}
return false, headFoundIndex

return false, 0
}
27 changes: 21 additions & 6 deletions utils/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/stretchr/testify/assert"
)

func TestContainsOrdered(t *testing.T) {
func TestSubsliceExists(t *testing.T) {
testCases := []struct {
slice []string
lookingFor []string
subslice []string
expectedResult bool
expectedHeadIndex int
}{
Expand All @@ -30,7 +30,22 @@ func TestContainsOrdered(t *testing.T) {
true, 1,
},
{
// return true for empty `lookingFor` elements
[]string{"a", "b", "c"},
[]string{"b"},
true, 1,
},
{
[]string{"a", "b", "c", "b"},
[]string{"c"},
true, 2,
},
{
[]string{"a", "b", "c", "d"},
[]string{"b", "c", "d"},
true, 1,
},
{
// return true for empty `subslice` elements
[]string{"a", "b", "c", "d"},
[]string{},
true, 0,
Expand All @@ -48,12 +63,12 @@ func TestContainsOrdered(t *testing.T) {
{
[]string{"a", "b", "c", "d"},
[]string{"c", "c", "d"},
false, 2,
false, 0,
},
{
[]string{"a", "b", "c", "d"},
[]string{"b", "d"},
false, 1,
false, 0,
},
{
[]string{"a", "b", "c", "d"},
Expand All @@ -73,7 +88,7 @@ func TestContainsOrdered(t *testing.T) {
}

for _, tc := range testCases {
result, headIndex := utils.ContainsOrdered(tc.slice, tc.lookingFor)
result, headIndex := utils.SubsliceExists(tc.slice, tc.subslice)
assert.Equal(t, tc.expectedResult, result)
assert.Equal(t, tc.expectedHeadIndex, headIndex)
}
Expand Down

0 comments on commit 4f7bffb

Please sign in to comment.