Esempio n. 1
0
        public void TestCreateGetSimpleResourceAuthorizationsByPrincipalId_PrincipalDoesHaveNotRoles()
        {
            var principalId = 1;
            var principal   = new Principal
            {
                PrincipalId = principalId,
            };
            var resourceType = new ResourceType
            {
                ResourceTypeId   = ResourceType.Project.Id,
                ResourceTypeName = ResourceType.Project.Value
            };
            var resource = new Resource
            {
                ResourceId        = 1,
                ForeignResourceId = 2,
                ResourceTypeId    = resourceType.ResourceTypeId,
                ResourceType      = resourceType,
            };
            var permission = new Permission
            {
                PermissionId   = 1,
                PermissionName = "Permission"
            };
            var role = new Role
            {
                IsActive = true,
                RoleId   = 1,
                RoleName = "role name"
            };
            var principalRole = new PrincipalRole
            {
                RoleId      = role.RoleId,
                Role        = role,
                Principal   = principal,
                PrincipalId = principal.PrincipalId
            };
            var roleResourcePermission = new RoleResourcePermission
            {
                Permission   = permission,
                PermissionId = permission.PermissionId,
                Resource     = resource,
                ResourceId   = resource.ResourceId,
                Role         = role,
                RoleId       = role.RoleId
            };

            context.Principals.Add(principal);
            context.ResourceTypes.Add(resourceType);
            context.Resources.Add(resource);
            context.Permissions.Add(permission);
            context.Roles.Add(role);
            context.PrincipalRoles.Add(principalRole);
            context.RoleResourcePermissions.Add(roleResourcePermission);

            var results = UserQueries.CreateGetSimpleResourceAuthorizationsByPrincipalId(context, principalId + 1).ToList();

            Assert.AreEqual(0, results.Count);
        }
Esempio n. 2
0
        private AddedPermissableEntityResult DoOnAdded(IPermissable addedEntity,
                                                       Resource existingResource,
                                                       Resource parentResource,
                                                       List <ResourcePermissionDTO> resourceTypePermissions,
                                                       List <Role> roles)
        {
            var modifiedRoles = new List <Role>();

            if (existingResource == null)
            {
                var newResource = AddResourceToCAM(addedEntity, parentResource);
                var now         = DateTimeOffset.UtcNow;

                foreach (var role in roles)
                {
                    foreach (var resourceTypePermission in resourceTypePermissions)
                    {
                        var addPermission = addedEntity.AssignPermissionToRoleOnCreate(role.RoleName, resourceTypePermission.PermissionName);
                        if (addPermission)
                        {
                            var rolePermission = new RoleResourcePermission
                            {
                                AssignedBy   = 1,
                                AssignedOn   = now,
                                RoleId       = role.RoleId,
                                Resource     = newResource,
                                PermissionId = resourceTypePermission.PermissionId
                            };
                            Context.RoleResourcePermissions.Add(rolePermission);
                            modifiedRoles.Add(role);
                        }
                    }
                }
            }
            RemoveFromCache(addedEntity);
            return(new AddedPermissableEntityResult(addedEntity, modifiedRoles.Select(x => x.RoleId).Distinct().ToList()));
        }
Esempio n. 3
0
        public void TestCreateGetSimpleResourceAuthorizationsByPrincipalId_PrincipalHasRoleAndPermissionAssignment_RoleIsInactive_PermissionIsNotAllowed()
        {
            var principalId = 1;
            var principal   = new Principal
            {
                PrincipalId = principalId,
            };
            var resourceType = new ResourceType
            {
                ResourceTypeId   = ResourceType.Project.Id,
                ResourceTypeName = ResourceType.Project.Value
            };
            var resource = new Resource
            {
                ResourceId        = 1,
                ForeignResourceId = 2,
                ResourceTypeId    = resourceType.ResourceTypeId,
                ResourceType      = resourceType,
            };
            var permission = new Permission
            {
                PermissionId   = 1,
                PermissionName = "Permission"
            };
            var role = new Role
            {
                IsActive = false,
                RoleId   = 1,
                RoleName = "role name"
            };
            var principalRole = new PrincipalRole
            {
                RoleId      = role.RoleId,
                Role        = role,
                Principal   = principal,
                PrincipalId = principal.PrincipalId
            };
            var roleResourcePermission = new RoleResourcePermission
            {
                Permission   = permission,
                PermissionId = permission.PermissionId,
                Resource     = resource,
                ResourceId   = resource.ResourceId,
                Role         = role,
                RoleId       = role.RoleId
            };
            var permissionAssignment = new PermissionAssignment
            {
                IsAllowed    = false,
                Permission   = permission,
                PermissionId = permission.PermissionId,
                Principal    = principal,
                Resource     = resource,
                ResourceId   = resource.ResourceId,
                PrincipalId  = principal.PrincipalId,
            };

            context.Principals.Add(principal);
            context.ResourceTypes.Add(resourceType);
            context.Resources.Add(resource);
            context.Permissions.Add(permission);
            context.Roles.Add(role);
            context.PrincipalRoles.Add(principalRole);
            context.RoleResourcePermissions.Add(roleResourcePermission);
            context.PermissionAssignments.Add(permissionAssignment);

            var results = UserQueries.CreateGetSimpleResourceAuthorizationsByPrincipalId(context, principalId).ToList();

            Assert.AreEqual(1, results.Count);
            var firstResult = results.First();

            Assert.AreEqual(principal.PrincipalId, firstResult.PrincipalId);
            Assert.AreEqual(permission.PermissionId, firstResult.PermissionId);
            Assert.AreEqual(resource.ResourceId, firstResult.ResourceId);
            Assert.AreEqual(resource.ForeignResourceId, firstResult.ForeignResourceId);
            Assert.IsFalse(firstResult.IsAllowed);
        }