AuthorizationConfigHandler.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // System.Web.Configuration.AuthorizationConfigHandler
  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.Configuration;
  12. using System.Xml;
  13. namespace System.Web.Configuration
  14. {
  15. class AuthorizationConfigHandler : IConfigurationSectionHandler
  16. {
  17. public object Create (object parent, object context, XmlNode section)
  18. {
  19. AuthorizationConfig config = new AuthorizationConfig (parent);
  20. if (section.Attributes != null && section.Attributes.Count != 0)
  21. ThrowException ("Unrecognized attribute", section);
  22. XmlNodeList authNodes = section.ChildNodes;
  23. foreach (XmlNode child in authNodes) {
  24. XmlNodeType ntype = child.NodeType;
  25. if (ntype != XmlNodeType.Element)
  26. continue;
  27. string childName = child.Name;
  28. bool allow = (childName == "allow");
  29. bool deny = (childName == "deny");
  30. if (!allow && !deny)
  31. ThrowException ("Element name must be 'allow' or 'deny'.", child);
  32. string users = AttValue ("users", child);
  33. string roles = AttValue ("roles", child);
  34. if (users == null && roles == null)
  35. ThrowException ("At least 'users' or 'roles' must be present.", child);
  36. string verbs = AttValue ("verbs", child);
  37. if (child.Attributes != null && child.Attributes.Count != 0)
  38. ThrowException ("Unrecognized attribute.", child);
  39. bool added;
  40. if (allow)
  41. added = config.Allow (users, roles, verbs);
  42. else
  43. added = config.Deny (users, roles, verbs);
  44. if (!added)
  45. ThrowException ("User and role names cannot contain '?' or '*'.", child);
  46. }
  47. return config;
  48. }
  49. // A few methods to save some typing
  50. static string AttValue (string name, XmlNode node, bool optional)
  51. {
  52. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  53. }
  54. static string AttValue (string name, XmlNode node)
  55. {
  56. return HandlersUtil.ExtractAttributeValue (name, node, true);
  57. }
  58. static void ThrowException (string message, XmlNode node)
  59. {
  60. HandlersUtil.ThrowException (message, node);
  61. }
  62. //
  63. }
  64. }