mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: implement generalized symmetric difference for set comparison (#14407)
* chore: implement generalized symmetric difference for set comparison Going to be used in Organization Sync + maybe group sync. Felt better to reuse, rather than copy
This commit is contained in:
+3
-23
@@ -764,29 +764,9 @@ func SiteRoles() []Role {
|
||||
// RBAC checks can be applied using "ActionCreate" and "ActionDelete" for
|
||||
// "added" and "removed" roles respectively.
|
||||
func ChangeRoleSet(from []RoleIdentifier, to []RoleIdentifier) (added []RoleIdentifier, removed []RoleIdentifier) {
|
||||
has := make(map[RoleIdentifier]struct{})
|
||||
for _, exists := range from {
|
||||
has[exists] = struct{}{}
|
||||
}
|
||||
|
||||
for _, roleName := range to {
|
||||
// If the user already has the role assigned, we don't need to check the permission
|
||||
// to reassign it. Only run permission checks on the difference in the set of
|
||||
// roles.
|
||||
if _, ok := has[roleName]; ok {
|
||||
delete(has, roleName)
|
||||
continue
|
||||
}
|
||||
|
||||
added = append(added, roleName)
|
||||
}
|
||||
|
||||
// Remaining roles are the ones removed/deleted.
|
||||
for roleName := range has {
|
||||
removed = append(removed, roleName)
|
||||
}
|
||||
|
||||
return added, removed
|
||||
return slice.SymmetricDifferenceFunc(from, to, func(a, b RoleIdentifier) bool {
|
||||
return a.Name == b.Name && a.OrganizationID == b.OrganizationID
|
||||
})
|
||||
}
|
||||
|
||||
// Permissions is just a helper function to make building roles that list out resources
|
||||
|
||||
Reference in New Issue
Block a user