Ejemplo n.º 1
0
        public async Task Users()
        {
            using var roleStore = new RoleStore <AppRole>(_context);
            using var userStore = new UserStore <AppUser>(_context);
            if (!_context.AppUserRole.Any())
            {
                if (!_context.Users.Any())
                {
                    if (!_context.AppRole.Any())
                    {
                        var applicationRoles = new List <AppRole>();
                        foreach (var item in RoleData.AppRoles)
                        {
                            applicationRoles.Add(
                                new AppRole
                            {
                                CreatedDate    = DateTime.Now,
                                Name           = item,
                                Description    = "",
                                NormalizedName = _normalizer.NormalizeName(item)
                            });
                        }
                        ;

                        foreach (var role in applicationRoles)
                        {
                            await _context.AppRole
                            .AddAsync(role)
                            .ConfigureAwait(false);
                        }
                        await _context.SaveChangesAsync()
                        .ConfigureAwait(false);
                    }
                    var users   = new List <UserInitializerVM>();
                    var userPer = new UserInitializerVM
                    {
                        Name     = "PER",
                        Email    = "*****@*****.**",
                        Key      = "per2018",
                        ImageUri = new Uri("~/images/ico/mejillondechile.svg", UriKind.Relative)
                    };
                    userPer.Roles.Add(RoleData.AppRoles.ElementAt(0));
                    userPer.Plataforma.Add(Plataforma.mytilidb);
                    userPer.Claims.Add("per");
                    users.Add(userPer);
                    var userMitilidb = new UserInitializerVM
                    {
                        Name     = "MytiliDB",
                        Email    = "*****@*****.**",
                        Key      = "sivisam2016",
                        ImageUri = new Uri("~/images/ico/bibliomit.svg", UriKind.Relative)
                    };
                    userMitilidb.Roles.Add(RoleData.AppRoles.ElementAt(0));
                    userMitilidb.Plataforma.Add(Plataforma.mytilidb);
                    userMitilidb.Claims.Add("mitilidb");
                    users.Add(userMitilidb);
                    var userWebmaster = new UserInitializerVM
                    {
                        Name     = "WebMaster",
                        Email    = "*****@*****.**",
                        Key      = "34#$erERdfDFcvCV",
                        ImageUri = new Uri("~/images/ico/bibliomit.svg", UriKind.Relative),
                        Rating   = 10
                    };
                    userWebmaster.Roles.AddRange(RoleData.AppRoles);
                    userWebmaster.Plataforma.AddRange(EnumUtils.Enum2List <Plataforma>());
                    userWebmaster.Claims.AddRange(ClaimData.UserClaims);
                    users.Add(userWebmaster);
                    var userSernapesca = new UserInitializerVM
                    {
                        Name     = "Sernapesca",
                        Email    = "*****@*****.**",
                        Key      = "sernapesca2018",
                        ImageUri = new Uri("~/images/ico/bibliomit.svg", UriKind.Relative)
                    };
                    userSernapesca.Roles.Add(RoleData.AppRoles.ElementAt(0));
                    userSernapesca.Plataforma.Add(Plataforma.boletin);
                    userSernapesca.Claims.Add("sernapesca");
                    users.Add(userSernapesca);
                    var userIntemit = new UserInitializerVM
                    {
                        Name     = "Intemit",
                        Email    = "*****@*****.**",
                        Key      = "intemit2018",
                        ImageUri = new Uri("~/images/ico/bibliomit.svg", UriKind.Relative)
                    };
                    userIntemit.Roles.Add(RoleData.AppRoles.ElementAt(0));
                    userIntemit.Plataforma.Add(Plataforma.psmb);
                    userIntemit.Claims.Add("intemit");
                    users.Add(userIntemit);

                    foreach (var item in users)
                    {
                        var user = new AppUser
                        {
                            UserName           = item.Name,
                            NormalizedUserName = _normalizer.NormalizeName(item.Name),
                            Email           = item.Email,
                            NormalizedEmail = _normalizer.NormalizeEmail(item.Email),
                            EmailConfirmed  = true,
                            LockoutEnabled  = false,
                            SecurityStamp   = Guid.NewGuid().ToString(),
                            ProfileImageUrl = item.ImageUri
                        };

                        user.NormalizedUserName = _normalizer
                                                  .NormalizeName(user.UserName);
                        var hasher         = new PasswordHasher <AppUser>();
                        var hashedPassword = hasher.HashPassword(user, item.Key);
                        user.PasswordHash = hashedPassword;

                        foreach (var claim in item.Claims)
                        {
                            user.Claims.Add(new IdentityUserClaim <string>
                            {
                                ClaimType  = claim,
                                ClaimValue = claim
                            });
                        }

                        foreach (var role in item.Roles)
                        {
                            var roller = await _context.Roles
                                         .SingleOrDefaultAsync(r => r.Name == role)
                                         .ConfigureAwait(false);

                            user.Roles.Add(new IdentityUserRole <string>
                            {
                                UserId = user.Id,
                                RoleId = roller.Id
                            });
                        }
                        await _context.Users.AddAsync(user)
                        .ConfigureAwait(false);
                    }
                    await _context.SaveChangesAsync()
                    .ConfigureAwait(false);
                }
            }
            return;
        }