te_vw_v1_permissions_EffectivePermissions breaks existing queries

Background Our plugin checks whether a user can view its items by joining to dbo.cs_vw_Security_UserEffectivePermissions on UserId, NodeId and PermissionId, where IsAllowed = 1. For v14, we're moving to the supported Api.te_vw_v1_permissions_EffectivePermissions view. Both views have the same columns, but they return different rows:

  • The old view returns one row for each user, node and permission. IsAllowed is 1 if any of the user's roles allows it.
  • The Api view returns one row for each role that grants or denies the permission. A user with several matching roles gets several rows, so a join returns each item more than once.

Steps to reproduce

  1. Install Community 14.0.0.684 with the standard installer.
  2. Run the attached script, verint-support-repro.sql, against the application database. It only reads data.

Results on a fresh install

Check Old view Api view
Total rows 67 80
Unique user, node and permission combinations 67 67
Rows for the installer's admin user, root node, permission 9A18FEFB-DC18-4001-A773-8B0D12EBE48D 1 4
Rows returned by joining one item on that node 1 4

The admin user is in four roles: Everyone, Registered Users, Moderators and Administrators. Each role that allows the permission adds a row to the Api view. In customer data, this made the same tickets appear two or three times, and made totals and paging wrong.

Our workaround We group the Api view, as the old view did, and treat access as allowed if any row allows it:

WITH uep AS (
    SELECT UserId, NodeId, PermissionId, CAST(MAX(CAST(IsAllowed AS int)) AS bit) AS IsAllowed
    FROM [Api].[te_vw_v1_permissions_EffectivePermissions]
    GROUP BY UserId, NodeId, PermissionId
)

/*
    Verint Community 14: dbo.cs_vw_Security_UserEffectivePermissions compared with
    Api.te_vw_v1_permissions_EffectivePermissions

    Run in the Community application database. The script only reads data.

    Both views have the same columns (UserId, NodeId, PermissionId, IsAllowed), but:
      - the old view returns one row for each user, node and permission, and IsAllowed is 1 if any of the user's
        roles allows it;
      - the Api view returns one row for each of the user's roles, so the same combination can appear more than once.
*/
SET NOCOUNT ON;

-- 1. Total rows in each view
SELECT
    (SELECT COUNT(*) FROM dbo.cs_vw_Security_UserEffectivePermissions) AS OldViewRows,
    (SELECT COUNT(*) FROM Api.te_vw_v1_permissions_EffectivePermissions) AS ApiViewRows,
    (SELECT COUNT(*) FROM (SELECT DISTINCT UserId, NodeId, PermissionId
                           FROM Api.te_vw_v1_permissions_EffectivePermissions) d) AS ApiViewDistinctCombinations;

-- 2. Combinations that appear more than once in the Api view
SELECT TOP 10 UserId, NodeId, PermissionId,
       COUNT(*) AS ApiViewRows,
       SUM(CASE WHEN IsAllowed = 1 THEN 1 ELSE 0 END) AS AllowedRows
FROM Api.te_vw_v1_permissions_EffectivePermissions
GROUP BY UserId, NodeId, PermissionId
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC, UserId;

-- 3. The combination allowed by the most roles, in both views
DECLARE @UserId int, @NodeId uniqueidentifier, @PermissionId uniqueidentifier;

SELECT TOP 1 @UserId = UserId, @NodeId = NodeId, @PermissionId = PermissionId
FROM Api.te_vw_v1_permissions_EffectivePermissions
WHERE IsAllowed = 1
GROUP BY UserId, NodeId, PermissionId
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC, UserId;

SELECT 'dbo.cs_vw_Security_UserEffectivePermissions' AS SourceView, UserId, NodeId, PermissionId, IsAllowed
FROM dbo.cs_vw_Security_UserEffectivePermissions
WHERE UserId = @UserId AND NodeId = @NodeId AND PermissionId = @PermissionId
UNION ALL
SELECT 'Api.te_vw_v1_permissions_EffectivePermissions', UserId, NodeId, PermissionId, IsAllowed
FROM Api.te_vw_v1_permissions_EffectivePermissions
WHERE UserId = @UserId AND NodeId = @NodeId AND PermissionId = @PermissionId;

-- 4. Effect on a plugin query: join one item stored against that node to each view, as a permission check would
SELECT
    (SELECT COUNT(*)
     FROM (VALUES (@NodeId)) AS PluginItem(NodeId)
     JOIN dbo.cs_vw_Security_UserEffectivePermissions p
       ON p.NodeId = PluginItem.NodeId AND p.UserId = @UserId AND p.PermissionId = @PermissionId AND p.IsAllowed = 1)
        AS RowsReturnedUsingOldView,
    (SELECT COUNT(*)
     FROM (VALUES (@NodeId)) AS PluginItem(NodeId)
     JOIN Api.te_vw_v1_permissions_EffectivePermissions p
       ON p.NodeId = PluginItem.NodeId AND p.UserId = @UserId AND p.PermissionId = @PermissionId AND p.IsAllowed = 1)
        AS RowsReturnedUsingApiView;