/src/Framework/N2/Integrity/RestrictParentsAttribute.cs

https://github.com/lundbeck/n2cms
C# | 96 lines | 58 code | 13 blank | 25 comment | 9 complexity | 9c94c5ae3528f9127a2d0037e0cb2b90 MD5 | raw file
  1. #region License
  2. /* Copyright (C) 2007 Cristian Libardo
  3. *
  4. * This is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as
  6. * published by the Free Software Foundation; either version 2.1 of
  7. * the License, or (at your option) any later version.
  8. */
  9. #endregion
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using N2.Definitions;
  14. namespace N2.Integrity
  15. {
  16. /// <summary>
  17. /// A class decoration used to restrict which items may be placed under
  18. /// which. When this attribute intersects with
  19. /// <see cref="AllowedChildrenAttribute"/>, the union of these two are
  20. /// considered to be allowed.</summary>
  21. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
  22. public class RestrictParentsAttribute : TypeIntegrityAttribute, IInheritableDefinitionRefiner
  23. {
  24. /// <summary>
  25. /// Restrict children by template name, allow only children with these template name.
  26. /// </summary>
  27. public string[] TemplateKeys { get; set; }
  28. /// <summary>Initializes a new instance of the RestrictParentsAttribute which is used to restrict which types of items may be added below which.</summary>
  29. public RestrictParentsAttribute()
  30. {
  31. RefinementOrder = RefineOrder.Before;
  32. }
  33. /// <summary>Initializes a new instance of the RestrictParentsAttribute which is used to restrict which types of items may be added below which.</summary>
  34. /// <param name="allowedTypes">Defines wether all types of items are allowed as parent items.</param>
  35. public RestrictParentsAttribute(AllowedTypes allowedTypes)
  36. : this()
  37. {
  38. if (allowedTypes == AllowedTypes.All)
  39. Types = null;
  40. else
  41. Types = new Type[0];
  42. }
  43. /// <summary>Initializes a new instance of the RestrictParentsAttribute which is used to restrict which types of items may be added below which.</summary>
  44. /// <param name="allowedParentTypes">A list of allowed types. Null is interpreted as all types are allowed.</param>
  45. public RestrictParentsAttribute(params Type[] allowedParentTypes)
  46. : this()
  47. {
  48. Types = allowedParentTypes;
  49. }
  50. /// <summary>Initializes a new instance of the RestrictParentsAttribute which is used to restrict which types of items may be added below which.</summary>
  51. /// <param name="allowedParentType">A list of allowed types. Null is interpreted as all types are allowed.</param>
  52. public RestrictParentsAttribute(Type allowedParentType)
  53. : this()
  54. {
  55. Types = new [] { allowedParentType };
  56. }
  57. /// <summary>Changes allowed parents on the item definition.</summary>
  58. /// <param name="currentDefinition">The definition to alter.</param>
  59. /// <param name="allDefinitions">All definitions.</param>
  60. public override void Refine(ItemDefinition currentDefinition, IList<ItemDefinition> allDefinitions)
  61. {
  62. currentDefinition.AllowedParentFilters.Add(new Helper { ChildType = currentDefinition.ItemType, Attribute = this });
  63. }
  64. class Helper : IAllowedDefinitionFilter
  65. {
  66. public Type ChildType { get; set; }
  67. public RestrictParentsAttribute Attribute { get; set; }
  68. #region IAllowedDefinitionFilter Members
  69. public AllowedDefinitionResult IsAllowed(AllowedDefinitionQuery context)
  70. {
  71. if (ChildType.IsAssignableFrom(context.ChildDefinition.ItemType))
  72. {
  73. if (this.Attribute.Types != null && !this.Attribute.Types.Any(t => t.IsAssignableFrom(context.ParentDefinition.ItemType)))
  74. return AllowedDefinitionResult.Deny;
  75. if (this.Attribute.TemplateKeys != null && !this.Attribute.TemplateKeys.Contains(context.ParentDefinition.TemplateKey))
  76. return AllowedDefinitionResult.Deny;
  77. }
  78. return AllowedDefinitionResult.DontCare;
  79. }
  80. #endregion
  81. }
  82. }
  83. }