/// <summary>
        /// 根据id删除角色权限信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public static bool DeleteRolePermissionInfo(Guid id)
        {
            bool           ret   = false;
            IDbConnection  conn  = null;
            IDbCommand     cmd   = null;
            IDbTransaction trans = null;

            try
            {
                IRolePermission dp = DataProvider.DbRolePermissionDP;
                conn = DbConnOperation.CreateMySqlConnection();
                cmd  = conn.CreateCommand();
                conn.Open();
                trans           = conn.BeginTransaction();
                cmd.Transaction = trans;
                ret             = dp.DeleteRolePermissionInfo(cmd, id);
                trans.Commit();
            }
            catch (Exception ex)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                log.Error(string.Format("DeleteRolePermissionInfo()出错,错误信息如下:{0}", ex.Message));
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return(ret);
        }
        /// <summary>
        /// 根据rid获取角色权限列表信息
        /// </summary>
        /// <param name="rid">角色编号</param>
        /// <returns></returns>
        public static List <RolePermission> GetRolePermissionByRid(Guid rid)
        {
            List <RolePermission> list  = new List <RolePermission>();
            IDbConnection         conn  = null;
            IDbCommand            cmd   = null;
            IDbTransaction        trans = null;

            try
            {
                IRolePermission dp = DataProvider.DbRolePermissionDP;
                conn = DbConnOperation.CreateMySqlConnection();
                cmd  = conn.CreateCommand();
                conn.Open();
                trans           = conn.BeginTransaction();
                cmd.Transaction = trans;
                list            = dp.GetRolePermissionByRid(cmd, rid);
                trans.Commit();
            }
            catch (Exception ex)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                log.Error(string.Format("GetRolePermissionByRid()出错,错误信息如下:{0}", ex.Message));
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return(list);
        }
        public List <string> ClearCachePrincipalsRoles_GetAuthorization(bool editSystemRole = false)
        {
            var principals = new IPrincipal[] {
                new MockPrincipal {
                    ID = new Guid("11195E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = "pr0"
                },
                new MockPrincipal {
                    ID = new Guid("22295E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = "pr1"
                }
            };

            var roles = new IRole[] {
                new MockRole {
                    ID = new Guid("33395E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = "r0"
                },
                new MockRole {
                    ID = new Guid("44495E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = "r1"
                },
                new MockRole {
                    ID = new Guid("55595E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = SystemRole.AllPrincipals.ToString()
                }
            };

            var principalRoles = new IPrincipalHasRole[] {
                new MockPrincipalHasRole(principals[0], roles[0]),
                new MockPrincipalHasRole(principals[1], roles[1])
            };

            var roleRoles = new IRoleInheritsRole[] {
                new MockRoleInheritsRole(usersFrom: roles[0], permissionsFrom: roles[1])
            };

            var claims = new Claim[]
            {
                new Claim("c0", "c0ri"),
                new Claim("c1", "c1ri"),
                new Claim("c2", "c2ri"),
                new Claim("c3", "c3ri"),
                new Claim("c4", "c4ri"),
                new Claim("c5", "c5ri"),
                new Claim("c6", "c6ri"),
                new Claim("c7", "c7ri"),
            };

            var commonClaims = claims
                               .Where(c => c.Resource != "c7")
                               .Select(c => new MockClaim(c.Resource, c.Right, true))
                               .ToArray <ICommonClaim>();

            commonClaims[3].Active = false;
            commonClaims[4].Active = null;

            var rolePermissions = new IRolePermission[]
            {
                new MockRolePermission(roles[0], commonClaims[0], true),
                new MockRolePermission(roles[1], commonClaims[1], true),  // Also inherited to role0.
                new MockRolePermission(roles[0], commonClaims[2], true),
                new MockRolePermission(roles[1], commonClaims[2], false), // Also denied to role0.
                new MockRolePermission(roles[0], commonClaims[3], true),  // Inactive claim (active = null)
                new MockRolePermission(roles[0], commonClaims[4], true),  // Inactive claim.
                new MockRolePermission(roles[1], commonClaims[6], true),
            };

            var principalPermissions = new IPrincipalPermission[]
            {
                new MockPrincipalPermission(principals[0], commonClaims[5], true),
                new MockPrincipalPermission(principals[0], commonClaims[6], false),
            };

            var authorizationContext = NewAuthorizationContext(principals, roles, principalRoles, roleRoles, commonClaims, rolePermissions, principalPermissions,
                                                               useCache: true);

            var cache = authorizationContext.AuthorizationDataCache;

            cache.ClearCachePrincipals(new[] { principals[0] });
            cache.ClearCacheRoles(new[] { roles[0].ID });
            if (editSystemRole)
            {
                cache.ClearCacheRoles(new[] { roles[2].ID });
            }

            var provider = authorizationContext.CommonAuthorizationProvider;

            Assert.AreEqual("True, True, False, False, False, True, False, False", TestUtility.Dump(provider.GetAuthorizations(new TestUserInfo("pr0"), claims)));
            Assert.AreEqual("False, True, False, False, False, False, True, False", TestUtility.Dump(provider.GetAuthorizations(new TestUserInfo("pr1"), claims)));

            return(authorizationContext.Log);
        }
        public List <string> SimilarClaimsTest(bool useCache)
        {
            var principals = new IPrincipal[] {
                new MockPrincipal {
                    ID = new Guid("11195E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = "pr0"
                },
                new MockPrincipal {
                    ID = new Guid("22295E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = "pr1"
                }
            };

            var roles = new IRole[] {
                new MockRole {
                    ID = new Guid("33395E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = "r0"
                },
                new MockRole {
                    ID = new Guid("44495E07-8D14-4DB9-BD79-C0C3E8407FEB"), Name = "r1"
                }
            };

            var principalRoles = new IPrincipalHasRole[] {
                new MockPrincipalHasRole(principals[0], roles[0]),
                new MockPrincipalHasRole(principals[1], roles[1])
            };

            var roleRoles = new IRoleInheritsRole[] {
                new MockRoleInheritsRole(usersFrom: roles[0], permissionsFrom: roles[1])
            };

            var claims = new Claim[] {
                new Claim("a.b", "c"),
                new Claim("a", "b.c"),
                new Claim("a2.b", "c"),
                new Claim("a2", "b.c"),
                new Claim("a4.b", "c"),
                new Claim("a4", "b.c"),
                new Claim("a6", "b"), // index 6, commonClaims[6]
                new Claim("A6", "B"), // index 7
                new Claim("A8", "B"), // commonClaims[7]
                new Claim("a8", "b"), // index 9
            };

            var commonClaims = claims
                               .Where((claim, index) => index != 7 && index != 9)
                               .Select(c => new MockClaim(c.Resource, c.Right, true))
                               .ToArray <ICommonClaim>();

            var rolePermissions = new IRolePermission[]
            {
                new MockRolePermission(roles[0], commonClaims[0], true),
                new MockRolePermission(roles[1], commonClaims[1], true),
                new MockRolePermission(roles[0], commonClaims[2], true),
                new MockRolePermission(roles[1], commonClaims[3], false),
                new MockRolePermission(roles[0], commonClaims[4], true),
                new MockRolePermission(roles[0], commonClaims[6], true),
                new MockRolePermission(roles[1], commonClaims[7], true),
            };

            var principalPermissions = new IPrincipalPermission[] { };

            var authorizationContext = NewAuthorizationContext(principals, roles, principalRoles, roleRoles, commonClaims, rolePermissions, principalPermissions,
                                                               useCache);
            var provider = authorizationContext.CommonAuthorizationProvider;

            Assert.AreEqual("True, True, True, False, True, False, True, True, True, True", TestUtility.Dump(provider.GetAuthorizations(new TestUserInfo("pr0"), claims)));
            Assert.AreEqual("False, True, False, False, False, False, False, False, True, True", TestUtility.Dump(provider.GetAuthorizations(new TestUserInfo("pr1"), claims)));

            return(authorizationContext.Log);
        }
        public List <string> SimpleTest(bool useCache)
        {
            var principals = new IPrincipal[] {
                new MockPrincipal {
                    ID = new Guid("11195e07-8d14-4db9-bd79-c0c3e8407feb"), Name = "pr0"
                },
                new MockPrincipal {
                    ID = new Guid("22295e07-8d14-4db9-bd79-c0c3e8407feb"), Name = "pr1"
                }
            };

            var roles = new IRole[] {
                new MockRole {
                    ID = new Guid("33395e07-8d14-4db9-bd79-c0c3e8407feb"), Name = "r0"
                },
                new MockRole {
                    ID = new Guid("44495e07-8d14-4db9-bd79-c0c3e8407feb"), Name = "r1"
                },
                new MockRole {
                    ID = new Guid("55595e07-8d14-4db9-bd79-c0c3e8407feb"), Name = "r2"
                }
            };

            var principalRoles = new IPrincipalHasRole[] {
                new MockPrincipalHasRole(principals[0], roles[0]),
                new MockPrincipalHasRole(principals[1], roles[1]),
                new MockPrincipalHasRole(principals[1], roles[2])
            };

            var roleRoles = new IRoleInheritsRole[] {
                new MockRoleInheritsRole(usersFrom: roles[0], permissionsFrom: roles[1])
            };

            var claims = new Claim[]
            {
                new Claim("c0", "c0ri"),
                new Claim("c1", "c1ri"),
                new Claim("c2", "c2ri"),
                new Claim("c3", "c3ri"),
                new Claim("c4", "c4ri"),
                new Claim("c5", "c5ri"),
                new Claim("c6", "c6ri"),
                new Claim("c7", "c7ri"),
            };

            var commonClaims = claims
                               .Where(c => c.Resource != "c7")
                               .Select(c => new MockClaim(c.Resource, c.Right, true))
                               .ToArray <ICommonClaim>();

            commonClaims[3].Active = false;
            commonClaims[4].Active = null;

            var rolePermissions = new IRolePermission[]
            {
                new MockRolePermission(roles[0], commonClaims[0], true),
                new MockRolePermission(roles[1], commonClaims[1], true),  // Also inherited to role0.
                new MockRolePermission(roles[0], commonClaims[2], true),
                new MockRolePermission(roles[1], commonClaims[2], false), // Also denied to role0.
                new MockRolePermission(roles[0], commonClaims[3], true),  // Inactive claim (active = null)
                new MockRolePermission(roles[0], commonClaims[4], true),  // Inactive claim.
                new MockRolePermission(roles[1], commonClaims[6], true),
            };

            var principalPermissions = new IPrincipalPermission[]
            {
                new MockPrincipalPermission(principals[0], commonClaims[5], true),
                new MockPrincipalPermission(principals[0], commonClaims[6], false),
            };

            var authorizationContext = NewAuthorizationContext(principals, roles, principalRoles, roleRoles, commonClaims, rolePermissions, principalPermissions,
                                                               useCache);
            var provider = authorizationContext.CommonAuthorizationProvider;

            Assert.AreEqual("True, True, False, False, False, True, False, False", TestUtility.Dump(provider.GetAuthorizations(new TestUserInfo("pr0"), claims)));
            Assert.AreEqual("False, True, False, False, False, False, True, False", TestUtility.Dump(provider.GetAuthorizations(new TestUserInfo("pr1"), claims)));

            return(authorizationContext.Log);
        }