public static void Revoke(HeContext heContext, ObjectKey key, bool isPersistent, int userId, int eSpaceId)
        {
            int tenantId = heContext.Session.TenantId;

            if (userId == 0)
            {
                userId = heContext.Session.UserId;
            }

            if (userId == heContext.Session.UserId)
            {
                heContext.Session.RemoveRole(heContext.GetRoleIdFromKey(key.ToString(ObjectKey.DatabaseFormat), eSpaceId));
            }

            if (isPersistent && userId != 0)
            {
                using (Transaction tran = DatabaseAccess.ForSystemDatabase.GetRequestTransaction()) {
                    // Check userID exists in DB
                    if (Convert.ToInt32(DBRuntimePlatform.Instance.GetNumUsersWithId(tran, userId)) != 1)
                    {
                        throw new InvalidOperationException("Invalid User Id");
                    }

                    DBRuntimePlatform.Instance.DeleteUserRole(tran, userId, eSpaceId, key, tenantId);
                }
            }
        }
        public static bool Check(HeContext heContext, ObjectKey key, int userId, int eSpaceId)
        {
            bool hasRole;

            int tenantId = heContext.Session.TenantId;

            if (userId == 0)
            {
                userId = heContext.Session.UserId;
            }

            if (userId == heContext.Session.UserId)
            {
                return(heContext.Session.CheckRole(heContext.GetRoleIdFromKey(key.ToString(ObjectKey.DatabaseFormat), eSpaceId)));
            }
            else
            {
                // Check userID exists in DB
                using (Transaction tran = DatabaseAccess.ForSystemDatabase.GetReadOnlyTransaction()) {
                    if (Convert.ToInt32(DBRuntimePlatform.Instance.GetNumUsersWithId(tran, userId)) != 1)
                    {
                        throw new InvalidOperationException("Invalid User Id");
                    }

                    // see if user has permission
                    hasRole = DBRuntimePlatform.Instance.CheckRole(tran, userId, eSpaceId, key, tenantId);
                }
            }
            return(hasRole);
        }
        public static void Grant(HeContext heContext, ObjectKey key, bool isPersistent, int userId, int eSpaceId)
        {
            int tenantId = heContext.Session.TenantId;

            if (userId == 0)
            {
                userId = heContext.Session.UserId;
            }

            if (userId == heContext.Session.UserId)
            {
                // update session state
                heContext.Session.AddRole(heContext.GetRoleIdFromKey(key.ToString(ObjectKey.DatabaseFormat), eSpaceId), null);
            }

            if (isPersistent)
            {
                int roleId;

                if (userId != 0)
                {
                    // Check userID exists in DB
                    using (Transaction tran = DatabaseAccess.ForSystemDatabase.GetRequestTransaction()) {
                        if (Convert.ToInt32(DBRuntimePlatform.Instance.GetNumUsersWithId(tran, userId)) != 1)
                        {
                            throw new InvalidOperationException("Invalid User Id");
                        }

                        roleId = Convert.ToInt32(DBRuntimePlatform.Instance.GetRoleId(tran, eSpaceId, key));

                        // Check for a previous grant
                        int grantCount = Convert.ToInt32(DBRuntimePlatform.Instance.CheckRole(tran, userId, roleId, tenantId));

                        if (grantCount == 0)
                        {
                            DBRuntimePlatform.Instance.CreateUserRole(tran, userId, tenantId, roleId);
                        }
                    }
                }
            }
        }