public void TestParseOnlyClosingBracket()
        {
            string textToParse = "test.property}_update";

            // when, then:
            Assert.Throws <InvalidOperationException>(() => AttributeExpressionParser.Parse(textToParse));
        }
        public void TestParseOnlyOpeningBracket()
        {
            string textToParse = "user_{test.property1_read";

            // when:then:
            Assert.Throws <InvalidOperationException>(() => AttributeExpressionParser.Parse(textToParse));
        }
Пример #3
0
        /// <summary>
        ///     Liefert alle <see cref="IGrantedAuthority" /> der angebenen Rolle für den Nutzer im AuthotizationManager.
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public IList <IGrantedAuthority> GetAuthorities(IList <string> roles)
        {
            Require.NotNull(roles, nameof(roles));

            List <IGrantedAuthority> grantedAuthorities = new List <IGrantedAuthority>();

            foreach (string role in roles)
            {
                if (_rolesToPermissions.ContainsKey(role))
                {
                    // Rolle als Authority hinzufügen
                    grantedAuthorities.Add(new SimpleGrantedAuthority(role));
                    foreach (string permission in _rolesToPermissions[role])
                    {
                        if (AttributeExpressionParser.IsExpression(permission))
                        {
                            SimpleGrantedAuthority authorityForExpression = GetAuthorityForExpression(permission);
                            if (!grantedAuthorities.Contains(authorityForExpression))
                            {
                                grantedAuthorities.Add(authorityForExpression);
                            }
                        }
                        else
                        {
                            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission);
                            if (!grantedAuthorities.Contains(authority))
                            {
                                grantedAuthorities.Add(authority);
                            }
                        }
                    }
                }
            }
            return(grantedAuthorities);
        }
        public void TestIsExpressionBracketsPermuted()
        {
            string testText = "eins}zwei{drei";
            // when:
            bool isExpression = AttributeExpressionParser.IsExpression(testText);

            // then:
            isExpression.ShouldBeEquivalentTo(false);
        }
        public void TestIsExpressionOnlyClosingBracket()
        {
            string testText = "einszwei}drei";
            // when:
            bool isExpression = AttributeExpressionParser.IsExpression(testText);

            // then:
            isExpression.ShouldBeEquivalentTo(false);
        }
        public void TestIsExpressionTrue()
        {
            string testText = "eins{zwei}drei";
            // when:
            bool isExpression = AttributeExpressionParser.IsExpression(testText);

            // then
            isExpression.ShouldBeEquivalentTo(true);
        }
        public void TestOnlyExpressionTest()
        {
            string textToParse = "{test.property1}";
            // when:
            string actualExpression = AttributeExpressionParser.Parse(textToParse);

            // then:
            actualExpression.ShouldBeEquivalentTo("test.property1");
        }
        public void TestStandardText()
        {
            string textToParse = "test_{12345.test}_update";
            // when:
            string actualExpression = AttributeExpressionParser.Parse(textToParse);

            // then:
            actualExpression.ShouldBeEquivalentTo("12345.test");
        }
Пример #9
0
        private SimpleGrantedAuthority GetAuthorityForExpression(string permission)
        {
            // Wenn es keinen Nutzer gibt, kann nichts evaluiert werden, also gibt es auch keine Berechtigung.
            if (_user == null)
            {
                return(null);
            }
            string expression          = AttributeExpressionParser.Parse(permission);
            object evaluatedExpression = ExpressionEvaluator.GetValue(_user, expression);

            if (evaluatedExpression == null)
            {
                // Es konnte kein Wert bestimmt werden, also gibt es auch keine Berechtigung.
                return(null);
            }
            string first = permission.Split('{').First();
            string last  = permission.Split('}').Last();
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(first + evaluatedExpression + last);

            return(simpleGrantedAuthority);
        }