private void LogQueryResults(
            ExchangeUserDict userCollection, string[] searchTerms, string ldapAttribute)
        {
            if (userCollection.Count != searchTerms.Length)
            {
                if (ldapAttribute == "sAMAccountName")
                {
                    foreach (string term in searchTerms)
                    {
                        if (!userCollection.ContainsKey(term.ToLower()))
                        {
                            log.InfoFormat("Unable to find Active Directory user where '{0}'='{1}'.", ldapAttribute, term);
                        }
                    }
                }
                else if (ldapAttribute == "mail")
                {
                    foreach (string email in searchTerms)
                    {
                        string login = email.Split('@')[0];

                        if (!userCollection.ContainsKey(login.ToLower()))
                        {
                            log.InfoFormat("Unable to find Active Directory user where '{0}'='{1}'.", ldapAttribute, email);
                        }
                    }
                }
                else
                {
                    log.InfoFormat(
                        "Unable to find all users in Active Directory.  [Users={0}]",
                        string.Join(";", searchTerms));
                }
            }
        }
        /// <summary>
        /// Returns the free busy times for the specified exchange users
        /// </summary>
        /// <param name="users">The user which free/busy blocks will be looked up for</param>
        /// <param name="window">The time period to look up FB info for</param>
        /// <returns></returns>
        public Dictionary <ExchangeUser, FreeBusy> LookupFreeBusyTimes(
            ExchangeUserDict users,
            DateTimeRange window)
        {
            /* Create an array of mailboxes to retrieve from exchange */
            Dictionary <ExchangeUser, FreeBusy> result = new Dictionary <ExchangeUser, FreeBusy>();

            try
            {
                using (BlockTimer bt = new BlockTimer("LookupFreeBusyTimes"))
                {
                    /* Perform the retrieval of free busy times through WebDav */
                    result = webDavQuery.LoadFreeBusy(exchangeServerUrl, users, window);
                }
            }
            catch (Exception ex)
            {
                throw new GCalExchangeException(
                          GCalExchangeErrorCode.ExchangeUnreachable,
                          "Error occured while retrieving free busy ranges",
                          ex);
            }

            return(result);
        }
        /// <summary>
        /// Assigns free busy times to the exchange users that are passed into the method
        /// </summary>
        public virtual void GetCalendarInfoForUsers(ExchangeUserDict users, DateTimeRange window)
        {
            // Perform  appointment lookup in parallel with FB lookup
            using (AppointmentLookupFuture future =
                       new AppointmentLookupFuture(this, users, window))
            {
                Dictionary <ExchangeUser, FreeBusy> freeBusyBlocks =
                    freebusy.LookupFreeBusyTimes(users, window);

                foreach (ExchangeUser user in users.Values)
                {
                    // If the server gave us no data for a user, that's
                    // fine.  We just skip the user.
                    if (!freeBusyBlocks.ContainsKey(user))
                    {
                        continue;
                    }

                    /* Retrieve the free busy blocks */
                    FreeBusy freeBusy = freeBusyBlocks[user];

                    user.AccessLevel = GCalAccessLevel.ReadAccess;

                    List <Appointment> appointments = future.getResult(user);

                    MergeFreeBusyWithAppointments(
                        user,
                        freeBusy,
                        appointments,
                        window);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Generates a response from a GCalRequest
        /// </summary>
        /// <returns></returns>
        public string GenerateResponse()
        {
            /* Create a string builder to hold the text for the response */
            StringBuilder result = new StringBuilder(4096);

            /* Create an exchange provider */
            ExchangeService gateway = new ExchangeService(
                ConfigCache.ExchangeServerUrl,
                ConfigCache.ExchangeUserLogin,
                ConfigCache.ExchangeUserPassword);

            /* Return the exchangers from the GCal Request that was passed in */
            DateTimeRange    range         = new DateTimeRange(request.UTCStartDate, request.UTCEndDate);
            ExchangeUserDict exchangeUsers = gateway.SearchByEmail(range, request.ExchangeUsers);

            /* Create the header of the request */
            result.AppendFormat("['{0}','{1}',", request.VersionNumber, request.MessageId);

            result.AppendFormat("['_ME_AddData','{0}/{1}','{2}'",
                                DateUtil.FormatDateForGoogle(request.StartDate),
                                DateUtil.FormatDateForGoogle(request.EndDate),
                                DateUtil.FormatDateTimeForGoogle(request.Since));

            /* Flag for inserting commas */
            bool firstUser = true;

            result.Append(",[");

            foreach (ExchangeUser user in exchangeUsers.Values)
            {
                /* Don't add a comma if this is the first user */
                if (!firstUser)
                {
                    result.Append(",");
                }

                /* Add the user's credentials */
                string email = ConfigCache.MapToExternalDomain(user.Email);
                result.AppendFormat("'{0}','{1}','{2}',[",
                                    user.DisplayName,
                                    email,
                                    (int)user.AccessLevel);

                GenerateResponseForTimeBlocks(user,
                                              result);

                result.Append("]");
                firstUser = false;
            }

            result.Append("]");
            result.Append("]");
            result.Append("]");

            log.Info("GCal Free/Busy response successfully generated.");
            log.DebugFormat("Response = {0}", result);

            return(result.ToString());
        }
        /// <summary>
        /// Assigns free busy times to the exchange users that are passed into the method
        /// </summary>
        public virtual void GetCalendarInfoForUser(ExchangeUser user, DateTimeRange window)
        {
            ExchangeUserDict users = new ExchangeUserDict();

            users.AddExchangeUser(user.Email, user);

            GetCalendarInfoForUsers(users, window);
        }
        /// <summary>
        /// Returns the free busy times for the specified exchange users
        /// </summary>
        /// <param name="user">The user which free/busy blocks will be looked up for</param>
        /// <param name="window">The date range to do the lookup</param>
        /// <returns>FreeBusy data for user in the daterange</returns>
        public FreeBusy LookupFreeBusyTimes(ExchangeUser user, DateTimeRange window)
        {
            ExchangeUserDict users = new ExchangeUserDict();

            users.Add(user.Email, user);

            Dictionary <ExchangeUser, FreeBusy> result = LookupFreeBusyTimes(users, window);

            return(result[user]);
        }
        /// <summary>
        /// Query Active Directory and return all users matching the LDAP query
        /// </summary>
        /// <param name="ldapFilter">An LDAP query to match users against</param>
        /// <returns>The set of users matching the query</returns>
        public ExchangeUserDict QueryActiveDirectory(string ldapFilter)
        {
            SearchResultCollection searchResults = null;

            /* Perform the mailbox search */
            using (ActiveDirectoryService ad = new ActiveDirectoryService())
            {
                searchResults = ad.SearchDirectory(ldapFilter);
            }

            ExchangeUserDict exchangeUsers = CreateExchangeUserCollection(searchResults);

            return(exchangeUsers);
        }
Example #8
0
        private ExchangeUserDict QueryExchangeForValidUsers()
        {
            ExchangeUserDict users = null;

            if (string.IsNullOrEmpty(ConfigCache.ServiceLDAPUserFilter))
            {
                users = exchangeGateway.RetrieveAllExchangeUsers();
            }
            else
            {
                users = exchangeGateway.QueryActiveDirectory(ConfigCache.ServiceLDAPUserFilter);
            }

            return(users);
        }
Example #9
0
        public AppointmentLookupFuture(
            ExchangeService exchange,
            ExchangeUserDict users,
            DateTimeRange window)
        {
            this.exchange   = exchange;
            this.users      = users;
            this.syncWindow = window;

            // Only do this if appointment
            // lookup is enabled
            if (ConfigCache.EnableAppointmentLookup)
            {
                this.start();
            }
        }
        private ExchangeUserDict QueryActiveDirectoryByAttribute(
            string ldapAttribute, params string[] searchTerms)
        {
            SearchResultCollection searchResults = null;

            /* Perform the mailbox search */
            using (ActiveDirectoryService ad = new ActiveDirectoryService())
            {
                searchResults = ad.SearchDirectoryByAttribute(ldapAttribute, searchTerms);
            }

            ExchangeUserDict userCollection = CreateExchangeUserCollection(searchResults);

            LogQueryResults(userCollection, searchTerms, ldapAttribute);

            return(userCollection);
        }
        /// <summary>
        /// Returns a list of Exchange users, provides free busy times and
        /// appointments if available within the date range.
        /// </summary>
        /// <param name="ldapAttribute">Parameter to search upon</param>
        /// <param name="utcRange">DateRange for search</param>
        /// <param name="searchTerms">Search terms to search exchange for</param>
        /// <returns></returns>
        public ExchangeUserDict Search(
            string ldapAttribute, DateTimeRange utcRange, params string[] searchTerms)
        {
            if (utcRange.Equals(DateTimeRange.Full))
            {
                throw new Exception("Must specify a time range");
            }

            /* Create the network credentials and WebDav resources needed for user */
            ExchangeUserDict activeDirectoryResults = QueryActiveDirectoryByAttribute(ldapAttribute, searchTerms);

            /* Assign user free busy times if any users were returned */
            if (activeDirectoryResults != null && activeDirectoryResults.Count > 0)
            {
                GetCalendarInfoForUsers(activeDirectoryResults, utcRange);
            }
            return(activeDirectoryResults);
        }
        private ExchangeUserDict CreateExchangeUserCollection(SearchResultCollection searchResults)
        {
            ExchangeUserDict userCollection = new ExchangeUserDict();

            if (searchResults != null)
            {
                /* For each result set in the result set */
                foreach (System.DirectoryServices.SearchResult result in searchResults)
                {
                    /* Extract the property collection and create a new exchange user with it
                     * Add the new user to the result set and use the account name as the index for
                     * the dictionary collection */
                    ResultPropertyCollection property = result.Properties;
                    ExchangeUser             user     = new ExchangeUser(property);

                    if (!user.IsValid)
                    {
                        log.WarnFormat("User '{0}' is invalid and will not be synchronized.", user.CommonName);
                    }
                    else if (userCollection.ContainsKey(user.Email.ToLower()))
                    {
                        log.WarnFormat("User '{0}' was returned multiple times in the LDAP query. " +
                                       "Only the first instance was added.", user.Email);
                    }
                    else
                    {
                        userCollection.Add(user.Email.ToLower(), user);
                        log.InfoFormat("Found and added '{0}' as an ExchangeUser.", user.Email);
                    }

                    log.DebugFormat("LDAP object debug info: {0}", user);
                }
            }

            return(userCollection);
        }