public ActionResult Edit(Guid? id, PersonEditModel personEditModel, HttpPostedFileBase profilepic)
        {
            User user = null;

            // admin is trying to edit, authorize them
            if (id.HasValue)
            {
                // current user must be in User role
                if (Roles.IsUserInRole(RoleNames.User))
                {
                    user = _userRepository.GetNullableById(id.Value);
                }
            }
            else
            {
                user = Repository.OfType<User>().Queryable.Where(a => a.LoweredUserName == CurrentUser.Identity.Name.ToLower()).FirstOrDefault();
            }

            if (user == null)
            {
                return this.RedirectToAction<ErrorController>(a => a.NotAuthorized());
            }

            //var seminarPerson = _seminarPersonRepository.GetNullableById(personEditModel.SeminarPersonId);
            var person = SetPerson(personEditModel, null, ModelState, user.Person, profilepic);

            var membership = user.Membership;
            membership.SetEmail(personEditModel.Email);

            if (ModelState.IsValid)
            {
                _personRepository.EnsurePersistent(person);
                _membershipRepository.EnsurePersistent(membership);

                Message = string.Format(Messages.Saved, "Person");

                if (personEditModel.UserName != CurrentUser.Identity.Name.ToLower())
                {
                    user.SetUserName(personEditModel.UserName);
                    _userRepository.EnsurePersistent(user);

                    var formsService = new FormsAuthenticationService();
                    formsService.SignOut();
                    formsService.SignIn(user.LoweredUserName, false);
                }

                // send to crop photo if one was uploaded
                if (profilepic != null) return this.RedirectToAction(a => a.UpdateProfilePicture(person.Id, null, true));
            }

            var viewModel = PersonViewModel.Create(Repository, _firmService, Site, null, person, user.Email);
            return View(viewModel);
        }
        public ActionResult AdminEdit(Guid id, int? seminarId, bool? allList, PersonEditModel personEditModel, HttpPostedFileBase profilepic)
        {
            var user = _userRepository.GetNullableById(id);

            if (user == null)
            {
                Message = string.Format(Messages.NotFound, "user", id);
                int sid = 0;

                if (!seminarId.HasValue)
                {
                    sid = SiteService.GetLatestSeminar(Site).Id;
                }
                else
                {
                    sid = seminarId.Value;
                }

                return this.RedirectToAction<AttendeeController>(a => a.Index(sid));
            }

            var seminarPerson = _seminarPersonRepository.GetNullableById(personEditModel.SeminarPersonId);
            var person = SetPerson(personEditModel, seminarPerson, ModelState, user.Person, profilepic);

            var membership = user.Membership;
            user.SetUserName(personEditModel.UserName);
            membership.SetEmail(personEditModel.Email);

            if (ModelState.IsValid)
            {
                _personRepository.EnsurePersistent(person);
                _userRepository.EnsurePersistent(user);
                _membershipRepository.EnsurePersistent(membership);

                if (seminarPerson != null) _seminarPersonRepository.EnsurePersistent(seminarPerson);
                Message = string.Format(Messages.Saved, "Person");

                // send to crop photo if one was uploaded
                if (profilepic != null) return this.RedirectToAction(a => a.UpdateProfilePicture(person.Id, seminarId, true));

                return this.RedirectToAction(a => a.AdminEdit(person.User.Id, seminarId, null));
            }

            ViewBag.AllList =  allList ?? false;
            var viewModel = AdminPersonViewModel.Create(Repository, _firmService, Site, seminarId, user.Person, user.Email);
            return View(viewModel);
        }
        public ActionResult Create(int? id, PersonEditModel personEditModel, HttpPostedFileBase profilepic)
        {
            ModelState.Clear();

            var seminar = _seminarRepository.GetNullableById(id.HasValue ? id.Value : 0);

            var person = personEditModel.Person;

            var user = _userRepository.Queryable.FirstOrDefault(a => a.LoweredUserName == personEditModel.UserName.ToLower());
            person.User = user;
            person.AddSite(SiteService.LoadSite(Site));

            SeminarPerson seminarPerson = null;
            if (seminar != null)
            {
                seminarPerson = new SeminarPerson(seminar, person) { Invite = true, Title = personEditModel.Title };
                person.AddSeminarPerson(seminarPerson);
                seminarPerson.TransferValidationMessagesTo(ModelState);
            }

            person = SetPerson(personEditModel, seminarPerson, ModelState, person, profilepic);

            ModelState.Remove("Person.User");

            if (ModelState.IsValid)
            {

                // try to create the user
                var createStatus = _membershipService.CreateUser(personEditModel.UserName
                                              , Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 10)
                                              , personEditModel.Email);

                // retrieve the user to assign
                var createdUser = _userRepository.Queryable.FirstOrDefault(a => a.LoweredUserName == personEditModel.UserName.ToLower());
                person.User = createdUser;

                // save only if user creation was successful
                if (createStatus == MembershipCreateStatus.Success)
                {
                    // we're good save the person object
                    _personRepository.EnsurePersistent(person);
                    Message = string.Format(Messages.Saved, "Person");

                    if (person.OriginalPicture != null) return this.RedirectToAction(a => a.UpdateProfilePicture(person.Id, null, false));
                    else return this.RedirectToAction(a => a.AdminEdit(createdUser.Id, null, null));
                }

                ModelState.AddModelError("Create User", AccountValidation.ErrorCodeToString(createStatus));
            }

            ViewData["site"] = Site;

            var viewModel = PersonViewModel.Create(Repository, _firmService, Site, seminar, person, personEditModel.Email);
            viewModel.Addresses = personEditModel.Addresses;
            viewModel.UserName = personEditModel.UserName;
            return View(viewModel);
        }
        private Person SetPerson(PersonEditModel personEditModel, SeminarPerson seminarPerson, ModelStateDictionary modelState, Person person = null, HttpPostedFileBase profilePic = null)
        {
            modelState.Clear();

            person = person ?? personEditModel.Person;

            // copy all the fields
            Mapper.Map(personEditModel, person);

            SetAddresses(person, personEditModel.Addresses, ModelState);
            SetContacts(person, personEditModel.Contacts, ModelState);

            if (seminarPerson != null)
            {
                SetCommodities(seminarPerson, personEditModel.Commodities);

                if (personEditModel.Firm != null || !string.IsNullOrEmpty(personEditModel.FirmName))
                {
                    seminarPerson.Firm = personEditModel.Firm ?? new Firm(personEditModel.FirmName, personEditModel.FirmDescription) { WebAddress = personEditModel.FirmWebAddress };
                }
                if (personEditModel.Title != null) //Not sure what is happening here. This may fix it.
                {
                    seminarPerson.Title = personEditModel.Title;
                }
            }

            // deal with the image))
            if (profilePic != null)
            {
                // blank out existing image files
                person.OriginalPicture = null;
                person.MainProfilePicture = null;
                person.ThumbnailPicture = null;

                // read the file and set the original picture
                var reader = new BinaryReader(profilePic.InputStream);
                person.OriginalPicture = reader.ReadBytes(profilePic.ContentLength);
                person.ContentType = profilePic.ContentType;
            }

            // run the validation
            person.TransferValidationMessagesTo(modelState);

            return person;
        }