Example #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));
        }
Example #2
0
 public IActionResult SaveCapabilities(int userId, IList <int> capabilityIds)
 {
     if (userId <= 0 || _userService.Count(x => x.Id == userId) == 0)
     {
         return(NotFound());
     }
     _capabilityService.SetUserCapabilities(userId, capabilityIds.ToArray());
     return(R.Success.Result);
 }