Beispiel #1
0
        public void Role_capability_tests_Succeeds()
        {
            var role1 = new Role()
            {
                Name         = "Test Role",
                SystemName   = "TestRole",
                IsActive     = true,
                IsSystemRole = false
            };

            _roleService.Insert(role1);

            var role2 = new Role()
            {
                Name         = "Test Role 2",
                SystemName   = "TestRole",
                IsActive     = true,
                IsSystemRole = false
            };

            _roleService.Insert(role2);

            var accessAdminCap =
                _capabilityService.FirstOrDefault(x => x.Name == CapabilitySystemNames.AccessAdministration);
            var createOrderCap =
                _capabilityService.FirstOrDefault(x => x.Name == CapabilitySystemNames.CreateOrder);
            var editProductCap =
                _capabilityService.FirstOrDefault(x => x.Name == CapabilitySystemNames.EditProduct);

            var user = new User()
            {
                Name          = "Test User",
                CreatedOn     = DateTime.UtcNow,
                UpdatedOn     = DateTime.UtcNow,
                LastLoginDate = null,
                Active        = true
            };

            _userService.Insert(user);


            _capabilityService.SetRoleCapabilities(role1.Id, new [] { accessAdminCap.Id });
            _capabilityService.SetRoleCapabilities(role2.Id, new [] { editProductCap.Id });
            _capabilityService.SetUserCapabilities(user.Id, new[] { createOrderCap.Id });

            _roleService.SetUserRoles(user.Id, new [] { role1.Id, role2.Id });
            //refetch to load all data
            user = _userService.Get(user.Id);

            var consoliatedCapabilities = _capabilityService.GetByRolesConsolidated(new[] { role1.Id, role2.Id });

            Assert.AreEqual(2, consoliatedCapabilities.Count());

            Assert.IsTrue(user.Can(CapabilitySystemNames.AccessAdministration));
            Assert.IsTrue(user.Can(CapabilitySystemNames.EditProduct));
            Assert.IsTrue(user.Can(CapabilitySystemNames.CreateOrder));
            Assert.IsFalse(user.Can(CapabilitySystemNames.DeleteUser));
        }
Beispiel #2
0
        public IActionResult SaveRole(RoleModel roleModel)
        {
            var role = roleModel.Id > 0 ? _roleService.Get(roleModel.Id) : new Role();

            if (role == null)
            {
                return(NotFound());
            }
            if (role.Id == 0)
            {
                //check if this system name is available?
                if (_roleService.Count(x => x.SystemName == roleModel.SystemName) > 0)
                {
                    return(R.Fail.With("error", T("A role with this system name already exists")).Result);
                }
            }
            role.Name     = roleModel.Name;
            role.IsActive = roleModel.IsActive;
            if (!role.IsSystemRole)
            {
                role.SystemName = roleModel.SystemName;
            }

            _roleService.InsertOrUpdate(role);

            //get valid capabilities
            var capabilityIds = roleModel.Capabilities?.Where(x => !x.IsNullEmptyOrWhiteSpace() && x.IsInteger())
                                .Select(int.Parse)
                                .ToArray() ?? new int[] { };

            //set the values
            _capabilityService.SetRoleCapabilities(role.Id, capabilityIds);
            return(R.Success.With("id", role.Id).Result);
        }