Skip to content

Commit e496c71

Browse files
committed
fix(contrib/auth): correct roleGiven and roleRemoved checks to use slice containment instead of aggregate ID comparison
refactor(auth/permissions.go): import internal slice package and use slice.Unique to remove duplicate roles in finalize method
1 parent b5c1457 commit e496c71

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

contrib/auth/permissions.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/modernice/goes/aggregate"
1010
"github.com/modernice/goes/event"
1111
"github.com/modernice/goes/helper/pick"
12+
"github.com/modernice/goes/internal/slice"
1213
"github.com/modernice/goes/projection"
1314
)
1415

@@ -161,7 +162,7 @@ func (perms *Permissions) revoked(evt event.Of[PermissionRevokedData]) {
161162
}
162163

163164
func (perms *Permissions) roleGiven(evt event.Of[[]uuid.UUID]) {
164-
if perms.ActorID != pick.AggregateID(evt) {
165+
if !slices.Contains(evt.Data(), perms.ActorID) {
165166
return
166167
}
167168

@@ -170,7 +171,7 @@ func (perms *Permissions) roleGiven(evt event.Of[[]uuid.UUID]) {
170171
}
171172

172173
func (perms *Permissions) roleRemoved(evt event.Of[[]uuid.UUID]) {
173-
if perms.ActorID != pick.AggregateID(evt) {
174+
if !slices.Contains(evt.Data(), perms.ActorID) {
174175
return
175176
}
176177

@@ -185,9 +186,12 @@ func (perms *Permissions) roleRemoved(evt event.Of[[]uuid.UUID]) {
185186
}
186187

187188
func (perms *Permissions) finalize(ctx context.Context, roles RoleRepository) error {
189+
perms.Roles = slice.Unique(perms.Roles)
190+
188191
if !perms.rolesHaveChanged {
189192
return nil
190193
}
194+
191195
perms.rolesHaveChanged = false
192196
perms.OfRoles = make(Actions)
193197

0 commit comments

Comments
 (0)