PermissionNode.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. namespace VCommon.VApplication.Authorization
  4. {
  5. public class PermissionNode
  6. {
  7. private readonly PermissionTree _tree;
  8. private readonly List<PermissionNode> _children = new List<PermissionNode>();
  9. /// <summary>
  10. /// 权限代码, 产品内唯一
  11. /// </summary>
  12. public string Code { get; }
  13. /// <summary>
  14. /// 面向用户的显示名称
  15. /// </summary>
  16. public string Name { get; }
  17. /// <summary>
  18. /// 面向用户的描述
  19. /// </summary>
  20. public string Description { get; }
  21. /// <summary>
  22. /// 面向运营商(平台)的显示名称
  23. /// </summary>
  24. public string NameForOperator { get; }
  25. /// <summary>
  26. /// 面向运营商(平台)的描述
  27. /// </summary>
  28. public string DescriptionForOperator { get; }
  29. public MultiTenancySides TenancySide { get; }
  30. public IReadOnlyList<PermissionNode> Children => _children;
  31. public object CustomData { get; set; }
  32. internal PermissionNode(PermissionTree tree, string code, string name, string description, string nameForOperator, string descriptionForOperator, MultiTenancySides side = MultiTenancySides.Both, object customData = null)
  33. {
  34. _tree = tree;
  35. Code = code;
  36. Name = name;
  37. Description = description;
  38. NameForOperator = nameForOperator;
  39. DescriptionForOperator = descriptionForOperator;
  40. TenancySide = side;
  41. CustomData = customData;
  42. }
  43. public PermissionNode CreateChildNode(string code, string name, string description = null, string nameForOperator = null, string descriptionForOperator = null, MultiTenancySides side = MultiTenancySides.Both, object customData = null)
  44. {
  45. if (_tree.CodeHashSet.Contains(code)) throw new ArgumentException($"重复的权限代码:{code},名称:{name}");
  46. var node = new PermissionNode(_tree, code, name, description, nameForOperator, descriptionForOperator, side, customData);
  47. _children.Add(node);
  48. _tree._allNode.Add(node);
  49. _tree.CodeHashSet.Add(code);
  50. return node;
  51. }
  52. public void Traverse(Action<PermissionNode> visitor)
  53. {
  54. visitor(this);
  55. foreach (var child in Children) child.Traverse(visitor);
  56. }
  57. }
  58. }