AuthorizationConfig.cs 3.3 KB

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