Beispiel #1
0
        private async Task <IEnumerable <LdapRequestMessage> > SearchEventAsync(IQueryable <Event> query, LdapSearchRequest request, LdapClientConnection connection, CancellationToken cancellationToken)
        {
            var eventQuery = SearchEvent(query, request, connection);

            if (request.SizeLimit > 0)
            {
                eventQuery = eventQuery.Take(request.SizeLimit);
            }
            var result = await eventQuery.ToArrayAsync(cancellationToken);

            var events = result.Select(x => new OrganizationalUnitObjectClass
            {
                Ou          = { Entries = { x.Name } },
                Description = String.IsNullOrEmpty(x.Description)
                    ? null
                    : new DescriptionAttribute {
                    Entries = { x.Description }
                },
                Locality = String.IsNullOrEmpty(x.Location) ? null : new LocalityAttribute {
                    Entries = { x.Location }
                }
            }).ToList();
            var results = new List <LdapRequestMessage>(events.Count);

            foreach (var item in events)
            {
                var attributes = item.GetAttributes(request.Attributes, request.TypesOnly);
                var dn         = new LdapDistinguishedName(item.Ou.Name, item.Ou.Entries[0], RootDN);
                results.Add(request.Result(dn, attributes.ToArray(), new LdapControl[0]));
            }
            return(results);
        }
Beispiel #2
0
        private async Task <IEnumerable <LdapRequestMessage> > SearchExtensionAsync(IQueryable <Extension> query, LdapSearchRequest request, CancellationToken cancellationToken)
        {
            var extensionQuery = SearchExtension(query, request);

            if (request.SizeLimit > 0)
            {
                extensionQuery = extensionQuery.Take(request.SizeLimit);
            }
            var result = await extensionQuery.ToArrayAsync(cancellationToken);

            var extensions = result.Select(x => new OrganizationalPersonObjectClass
            {
                Parent   = new LdapDistinguishedName("ou", x.Event, RootDN),
                Cn       = { Entries = { x.Number } },
                Sn       = { Entries = { String.IsNullOrEmpty(x.Name) ? x.Number : x.Name } },
                Locality = String.IsNullOrEmpty(x.Location) ? null : new LocalityAttribute {
                    Entries = { x.Location }
                },
                OrganizationalUnitName = new OuAttribute {
                    Entries = { x.Event }
                },
                TelephoneNumber = new TelephoneNumberAttribute {
                    Entries = { x.Number }
                },
                CreatorsName = new CreatorsNameAttribute {
                    Entries = { new LdapDistinguishedName("cn", "GURU3", RootDN) }
                },
                ModifiersName = new ModifiersNameAttribute {
                    Entries = { new LdapDistinguishedName("cn", "GURU3", RootDN) }
                },
                ModifyTimestamp = new ModifyTimestampAttribute {
                    Entries = { x.LastModified }
                },
            })
                             .ToList();
            var results = new List <LdapRequestMessage>(extensions.Count);

            foreach (var item in extensions)
            {
                var attributes = item.GetAttributes(request.Attributes, request.TypesOnly);
                var dn         = new LdapDistinguishedName(item.Cn.Name, item.Cn.Entries[0], item.Parent);
                results.Add(request.Result(dn, attributes.ToArray(), new LdapControl[0]));
            }
            return(results);
        }
Beispiel #3
0
        protected override async Task <IEnumerable <LdapRequestMessage> > OnSearchAsync(LdapSearchRequest request, LdapClientConnection connection, CancellationToken cancellationToken)
        {
            Console.WriteLine($"search for {request.Filter} in {request.BaseObject} ({request.Scope}) [{connection.Id}]");
            using (var context = GetContext())
            {
                IQueryable <Event> dbEvents = context.Events;
                if (Sessions.TryGetValue(connection.Id, out var eventId))
                {
                    dbEvents = dbEvents.Where(x => x.Id == eventId);
                }
                if (String.Equals(request.BaseObject.ToString(), RootDN.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    if (request.Scope == SearchScope.BaseObject)
                    {
                        //search root
                        var attributes = GetRootDSE().GetAttributes(request.Attributes, request.TypesOnly);
                        var result     = request.Result(RootDN, attributes.ToArray(), new LdapControl[0]);
                        return(new[] { result });
                    }
                    else
                    {
                        //search events
                        var events = await SearchEventAsync(dbEvents, request, connection, cancellationToken);

                        var results = events.ToList();

                        if (request.Scope == SearchScope.WholeSubtree)
                        {
                            //include extensions
                            var dbExtensions = SearchEvent(dbEvents, connection).Join(context.Extensions, x => x.Id, x => x.EventId, (x, y) => y);
                            var extension    = await SearchExtensionAsync(dbExtensions, request, cancellationToken);

                            if (request.SizeLimit > 0)
                            {
                                extension = extension.Take(request.SizeLimit - results.Count);
                            }
                            results.AddRange(extension);
                        }
                        return(results);
                    }
                }
                else
                {
                    var rdns = request.BaseObject.RDNs;
                    if (rdns.Count <= 2 || rdns.Count > 4)
                    {
                        return(new LdapRequestMessage[0]);
                    }

                    //search extensions
                    IQueryable <Extension> query = SearchEvent(dbEvents, connection).Join(context.Extensions, x => x.Id, x => x.EventId, (x, y) => y);

                    if (rdns.Count >= 3)
                    {
                        var eventName = rdns[rdns.Count - 3].Values[0].Value;
                        if (rdns.Count == 3 && request.Scope == SearchScope.BaseObject)
                        {
                            //get event
                            var events = await SearchEventAsync(dbEvents.Where(x => x.Name == eventName), request, connection, cancellationToken);

                            return(events.ToList());
                        }
                        query = query.Where(x => x.Event.Name == eventName);
                    }

                    if (rdns.Count == 4)
                    {
                        if (request.Scope != SearchScope.BaseObject)
                        {
                            return(new LdapRequestMessage[0]);
                        }

                        var extension = rdns[0].Values[0].Value;
                        query = query.Where(x => x.Number == extension);
                    }

                    if (Sessions.TryGetValue(connection.Id, out eventId))
                    {
                        query = query.Where(x => x.EventId == eventId);
                    }
                    var result = await SearchExtensionAsync(query, request, cancellationToken);

                    return(result);
                }
            }
        }