Exemple #1
0
        public void AllPersonAttributeColumnsCanBuild()
        {
            var dataContext = new RockContext();

            var personService = new PersonService(dataContext);

            // Get Admin Person as Report User.
            var adminPerson = GetAdminPersonOrThrow(personService);

            var parameterExpression = personService.ParameterExpression;

            var filterQuery = personService.Queryable();

            var filterExpression = FilterExpressionExtractor.Extract <Person>(filterQuery, parameterExpression, "p");

            // Get Person Attributes
            var person = new Person();

            person.LoadAttributes();

            // Create a report for each Person Attribute, and build the output.
            var sortedAttributes = person.Attributes.OrderBy(x => x.Value.Name).Select(x => x.Value).ToList();

            foreach (var attribute in sortedAttributes)
            {
                Debug.Print($"Building Person Report [IncludedAttribute={attribute.Name}]...");

                // Create the Report Template.
                var templateBuilder = new ReportTemplateBuilder(typeof(Person));

                var attributeName = attribute.Name;

                var field = templateBuilder.AddAttributeField(attribute.Guid, "AttributeValue");

                field.SortDirection = System.Web.UI.WebControls.SortDirection.Ascending;
                field.SortOrder     = 1;

                var report = templateBuilder.Report;

                // Build the output data for the Report by combining the report template with the filter.
                var builder = new ReportOutputBuilder(report, dataContext);

                var results = builder.GetReportData(adminPerson,
                                                    filterExpression,
                                                    parameterExpression,
                                                    dataContext,
                                                    ReportOutputBuilder.ReportOutputBuilderFieldContentSpecifier.FormattedText);

                var dataTable = results.Data;

                var valueCount = dataTable.Select("[AttributeValue] > ''").Count();

                if (valueCount == 0)
                {
                    Debug.Print($"WARNING: Report contains no values for this Attribute.");
                }
            }
        }
Exemple #2
0
        public void UnauthorizedUserCannotViewAttributeColumnOutput()
        {
            var dataContext = new RockContext();

            var personService = new PersonService(dataContext);

            // Get Admin User (authorized) and Staff Member (unauthorized) as our Report Users.
            var adminPerson        = GetAdminPersonOrThrow(personService);
            var unauthorizedPerson = GetStaffPersonOrThrow(personService);

            // Create a basic query for the report.
            var parameterExpression = personService.ParameterExpression;

            var filterQuery = personService.Queryable();

            var filterExpression = FilterExpressionExtractor.Extract <Person>(filterQuery, parameterExpression, "p");

            // Get a Person Attribute for which the unauthorized Person does not have View permission.
            var person = new Person();

            person.LoadAttributes();

            var unauthorizedAttribute = person.Attributes
                                        .Select(x => x.Value).FirstOrDefault(a => !a.IsAuthorized(Rock.Security.Authorization.VIEW, unauthorizedPerson));

            Assert.That.IsNotNull(unauthorizedAttribute, "Test User must have at least one unauthorized Attribute.");

            // Create a report template containing the test Attribute.
            var templateBuilder = new ReportTemplateBuilder(typeof(Person));

            var attributeName = unauthorizedAttribute.Name;

            var field = templateBuilder.AddAttributeField(unauthorizedAttribute.Guid, "AttributeValue");

            field.SortDirection = System.Web.UI.WebControls.SortDirection.Ascending;
            field.SortOrder     = 1;

            var report = templateBuilder.Report;

            // Build the output data for the Report by combining the report template with the filter.
            var builder = new ReportOutputBuilder(report, dataContext);

            // Build and verify the report output for the authorized user.
            var results1 = builder.GetReportData(adminPerson,
                                                 filterExpression,
                                                 parameterExpression,
                                                 dataContext,
                                                 ReportOutputBuilder.ReportOutputBuilderFieldContentSpecifier.FormattedText);

            var valueCount1 = results1.Data.Select("[AttributeValue] > ''").Count();

            Assert.That.IsTrue(valueCount1 > 0, "Attribute column must contain at least one value.");

            // Build and verify the report output for the unauthorized user.
            builder.OutputFieldMask = "@@@";

            var results2 = builder.GetReportData(unauthorizedPerson,
                                                 filterExpression,
                                                 parameterExpression,
                                                 dataContext,
                                                 ReportOutputBuilder.ReportOutputBuilderFieldContentSpecifier.FormattedText);

            var valueCount2 = results2.Data.Select("[AttributeValue] <> '@@@'").Count();

            Assert.That.IsTrue((valueCount2 == 0), "Attribute column contains unauthorized values.");
        }