Exemplo n.º 1
0
        private void AddDefaultPermissions()
        {
            if (!_context.RoleSubsystems.Any())
            {
                _logger.LogInformation("Adding default permissions");

                foreach (var subsystem in _subsystems)
                {
                    if (subsystem.Name == Constants.AdminSubsystem)
                    {
                        continue;
                    }

                    var userRole = _applicationData.Data.Roles.FindRole(_defaultRoles[1].Name).Result;
                    _accessManager.IssuePermission(userRole, subsystem.Name);
                }

                foreach (var subsystem in _subsystems)
                {
                    var adminRole = _applicationData.Data.Roles.FindRole(_defaultRoles[0].Name).Result;
                    _accessManager.IssuePermission(adminRole, subsystem.Name);
                }

                _logger.LogInformation("Default permissions added successfully");
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddRole(PostRoleViewModel model)
        {
            if (ModelState.IsValid)
            {
                var isAdded = await _userData.AddRole(model.Name);

                if (!isAdded)
                {
                    ModelState.AddModelError("", "Роль с таким именем уже существует");
                    return(View(model));
                }

                var roleEntity = await _userData.FindRole(model.Name);

                if (roleEntity == null)
                {
                    ModelState.AddModelError("", "Не удалось добавить роль");
                    return(View(model));
                }

                var subsystemAccesses = model.SubsystemAccesses
                                        .FindAll(o => o.IsOptionSelected)
                                        .ToList();

                if (subsystemAccesses.Count == 0)
                {
                    ModelState.AddModelError("", "Выберите хотя бы одну подсистему для доступа");
                    return(View(model));
                }

                foreach (var subsystem in subsystemAccesses)
                {
                    _accessManager.IssuePermission(roleEntity, subsystem.Name);
                }

                return(RedirectToAction("Index", "Admin"));
            }

            return(View(model));
        }