Accuraty / AccuTheme-Bs4

Dnn Theme Template Project; boilerplate using Bootstrap 4.6.2
0 stars 0 forks source link

Need a Simple Way to Show if the current Page is Public or Private - for Authenticated Users #19

Open jeremy-farrance opened 1 month ago

jeremy-farrance commented 1 month ago

This appears to be the winner on simplicity, readability, performance, and accuracy.

<% 
    // is the current page public?
    // retry using PortalSettings.ActiveTab.TabPermissions
    int allUsersRoleId = int.Parse(DotNetNuke.Common.Globals.glbRoleAllUsers); 
    bool IsPublic = PortalSettings.ActiveTab
      .TabPermissions.Cast<DotNetNuke.Security.Permissions.TabPermissionInfo>()
        .Any(pi => pi.RoleID == allUsersRoleId 
          && pi.AllowAccess 
          && pi.PermissionID == 3 // 3 is SYSTEM_TAB, VIEW
        )
    ; 
    debugOutput.AppendLine("--------------------");
    debugOutput.AppendLine($"IsPublic: {IsPublic}");
    debugOutput.AppendLine($"allUsersRoleId: {allUsersRoleId}");

%>
jeremy-farrance commented 1 month ago

Worked through to this solution on Accu4.com in __debug.ascx

TODO - will this error on a Host level page? I think we bypassed that by using ActiveTab.

Reference materials: DotNetNuke.Entities.Tabs.TabInfo DotNetNuke.Security.Permissions.TabPermissionCollection using DotNetNuke.Security.Permissions;

<% 
  bool showDetails = isIpSpecial() 
    || isUrlSpecial(DebugSettings.SpecialUrlOutputName, DebugSettings.SpecialUrlOutputValue)
    || isUrlSpecial(DebugSettings.SpecialUrlOutputName, DebugSettings.SpecialUrlAllValue)
    || currUserInfo().IsSuperUser && showAllForSuper
  ;
  bool showDebug = isDebug
    || isUrlSpecial(DebugSettings.SpecialUrlOutputName, DebugSettings.SpecialUrlDebugValue)
    || isUrlSpecial(DebugSettings.SpecialUrlOutputName, DebugSettings.SpecialUrlAllValue)
    || currUserInfo().IsSuperUser && showAllForSuper
  ;

  if ( showDetails ) { 

    //get the TabPermission for the current tab and cast from Collection to List<TabPermissionInfo>
    List<DotNetNuke.Security.Permissions.TabPermissionInfo> tabPermissionInfo = 
      DotNetNuke.Security.Permissions.TabPermissionController.GetTabPermissions(PortalSettings.ActiveTab.TabID , PortalSettings.PortalId)
        .Cast<DotNetNuke.Security.Permissions.TabPermissionInfo>()
          .ToList()
    ;

    //loop all the permissionInfo objects and check for RoleId -1 (= all users)
    bool allUsers = false;
    foreach (DotNetNuke.Security.Permissions.TabPermissionInfo pi in tabPermissionInfo)
    {
        if (pi.RoleID == -1)
        {
            allUsers = true;
        }

        //for visualization of all roles and id's for current tab
        debugOutput.AppendLine($"Role ID/Name: {pi.RoleID}/{pi.RoleName}, KeyID: {pi.KeyID}, TabID: {pi.TabID}, PermissionID: {pi.PermissionID}, AllowAccess: {pi.AllowAccess}");
    }

    // is the current page public?
    int allUsersRoleId2 = int.Parse(DotNetNuke.Common.Globals.glbRoleAllUsers); 
    int portalId2 = PortalSettings.PortalId;
    bool IsPublic2 = DotNetNuke.Security.Permissions.TabPermissionController
      .GetTabPermissions(PortalSettings.ActiveTab.TabID, portalId2)
        .ToList()
        .Any(pi => pi.RoleID == allUsersRoleId2 && pi.AllowAccess && pi.PermissionID == 3); // SYSTEM_TAB, VIEW

    debugOutput.AppendLine("--------------------");
    debugOutput.AppendLine($"IsPublic2: {IsPublic2}, AllUsers: {allUsers}");
    debugOutput.AppendLine($"allUsersRoleId2: {allUsersRoleId2}");
    debugOutput.AppendLine($"portalId2: {portalId2}");

    // is the current page public?
    // retry using PortalSettings.ActiveTab.TabPermissions
    int allUsersRoleId = int.Parse(DotNetNuke.Common.Globals.glbRoleAllUsers); 
    bool IsPublic = PortalSettings.ActiveTab
      .TabPermissions.Cast<DotNetNuke.Security.Permissions.TabPermissionInfo>()
        .Any(pi => pi.RoleID == allUsersRoleId 
          && pi.AllowAccess 
          && pi.PermissionID == 3 // 3 is SYSTEM_TAB, VIEW
        )
    ; 
    debugOutput.AppendLine("--------------------");
    debugOutput.AppendLine($"IsPublic: {IsPublic}");
    debugOutput.AppendLine($"allUsersRoleId: {allUsersRoleId}");

%>
jeremy-farrance commented 1 month ago

image

image

jeremy-farrance commented 1 month ago

Also, in AccuTheme-Bs4 this is now implemented in the Footer/Debug output. When you are logged in (authenticated), to the left of the "Page:" output you will see a lock or lock-open icon indicating whether the page is visible to the public or not.

The code is called here (example from FCTRL2024).