예제 #1
0
        public async ValueTask <T> GetPermissionValue <T>(PermissionDescriptor <T> descriptor)
        {
            foreach (var layer in _stack.Reverse())
            {
                if (layer.TryGetValue(descriptor.Key, out var value))
                {
                    switch (descriptor.Type)
                    {
                    case PermissionValueType.Boolean:
                        return((T)(object)value.ToObject <bool>());

                    case PermissionValueType.Integer:
                        return((T)(object)value.ToObject <int>());

                    case PermissionValueType.Decimal:
                        return((T)(object)value.ToObject <double>());

                    case PermissionValueType.Text:
                        var val = value.ToObject <string?>();
                        if (val == null)
                        {
                            throw new NullReferenceException("The permission descriptor has a null value.");
                        }

                        return((T)(object)val);

                    default:
                        throw new ArgumentOutOfRangeException(nameof(descriptor),
                                                              $"Invalid descriptor type. \"{descriptor.Type}\" is not supported.");
                    }
                }
            }

            return((T)descriptor.DefaultValue);
        }
예제 #2
0
 public async ValueTask <T> GetPermissionValue <T>(PermissionDescriptor <T> descriptor)
 {
     return(await _repo.GetPermissionsValue <T>(_participant, descriptor.Key) ?? (T)descriptor.DefaultValue);
 }