public SimpleMembershipInitializer()
            {
                Database.SetInitializer<CodeReviewerContext>(null);

                try
                {
                    using (var context = new CodeReviewerContext())
                    {
                        if (!context.Database.Exists())
                        {
                            throw new Exception("CodeReviewer database does not exist");
                        }
                    }

                    if (ConfigurationManager.AppSettings.LookupValue("useLDAP", false))
                        return;

                    // TODO: Lookup CodeReviewerContext
                    WebSecurity.InitializeDatabaseConnection("CodeReviewerContext", "UserProfile", "Id", "Email", true);
                    var roles = (SimpleRoleProvider)Roles.Provider;
                    var membership = (SimpleMembershipProvider)Membership.Provider;

                    if (!roles.RoleExists("Admin"))
                    {
                        roles.CreateRole("Admin");
                    }
                    if (membership.GetUser("admin", false) == null)
                    {
                        membership.CreateUserAndAccount("admin", "sa");
                    }
                #if ForReference
                if (!roles.GetRolesForUser("admin").Contains("Admin"))
                {
                roles.AddUsersToRoles(new[] { "sallen" }, new[] { "admin" });
                }
                #endif
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        public static void ProcessMailRequests()
        {
            var context = new CodeReviewerContext();
            Dictionary<int, string> sourceControlRoots = new Dictionary<int, string>();

            var sourceControlsQuery = from sc in context.SourceControls select sc;
            foreach (CodeReviewer.Models.SourceControl sc in sourceControlsQuery)
            {
                string site = String.Empty;
                if (!String.IsNullOrEmpty(sc.WebsiteName))
                {
                    if (!sc.WebsiteName.StartsWith("/"))
                        site = "/" + sc.WebsiteName.Substring(1);
                    else
                        site = sc.WebsiteName;
                }
                sourceControlRoots[sc.Id] = site;
            }

            var mailChangeListQuery = from cl in context.MailChangeLists
                                      join rv in context.Reviewers on cl.ReviewerId equals rv.Id
                                      join ch in context.ChangeLists on cl.ChangeListId equals ch.Id
                                      select new { cl, ch, rv.ReviewerAlias };
            var reviewInviteQuery = (from ri in context.MailReviewRequests
                                     join ch in context.ChangeLists on ri.ChangeListId equals ch.Id
                                     select new { ri, ch }).ToArray();

            var itemGroups = mailChangeListQuery.GroupBy(item => item.cl.RequestType);
            var mailItems = new List<MailItem>();

            foreach (var itemGroup in itemGroups)
            {
                var request = itemGroup.First();
                var groupType = (MailType)request.cl.RequestType;
                var userNameInfo = new UserName(request.ch.ReviewerAlias);
                var body = String.Format((string) MailTemplates.Request, request.ch.CL, request.ch.Url, userNameInfo.displayName,
                                         request.ch.Description);

                switch (groupType)
                {
                    case MailType.Request:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.RequestSubject, request.ch.CL,
                                                             request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;

                    case MailType.Iteration:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.IterationSubject, request.ch.CL,
                                                             request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;

                    case MailType.WaitingOnAuthor:
                        {
                            itemGroup.ToList().ForEach(item =>
                            {
                                var mailItem = new MailItem();
                                mailItem.ToAliases.Add(userNameInfo.emailAddress);
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.CcAliases.Add(reviewerUserInfo.emailAddress);
                                mailItem.Subject = String.Format((string)MailTemplates.WaitingOnAuthorSubject, request.ch.CL,
                                                                 item.ReviewerAlias);
                                mailItem.Body = body;
                                mailItems.Add(mailItem);
                            });
                        }
                        break;

                    case MailType.SignedOff:
                        {
                            itemGroup.ToList().ForEach(item =>
                            {
                                var mailItem = new MailItem();
                                mailItem.ToAliases.Add(userNameInfo.emailAddress);
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.CcAliases.Add(reviewerUserInfo.emailAddress);
                                mailItem.Subject = String.Format((string)MailTemplates.SignedOffSubject, request.ch.CL,
                                                                 item.ReviewerAlias);
                                mailItem.Body = body;
                                mailItems.Add(mailItem);
                            });
                        }
                        break;

                    case MailType.SignedOffWithComments:
                        {
                            itemGroup.ToList().ForEach(item =>
                            {
                                var mailItem = new MailItem();
                                mailItem.ToAliases.Add(userNameInfo.emailAddress);
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.CcAliases.Add(reviewerUserInfo.emailAddress);
                                mailItem.Subject = String.Format((string)MailTemplates.SignedOffSubject, request.ch.CL,
                                                                 item.ReviewerAlias);
                                mailItem.Body = body;
                                mailItems.Add(mailItem);
                            });
                        }
                        break;

                    case MailType.Reminder:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.ReminderSubject, request.ch.CL, request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;

                    case MailType.Complete:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.CompleteSubject, request.ch.CL, request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;

                    case MailType.Deleted:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.DeleteSubject, request.ch.CL, request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;
                }
            }

            if (ExchangeMode)
            {
                var exchangeItems = new List<MessageType>();
                mailItems.ForEach(item => exchangeItems.Add(item.ExchangeItem));
                SendExchangeMail(Config, exchangeItems);
            }

            if (SmtpMode)
            {
                var smtpItems = new List<MailMessage>();
                mailItems.ForEach(item => smtpItems.Add(item.SmtpItem));
                SendSmtpMail(Config, smtpItems);
            }

            foreach (var item in mailChangeListQuery)
            {
                context.Entry(item.cl).State = EntityState.Deleted;
            }

            foreach (var item in reviewInviteQuery)
            {
                context.Entry(item.ri).State = EntityState.Deleted;
            }

            context.SaveChanges();
        }
        public UserName(string fullUserName, CodeReviewerContext db)
        {
            if (!_useLDAP && db != null)
            {
                var userProfile = (from item in db.UserProfiles
                                   where item.Email == fullUserName
                                   select item).FirstOrDefault();
                if (userProfile != null)
                {
                    this.fullUserName = fullUserName;
                    this.displayName = !string.IsNullOrEmpty(userProfile.UserName) ? userProfile.UserName : fullUserName;
                    this.emailAddress = userProfile.Email;
                    this.reviewerAlias = userProfile.Email;
                    this.userName = this.displayName;
                    return;
                }
            }

            if (string.IsNullOrEmpty(fullUserName))
            {
                SetAllValues(Environment.UserName);
                return;
            }

            if (_testMode)
            {
                if (0 == string.Compare(fullUserName, "Test.Me", true, CultureInfo.InvariantCulture))
                {
                    MapToTestAccount();
                    return;
                }
                if (0 == string.Compare(fullUserName, "Test.Me2", true, CultureInfo.InvariantCulture))
                {
                    MapToTestAccount2();
                    return;
                }
            }

            this.fullUserName = fullUserName;
            var data = fullUserName.Split('\\');
            switch (data.Length)
            {
                case 1:
                    this.userName = fullUserName;
                    break;

                case 2:
                    this.userName = data[1];
                    break;

                default:
                    userName = fullUserName;
                    break;
            }

            if (string.IsNullOrEmpty(_domain))
            {
                _domain = _ldapDomain;
                var parts = _domain.Split('.');
                if (parts.Length > 0)
                    domain = parts[0].ToUpper();

                if (_domain != _appDomain)
                {
                    const string ldapQueryFormat = @"LDAP://{0}/{1}";
                    var dcList = new List<string>();
                    parts.ToList().ForEach(part => dcList.Add("DC=" + part));
                    _rootQuery = string.Format(ldapQueryFormat, _domain, string.Join(",", dcList));
                }
            }

            if (string.IsNullOrEmpty(_domain))
            {
                SetAllValues(fullUserName);
                return;
            }

            QueryEmailAddress();
        }