private Task <List <UserListViewModel> > LoadUsersAsync()
        {
            return(Task.Run(
                       () =>
            {
                var query = _context.Query <User>();
                if (!string.IsNullOrWhiteSpace(SearchText))
                {
                    if (CustomFilter)
                    {
                        //by default filters passed to the Where clause are not cleaned.
                        //you can attempte to clean them yourselves using the CleanFilterValue method, but it has its limits.
                        //if your users don't understand valid filters I would go with fixed search options.
                        query = query.Where(SearchText);
                    }
                    else
                    {
                        var split = SearchText.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                        var expression = PredicateBuilder.Create <User>();
                        expression = split.Length == 2
                                                     ? expression.And(
                            s =>
                            s.FirstName.StartsWith(split[0]) &&
                            s.LastName.StartsWith(split[1]))
                                                     : split.Aggregate(expression,
                                                                       (current, t) =>
                                                                       current.Or(
                                                                           s =>
                                                                           s.UserId == t ||
                                                                           s.FirstName.StartsWith(t) ||
                                                                           s.LastName.StartsWith(t)));

                        query = query.Where(expression);
                    }
                }

                return query.Select(
                    s => new UserListViewModel(s.DistinguishedName, s.UserId, s.FirstName, s.LastName, s.PrimaryAffiliation,
                                               LoadUser))
                .Take(50)
                .ToList();
            }));
        }
        public object Find(string q, bool custom)
        {
            var query = _context.Query <User>();

            if (!string.IsNullOrWhiteSpace(q))
            {
                if (custom)
                {
                    //by default filters passed to the Where method are not cleaned.
                    //if your users don't understand valid filters I would go with fixed search options.
                    query = query.FilterWith(q);
                }
                else
                {
                    var split = q.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    var expression = PredicateBuilder.Create <User>();
                    expression = split.Length == 2
                        ? expression.And(s => s.FirstName.StartsWith(split[0]) && s.LastName.StartsWith(split[1]))
                        : split.Aggregate(expression, (current, t) => current.Or(s => s.UserId == t || s.FirstName.StartsWith(t) || s.LastName.StartsWith(t)));

                    query = query.Where(expression);
                }
            }

            var results = query
                          .Select(s =>
                                  new UserListItem()
            {
                DistinguishedName = s.DistinguishedName,
                Name = string.Format("{0} {1}", s.FirstName, s.LastName),
                PrimaryAffiliation = s.PrimaryAffiliation,
                UserId             = s.UserId
            })
                          .Take(50)
                          .ToArray();

            return(results);
        }
Exemplo n.º 3
0
        public void Can_Add_Update_Remove_Static()
        {
            var test = new IntegrationUserTest
            {
                Cn             = "IntegrationTest",
                AccountExpires = DateTime.MinValue
            };

            var added = _context.AddAndGet(test, test.GetDistinguishedName());

            added.Should().Not.Be.Null();
            added.AccountExpires.Should().Not.Have.Value();
            added.Cn.Should().Be.EqualTo(test.Cn);
            added.AccountExpires.Should().Not.Have.Value();
            added.ObjectGuid.Should().Not.Be.Null().And.Not.Be.EqualTo(default(Guid));
            added.ObjectSid.Should().Not.Be.Null();
            added.Manager.Should().Be.Null();
            added.Employees.Should().Be.Null();
            added.WhenCreated.Should().Be.GreaterThan(DateTime.Now.Subtract(TimeSpan.FromHours(1)));

            var queryResult = _context.Query <IntegrationUserTest>()
                              .Where(u => u.ObjectGuid == added.ObjectGuid)
                              .Select(u => u.ObjectGuid)
                              .FirstOrDefault();

            queryResult.Should().Be.EqualTo(added.ObjectGuid);

            var now = DateTime.Now;

            added.AccountExpires = now;

            added = _context.UpdateAndGet(added);

            added.AccountExpires.Should().Be.EqualTo(now);

            _context.Delete(added.DistinguishedName);

            Executing.This(() => _context.GetByDN(added.DistinguishedName))
            .Should().Throw <DirectoryOperationException>().And.Exception.Message
            .Should().Contain("does not exist");
        }
Exemplo n.º 4
0
        public void AnyAsync_returns_same_result_as_Any()
        {
            //arrange
            var any          = _context.Query <PersonInheritanceTest>().Any();
            var anyPredicate = _context.Query <PersonInheritanceTest>().Any(x => x.CommonName.StartsWith("X"));

            //act
            var anyAsnyc          = _context.Query <PersonInheritanceTest>().AnyAsync().Result;
            var anyPredicateAsnyc = _context.Query <PersonInheritanceTest>().AnyAsync(x => x.CommonName.StartsWith("X")).Result;

            //assert
            any.Should().Be.EqualTo(anyAsnyc);
            anyPredicate.Should().Be.EqualTo(anyPredicateAsnyc);
        }