Exemplo n.º 1
0
        public async Task<ActionResult> RegisterAdministrator(RegisterAdministratorViewModel m)
        {
            //If input was valid
            if (ModelState.IsValid)
            {
                //Create an account so they can login using their username and password
                var user = new ApplicationUser { UserName = m.Username, Email = m.Email };
                var result = await UserManager.CreateAsync(user, m.Password);
                
                //If creating login account succeeded
                if (result.Succeeded)
                {
                    //Log in as user
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    //Create person in the database
                    var person = new PX_Model.Person();

                    person.First_Name = m.FirstName;
                    person.Last_Name = m.LastName;
                    person.Phone_Number = m.PhoneNumber;
                    person.Postcode = m.Postcode;
                    person.State = m.State;
                    person.Street = m.Street;
                    person.Suburb = m.Suburb;
                    person.Email = m.Email;
                    person.Username = m.Username;

                    //Push person to the database
                    _db.People.Add(person);
                    _db.SaveChanges();

                    //Create admin account
                    PX_Model.Administrator admin = new PX_Model.Administrator();
                    admin.Person_Id = person.Id;
                   
                    //Push admin account to the database
                    _db.Administrators.Add(admin);
                    _db.SaveChanges();

                    //Assign them to the role of admin
                    var u = UserManager.FindByName(m.Username);
                    UserManager.AddToRole(u.Id, "Admin");

                    //Go to admin dashboard
                    return RedirectToAction("Index", "Administrator");
                }
                AddErrors(result);
            }
            //If error, then show the current view with error messages
            return View(m);
        }
        public async Task<ActionResult> RegisterAdministrator(RegisterAdministratorViewModel m)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = m.Username, Email = m.Email };
                var result = await UserManager.CreateAsync(user, m.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    var person = new PX_Model.Person();

                    person.First_Name = m.FirstName;
                    person.Last_Name = m.LastName;
                    person.Phone_Number = m.PhoneNumber;
                    person.Postcode = m.Postcode;
                    person.State = m.State;
                    person.Street = m.Street;
                    person.Suburb = m.Suburb;
                    person.Email = m.Email;
                    person.Username = m.Username;

                    _db.People.Add(person);
                    _db.SaveChanges();

                    PX_Model.Administrator admin = new PX_Model.Administrator();
                    admin.Person_Id = person.Id;
                   
                    _db.Administrators.Add(admin);
                    _db.SaveChanges();

                    var u = UserManager.FindByName(m.Username);
                    UserManager.AddToRole(u.Id, "Admin");

                    return RedirectToAction("Index", "Administrator");
                }
                AddErrors(result);
            }
            return View(m);
        }