示例#1
0
        public async Task <IActionResult> Add(DynmicRoleViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(model.NodeSelected))
                {
                    var guid = Guid.NewGuid();
                    var role = new Role
                    {
                        Id          = guid,
                        Name        = model.Title,
                        Title       = model.Title,
                        Enable      = model.Enable,
                        Description = model.Description,
                        ActionArray = model.NodeSelected,
                    };

                    var result = await _roleManager.CreateAsync(role);

                    if (result.Succeeded)
                    {
                        await _roleManager.AddOrUpdateRoleClaims(guid, GlobalEnum.DynamicRole, model.NodeSelected);

                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        ModelState.AddErrorsFromResult(result);
                    }
                }
                else
                {
                    ModelState.AddModelError("JsonJSTree", "حداقل باید یک سطح دسترسی انتخاب کنید.");
                }
            }
            return(View(model));
        }
示例#2
0
        public async Task <IdentityResult> SeedDatabaseWithAdminUserAsync()
        {
            var name     = "admin";
            var password = "******";
            var email    = "*****@*****.**";
            var roleName = "admin";

            var thisMethodName = nameof(SeedDatabaseWithAdminUserAsync);

            var adminUser = await _applicationUserManager.FindByNameAsync(name);

            if (adminUser != null)
            {
                _logger.LogInformation($"{thisMethodName}: adminUser already exists.");
                var role = _roleManager.GetRoleByUserGuid(adminUser.Id);
                if (_env.IsDevelopment() && role != null)
                {
                    await _roleManager.AddOrUpdateRoleClaims(role.Id, GlobalEnum.DynamicRole, _mvcActionsDiscoveryService.GetAllAdminActionRoute());
                }
                await _uow.SaveChangesAsync();

                return(IdentityResult.Success);
            }

            //Create the `Admin` Role if it does not exist
            var adminRole = await _roleManager.FindByNameAsync(roleName);

            if (adminRole == null)
            {
                adminRole    = new Role(roleName);
                adminRole.Id = new Guid();
                var adminRoleResult = await _roleManager.CreateAsync(adminRole);

                if (adminRoleResult == IdentityResult.Failed())
                {
                    _logger.LogError($"{thisMethodName}: adminRole CreateAsync failed. {adminRoleResult.DumpErrors()}");
                    return(IdentityResult.Failed());
                }
                else
                {
                    // await _roleManager.AddOrUpdateRoleClaims(adminRole.Id, GlobalEnum.DynamicRole, _mvcControllerDiscovery.GetAllAdminActionRoute());
                }
            }
            else
            {
                _logger.LogInformation($"{thisMethodName}: adminRole already exists.");
            }

            adminUser = new User
            {
                UserName       = name,
                Email          = email,
                EmailConfirmed = true,
                IsEmailPublic  = true,
                LockoutEnabled = true
            };
            var adminUserResult = await _applicationUserManager.CreateAsync(adminUser, password);

            if (adminUserResult == IdentityResult.Failed())
            {
                _logger.LogError($"{thisMethodName}: adminUser CreateAsync failed. {adminUserResult.DumpErrors()}");
                return(IdentityResult.Failed());
            }

            var setLockoutResult = await _applicationUserManager.SetLockoutEnabledAsync(adminUser, enabled : false);

            if (setLockoutResult == IdentityResult.Failed())
            {
                _logger.LogError($"{thisMethodName}: adminUser SetLockoutEnabledAsync failed. {setLockoutResult.DumpErrors()}");
                return(IdentityResult.Failed());
            }

            var addToRoleResult = await _applicationUserManager.AddToRoleAsync(adminUser, adminRole.Name);

            if (addToRoleResult == IdentityResult.Failed())
            {
                _logger.LogError($"{thisMethodName}: adminUser AddToRoleAsync failed. {addToRoleResult.DumpErrors()}");
                return(IdentityResult.Failed());
            }

            return(IdentityResult.Success);
        }