public LearningOutcomeNotesController(LOContext context, RoleManager <IdentityRole> roleManager, UserManager <IdentityUser> userManager, UserRolesDB userContext)
 {
     _context     = context;
     _roleManager = roleManager;
     _userManager = userManager;
     _userContext = userContext;
 }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                IServiceProvider services = scope.ServiceProvider;

                UserRolesDB   roleContext   = services.GetRequiredService <UserRolesDB>();
                WorkerContext workerContext = services.GetRequiredService <WorkerContext>();
                DbInitializer.InitializeAsync(roleContext, workerContext, services).Wait();
            }

            host.Run();
        }
Ejemplo n.º 3
0
 public UsersPermissionsController(RoleManager <IdentityRole> roleManager, UserManager <IdentityUser> userManager, UserRolesDB context)
 {
     _roleManager = roleManager;
     _userManager = userManager;
     _context     = context;
 }
Ejemplo n.º 4
0
        public static async Task InitializeUsers(UserRolesDB udb, IServiceProvider serviceProvider)
        {
            udb.Database.EnsureCreated();
            if (udb.Users.Any())
            {
                return;
            }
            //seed user roles to db https://forums.asp.net/t/2151809.aspx?MVC+Core+2+1+Seeding+Users+Roles+Issue
            string[] roles       = new string[] { "Instructor", "Admin", "Chair" };
            var      RoleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >();
            var      UserManager = serviceProvider.GetRequiredService <UserManager <IdentityUser> >();

            IdentityResult roleRes;

            foreach (string r in roles)
            {
                roleRes = await RoleManager.CreateAsync(new IdentityRole(r));
            }

            var admins = new IdentityUser[]
            {
                new IdentityUser {
                    UserName = "******", PasswordHash = "123ABC!@#def", EmailConfirmed = true
                }
            };

            IdentityResult res;

            foreach (IdentityUser i in admins)
            {
                res = UserManager.CreateAsync(i, i.PasswordHash).Result;
                if (res.Succeeded)
                {
                    UserManager.AddToRoleAsync(i, "Admin").Wait();
                }
            }

            var instructors = new IdentityUser[]
            {
                new IdentityUser {
                    UserName = "******", PasswordHash = "123ABC!@#def", EmailConfirmed = true
                },
                new IdentityUser {
                    UserName = "******", PasswordHash = "123ABC!@#def", EmailConfirmed = true
                },
                new IdentityUser {
                    UserName = "******", PasswordHash = "123ABC!@#def", EmailConfirmed = true
                }
            };

            IdentityResult res2;

            foreach (IdentityUser i in instructors)
            {
                res2 = UserManager.CreateAsync(i, i.PasswordHash).Result;
                if (res2.Succeeded)
                {
                    UserManager.AddToRoleAsync(i, "Instructor").Wait();
                }
            }

            var chairs = new IdentityUser[]
            {
                new IdentityUser {
                    UserName = "******", PasswordHash = "123ABC!@#def", EmailConfirmed = true
                }
            };

            IdentityResult res3;

            foreach (IdentityUser i in chairs)
            {
                res3 = UserManager.CreateAsync(i, i.PasswordHash).Result;
                if (res3.Succeeded)
                {
                    UserManager.AddToRoleAsync(i, "Chair").Wait();
                }
            }

            return;
        }
Ejemplo n.º 5
0
        public static async Task InitializeAsync(UserRolesDB context, WorkerContext workerContext, IServiceProvider serviceProvider)
        {
            //context.Database.EnsureDeleted();
            //workerContext.Database.EnsureDeleted();

            context.Database.Migrate();
            workerContext.Database.Migrate();

            if (context.Users.Any())
            {
                return;   // DB has been seeded
            }

            RoleManager <IdentityRole> RoleManager = serviceProvider
                                                     .GetRequiredService <RoleManager <IdentityRole> >();

            // Create roles
            string[]       roleNames = { "Admin", "Worker", "Customer" };
            IdentityResult roleResult;

            foreach (string roleName in roleNames)
            {
                bool roleExist = await RoleManager.RoleExistsAsync(roleName);

                if (!roleExist)
                {
                    roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
                }
            }

            UserManager <IdentityUser> userManager = serviceProvider
                                                     .GetRequiredService <UserManager <IdentityUser> >();

            string UserPassword = "******";

            // Create user Barber01
            IdentityUser user = new IdentityUser
            {
                Id                 = "1",
                UserName           = "******",
                NormalizedUserName = "******",
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**",
                EmailConfirmed     = true,
            };
            Worker barber01 = new Worker()
            {
                Name        = "Dan Smith",
                Services    = "Haircuts, Beards",
                Description = "Barber for 8 years professionally.",
                User        = user,
                Job         = "Barber",
                Schedule    = new List <DaySchedule>
                {
                    new DaySchedule {
                        dateTime = new DateTime(2019, 11, 20), Nine = true, NineThirty = true, Ten = true
                    },
                    new DaySchedule {
                        dateTime = new DateTime(2019, 12, 5), Twelve = true, TwelveThirty = true, One = true
                    },
                    new DaySchedule {
                        dateTime = new DateTime(2018, 1, 5), Four = true, FourThirty = true
                    }
                }
            };

            workerContext.Add(barber01);
            workerContext.SaveChanges();
            IdentityResult createUser = await userManager.CreateAsync(user, UserPassword);

            if (createUser.Succeeded)
            {
                await userManager.AddToRoleAsync(user, "Worker");
            }

            List <WorkerComment> barber01Comments = new List <WorkerComment>
            {
                new WorkerComment()
                {
                    Comment = "Good Job!", StarRating = 5, WorkerID = 1, Worker = barber01, Name = "Bob Ross", LastUpdated = DateTime.UtcNow.ToLocalTime()
                },
                new WorkerComment()
                {
                    Comment = "Decent Job.", StarRating = 3, WorkerID = 1, Worker = barber01, Name = "Debra Burks", LastUpdated = DateTime.UtcNow.ToLocalTime()
                },
                new WorkerComment()
                {
                    Comment = "Terrible Job.", StarRating = 1, WorkerID = 1, Worker = barber01, Name = "Genoveva Baldwin", LastUpdated = DateTime.UtcNow.ToLocalTime()
                }
            };

            foreach (WorkerComment s in barber01Comments)
            {
                workerContext.WorkerComment.Add(s);
            }

            // Create user Barber02
            IdentityUser user2 = new IdentityUser
            {
                Id                 = "2",
                UserName           = "******",
                NormalizedUserName = "******",
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**",
                EmailConfirmed     = true,
            };
            Worker barber02 = new Worker()
            {
                Name        = "Ben Jones",
                Services    = "Haircuts",
                Description = "Barber for 2 years.",
                User        = user2,
                Job         = "Barber",
                Schedule    = new List <DaySchedule>
                {
                    new DaySchedule {
                        dateTime = new DateTime(2019, 11, 20), Nine = true, NineThirty = true, Ten = true
                    },
                    new DaySchedule {
                        dateTime = new DateTime(2019, 12, 5), Twelve = true, TwelveThirty = true, One = true
                    },
                    new DaySchedule {
                        dateTime = new DateTime(2018, 1, 5), Four = true, FourThirty = true
                    }
                }
            };

            workerContext.Add(barber02);
            IdentityResult createUser4 = await userManager.CreateAsync(user2, UserPassword);

            if (createUser4.Succeeded)
            {
                await userManager.AddToRoleAsync(user2, "Worker");
            }

            WorkerComment[] barber02Comments = new WorkerComment[]
            {
                new WorkerComment()
                {
                    Comment = "Good Job!", StarRating = 5, WorkerID = 2, Worker = barber02, Name = "Jacquilne Duncan", LastUpdated = DateTime.UtcNow.ToLocalTime()
                },
                new WorkerComment()
                {
                    Comment = "Decent Job.", StarRating = 3, WorkerID = 2, Worker = barber02, Name = "Lyndsey Bean", LastUpdated = DateTime.UtcNow.ToLocalTime()
                },
                new WorkerComment()
                {
                    Comment = "Terrible Job.", StarRating = 1, WorkerID = 2, Worker = barber02, Name = "Pamelia Newman", LastUpdated = DateTime.UtcNow.ToLocalTime()
                }
            };
            foreach (WorkerComment s in barber02Comments)
            {
                workerContext.WorkerComment.Add(s);
            }

            // Create user Barber03
            IdentityUser user3 = new IdentityUser
            {
                UserName           = "******",
                NormalizedUserName = "******",
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                EmailConfirmed  = true,
            };
            Worker barber03 = new Worker()
            {
                Name        = "Samantha Harris",
                Services    = "Haircuts/hair styling, beards, nails",
                Description = "Barber for 9 years.",
                Job         = "Hair Stylist",
                User        = user3,
                Schedule    = new List <DaySchedule>
                {
                    new DaySchedule {
                        dateTime = new DateTime(2019, 11, 22), Nine = true, NineThirty = true, Ten = true, ElevenThirty = true
                    },
                    new DaySchedule {
                        dateTime = new DateTime(2019, 12, 5), Twelve = true
                    },
                    new DaySchedule {
                        dateTime = new DateTime(2018, 1, 5), Four = true, FourThirty = true
                    }
                }
            };

            workerContext.Add(barber03);
            IdentityResult createUser5 = await userManager.CreateAsync(user3, UserPassword);

            if (createUser5.Succeeded)
            {
                await userManager.AddToRoleAsync(user3, "Worker");
            }
            WorkerComment[] barber03Comments = new WorkerComment[]
            {
                new WorkerComment()
                {
                    Comment = "Good Job!", StarRating = 5, WorkerID = 3, Worker = barber03, Name = "Jerry Seinfeld", LastUpdated = DateTime.UtcNow.ToLocalTime()
                },
                new WorkerComment()
                {
                    Comment = "Decent Job.", StarRating = 3, WorkerID = 3, Worker = barber03, Name = "Elaine Benes", LastUpdated = DateTime.UtcNow.ToLocalTime()
                },
                new WorkerComment()
                {
                    Comment = "Terrible Job.", StarRating = 1, WorkerID = 3, Worker = barber03, Name = "George Costansa", LastUpdated = DateTime.UtcNow.ToLocalTime()
                }
            };
            foreach (WorkerComment s in barber03Comments)
            {
                workerContext.WorkerComment.Add(s);
            }

            // Create user Customer
            IdentityUser Customer = new IdentityUser
            {
                Id                 = "4",
                UserName           = "******",
                NormalizedUserName = "******",
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**",
                EmailConfirmed     = true,
            };
            IdentityResult createUser2 = await userManager.CreateAsync(Customer, UserPassword);

            if (createUser2.Succeeded)
            {
                await userManager.AddToRoleAsync(Customer, "Customer");
            }

            // Create user Admin
            user = new IdentityUser
            {
                Id                 = "5",
                UserName           = "******",
                NormalizedUserName = "******",
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**",
                EmailConfirmed     = true,
            };

            IdentityResult createUser3 = await userManager.CreateAsync(user, UserPassword);

            if (createUser3.Succeeded)
            {
                await userManager.AddToRoleAsync(user, "Admin");
            }

            workerContext.SaveChanges();
        }