示例#1
0
 // начальная инициализация бд
 public void SetInitialData(UserModelUoW adminDto, List <string> roles)
 {
     foreach (string roleName in roles)
     {
         var role = RoleManager.FindByName(roleName);
         if (role == null)
         {
             role = new ShopRole {
                 Name = roleName
             };
             RoleManager.Create(role);
         }
     }
     Create(adminDto);
 }
示例#2
0
        public ShopRole GetUserRole(string userId)
        {
            var role = this.dbContext.Roles.FirstOrDefault(r => r.Users.Any(u => u.UserId == userId));

            if (role == null)
            {
                throw new InvalidOperationException("Invalid role or user id.");
            }

            var shopRole = new ShopRole
            {
                Id   = role.Id,
                Name = role.Name
            };

            return(shopRole);
        }
示例#3
0
        public ShopRole Get(string roleId)
        {
            var role = this.dbContext.Roles.FirstOrDefault(r => r.Id == roleId);

            if (role == null)
            {
                throw new InvalidOperationException("Invalid role id.");
            }

            var shopRole = new ShopRole
            {
                Id   = role.Id,
                Name = role.Name
            };

            return(shopRole);
        }
        static internal IApplicationBuilder SeedIdentity(this IApplicationBuilder builder)
        {
            using (var scope = builder.ApplicationServices.CreateScope())
            {
                var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ShopUser> >();
                var roleManager = scope.ServiceProvider.GetRequiredService <RoleManager <ShopRole> >();

                ShopRole superAdminRole = roleManager.FindByNameAsync("Admin").Result;

                if (superAdminRole == null)
                {
                    superAdminRole = new ShopRole
                    {
                        Name = "Admin"
                    };
                    roleManager.CreateAsync(superAdminRole).Wait();
                }

                ShopUser superAdminUser = userManager.FindByEmailAsync("*****@*****.**").Result;

                if (superAdminUser == null)
                {
                    superAdminUser = new ShopUser
                    {
                        Email          = "*****@*****.**",
                        UserName       = "******",
                        EmailConfirmed = true
                    };

                    var resultUser = userManager.CreateAsync(superAdminUser, "123").Result;

                    if (resultUser.Succeeded)
                    {
                        var roleResult = userManager.AddToRoleAsync(superAdminUser, "Admin").Result;
                    }
                }
                else if (!userManager.IsInRoleAsync(superAdminUser, "Admin").Result)
                {
                    userManager.AddToRoleAsync(superAdminUser, "Admin").Wait();
                }
            }


            return(builder);
        }
示例#5
0
        public async Task <IActionResult> Create(CreateViewModel model)
        {
            if (model == null)
            {
                return(NotFound());
            }
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                var role = new ShopRole {
                    Name = model.Name, Description = model.Description, ModuleID = model.ModuleID
                };

                var result = await _roleManager.CreateAsync(role);

                if (result.Succeeded)
                {
                    _logger.Information("Role {@Role} successfully added.", role);
                    StatusMessage = (new AlertMessage(AlertStatus.success, "Роль успешно добавлена.")).SerializeObject();
                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    _logger.Error("Error {@Error} adding role {@Role}.", result.Errors, role);
                }

                AddErrors(result);
                return(View(model));
            }
            catch (Exception ex)
            {
                Log.Error(ex, "There was an error adding {RoleName} role", model.Name);
                return(View());
            }
        }