Exemplo n.º 1
0
        public async Task <ActionResult> RegisterEmployeeUser(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Check to see if this user is in the existing database
                LogicLayer.UserManager usrMgr = new LogicLayer.UserManager();
                try
                {
                    if (usrMgr.FindUser(model.Email))
                    {
                        // If this user already exists, we need to use the regular Register method
                        return(RedirectToAction("Register", "Account"));
                    }
                    // Not an existing user, create a DataObjects.User without roles
                    else
                    {
                        var employee = new DataObjects.User()
                        {
                            // These fields are need by sp_insert_user
                            Email       = model.Email,
                            FirstName   = model.GivenName,
                            LastName    = model.FamilyName,
                            PhoneNumber = model.PhoneNumber,
                        };
                        // Add the DataObjects.User to Employe table
                        if (usrMgr.AddUser(employee))
                        {
                            var employeeID = usrMgr.RetrieveUserIDFromEmail(model.Email);
                            var user       = new ApplicationUser()
                            {
                                EmployeeID = employeeID,
                                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
                {
                    // Creating Employee Failed
                    return(View(model));
                }
            }
            // ModelState was not valid
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> RegisterPersonUser(RegisterPersonViewModel model)
        {
            if (ModelState.IsValid)
            {
                // check to see if this user is in the existing database
                LogicLayer.UserManager usrMgr = new LogicLayer.UserManager();
                try
                {
                    if (usrMgr.FindUser(model.Email))
                    {
                        // if this user already exists, we need to use the regular Register method
                        return(RedirectToAction("Register", "Account"));
                    }
                    else // not a existing user, creae a DataObjects.User without roles
                    {
                        var person = new DataObjects.User()
                        {
                            Email       = model.Email,
                            FirstName   = model.GivenName,
                            LastName    = model.FamilyName,
                            PhoneNumber = model.PhoneNumber
                        };
                        if (usrMgr.AddUser(person)) // add the DataObjects.User to Person table
                        {
                            var personID = usrMgr.RetrieveUserIDFromEmail(model.Email);
                            var user     = new ApplicationUser // if it worked, create an identity user
                            {
                                PersonID   = personID,
                                GivenName  = model.GivenName,
                                FamilyName = model.FamilyName,
                                UserName   = model.Email,
                                Email      = model.Email
                            };
                            var result = await UserManager.CreateAsync(user, "newuser");

                            if (result.Succeeded) // go back to Admin/Index View
                            {
                                return(RedirectToAction("Index", "Admin"));
                            }
                            AddErrors(result);
                        }
                    }
                }
                catch (Exception)
                {
                    // creating employee failed
                    return(View(model));
                }
            }
            // modelstate was not valid
            return(View(model));
        }