AuthorizationConfig.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. namespace System.Web.Configuration
  34. {
  35. class AuthorizationConfig
  36. {
  37. AuthorizationConfig parent;
  38. ArrayList list;
  39. internal AuthorizationConfig (object parent)
  40. {
  41. this.parent = parent as AuthorizationConfig;
  42. }
  43. static string [] SplitAndTrim (string s)
  44. {
  45. if (s == null || s == "")
  46. return null;
  47. string [] all = s.Split (',');
  48. for (int i = 0; i < all.Length; i++)
  49. all [i] = all [i].Trim ();
  50. return all;
  51. }
  52. static bool CheckWildcards (string [] values)
  53. {
  54. if (values == null)
  55. return true;
  56. foreach (string s in values) {
  57. if (s == null || s.Length == 1)
  58. continue;
  59. if (s.IndexOf ('?') != -1 || s.IndexOf ('*') != -1)
  60. return false;
  61. }
  62. return true;
  63. }
  64. bool Add (bool allow, string users, string roles, string verbs)
  65. {
  66. string [] allUsers = SplitAndTrim (users);
  67. string [] allRoles = SplitAndTrim (roles);
  68. string [] allVerbs = SplitAndTrim (verbs);
  69. if (!CheckWildcards (allUsers) || !CheckWildcards (allRoles))
  70. return false;
  71. if (list == null)
  72. list = new ArrayList ();
  73. list.Add (new UserData (allow, allUsers, allRoles, allVerbs));
  74. return true;
  75. }
  76. internal bool Allow (string users, string roles, string verbs)
  77. {
  78. return Add (true, users, roles, verbs);
  79. }
  80. internal bool Deny (string users, string roles, string verbs)
  81. {
  82. return Add (false, users, roles, verbs);
  83. }
  84. internal bool IsValidUser (IPrincipal user, string verb)
  85. {
  86. if (user == null)
  87. return false;
  88. if (list == null) {
  89. if (parent != null)
  90. return parent.IsValidUser (user, verb);
  91. return true;
  92. }
  93. foreach (UserData data in list) {
  94. if (data.Verbs != null && !data.CheckVerb (verb))
  95. continue;
  96. if ((data.Users !=null && data.CheckUser(user.Identity.Name)) ||
  97. (data.Roles != null && data.CheckRole(user)))
  98. return data.Allow;
  99. }
  100. if (parent != null)
  101. return parent.IsValidUser (user, verb);
  102. return true;
  103. }
  104. struct UserData
  105. {
  106. public bool Allow;
  107. public string [] Users;
  108. public string [] Roles;
  109. public string [] Verbs;
  110. public UserData (bool allow, string [] users, string [] roles, string [] verbs)
  111. {
  112. Allow = allow;
  113. Users = users;
  114. Roles = roles;
  115. Verbs = verbs;
  116. }
  117. public bool CheckUser (string user)
  118. {
  119. foreach (string u in Users) {
  120. if (String.Compare (u, user, true) == 0 ||
  121. u == "*" ||
  122. (u == "?" && user == ""))
  123. return true;
  124. }
  125. return false;
  126. }
  127. public bool CheckRole (IPrincipal user)
  128. {
  129. foreach (string r in Roles) {
  130. if (user.IsInRole (r))
  131. return true;
  132. }
  133. return false;
  134. }
  135. public bool CheckVerb (string verb)
  136. {
  137. foreach (string u in Verbs) {
  138. if (String.Compare (u, verb, true) == 0)
  139. return true;
  140. }
  141. return false;
  142. }
  143. }
  144. }
  145. }