Skip to content

Commit 8e540c7

Browse files
author
Warren Buckley
authored
Merge pull request #18 from umbraco-community/feature/our-member
Feature/our member
2 parents 4f6c60c + f184b8d commit 8e540c7

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Umbraco.Cms.Core.Security;
6+
7+
namespace Our.Umbraco.TagHelpers
8+
{
9+
/// <summary>
10+
/// An attribute TagHelper to help show or hide DOM elements for members
11+
///
12+
/// </summary>
13+
[HtmlTargetElement("*", Attributes = "our-member-include")]
14+
[HtmlTargetElement("*", Attributes ="our-member-exclude")]
15+
public class MemberTagHelper : TagHelper
16+
{
17+
private IMemberManager _memberManager;
18+
19+
/// <summary>
20+
/// A comma seperated list of Member Groups to exclude
21+
/// ? = All anonymous users
22+
/// * = All authenticated users
23+
/// </summary>
24+
[HtmlAttributeName("our-member-exclude")]
25+
public string ExcludeRoles { get; set; }
26+
27+
/// <summary>
28+
/// A comma seperated list of Member Groups to include
29+
/// ? = All anonymous users
30+
/// * = All authenticated users
31+
/// </summary>
32+
[HtmlAttributeName("our-member-include")]
33+
public string IncludeRoles { get; set; }
34+
35+
public MemberTagHelper(IMemberManager memberManager)
36+
{
37+
_memberManager = memberManager;
38+
}
39+
40+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
41+
{
42+
var currentMember = await _memberManager.GetCurrentMemberAsync();
43+
var currentMemberRoles = new List<string>();
44+
if(currentMember != null)
45+
{
46+
currentMemberRoles.AddRange(await _memberManager.GetRolesAsync(currentMember));
47+
}
48+
49+
// Process excluded roles
50+
if (!string.IsNullOrWhiteSpace(this.ExcludeRoles) && IsUserInRole(ExcludeRoles, currentMemberRoles) == true)
51+
{
52+
output.SuppressOutput();
53+
return;
54+
}
55+
56+
// Process included roles
57+
else if (!string.IsNullOrWhiteSpace(this.IncludeRoles) && IsUserInRole(IncludeRoles, currentMemberRoles) == false)
58+
{
59+
output.SuppressOutput();
60+
return;
61+
}
62+
}
63+
64+
private bool IsUserInRole(string roleString, List<string> currentMemberRoles)
65+
{
66+
// roles is a CSV of member groups they need to have access to
67+
var roles = roleString.Split(',').Select(x => x.Trim());
68+
foreach (var role in roles)
69+
{
70+
// Role ? == all anonymous users (User not logged in)
71+
if (role == "?" && _memberManager.IsLoggedIn() == false)
72+
{
73+
return true;
74+
}
75+
76+
// Role * == all authenticated users
77+
if (role == "*" && _memberManager.IsLoggedIn())
78+
{
79+
return true;
80+
}
81+
82+
if (currentMemberRoles.Contains(role))
83+
{
84+
return true;
85+
}
86+
}
87+
88+
return false;
89+
}
90+
}
91+
}

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ This tag helper element `<our-version>` prints out version number for a given As
209209
<our-version="Our.Umbraco.TagHelpers" />
210210
```
211211

212+
## `our-member-include` and `our-member-exclude`
213+
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.
214+
215+
There are two special Member Groups you can use:
216+
* `*` - All anonymous users
217+
* `?` - All authenticated users
218+
219+
```cshtml
220+
<div our-member-include="Staff">Only members of Staff Member Group will see this.</div>
221+
<div our-member-include="Staff,Admins">Only members of Staff OR Admins member group will see this.</div>
222+
<div our-member-include="*">Only logged in members will see this.</div>
223+
<div our-member-include="?">Only anonymous members will see this.</div>
224+
225+
<div our-member-exclude="Staff">Only Staff members can't see this (Including anonymous).</div>
226+
<div our-member-exclude="?">Everyone except Anonymous members will see this.</div>
227+
<div our-member-exclude="*">Everyone except who is authenticated will see this.</div>
228+
```
229+
212230
## Video 📺
213231
[![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)
214232

0 commit comments

Comments
 (0)