From fa5cba9cc79b3bdc48ad42f276652bc699795a39 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Thu, 8 Feb 2018 16:07:40 +0000 Subject: XYZ-76: Add license check to patchRoles endpoint. (#8224) --- model/role.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'model/role.go') diff --git a/model/role.go b/model/role.go index 254513ae0..eedf97d25 100644 --- a/model/role.go +++ b/model/role.go @@ -81,6 +81,41 @@ func (o *Role) Patch(patch *RolePatch) { } } +// Returns an array of permissions that are in either role.Permissions +// or patch.Permissions, but not both. +func PermissionsChangedByPatch(role *Role, patch *RolePatch) []string { + var result []string + + if patch.Permissions == nil { + return result + } + + roleMap := make(map[string]bool) + patchMap := make(map[string]bool) + + for _, permission := range role.Permissions { + roleMap[permission] = true + } + + for _, permission := range *patch.Permissions { + patchMap[permission] = true + } + + for _, permission := range role.Permissions { + if !patchMap[permission] { + result = append(result, permission) + } + } + + for _, permission := range *patch.Permissions { + if !roleMap[permission] { + result = append(result, permission) + } + } + + return result +} + func (role *Role) IsValid() bool { if len(role.Id) != 26 { return false -- cgit v1.2.3-1-g7c22