public virtual PermissionDefinition AddPermission([NotNull] string name, string?displayName = null, bool isEnabled = true)
        {
            var permission = new PermissionDefinition(name, displayName, isEnabled);

            _permissions.Add(permission);
            return(permission);
        }
Esempio n. 2
0
        public virtual PermissionDefinition AddChild([NotNull] string name, string?displayName = null, bool isEnabled = true)
        {
            var child = new PermissionDefinition(name, displayName, isEnabled)
            {
                Parent = this
            };

            _children.Add(child);
            return(child);
        }
        public async Task <PermissionGrantResult> CheckAsync(ClaimsPrincipal principal, PermissionDefinition permission)
        {
            var userId = principal?.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (userId is null)
            {
                return(PermissionGrantResult.Undefined);
            }

            return(await _permissionStore.IsGrantedAsync(permission.Name, Name, userId) ? PermissionGrantResult.Granted : PermissionGrantResult.Undefined);
        }
        public async Task <PermissionGrantResult> CheckAsync(ClaimsPrincipal principal, PermissionDefinition permission)
        {
            var roles = principal?.FindAll(ClaimTypes.Role).Select(c => c.Value).ToArray();

            if (roles == null || !roles.Any())
            {
                return(PermissionGrantResult.Undefined);
            }

            foreach (var role in roles)
            {
                if (await _permissionStore.IsGrantedAsync(permission.Name, Name !, role))
                {
                    return(PermissionGrantResult.Granted);
                }
            }

            return(PermissionGrantResult.Undefined);
        }
        protected virtual void AddPermissionToDictionaryRecursively(Dictionary <string, PermissionDefinition> permissions, PermissionDefinition permission)
        {
            if (permissions.ContainsKey(permission.Name))
            {
                throw new InvalidOperationException($"Duplicate permission name {permission.Name}");
            }

            permissions[permission.Name] = permission;

            foreach (var child in permission.Children)
            {
                AddPermissionToDictionaryRecursively(permissions, child);
            }
        }
        private void AddPermissionToListRecursively(List <PermissionDefinition> permissions, PermissionDefinition permission)
        {
            permissions.Add(permission);

            foreach (var child in permission.Children)
            {
                AddPermissionToListRecursively(permissions, child);
            }
        }