PermissionsUtil.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 (str == "")
  60. str = ".";
  61. return str;
  62. }
  63. /// <summary>
  64. /// Applies an object's folded permissions to its regular permissions.
  65. /// </summary>
  66. /// <param name="foldedPerms">The folded permissions. Only the lowest 7 bits are examined.</param>
  67. /// <param name="mainPerms">The permissions variable to modify.</param>
  68. public static void ApplyFoldedPermissions(uint foldedPerms, ref uint mainPerms)
  69. {
  70. // if ((foldedPerms & 7) == 0)
  71. // return; // assume that if the folded permissions are 0 then this means that they weren't actually recorded
  72. if ((foldedPerms & ((uint)PermissionMask.Copy >> 13)) == 0)
  73. mainPerms &= ~(uint)PermissionMask.Copy;
  74. if ((foldedPerms & ((uint)PermissionMask.Transfer >> 13)) == 0)
  75. mainPerms &= ~(uint)PermissionMask.Transfer;
  76. if ((foldedPerms & ((uint)PermissionMask.Modify >> 13)) == 0)
  77. mainPerms &= ~(uint)PermissionMask.Modify;
  78. }
  79. }
  80. }