|
| 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 | +} |
0 commit comments