Exemplo n.º 1
0
        public async Task <ActionResult> RegisterVolunteerUser(RegisterVolunteerViewModel model)
        {
            if (ModelState.IsValid)
            {
                LogicLayer.VolunteerManager volMgr = new LogicLayer.VolunteerManager();
                try
                {
                    if (volMgr.FindVolunteer(model.Email))
                    {
                        return(RedirectToAction("Register", "Account"));
                    }
                    else
                    {
                        var volunteer = new DataTransferObjects.Volunteer
                        {
                            FirstName   = model.GivenName,
                            LastName    = model.FamilyName,
                            Email       = model.Email,
                            PhoneNumber = model.PhoneNumber
                        };
                        if (volMgr.AddVolunteer(volunteer))
                        {
                            var VolunteerID = volMgr.RetrieveVolunteerIDFromEmail(model.Email);
                            var user        = new ApplicationUser
                            {
                                VolEmail   = model.Email,
                                GivenName  = model.GivenName,
                                FamilyName = model.FamilyName,
                                UserName   = model.Email,
                                Email      = model.Email
                            };
                            var result = await UserManager.CreateAsync(user, "newuser");

                            if (result.Succeeded)
                            {
                                return(RedirectToAction("Index", "Admin"));
                            }
                            AddErrors(result);
                        }
                    }
                }
                catch (Exception)
                {
                    return(View(model));
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                LogicLayer.UserManager      userMgr = new LogicLayer.UserManager();
                LogicLayer.VolunteerManager volMgr  = new LogicLayer.VolunteerManager();
                try
                {
                    if (userMgr.FindUser(model.Email))
                    {
                        var oldUser = userMgr.AuthenticateUser(model.Email, model.Password);
                        var newuser = new ApplicationUser
                        {
                            GivenName  = oldUser.FirstName,
                            FamilyName = oldUser.LastName,
                            EmployeeID = oldUser.PUUserID,

                            UserName = model.Email,
                            Email    = model.Email
                        };
                        var newresult = await UserManager.CreateAsync(newuser, model.Password);

                        if (newresult.Succeeded)
                        {
                            foreach (var role in oldUser.PURoles)
                            {
                                UserManager.AddToRole(newuser.Id, role);
                            }
                            await SignInManager.SignInAsync(newuser, isPersistent : false, rememberBrowser : false);

                            return(RedirectToAction("Index", "Home"));
                        }
                        AddErrors(newresult);
                    }
                    else if (volMgr.FindVolunteer(model.Email))
                    {
                        var oldVolunteer = volMgr.AuthenticateVolunteer(model.Email, model.Password);
                        var newVolunteer = new ApplicationUser
                        {
                            GivenName  = oldVolunteer.FirstName,
                            FamilyName = oldVolunteer.LastName,
                            VolEmail   = oldVolunteer.Email,

                            UserName = model.Email,
                            Email    = model.Email
                        };
                        var newresult = await UserManager.CreateAsync(newVolunteer, model.Password);

                        if (newresult.Succeeded)
                        {
                            foreach (var role in oldVolunteer.Skills)
                            {
                                UserManager.AddToRole(newVolunteer.Id, role);
                            }
                            await SignInManager.SignInAsync(newVolunteer, isPersistent : false, rememberBrowser : false);

                            return(RedirectToAction("Index", "Home"));
                        }
                        AddErrors(newresult);
                    }
                    else
                    {
                        LogicLayer.CustomerManager custMgr = new LogicLayer.CustomerManager();
                        if (custMgr.FindCustomer(model.Email))
                        {
                            var oldUser = custMgr.AuthenticateCustomer(model.Email, model.Password);
                            var newuser = new ApplicationUser
                            {
                                GivenName  = oldUser.FirstName,
                                FamilyName = oldUser.LastName,
                                CustEmail  = oldUser.Email,

                                UserName = model.Email,
                                Email    = model.Email
                            };
                            var newresult = await UserManager.CreateAsync(newuser, model.Password);

                            if (newresult.Succeeded)
                            {
                                await SignInManager.SignInAsync(newuser, isPersistent : false, rememberBrowser : false);

                                return(RedirectToAction("Index", "Home"));
                            }
                            AddErrors(newresult);
                        }
                        else
                        {
                            var newUser = new ApplicationUser
                            {
                                UserName = model.Email,
                                Email    = model.Email
                            };
                            var newResult = await UserManager.CreateAsync(newUser, model.Password);

                            if (newResult.Succeeded)
                            {
                                await SignInManager.SignInAsync(newUser, isPersistent : false, rememberBrowser : false);

                                return(RedirectToAction("RegisterContinuation", "Account", model));
                            }
                            AddErrors(newResult);
                        }
                    }
                }
                // Did this next part in the exception because if it can't find a user in the db its a customer. If a customer already exists in our db we don't want to add them again
                catch
                {
                    var user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email
                    };
                    var result = await UserManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                        // Send an email with this link
                        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                        return(RedirectToAction("Index", "Home"));
                    }
                    AddErrors(result);
                }
            }
            return(View(model));
        }