-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from umbraco-community/feature/our-member
Feature/our member
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters