Skip to content

Commit

Permalink
Merge pull request #18 from umbraco-community/feature/our-member
Browse files Browse the repository at this point in the history
Feature/our member
  • Loading branch information
Warren Buckley authored Oct 28, 2021
2 parents 4f6c60c + f184b8d commit 8e540c7
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Our.Umbraco.TagHelpers/MemberTagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Umbraco.Cms.Core.Security;

namespace Our.Umbraco.TagHelpers
{
/// <summary>
/// An attribute TagHelper to help show or hide DOM elements for members
///
/// </summary>
[HtmlTargetElement("*", Attributes = "our-member-include")]
[HtmlTargetElement("*", Attributes ="our-member-exclude")]
public class MemberTagHelper : TagHelper
{
private IMemberManager _memberManager;

/// <summary>
/// A comma seperated list of Member Groups to exclude
/// ? = All anonymous users
/// * = All authenticated users
/// </summary>
[HtmlAttributeName("our-member-exclude")]
public string ExcludeRoles { get; set; }

/// <summary>
/// A comma seperated list of Member Groups to include
/// ? = All anonymous users
/// * = All authenticated users
/// </summary>
[HtmlAttributeName("our-member-include")]
public string IncludeRoles { get; set; }

public MemberTagHelper(IMemberManager memberManager)
{
_memberManager = memberManager;
}

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var currentMember = await _memberManager.GetCurrentMemberAsync();
var currentMemberRoles = new List<string>();
if(currentMember != null)
{
currentMemberRoles.AddRange(await _memberManager.GetRolesAsync(currentMember));
}

// Process excluded roles
if (!string.IsNullOrWhiteSpace(this.ExcludeRoles) && IsUserInRole(ExcludeRoles, currentMemberRoles) == true)
{
output.SuppressOutput();
return;
}

// Process included roles
else if (!string.IsNullOrWhiteSpace(this.IncludeRoles) && IsUserInRole(IncludeRoles, currentMemberRoles) == false)
{
output.SuppressOutput();
return;
}
}

private bool IsUserInRole(string roleString, List<string> currentMemberRoles)
{
// roles is a CSV of member groups they need to have access to
var roles = roleString.Split(',').Select(x => x.Trim());
foreach (var role in roles)
{
// Role ? == all anonymous users (User not logged in)
if (role == "?" && _memberManager.IsLoggedIn() == false)
{
return true;
}

// Role * == all authenticated users
if (role == "*" && _memberManager.IsLoggedIn())
{
return true;
}

if (currentMemberRoles.Contains(role))
{
return true;
}
}

return false;
}
}
}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@ This tag helper element `<our-version>` prints out version number for a given As
<our-version="Our.Umbraco.TagHelpers" />
```

## `our-member-include` and `our-member-exclude`
This is a tag helper attribute that can be applied to any DOM element in the razor template or partial. It will show or hide its element and children on the page when passing a comma seperated string of member groups that the current logged in member for the exclude or include variants.

There are two special Member Groups you can use:
* `*` - All anonymous users
* `?` - All authenticated users

```cshtml
<div our-member-include="Staff">Only members of Staff Member Group will see this.</div>
<div our-member-include="Staff,Admins">Only members of Staff OR Admins member group will see this.</div>
<div our-member-include="*">Only logged in members will see this.</div>
<div our-member-include="?">Only anonymous members will see this.</div>
<div our-member-exclude="Staff">Only Staff members can't see this (Including anonymous).</div>
<div our-member-exclude="?">Everyone except Anonymous members will see this.</div>
<div our-member-exclude="*">Everyone except who is authenticated will see this.</div>
```

## Video 📺
[![How to create ASP.NET TagHelpers for Umbraco](https://user-images.githubusercontent.com/1389894/138666925-15475216-239f-439d-b989-c67995e5df71.png)](https://www.youtube.com/watch?v=3fkDs0NwIE8)

Expand Down

0 comments on commit 8e540c7

Please sign in to comment.