Exemplo n.º 1
0
        public static async Task <IFilter> GetUserCertificateFilter(IEnumerable <Guid> necessaryMias)
        {
            UserProfile     userProfile               = null;
            bool            applyUserRestrictions     = false;
            IUserManagement userProfileDataManagement = ServiceRegistration.Get <IUserManagement>();

            if (userProfileDataManagement != null && userProfileDataManagement.IsValidUser)
            {
                userProfile           = userProfileDataManagement.CurrentUser;
                applyUserRestrictions = userProfileDataManagement.ApplyUserRestriction;
            }
            if (userProfile == null || !applyUserRestrictions)
            {
                return(null);
            }

            ICollection <Share>      shares = null;
            IServerConnectionManager serverConnectionManager = ServiceRegistration.Get <IServerConnectionManager>();
            IContentDirectory        contentDirectory        = serverConnectionManager.ContentDirectory;

            if (contentDirectory != null)
            {
                shares = await contentDirectory.GetSharesAsync(null, SharesFilter.All);
            }

            int?           allowedAge = null;
            bool?          includeParentalGuidedContent = null;
            bool?          includeUnratedContent        = null;
            bool           allowAllShares = true;
            bool           allowAllAges   = true;
            List <IFilter> shareFilters   = new List <IFilter>();

            foreach (var key in userProfile.AdditionalData)
            {
                foreach (var val in key.Value)
                {
                    if (key.Key == UserDataKeysKnown.KEY_ALLOW_ALL_SHARES)
                    {
                        string allow = val.Value;
                        if (!string.IsNullOrEmpty(allow) && Convert.ToInt32(allow) >= 0)
                        {
                            allowAllShares = Convert.ToInt32(allow) > 0;
                        }
                    }
                    else if (key.Key == UserDataKeysKnown.KEY_ALLOWED_SHARE)
                    {
                        Guid shareId = new Guid(val.Value);
                        if (shares == null || !shares.Where(s => s.ShareId == shareId).Any())
                        {
                            continue;
                        }
                        shareFilters.Add(new LikeFilter(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, shares.Where(s => s.ShareId == shareId).First().BaseResourcePath + "%", null, true));
                    }
                    else if (key.Key == UserDataKeysKnown.KEY_ALLOW_ALL_AGES)
                    {
                        string allow = val.Value;
                        if (!string.IsNullOrEmpty(allow) && Convert.ToInt32(allow) >= 0)
                        {
                            allowAllAges = Convert.ToInt32(allow) > 0;
                        }
                    }
                    else if (key.Key == UserDataKeysKnown.KEY_ALLOWED_AGE)
                    {
                        string age = val.Value;
                        if (!string.IsNullOrEmpty(age) && Convert.ToInt32(age) >= 0)
                        {
                            allowedAge = Convert.ToInt32(age);
                        }
                    }
                    else if (key.Key == UserDataKeysKnown.KEY_INCLUDE_PARENT_GUIDED_CONTENT)
                    {
                        string allow = val.Value;
                        if (!string.IsNullOrEmpty(allow) && Convert.ToInt32(allow) >= 0)
                        {
                            includeParentalGuidedContent = Convert.ToInt32(allow) > 0;
                        }
                    }
                    else if (key.Key == UserDataKeysKnown.KEY_INCLUDE_UNRATED_CONTENT)
                    {
                        string allow = val.Value;
                        if (!string.IsNullOrEmpty(allow) && Convert.ToInt32(allow) >= 0)
                        {
                            includeUnratedContent = Convert.ToInt32(allow) > 0;
                        }
                    }
                }
            }

            List <IFilter> filters = new List <IFilter>();

            // Shares filter
            if (allowAllShares == false)
            {
                if (shareFilters.Count > 0)
                {
                    filters.Add(BooleanCombinationFilter.CombineFilters(BooleanOperator.Or, shareFilters.ToArray()));
                }
                else
                {
                    filters.Add(new RelationalFilter(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, RelationalOperator.EQ, ""));
                }
            }

            // Content filter
            if (allowedAge.HasValue && allowAllAges == false)
            {
                if (necessaryMias.Contains(MovieAspect.ASPECT_ID))
                {
                    IEnumerable <CertificationMapping> certs = CertificationMapper.GetMovieCertificationsForAge(allowedAge.Value, includeParentalGuidedContent ?? false);
                    if (certs.Count() > 0)
                    {
                        if (!includeUnratedContent ?? false)
                        {
                            filters.Add(new InFilter(MovieAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)));
                        }
                        else
                        {
                            filters.Add(BooleanCombinationFilter.CombineFilters(BooleanOperator.Or,
                                                                                new InFilter(MovieAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)),
                                                                                new EmptyFilter(MovieAspect.ATTR_CERTIFICATION)));
                        }
                    }
                    else if (!includeUnratedContent ?? false)
                    {
                        filters.Add(new NotFilter(new EmptyFilter(MovieAspect.ATTR_CERTIFICATION)));
                    }
                }
                else if (necessaryMias.Contains(SeriesAspect.ASPECT_ID))
                {
                    //TODO: Should series filters reset the share filter? Series have no share dependency
                    IEnumerable <CertificationMapping> certs = CertificationMapper.GetSeriesCertificationsForAge(allowedAge.Value, includeParentalGuidedContent ?? false);
                    if (certs.Count() > 0)
                    {
                        if (!includeUnratedContent ?? false)
                        {
                            filters.Add(new InFilter(SeriesAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)));
                        }
                        else
                        {
                            filters.Add(BooleanCombinationFilter.CombineFilters(BooleanOperator.Or,
                                                                                new InFilter(SeriesAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)),
                                                                                new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION)));
                        }
                    }
                    else if (!includeUnratedContent ?? false)
                    {
                        filters.Add(new NotFilter(new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION)));
                    }
                }
                else if (necessaryMias.Contains(EpisodeAspect.ASPECT_ID))
                {
                    IEnumerable <CertificationMapping> certs = CertificationMapper.GetSeriesCertificationsForAge(allowedAge.Value, includeParentalGuidedContent ?? false);
                    if (certs.Count() > 0)
                    {
                        if (!includeUnratedContent ?? false)
                        {
                            filters.Add(new FilteredRelationshipFilter(EpisodeAspect.ROLE_EPISODE, SeriesAspect.ROLE_SERIES, new InFilter(SeriesAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId))));
                        }
                        else
                        {
                            filters.Add(new FilteredRelationshipFilter(EpisodeAspect.ROLE_EPISODE, SeriesAspect.ROLE_SERIES,
                                                                       BooleanCombinationFilter.CombineFilters(BooleanOperator.Or,
                                                                                                               new InFilter(SeriesAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)),
                                                                                                               new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION))));
                        }
                    }
                    else if (!includeUnratedContent ?? false)
                    {
                        filters.Add(new FilteredRelationshipFilter(EpisodeAspect.ROLE_EPISODE, SeriesAspect.ROLE_SERIES,
                                                                   new NotFilter(new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION))));
                    }
                }
            }

            if (filters.Count > 1)
            {
                return(BooleanCombinationFilter.CombineFilters(BooleanOperator.And, filters.ToArray()));
            }
            else if (filters.Count > 0)
            {
                return(filters[0]);
            }

            return(null);
        }
Exemplo n.º 2
0
        public IFilter GetUserFilter(ICollection <Guid> necessaryMias, bool ignoreShareRestriction = false)
        {
            List <IFilter> filters = new List <IFilter>();

            // Shares filter
            if (RestrictShares && !ignoreShareRestriction)
            {
                filters.Add(new SharePathFilter(GetAllowedShares()));
            }

            // Content filter
            if (AllowedAge.HasValue && RestrictAges)
            {
                if (necessaryMias.Contains(MovieAspect.ASPECT_ID))
                {
                    var certs = CertificationMapper.GetMovieCertificationsForAge(AllowedAge.Value, IncludeParentGuidedContent).ToList();
                    if (certs.Any())
                    {
                        if (!IncludeUnratedContent)
                        {
                            filters.Add(new InFilter(MovieAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)));
                        }
                        else
                        {
                            filters.Add(BooleanCombinationFilter.CombineFilters(BooleanOperator.Or,
                                                                                new InFilter(MovieAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)),
                                                                                new EmptyFilter(MovieAspect.ATTR_CERTIFICATION)));
                        }
                    }
                    else if (!IncludeUnratedContent)
                    {
                        filters.Add(new NotFilter(new EmptyFilter(MovieAspect.ATTR_CERTIFICATION)));
                    }
                }
                else if (necessaryMias.Contains(SeriesAspect.ASPECT_ID))
                {
                    //TODO: Should series filters reset the share filter? Series have no share dependency
                    var certs = CertificationMapper.GetSeriesCertificationsForAge(AllowedAge.Value, IncludeParentGuidedContent).ToList();
                    if (certs.Any())
                    {
                        if (!IncludeUnratedContent)
                        {
                            filters.Add(new InFilter(SeriesAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)));
                        }
                        else
                        {
                            filters.Add(BooleanCombinationFilter.CombineFilters(BooleanOperator.Or,
                                                                                new InFilter(SeriesAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)),
                                                                                new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION)));
                        }
                    }
                    else if (!IncludeUnratedContent)
                    {
                        filters.Add(new NotFilter(new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION)));
                    }
                }
                else if (necessaryMias.Contains(EpisodeAspect.ASPECT_ID))
                {
                    var certs = CertificationMapper.GetSeriesCertificationsForAge(AllowedAge.Value, IncludeParentGuidedContent).ToList();
                    if (certs.Any())
                    {
                        if (!IncludeUnratedContent)
                        {
                            filters.Add(new FilteredRelationshipFilter(EpisodeAspect.ROLE_EPISODE, SeriesAspect.ROLE_SERIES, new InFilter(SeriesAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId))));
                        }
                        else
                        {
                            filters.Add(new FilteredRelationshipFilter(EpisodeAspect.ROLE_EPISODE, SeriesAspect.ROLE_SERIES,
                                                                       BooleanCombinationFilter.CombineFilters(BooleanOperator.Or,
                                                                                                               new InFilter(SeriesAspect.ATTR_CERTIFICATION, certs.Select(c => c.CertificationId)),
                                                                                                               new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION))));
                        }
                    }
                    else if (!IncludeUnratedContent)
                    {
                        filters.Add(new FilteredRelationshipFilter(EpisodeAspect.ROLE_EPISODE, SeriesAspect.ROLE_SERIES,
                                                                   new NotFilter(new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION))));
                    }
                }
            }

            if (filters.Count > 1)
            {
                return(BooleanCombinationFilter.CombineFilters(BooleanOperator.And, filters.ToArray()));
            }
            else if (filters.Count > 0)
            {
                return(filters[0]);
            }

            return(null);
        }