public void Evaluate_InvalidExpressionThrows(string expression)
        {
            var tokens = PermissionExpressionParser.Tokenize(expression);
            var rpn    = PermissionExpressionParser.ShuntingYard(tokens);

            Assert.Throws <InvalidOperationException>(() =>
            {
                PermissionExpressionParser.Evaluate(rpn, HasPermission);
            });
        }
        public void Evaluate_Expressions(string expression, bool expected)
        {
            var tokens = PermissionExpressionParser.Tokenize(expression);
            var rpn    = PermissionExpressionParser.ShuntingYard(tokens);
            var actual = PermissionExpressionParser.Evaluate(rpn, HasPermission);

            if (expected)
            {
                Assert.True(actual);
            }
            else
            {
                Assert.False(actual);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns true if user has specified permission
        /// </summary>
        /// <param name="permission">Permission to check</param>
        /// <returns>True if user has specified permission</returns>
        public bool HasPermission(string permission)
        {
            if (string.IsNullOrEmpty(permission) ||
                permission.IndexOfAny(chars) < 0)
            {
                return(permissionService.HasPermission(permission));
            }

            string[] rpnTokens;
            if (!cache.TryGetValue(permission, out rpnTokens))
            {
                var tokens = PermissionExpressionParser.Tokenize(permission);
                cache[permission] = rpnTokens = PermissionExpressionParser.ShuntingYard(tokens).ToArray();
            }

            return(PermissionExpressionParser.Evaluate(rpnTokens, permissionService.HasPermission));
        }