Group.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.IO;
  17. using TechnitiumLibrary.IO;
  18. namespace DnsServerCore.Auth
  19. {
  20. class Group : IComparable<Group>
  21. {
  22. #region variables
  23. public const string ADMINISTRATORS = "Administrators";
  24. public const string EVERYONE = "Everyone";
  25. public const string DNS_ADMINISTRATORS = "DNS Administrators";
  26. public const string DHCP_ADMINISTRATORS = "DHCP Administrators";
  27. string _name;
  28. string _description;
  29. #endregion
  30. #region constructor
  31. public Group(string name, string description)
  32. {
  33. Name = name;
  34. Description = description;
  35. }
  36. public Group(BinaryReader bR)
  37. {
  38. switch (bR.ReadByte())
  39. {
  40. case 1:
  41. _name = bR.ReadShortString();
  42. _description = bR.ReadShortString();
  43. break;
  44. default:
  45. throw new InvalidDataException("Invalid data or version not supported.");
  46. }
  47. }
  48. #endregion
  49. #region public
  50. public void WriteTo(BinaryWriter bW)
  51. {
  52. bW.Write((byte)1);
  53. bW.WriteShortString(_name);
  54. bW.WriteShortString(_description);
  55. }
  56. public override bool Equals(object obj)
  57. {
  58. if (obj is not Group other)
  59. return false;
  60. return _name.Equals(other._name, StringComparison.OrdinalIgnoreCase);
  61. }
  62. public override int GetHashCode()
  63. {
  64. return HashCode.Combine(_name);
  65. }
  66. public override string ToString()
  67. {
  68. return _name;
  69. }
  70. public int CompareTo(Group other)
  71. {
  72. return _name.CompareTo(other._name);
  73. }
  74. #endregion
  75. #region properties
  76. public string Name
  77. {
  78. get { return _name; }
  79. set
  80. {
  81. if (string.IsNullOrWhiteSpace(value))
  82. throw new ArgumentException("Group name cannot be null or empty.", nameof(Name));
  83. if (value.Length > 255)
  84. throw new ArgumentException("Group name length cannot exceed 255 characters.", nameof(Name));
  85. switch (_name?.ToLowerInvariant())
  86. {
  87. case "everyone":
  88. case "administrators":
  89. case "dns administrators":
  90. case "dhcp administrators":
  91. throw new InvalidOperationException("Access was denied.");
  92. default:
  93. _name = value;
  94. break;
  95. }
  96. }
  97. }
  98. public string Description
  99. {
  100. get { return _description; }
  101. set
  102. {
  103. if (value.Length > 255)
  104. throw new ArgumentException("Group description length cannot exceed 255 characters.", nameof(Description));
  105. _description = value;
  106. }
  107. }
  108. #endregion
  109. }
  110. }