Add endpoint for listing permissions for a resource

This commit is contained in:
Benedikt Kulmann
2020-08-24 16:50:28 +02:00
parent e016ebdec8
commit 5fb1a8a2bb
14 changed files with 1060 additions and 452 deletions
+34
View File
@@ -0,0 +1,34 @@
package store
import (
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"github.com/owncloud/ocis-settings/pkg/util"
)
// ListPermissionsByResource collects all permissions from the provided roleIDs that match the requested resource
func (s Store) ListPermissionsByResource(resource *proto.Resource, roleIDs []string) ([]*proto.Permission, error) {
var records []*proto.Permission
for _, roleID := range roleIDs {
role, err := s.ReadBundle(roleID)
if err != nil {
s.Logger.Debug().Str("roleID", roleID).Msg("role not found, skipping")
continue
}
records = append(records, extractPermissionsByResource(resource, role)...)
}
return records, nil
}
// extractPermissionsByResource collects all permissions from the provided role that match the requested resource
func extractPermissionsByResource(resource *proto.Resource, role *proto.Bundle) []*proto.Permission {
var permissions []*proto.Permission
for _, setting := range role.Settings {
if _, ok := setting.Value.(*proto.Setting_PermissionValue); ok {
value := setting.Value.(*proto.Setting_PermissionValue).PermissionValue
if util.IsResourceMatched(setting.Resource, resource) {
permissions = append(permissions, value)
}
}
}
return permissions
}