This example demonstrates custom Tenants filtration. Tenants filtration will render and secure content appropriately based on the logged-in user's property values.
- Go to: Console > Admin > Properties > Web Site Audience Filtration & Security to get started.
- Be sure to read the help file prior to development.
- It will be easiest to download the starting files to begin from scratch.
This completed example does the following for a website user.
- Restricts content to users based on their user record's TenantsA selections. Users will only have access to content assigned to the same TenantsA selections. If a user has no TenantsA selections they have access to all tenants.
- Restricts content to users based on the content having a relationship to the user. When content is assigned a user, only the assigned users are able to view this content.
using Centralpoint.Web;
using System;
using System.Collections.Generic;
using System.Web;
namespace Centralpoint.WebSite.Custom
{
public class Tenants : Centralpoint.WebSite.Tenants
{
public Tenants(CpCollection properties) : base(properties)
{
}
public override string TenantFilter(string tableName, User user)
{
if (user == null) user = Management.User;
//HttpContext.Current.Trace.Warn("TenantFilter: TableName", tableName);
if (!this.IsEnabled(user)) return "";
var prefix = String.IsNullOrEmpty(tableName) ? String.Empty : tableName + ".";
var sites = user.Properties.GetAndSplit("TenantsA");
var siteFilter = TenantsFilter(prefix, "TenantsA", sites);
var userRestrictionFilter = RestrictedToUsersInDataCurrentFilter(prefix, user);
// HttpContext.Current.Trace.Warn("siteFilter", $" AND {siteFilter}");
if (String.IsNullOrEmpty(siteFilter))
{
HttpContext.Current.Trace.Warn("Custom.Tenants:1", $" AND {userRestrictionFilter}");
return $" AND {userRestrictionFilter}";
}
var filter = $" AND {siteFilter} AND {userRestrictionFilter}";
HttpContext.Current.Trace.Warn("Custom.Tenants:Filter", filter);
return filter;
}
private string TenantsFilter(string prefix, string attribute, string[] selections)
{
var dataIds = new List<Guid>(selections.Length);
foreach (string selection in selections)
{
if (selection.IsGuid(out Guid dataId)) dataIds.Add(dataId);
}
if (dataIds.Count == 0) return ""; // users with no selections have access to all
var noneFilter = $"({prefix}DataId NOT IN (SELECT dcdc.DataId FROM cpsys_DataCurrentInDataCurrent AS dcdc WHERE dcdc.AttributeSystemName = '{attribute}'))";
var dataFilter = CpContent.DataIdAttributeFilter(attribute, Guid.Empty, false, prefix + "DataId", dataIds.ToArray());
return $"({noneFilter} OR {dataFilter})";
}
private string RestrictedToUsersInDataCurrentFilter(string prefix, User user)
{
return $"({prefix}DataId NOT IN (SELECT uidca.DataId FROM cpsys_UsersInDataCurrentAttributes uidca WHERE AttributeSystemName = 'RestrictedTo') OR {prefix}DataId IN (SELECT uidca.DataId FROM cpsys_UsersInDataCurrentAttributes uidca WHERE AttributeSystemName = 'RestrictedTo' AND AttributeUserId = '{user.UserId}'))";
}
public override bool IsInTenants(DataInfo data, User user)
{
if (!this.IsEnabled(user)) return true;
var userAccessibleSites = user.Properties.GetAndSplit("TenantsA");
var hasSitePermissions = GetGuids(userAccessibleSites).Count == 0 || this.IsInTenants(data, "TenantsA", userAccessibleSites);
var hasContentRestrictedToPermissions = HasUserMetRestrictedToRequirements(data, user);
HttpContext.Current.Trace.Warn("Custom.Tenants Security: User Has Site Permissions | User Has Restricted To Access", $"{hasSitePermissions} | {hasContentRestrictedToPermissions}");
return hasSitePermissions && hasContentRestrictedToPermissions;
}
private bool HasUserMetRestrictedToRequirements(DataInfo data, User user)
{
var restrictedToUserIds = GetGuids(data.Attributes.GetAndSplit("RestrictedTo"));
if (restrictedToUserIds.Count == 0) return true;
var isPermittedUser = false;
foreach (var x in restrictedToUserIds)
{
if (x == user.UserId)
{
isPermittedUser = true;
break;
}
}
return isPermittedUser;
}
private List<Guid> GetGuids(string[] dataSelections)
{
var ids = new List<Guid>();
foreach (var x in dataSelections)
{
if (x.IsGuid(out var id)) ids.Add(id);
}
return ids;
}
}
}
Keywords: Tenant Filtration