/// <summary>
 /// Creator: Austin Gee
 /// Created: 4/28/2020
 /// Approver: Michael Thompson
 ///
 /// Lets a customer apply to adopt an animal
 /// </summary>
 /// <remarks>
 ///
 /// Updater: NA
 /// Updated: NA
 /// Update: NA
 /// </remarks>
 /// <param name="email"></param>
 /// <param name="animalID"></param>
 /// <returns></returns>
 public ActionResult CustomerStartApplication(string email, int animalID)
 {
     if (ModelState.IsValid)
     {
         LogicLayer.CustomerManager custMgr = new LogicLayer.CustomerManager();
         try
         {
             if (custMgr.FindCustomer(email))
             {
                 // if a customer is in the database they can go straight to applying
                 return(RedirectToAction("CustomerConfirmAdoptionApplication", new { animalID = animalID }));
             }
             else
             {
                 // if a customer isnt in the database they need to register as an
                 return(RedirectToAction("AdoptionCustomerRegister", new { email = email, animalID = animalID }));
             }
         }
         catch (Exception)
         {
             return(RedirectToAction("Index"));
         }
     }
     return(RedirectToAction("Index"));
 }
Example #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));
        }