1
0

AssetPermissions.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Nini.Config;
  5. using log4net;
  6. using OpenMetaverse;
  7. namespace OpenSim.Framework
  8. {
  9. public class AssetPermissions
  10. {
  11. private static readonly ILog m_log =
  12. LogManager.GetLogger(
  13. MethodBase.GetCurrentMethod().DeclaringType);
  14. private bool[] m_DisallowExport, m_DisallowImport;
  15. private string[] m_AssetTypeNames;
  16. public AssetPermissions(IConfig config)
  17. {
  18. Type enumType = typeof(AssetType);
  19. m_AssetTypeNames = Enum.GetNames(enumType);
  20. for (int i = 0; i < m_AssetTypeNames.Length; i++)
  21. m_AssetTypeNames[i] = m_AssetTypeNames[i].ToLower();
  22. int n = Enum.GetValues(enumType).Length;
  23. m_DisallowExport = new bool[n];
  24. m_DisallowImport = new bool[n];
  25. LoadPermsFromConfig(config, "DisallowExport", m_DisallowExport);
  26. LoadPermsFromConfig(config, "DisallowImport", m_DisallowImport);
  27. }
  28. private void LoadPermsFromConfig(IConfig assetConfig, string variable, bool[] bitArray)
  29. {
  30. if (assetConfig == null)
  31. return;
  32. string perms = assetConfig.GetString(variable, String.Empty);
  33. string[] parts = perms.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  34. foreach (string s in parts)
  35. {
  36. int index = Array.IndexOf(m_AssetTypeNames, s.Trim().ToLower());
  37. if (index >= 0)
  38. bitArray[index] = true;
  39. else
  40. m_log.WarnFormat("[Asset Permissions]: Invalid AssetType {0}", s);
  41. }
  42. }
  43. public bool AllowedExport(sbyte type)
  44. {
  45. string assetTypeName = ((AssetType)type).ToString();
  46. int index = Array.IndexOf(m_AssetTypeNames, assetTypeName.ToLower());
  47. if (index >= 0 && m_DisallowExport[index])
  48. {
  49. m_log.DebugFormat("[Asset Permissions]: Export denied: configuration does not allow export of AssetType {0}", assetTypeName);
  50. return false;
  51. }
  52. return true;
  53. }
  54. public bool AllowedImport(sbyte type)
  55. {
  56. string assetTypeName = ((AssetType)type).ToString();
  57. int index = Array.IndexOf(m_AssetTypeNames, assetTypeName.ToLower());
  58. if (index >= 0 && m_DisallowImport[index])
  59. {
  60. m_log.DebugFormat("[Asset Permissions]: Import denied: configuration does not allow import of AssetType {0}", assetTypeName);
  61. return false;
  62. }
  63. return true;
  64. }
  65. }
  66. }