Skip to content

Commit cb20f96

Browse files
author
Warren Buckley
authored
Merge pull request #25 from prjseal/feature/add-user-tag-helper
added user tag helper
2 parents dee84a4 + 14bcead commit cb20f96

File tree

3 files changed

+120
-3
lines changed

3 files changed

+120
-3
lines changed

Our.Umbraco.TagHelpers/MemberTagHelper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using Umbraco.Cms.Core.Security;
6+
using Umbraco.Extensions;
67

78
namespace Our.Umbraco.TagHelpers
89
{
@@ -17,15 +18,15 @@ public class MemberTagHelper : TagHelper
1718
private IMemberManager _memberManager;
1819

1920
/// <summary>
20-
/// A comma seperated list of Member Groups to exclude
21+
/// A comma separated list of Member Groups to exclude
2122
/// ? = All anonymous users
2223
/// * = All authenticated users
2324
/// </summary>
2425
[HtmlAttributeName("our-member-exclude")]
2526
public string ExcludeRoles { get; set; }
2627

2728
/// <summary>
28-
/// A comma seperated list of Member Groups to include
29+
/// A comma separated list of Member Groups to include
2930
/// ? = All anonymous users
3031
/// * = All authenticated users
3132
/// </summary>
@@ -79,7 +80,7 @@ private bool IsUserInRole(string roleString, List<string> currentMemberRoles)
7980
return true;
8081
}
8182

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

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,28 @@ There are two special Member Groups you can use:
227227
<div our-member-exclude="*">Everyone except who is authenticated will see this.</div>
228228
```
229229

230+
## `our-user-include` and `our-user-exclude`
231+
232+
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 user groups that the current logged in Umbraco backoffice user is in, for the exclude or include variants.
233+
234+
There are two special User Groups you can use:
235+
236+
- `*` - All anonymous users
237+
- `?` - All authenticated users
238+
239+
Use the alias of the User Group
240+
241+
````cshtml
242+
<div our-user-include="admin">Only users in the Admin group will see this.</div>
243+
<div our-user-include="admin,editor">Only users in the Admin or Editor user group will see this.</div>
244+
<div our-user-include="*">Only logged in users will see this.</div>
245+
<div our-user-include="?">Only anonymous users will see this.</div>
246+
247+
<div our-user-exclude="editor">Only Editor users can't see this (Including anonymous).</div>
248+
<div our-user-exclude="?">Everyone except Anonymous users will see this.</div>
249+
<div our-user-exclude="*">Everyone except who is authenticated will see this.</div>
250+
251+
230252
## `<our-edit-link>`
231253
This is a tag helper element which renders an edit link on the front end only if the current user is logged into umbraco and has access to the content section.
232254

0 commit comments

Comments
 (0)