public void SetupPermissions(Person currentPerson, Church church, bool sysAdmin)
        {
            currentPerson.Permissions = (from pr in Context.PersonChurches
                                         join r in Context.Roles
                                             on pr.RoleId equals r.RoleId
                                         join permissions in Context.PermissionRoles
                                             on r.RoleId equals permissions.RoleId
                                         where pr.PersonId == currentPerson.PersonId
                                               && r.ChurchId == church.ChurchId
                                         select permissions.PermissionId)
                .ToList();

            if (sysAdmin) currentPerson.Permissions.Add((int)Permissions.SystemAdministrator);
            var surname = currentPerson.Family.FamilyName;
            currentPerson.Church = church;
            currentPerson.ChurchId = church.ChurchId;
            var personChurch = currentPerson.PersonChurches.FirstOrDefault(pc => pc.ChurchId == currentPerson.ChurchId);
            Role role = null;
            if (personChurch != null)
            {
                role = Context.Roles.First(r => r.RoleId == personChurch.RoleId);
            }
            else if (currentPerson.HasPermission(Permissions.SystemAdministrator))
            {
                role = Context.Roles.FirstOrDefault(r => r.ChurchId == church.ChurchId && r.Name.Contains("Administrator"));
                if (role == null)
                    throw new ApplicationException("Cannot set role for new church");
            }
            else
            {
                throw new ApplicationException("Cannot set role for new church");
            }
            currentPerson.RoleId = role.RoleId;
            currentPerson.Role = role;
            var churchIds = (from p in currentPerson.PersonChurches select p.ChurchId).ToList();
            currentPerson.Churches = Context.Churches.Where(c => churchIds.Contains(c.ChurchId)).ToList();
        }
        public void EmailGroupLeader(PersonViewModel person, Person currentPerson, Church church, Person personToSave, bool addedToNewGroup)
        {
            if (!personToSave.HasPermission(Permissions.NotifyGroupLeaderOfVisit) || person.GroupId <= 0) return;
            var sendEmailToGroupLeader = person.PersonId == 0;
            var group = _groupRepository.GetGroup(person.GroupId);

            if (group==null)
                return;
            if (addedToNewGroup)
                sendEmailToGroupLeader = true;

            if (group.LeaderId == currentPerson.PersonId || group.AdministratorId == currentPerson.PersonId)
                sendEmailToGroupLeader = false;  //This is the groupleader

            if (!sendEmailToGroupLeader) return;
            if (group.Leader != null && group.Leader.HasValidEmail() && group.LeaderId != currentPerson.PersonId)
            {
                SendNewVisitorEmail(person, church, group.Leader.Firstname, group.Leader.Family.FamilyName, group.Leader.Email, currentPerson);
            }
            else if (group.Administrator != null && group.Administrator.HasValidEmail() && group.LeaderId != currentPerson.PersonId)
            {
                SendNewVisitorEmail(person, church, group.Administrator.Firstname, group.Administrator.Family.FamilyName, group.Administrator.Email, currentPerson);
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Churches EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToChurches(Church church)
 {
     base.AddObject("Churches", church);
 }
 /// <summary>
 /// Create a new Church object.
 /// </summary>
 /// <param name="churchId">Initial value of the ChurchId property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="created">Initial value of the Created property.</param>
 /// <param name="changed">Initial value of the Changed property.</param>
 /// <param name="sendWelcome">Initial value of the SendWelcome property.</param>
 /// <param name="emailLogin">Initial value of the EmailLogin property.</param>
 /// <param name="emailPassword">Initial value of the EmailPassword property.</param>
 /// <param name="statusId">Initial value of the StatusId property.</param>
 public static Church CreateChurch(global::System.Int32 churchId, global::System.String name, global::System.DateTime created, global::System.DateTime changed, global::System.Boolean sendWelcome, global::System.String emailLogin, global::System.String emailPassword, global::System.Int32 statusId)
 {
     Church church = new Church();
     church.ChurchId = churchId;
     church.Name = name;
     church.Created = created;
     church.Changed = changed;
     church.SendWelcome = sendWelcome;
     church.EmailLogin = emailLogin;
     church.EmailPassword = emailPassword;
     church.StatusId = statusId;
     return church;
 }
        private void SendNewVisitorEmail(PersonViewModel person, Church church, string firstname, string surname, string email, Person currentPerson)
        {
            var subject = "A new visitor to " + church.Name + " has been added to your homegroup";
               var body = GetNewVisitorEmailBody(firstname, surname, church.Name, person);

               _emailSender.QueueEmails(subject, body, new[] { email }, currentPerson.PersonId, church.ChurchId, new List<UploadFilesResult>());
        }
 public void SendWelcomeEmail(PersonViewModel person, bool sendWelcomeEmail, Church church, Person personToSave, Person currentPerson)
 {
     if (sendWelcomeEmail && personToSave.HasValidEmail() && (personToSave.HasPermission(Permissions.Login) || personToSave.HasPermission(Permissions.SendWelcomeLetter)))
     {
         SendEmailAndPassword(
             person.Firstname,
             person.Surname,
             person.Email,
             personToSave,
             currentPerson);
     }
 }
        public string SendResetPasswordEmail(Person person, Church church, string password)
        {
            var subject = "Your password has been reset on " + church.SiteHeader;
            var body = GetResetPasswordBody(person.Firstname,
                                           person.Email,
                                           password,
                                           church.SiteHeader,
                                           church.Name,
                                           church.OfficePhone,
                                           church.OfficeEmail);

            _emailSender.QueueEmails(subject, body, new[] { person.Email }, person.PersonId, church.ChurchId, new List<UploadFilesResult>());
            return "Password has been reset.  You should receive an email shortly explaining what to do next";
        }
        public Person UpdatePerson(PersonViewModel person, Person currentPerson, out bool sendWelcomeEmail, out Church church)
        {
            sendWelcomeEmail = false;

            //We need some settings from the Church table
            church = _churchRepository.GetChurch(currentPerson.ChurchId);

            var personToSave = new Person();
            if (person.PersonId != 0)
            {
                personToSave = FetchPerson(person.PersonId, currentPerson);
            }
            else
            {
                Context.AddToPeople(personToSave);
                personToSave.ChurchId = church.ChurchId;
                personToSave.Church = church;

                personToSave.Created = DateTime.Now;
                if (church.SendWelcome)
                {
                    sendWelcomeEmail = true;
                }

                if (person.GroupId > 0)
                {
                    var pg = new PersonGroup
                    {
                        GroupId = person.GroupId,
                        Person = personToSave,
                        Joined = DateTime.Now,
                        Created = DateTime.Now,
                        Changed = DateTime.Now
                    };
                    personToSave.PersonGroups.Add(pg);
                }
            }

            if (person.FamilyId == 0)
            {
                if (person.FindFamily)
                {
                    var family = (from f in Context.Families
                                  join p in Context.People
                                      on f.FamilyId equals p.FamilyId
                                  join g in Context.PersonGroups
                                      on p.PersonId equals g.PersonId
                                  where f.FamilyName == person.Surname
                                      && g.GroupId == person.GroupId
                                  select f).FirstOrDefault();
                    personToSave.Family = family ?? new Family {Created = DateTime.Now};
                }
                else
                {
                    personToSave.Family = new Family {Created = DateTime.Now};
                }
            }
            else
            {
                personToSave.Family = (from f in Context.Families
                                       where f.FamilyId == person.FamilyId
                                       select f).FirstOrDefault() ?? new Family {Created = DateTime.Now};
            }

            personToSave.Family.Changed = DateTime.Now;
            personToSave.Changed = DateTime.Now;

            personToSave.Firstname = person.Firstname;
            personToSave.Family.FamilyName = person.Surname;
            personToSave.Email = person.Email;
            personToSave.DateOfBirth = person.DateOfBirth_Value;
            Context.SaveChanges();

            return personToSave;
        }