AuthorizationConfig.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // System.Web.Configuration.AuthorizationConfig
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Security.Principal;
  32. using System.Web.UI;
  33. using System.Globalization;
  34. namespace System.Web.Configuration
  35. {
  36. class AuthorizationConfig
  37. {
  38. AuthorizationConfig parent;
  39. ArrayList list;
  40. internal AuthorizationConfig (object parent)
  41. {
  42. this.parent = parent as AuthorizationConfig;
  43. }
  44. static string [] SplitAndTrim (string s)
  45. {
  46. if (s == null || s == "")
  47. return null;
  48. string [] all = s.Split (',');
  49. for (int i = 0; i < all.Length; i++)
  50. all [i] = all [i].Trim ();
  51. return all;
  52. }
  53. static bool CheckWildcards (string [] values)
  54. {
  55. if (values == null)
  56. return true;
  57. foreach (string s in values) {
  58. if (s == null || s.Length == 1)
  59. continue;
  60. if (s.IndexOf ('?') != -1 || s.IndexOf ('*') != -1)
  61. return false;
  62. }
  63. return true;
  64. }
  65. bool Add (bool allow, string users, string roles, string verbs)
  66. {
  67. string [] allUsers = SplitAndTrim (users);
  68. string [] allRoles = SplitAndTrim (roles);
  69. string [] allVerbs = SplitAndTrim (verbs);
  70. if (!CheckWildcards (allUsers) || !CheckWildcards (allRoles))
  71. return false;
  72. if (list == null)
  73. list = new ArrayList ();
  74. list.Add (new UserData (allow, allUsers, allRoles, allVerbs));
  75. return true;
  76. }
  77. internal bool Allow (string users, string roles, string verbs)
  78. {
  79. return Add (true, users, roles, verbs);
  80. }
  81. internal bool Deny (string users, string roles, string verbs)
  82. {
  83. return Add (false, users, roles, verbs);
  84. }
  85. internal bool IsValidUser (IPrincipal user, string verb)
  86. {
  87. if (user == null)
  88. return false;
  89. if (list == null) {
  90. if (parent != null)
  91. return parent.IsValidUser (user, verb);
  92. return true;
  93. }
  94. foreach (UserData data in list) {
  95. if (data.Verbs != null && !data.CheckVerb (verb))
  96. continue;
  97. if ((data.Users !=null && data.CheckUser(user.Identity.Name)) ||
  98. (data.Roles != null && data.CheckRole(user)))
  99. return data.Allow;
  100. }
  101. if (parent != null)
  102. return parent.IsValidUser (user, verb);
  103. return true;
  104. }
  105. struct UserData
  106. {
  107. public bool Allow;
  108. public string [] Users;
  109. public string [] Roles;
  110. public string [] Verbs;
  111. public UserData (bool allow, string [] users, string [] roles, string [] verbs)
  112. {
  113. Allow = allow;
  114. Users = users;
  115. Roles = roles;
  116. Verbs = verbs;
  117. }
  118. public bool CheckUser (string user)
  119. {
  120. foreach (string u in Users) {
  121. if (String.Compare (u, user, true) == 0 ||
  122. u == "*" ||
  123. (u == "?" && user == ""))
  124. return true;
  125. }
  126. return false;
  127. }
  128. public bool CheckRole (IPrincipal user)
  129. {
  130. foreach (string r in Roles) {
  131. if (user.IsInRole (r))
  132. return true;
  133. }
  134. return false;
  135. }
  136. public bool CheckVerb (string verb)
  137. {
  138. foreach (string u in Verbs) {
  139. if (String.Compare (u, verb, true, CultureInfo.InvariantCulture) == 0)
  140. return true;
  141. }
  142. return false;
  143. }
  144. }
  145. }
  146. }