Exemplo n.º 1
0
        public async Task <ActionResult> AdminRegister(AdminUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName  = model.Email,
                    Email     = model.Email,
                    RoleID    = model.Role.RoleID,
                    FirstName = model.FirstName,
                    LastName  = model.LastName
                };

                var store   = new UserStore <ApplicationUser>(new ApplicationDbContext());
                var manager = new UserManager <ApplicationUser>(store);

                var result = await manager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    var rolesRepo = RoleFactory.GetRepository();

                    var modeluser = new AdminUserViewModel
                    {
                        Roles = rolesRepo.GetRoles()
                    };
                    return(View(modeluser));
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 2
0
        public ActionResult Users()
        {
            var rolesRepo = RoleFactory.GetRepository();
            var allUsers  = _context.Users.ToList();
            var users     = allUsers.Where(x => x.RoleID == 1).ToList();
            var userVM    = users.Select(user => new UserViewModel
            {
                UserID    = user.Id,
                Email     = user.Email,
                Role      = rolesRepo.GetRoleById(user.RoleID).RoleName,
                FirstName = user.FirstName,
                LastName  = user.LastName
            }).ToList();

            var admins   = allUsers.Where(x => x.RoleID == 2).ToList();
            var adminsVM = admins.Select(user => new UserViewModel
            {
                UserID    = user.Id,
                Email     = user.Email,
                Role      = rolesRepo.GetRoleById(user.RoleID).RoleName,
                FirstName = user.FirstName,
                LastName  = user.LastName
            }).ToList();
            var model = new GroupedUserViewModel {
                Users = userVM, Admins = adminsVM
            };

            return(View(model));
        }
Exemplo n.º 3
0
        public ActionResult AdminRegister()
        {
            var rolesRepo = RoleFactory.GetRepository();

            var user = new AdminUserViewModel
            {
                Roles = rolesRepo.GetRoles()
            };

            return(View(user));
        }
Exemplo n.º 4
0
        public ActionResult EditUser(string id)
        {
            var rolesRepo = RoleFactory.GetRepository();
            var userMgr   = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(_context));
            var appUser   = userMgr.FindById(id);

            var user = new UserEditViewModel
            {
                UserID    = appUser.Id,
                FirstName = appUser.FirstName,
                LastName  = appUser.LastName,
                Email     = appUser.Email,
                Roles     = rolesRepo.GetRoles(),
                RolesID   = appUser.RoleID
            };

            return(View(user));
        }
Exemplo n.º 5
0
        protected override void Seed(GuildCars.UI.Models.ApplicationDbContext context)
        {
            //AspNetRoles table
            if (!context.Roles.Any(r => r.Name == "Administrator"))
            {
                var store   = new RoleStore <IdentityRole>(context);
                var manager = new RoleManager <IdentityRole>(store);
                var role    = new IdentityRole {
                    Name = "Administrator"
                };

                manager.Create(role);
            }

            //AspNetUsers table
            if (!context.Users.Any(u => u.UserName == "*****@*****.**"))
            {
                var passwordHasher = new PasswordHasher();
                var store          = new UserStore <ApplicationUser>(context);
                var manager        = new UserManager <ApplicationUser>(store);
                var user           = new ApplicationUser
                {
                    FirstName    = "Bob",
                    LastName     = "Guild",
                    UserName     = "******",
                    Email        = "*****@*****.**",
                    PasswordHash = passwordHasher.HashPassword("Pa$$w0rd1"),
                    RoleID       = 2 // User = 1, Admin = 2
                };

                manager.Create(user);
                manager.AddToRole(user.Id, "Administrator");
            }

            if (context.States.Count() == 0)
            {
                var statesRepo = StateFactory.GetRepository();

                IList <State> statesToAdd = new List <State>();

                foreach (var state in statesRepo.GetStates())
                {
                    statesToAdd.Add(state);
                }

                context.States.AddRange(statesToAdd);

                base.Seed(context);
            }

            if (context.Makes.Count() == 0)
            {
                var makesRepo = MakeFactory.GetRepository();

                IList <Make> makesToAdd = new List <Make>();

                foreach (var make in makesRepo.GetMakes())
                {
                    makesToAdd.Add(make);
                }

                context.Makes.AddRange(makesToAdd);

                base.Seed(context);
            }

            if (context.Models.Count() == 0)
            {
                var modelsRepo = ModelFactory.GetRepository();

                IList <Model> modelsToAdd = new List <Model>();

                foreach (var model in modelsRepo.GetModels())
                {
                    modelsToAdd.Add(model);
                }

                context.Models.AddRange(modelsToAdd);

                base.Seed(context);
            }

            if (context.BodyStyles.Count() == 0)
            {
                var bodyStylesRepo = BodyStyleFactory.GetRepository();

                IList <BodyStyle> bodyStylesToAdd = new List <BodyStyle>();

                foreach (var bodyStyle in bodyStylesRepo.GetBodyStyles())
                {
                    bodyStylesToAdd.Add(bodyStyle);
                }

                context.BodyStyles.AddRange(bodyStylesToAdd);

                base.Seed(context);
            }

            if (context.Conditions.Count() == 0)
            {
                var conditionsRepo = ConditionFactory.GetRepository();

                IList <Condition> conditionsToAdd = new List <Condition>();

                foreach (var condition in conditionsRepo.GetConditions())
                {
                    conditionsToAdd.Add(condition);
                }

                context.Conditions.AddRange(conditionsToAdd);

                base.Seed(context);
            }

            if (context.ExteriorColors.Count() == 0)
            {
                var extColorsRepo = ExteriorColorFactory.GetRepository();

                IList <ExteriorColor> extColorsToAdd = new List <ExteriorColor>();

                foreach (var extColor in extColorsRepo.GetExteriorColors())
                {
                    extColorsToAdd.Add(extColor);
                }

                context.ExteriorColors.AddRange(extColorsToAdd);

                base.Seed(context);
            }

            if (context.ExteriorColors.Count() == 0)
            {
                var extColorsRepo = ExteriorColorFactory.GetRepository();

                IList <ExteriorColor> extColorsToAdd = new List <ExteriorColor>();

                foreach (var extColor in extColorsRepo.GetExteriorColors())
                {
                    extColorsToAdd.Add(extColor);
                }

                context.ExteriorColors.AddRange(extColorsToAdd);

                base.Seed(context);
            }

            if (context.InteriorColors.Count() == 0)
            {
                var intColorsRepo = InteriorColorFactory.GetRepository();

                IList <InteriorColor> intColorsToAdd = new List <InteriorColor>();

                foreach (var intColor in intColorsRepo.GetInteriorColors())
                {
                    intColorsToAdd.Add(intColor);
                }

                context.InteriorColors.AddRange(intColorsToAdd);

                base.Seed(context);
            }

            if (context.PurchaseTypes.Count() == 0)
            {
                var purchaseTypesRepo = PurchaseTypeFactory.GetRepository();

                IList <PurchaseType> purchaseTypesToAdd = new List <PurchaseType>();

                foreach (var purchaseType in purchaseTypesRepo.GetPurchaseTypes())
                {
                    purchaseTypesToAdd.Add(purchaseType);
                }

                context.PurchaseTypes.AddRange(purchaseTypesToAdd);

                base.Seed(context);
            }

            if (context.Specials.Count() == 0)
            {
                var specialsRepo = SpecialsFactory.GetRepository();

                IList <Specials> specialsToAdd = new List <Specials>();

                foreach (var special in specialsRepo.GetSpecials())
                {
                    specialsToAdd.Add(special);
                }

                context.Specials.AddRange(specialsToAdd);

                base.Seed(context);
            }

            if (context.Transmissions.Count() == 0)
            {
                var transmissionsRepo = TransmissionFactory.GetRepository();

                IList <Transmission> transmissionsToAdd = new List <Transmission>();

                foreach (var special in transmissionsRepo.GetTransmissions())
                {
                    transmissionsToAdd.Add(special);
                }

                context.Transmissions.AddRange(transmissionsToAdd);

                base.Seed(context);
            }

            if (context.Cars.Count() == 0)
            {
                var carsRepo = GuildRepositoryFactory.GetRepository();

                IList <Car> carsToAdd = new List <Car>();

                foreach (var car in carsRepo.GetAllCars())
                {
                    carsToAdd.Add(car);
                }

                context.Cars.AddRange(carsToAdd);

                base.Seed(context);
            }

            if (context.GuildRoles.Count() == 0)
            {
                var rolesRepo = RoleFactory.GetRepository();

                IList <GuildRole> rolesToAdd = new List <GuildRole>();

                foreach (var role in rolesRepo.GetRoles())
                {
                    rolesToAdd.Add(role);
                }

                context.GuildRoles.AddRange(rolesToAdd);

                base.Seed(context);
            }
        }