1
0

PermissionsUtil.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using log4net;
  32. namespace OpenSim.Framework
  33. {
  34. public static class PermissionsUtil
  35. {
  36. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  37. /// <summary>
  38. /// Logs permissions flags. Useful when debugging permission problems.
  39. /// </summary>
  40. /// <param name="message"></param>
  41. public static void LogPermissions(String name, String message, uint basePerm, uint curPerm, uint nextPerm)
  42. {
  43. m_log.DebugFormat("Permissions of \"{0}\" at \"{1}\": Base {2} ({3:X4}), Current {4} ({5:X4}), NextOwner {6} ({7:X4})",
  44. name, message,
  45. PermissionsToString(basePerm), basePerm, PermissionsToString(curPerm), curPerm, PermissionsToString(nextPerm), nextPerm);
  46. }
  47. /// <summary>
  48. /// Converts a permissions bit-mask to a string (e.g., "MCT").
  49. /// </summary>
  50. private static string PermissionsToString(uint perms)
  51. {
  52. string str = "";
  53. if ((perms & (int)PermissionMask.Modify) != 0)
  54. str += "M";
  55. if ((perms & (int)PermissionMask.Copy) != 0)
  56. str += "C";
  57. if ((perms & (int)PermissionMask.Transfer) != 0)
  58. str += "T";
  59. if ((perms & (int)PermissionMask.Export) != 0)
  60. str += "X";
  61. if (str == "")
  62. str = ".";
  63. return str;
  64. }
  65. public static void ApplyFoldedPermissions(uint foldedSourcePerms, ref uint targetPerms)
  66. {
  67. uint folded = foldedSourcePerms & (uint)PermissionMask.FoldedMask;
  68. if(folded == 0 || folded == (uint)PermissionMask.FoldedMask) // invalid we need to ignore, or nothing to do
  69. return;
  70. folded <<= (int)PermissionMask.FoldingShift;
  71. folded |= ~(uint)PermissionMask.UnfoldedMask;
  72. uint tmp = targetPerms;
  73. tmp &= folded;
  74. targetPerms = tmp;
  75. }
  76. // do not touch MOD
  77. public static void ApplyNoModFoldedPermissions(uint foldedSourcePerms, ref uint target)
  78. {
  79. uint folded = foldedSourcePerms & (uint)PermissionMask.FoldedMask;
  80. if(folded == 0 || folded == (uint)PermissionMask.FoldedMask) // invalid we need to ignore, or nothing to do
  81. return;
  82. folded <<= (int)PermissionMask.FoldingShift;
  83. folded |= (~(uint)PermissionMask.UnfoldedMask | (uint)PermissionMask.Modify);
  84. uint tmp = target;
  85. tmp &= folded;
  86. target = tmp;
  87. }
  88. public static uint FixAndFoldPermissions(uint perms)
  89. {
  90. uint tmp = perms;
  91. // C & T rule
  92. if((tmp & (uint)(PermissionMask.Copy | PermissionMask.Transfer)) == 0)
  93. tmp |= (uint)PermissionMask.Transfer;
  94. // unlock
  95. tmp |= (uint)PermissionMask.Move;
  96. tmp &= ~(uint)PermissionMask.FoldedMask;
  97. tmp |= ((tmp >> (int)PermissionMask.FoldingShift) & (uint)PermissionMask.FoldedMask);
  98. return tmp;
  99. }
  100. }
  101. }