Ejemplo 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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a query that gets all permissions (granted, revoked, and inherited) for the user with the given principal id.
        /// </summary>
        /// <param name="principalId">The principal id.</param>
        /// <returns>The granted, revoked, and inherited permissions of the principal with the given id.</returns>
        public IQueryable <IPermission> CreateGetAllowedPermissionsByPrincipalIdQuery(int principalId)
        {
            var query = UserQueries.CreateGetSimpleResourceAuthorizationsByPrincipalId(this.Context, principalId)
                        .OrderBy(x => x.PrincipalId)
                        .ThenBy(x => x.ResourceId)
                        .ThenBy(x => x.PermissionId);

            return(query);
        }
Ejemplo n.º 3
0
        public void TestCreateGetSimpleResourceAuthorizationsByPrincipalId_HasPermissionAssignment_CheckProperties()
        {
            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 permissionAssignment = new PermissionAssignment
            {
                IsAllowed    = true,
                Permission   = permission,
                PermissionId = permission.PermissionId,
                Principal    = principal,
                PrincipalId  = principal.PrincipalId,
                Resource     = resource,
                ResourceId   = resource.ResourceId
            };

            context.Principals.Add(principal);
            context.ResourceTypes.Add(resourceType);
            context.Resources.Add(resource);
            context.Permissions.Add(permission);
            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.IsTrue(firstResult.IsAllowed);
        }
Ejemplo n.º 4
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);
        }