Пример #1
0
        private FilterDefinition <User> BuildUsersQuery(SearchUsersFilters filters)
        {
            FilterDefinition <User> filter = null;

            if (filters.IsActive != null)
            {
                filter = filter.FilterJoin(Builders <User> .Filter.Eq(x => x.Security.IsBlocked, !filters.IsActive));
            }

            if (string.IsNullOrWhiteSpace(filters.Role) == false && string.IsNullOrWhiteSpace(filters.PermissionKey) == false)
            {
                filter = filter.FilterJoin(Builders <User> .Filter
                                           .ElemMatch(x => x.Roles, x => x.Keys.Contains(filters.PermissionKey) & x.Type == filters.Role));
            }
            else if (string.IsNullOrWhiteSpace(filters.Role) == false)
            {
                filter = filter.FilterJoin(Builders <User> .Filter.ElemMatch(x => x.Roles, x => x.Type == filters.Role));
            }
            else if (string.IsNullOrWhiteSpace(filters.PermissionKey) == false)
            {
                filter = filter.FilterJoin(Builders <User> .Filter.ElemMatch(x => x.Roles, x => x.Keys.Contains(filters.PermissionKey)));
            }

            if (string.IsNullOrWhiteSpace(filters.Keywords) == false)
            {
                var regexFilter = Regex.Escape(filters.Keywords);
                var bsonRegex   = new BsonRegularExpression(filters.Keywords, "i");

                var name = Builders <User> .Filter.Regex(x => x.Name, bsonRegex);

                var email = Builders <User> .Filter.Regex(x => x.Contacts.Email, bsonRegex);

                var phone = Builders <User> .Filter.Regex(x => x.Contacts.Phone, bsonRegex);

                var company = Builders <User> .Filter.Regex(x => x.Company, bsonRegex);

                var keywordFilter = (name | company | email | phone);

                filter = filter.FilterJoin(keywordFilter);
            }

            if (filter == null)
            {
                filter = Builders <User> .Filter.Empty;
            }

            return(filter);
        }
        private FilterDefinition <AccessToken> BuildAccessTokensQuery(SearchAccessTokensFilters filters)
        {
            FilterDefinition <AccessToken> filter = null;

            if (filters.UserKey != null)
            {
                filter = filter.FilterJoin(Builders <AccessToken> .Filter.Eq(x => x.UserKey, filters.UserKey));
            }

            if (filter == null)
            {
                filter = Builders <AccessToken> .Filter.Empty;
            }

            return(filter);
        }