PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llinventory/llpermissions.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 458 lines | 162 code | 76 blank | 220 comment | 3 complexity | 9e8dd839bd5092d5999159dc5bfc2980 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llpermissions.h
  3. * @brief Permissions structures for objects.
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLPERMISSIONS_H
  27. #define LL_LLPERMISSIONS_H
  28. #include "llpermissionsflags.h"
  29. #include "llsd.h"
  30. #include "lluuid.h"
  31. #include "llxmlnode.h"
  32. #include "reflective.h"
  33. #include "llinventorytype.h"
  34. // prototypes
  35. class LLMessageSystem;
  36. extern void mask_to_string(U32 mask, char* str);
  37. extern std::string mask_to_string(U32 mask);
  38. template<class T> class LLMetaClassT;
  39. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. // Class LLPermissions
  41. //
  42. // Class which encapsulates object and inventory permissions/ownership/etc.
  43. //
  44. // Permissions where originally a static state creator/owner and set
  45. // of cap bits. Since then, it has grown to include group information,
  46. // last owner, masks for different people. The implementation has been
  47. // chosen such that a uuid is stored for each current/past owner, and
  48. // a bitmask is stored for the base permissions, owner permissions,
  49. // group permissions, and everyone else permissions.
  50. //
  51. // The base permissions represent the most permissive state that the
  52. // permissions can possibly be in. Thus, if the base permissions do
  53. // not allow copying, no one can ever copy the object. The permissions
  54. // also maintain a tree-like hierarchy of permissions, thus, if we
  55. // (for sake of discussions) denote more permissive as '>', then this
  56. // is invariant:
  57. //
  58. // base mask >= owner mask >= group mask
  59. // >= everyone mask
  60. // >= next owner mask
  61. // NOTE: the group mask does not effect everyone or next, everyone
  62. // does not effect group or next, etc.
  63. //
  64. // It is considered a fair use right to move or delete any object you
  65. // own. Another fair use right is the ability to give away anything
  66. // which you cannot copy. One way to look at that is that if you have
  67. // a unique item, you can always give that one copy you have to
  68. // someone else.
  69. //
  70. // Most of the bitmask is easy to understand, PERM_COPY means you can
  71. // copy !PERM_TRANSFER means you cannot transfer, etc. Given that we
  72. // now track the concept of 'next owner' inside of the permissions
  73. // object, we can describe some new meta-meaning to the PERM_MODIFY
  74. // flag. PERM_MODIFY is usually meant to note if you can change an
  75. // item, but since we record next owner permissions, we can interpret
  76. // a no-modify object as 'you cannot modify this object and you cannot
  77. // make derivative works.' When evaluating functionality, and
  78. // comparisons against permissions, keep this concept in mind for
  79. // logical consistency.
  80. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. class LLPermissions : public LLReflective
  82. {
  83. private:
  84. LLUUID mCreator; // null if object created by system
  85. LLUUID mOwner; // null if object "unowned" (owned by system)
  86. LLUUID mLastOwner; // object's last owner
  87. LLUUID mGroup; // The group association
  88. PermissionMask mMaskBase; // initially permissive, progressively AND restricted by each owner
  89. PermissionMask mMaskOwner; // set by owner, applies to owner only, restricts lower permissions
  90. PermissionMask mMaskEveryone; // set by owner, applies to everyone else
  91. PermissionMask mMaskGroup; // set by owner, applies to group that is associated with permissions
  92. PermissionMask mMaskNextOwner; // set by owner, applied to base on transfer.
  93. // Usually set in the fixOwnership() method based on current uuid
  94. // values.
  95. bool mIsGroupOwned;
  96. // Correct for fair use - you can never take away the right to
  97. // move stuff you own, and you can never take away the right to
  98. // transfer something you cannot otherwise copy.
  99. void fixFairUse();
  100. // Fix internal consistency for group/agent ownership
  101. void fixOwnership();
  102. public:
  103. static const LLPermissions DEFAULT;
  104. LLPermissions(); // defaults to created by system
  105. //~LLPermissions();
  106. // base initialization code
  107. void init(const LLUUID& creator, const LLUUID& owner,
  108. const LLUUID& last_owner, const LLUUID& group);
  109. void initMasks(PermissionMask base, PermissionMask owner,
  110. PermissionMask everyone, PermissionMask group,
  111. PermissionMask next);
  112. // adjust permissions based on inventory type.
  113. void initMasks(LLInventoryType::EType type);
  114. //
  115. // ACCESSORS
  116. //
  117. // return the agent_id of the agent that created the item
  118. const LLUUID& getCreator() const { return mCreator; }
  119. // return the agent_id of the owner. returns LLUUID::null if group
  120. // owned or public (a really big group).
  121. const LLUUID& getOwner() const { return mOwner; }
  122. // return the group_id of the group associated with the
  123. // object.
  124. const LLUUID& getGroup() const { return mGroup; }
  125. // return the agent_id of the last agent owner. Only returns
  126. // LLUUID::null if there has never been a previous owner (*note: this is apparently not true, say for textures in inventory, it may return LLUUID::null even if there was a previous owner).
  127. const LLUUID& getLastOwner() const { return mLastOwner; }
  128. U32 getMaskBase() const { return mMaskBase; }
  129. U32 getMaskOwner() const { return mMaskOwner; }
  130. U32 getMaskGroup() const { return mMaskGroup; }
  131. U32 getMaskEveryone() const { return mMaskEveryone; }
  132. U32 getMaskNextOwner() const { return mMaskNextOwner; }
  133. // return TRUE if the object has any owner
  134. bool isOwned() const { return (mOwner.notNull() || mIsGroupOwned); }
  135. // return TRUE if group_id is owner.
  136. bool isGroupOwned() const { return mIsGroupOwned; }
  137. // This API returns TRUE if the object is owned at all, and FALSE
  138. // otherwise. If it is owned at all, owner id is filled with
  139. // either the owner id or the group id, and the is_group_owned
  140. // parameter is appropriately filled. The values of owner_id and
  141. // is_group_owned are not changed if the object is not owned.
  142. BOOL getOwnership(LLUUID& owner_id, BOOL& is_group_owned) const;
  143. // Gets the 'safe' owner. This should never return LLUUID::null.
  144. // If no group owned, return the agent owner id normally.
  145. // If group owned, return the group id.
  146. // If not owned, return a random uuid which should have no power.
  147. LLUUID getSafeOwner() const;
  148. // return a cheap crc
  149. U32 getCRC32() const;
  150. //
  151. // MANIPULATORS
  152. //
  153. // Fix hierarchy of permissions, applies appropriate permissions
  154. // at each level to ensure that base permissions are respected,
  155. // and also ensures that if base cannot transfer, then group and
  156. // other cannot copy.
  157. void fix();
  158. // All of these methods just do exactly what they say. There is no
  159. // permissions checking to see if the operation is allowed, and do
  160. // not fix the permissions hierarchy. So please only use these
  161. // methods when you are know what you're doing and coding on
  162. // behalf of the system - ie, acting as god.
  163. void set(const LLPermissions& permissions);
  164. void setMaskBase(U32 mask) { mMaskBase = mask; }
  165. void setMaskOwner(U32 mask) { mMaskOwner = mask; }
  166. void setMaskEveryone(U32 mask) { mMaskEveryone = mask;}
  167. void setMaskGroup(U32 mask) { mMaskGroup = mask;}
  168. void setMaskNext(U32 mask) { mMaskNextOwner = mask; }
  169. // Allow accumulation of permissions. Results in the tightest
  170. // permissions possible. In the case of clashing UUIDs, it sets
  171. // the ID to LLUUID::null.
  172. void accumulate(const LLPermissions& perm);
  173. //
  174. // CHECKED MANIPULATORS
  175. //
  176. // These functions return true on success. They return false if
  177. // the given agent isn't allowed to make the change. You can pass
  178. // LLUUID::null as the agent id if the change is being made by the
  179. // simulator itself, not on behalf of any agent - this will always
  180. // succeed. Passing in group id of LLUUID:null means no group, and
  181. // does not offer special permission to do anything.
  182. // saves last owner, sets current owner, and sets the group.
  183. // set is_atomic = true means that this permission represents
  184. // an atomic permission and not a collection of permissions.
  185. // Currently, the only way to have a collection is when an object
  186. // has inventory and is then itself rolled up into an inventory
  187. // item.
  188. BOOL setOwnerAndGroup(const LLUUID& agent, const LLUUID& owner, const LLUUID& group, bool is_atomic);
  189. // only call this if you know what you're doing
  190. // there are usually perm-bit consequences when the
  191. // ownerhsip changes
  192. void yesReallySetOwner(const LLUUID& owner, bool group_owned);
  193. // Last owner doesn't have much in the way of permissions so it's
  194. //not too dangerous to do this.
  195. void setLastOwner(const LLUUID& last_owner);
  196. // saves last owner, sets owner to uuid null, sets group
  197. // owned. group_id must be the group of the object (that's who it
  198. // is being deeded to) and the object must be group
  199. // modify. Technically, the agent id and group id are not
  200. // necessary, but I wanted this function to look like the other
  201. // checked manipulators (since that is how it is used.) If the
  202. // agent is the system or (group == mGroup and group modify and
  203. // owner transfer) then this function will deed the permissions,
  204. // set the next owner mask, and return TRUE. Otherwise, no change
  205. // is effected, and the function returns FALSE.
  206. BOOL deedToGroup(const LLUUID& agent, const LLUUID& group);
  207. // Attempt to set or clear the given bitmask. Returns TRUE if you
  208. // are allowed to modify the permissions. If you attempt to turn
  209. // on bits not allowed by the base bits, the function will return
  210. // TRUE, but those bits will not be set.
  211. BOOL setBaseBits( const LLUUID& agent, BOOL set, PermissionMask bits);
  212. BOOL setOwnerBits( const LLUUID& agent, BOOL set, PermissionMask bits);
  213. BOOL setGroupBits( const LLUUID& agent, const LLUUID& group, BOOL set, PermissionMask bits);
  214. BOOL setEveryoneBits(const LLUUID& agent, const LLUUID& group, BOOL set, PermissionMask bits);
  215. BOOL setNextOwnerBits(const LLUUID& agent, const LLUUID& group, BOOL set, PermissionMask bits);
  216. // This is currently only used in the Viewer to handle calling cards
  217. // where the creator is actually used to store the target. Use with care.
  218. void setCreator(const LLUUID& creator) { mCreator = creator; }
  219. //
  220. // METHODS
  221. //
  222. // All the allow* functions return true if the given agent or
  223. // group can perform the function. Prefer using this set of
  224. // operations to check permissions on an object. These return
  225. // true if the given agent or group can perform the function.
  226. // They also return true if the object isn't owned, or the
  227. // requesting agent is a system agent. See llpermissionsflags.h
  228. // for bits.
  229. bool allowOperationBy(PermissionBit op, const LLUUID& agent, const LLUUID& group = LLUUID::null) const;
  230. inline bool allowModifyBy(const LLUUID &agent_id) const;
  231. inline bool allowCopyBy(const LLUUID& agent_id) const;
  232. inline bool allowMoveBy(const LLUUID& agent_id) const;
  233. inline bool allowModifyBy(const LLUUID &agent_id, const LLUUID& group) const;
  234. inline bool allowCopyBy(const LLUUID& agent_id, const LLUUID& group) const;
  235. inline bool allowMoveBy(const LLUUID &agent_id, const LLUUID &group) const;
  236. // This somewhat specialized function is meant for testing if the
  237. // current owner is allowed to transfer to the specified agent id.
  238. inline bool allowTransferTo(const LLUUID &agent_id) const;
  239. //
  240. // DEPRECATED.
  241. //
  242. // These return true if the given agent can perform the function.
  243. // They also return true if the object isn't owned, or the
  244. // requesting agent is a system agent. See llpermissionsflags.h
  245. // for bits.
  246. //BOOL allowDeleteBy(const LLUUID& agent_id) const { return allowModifyBy(agent_id); }
  247. //BOOL allowEditBy(const LLUUID& agent_id) const { return allowModifyBy(agent_id); }
  248. // saves last owner and sets current owner
  249. //BOOL setOwner(const LLUUID& agent, const LLUUID& owner);
  250. // This method saves the last owner, sets the current owner to the
  251. // one provided, and sets the base mask as indicated.
  252. //BOOL setOwner(const LLUUID& agent, const LLUUID& owner, U32 new_base_mask);
  253. // Attempt to set or clear the given bitmask. Returns TRUE if you
  254. // are allowed to modify the permissions. If you attempt to turn
  255. // on bits not allowed by the base bits, the function will return
  256. // TRUE, but those bits will not be set.
  257. //BOOL setGroupBits( const LLUUID& agent, BOOL set, PermissionMask bits);
  258. //BOOL setEveryoneBits(const LLUUID& agent, BOOL set, PermissionMask bits);
  259. //
  260. // MISC METHODS and OPERATORS
  261. //
  262. LLSD packMessage() const;
  263. void unpackMessage(LLSD perms);
  264. // For messaging system support
  265. void packMessage(LLMessageSystem* msg) const;
  266. void unpackMessage(LLMessageSystem* msg, const char* block, S32 block_num = 0);
  267. // Load/save support
  268. BOOL importFile(LLFILE* fp);
  269. BOOL exportFile(LLFILE* fp) const;
  270. BOOL importLegacyStream(std::istream& input_stream);
  271. BOOL exportLegacyStream(std::ostream& output_stream) const;
  272. bool operator==(const LLPermissions &rhs) const;
  273. bool operator!=(const LLPermissions &rhs) const;
  274. friend std::ostream& operator<<(std::ostream &s, const LLPermissions &perm);
  275. // Reflection.
  276. friend class LLMetaClassT<LLPermissions>;
  277. virtual const LLMetaClass& getMetaClass() const;
  278. };
  279. // Inlines
  280. bool LLPermissions::allowModifyBy(const LLUUID& agent, const LLUUID& group) const
  281. {
  282. return allowOperationBy(PERM_MODIFY, agent, group);
  283. }
  284. bool LLPermissions::allowCopyBy(const LLUUID& agent, const LLUUID& group) const
  285. {
  286. return allowOperationBy(PERM_COPY, agent, group);
  287. }
  288. bool LLPermissions::allowMoveBy(const LLUUID& agent, const LLUUID& group) const
  289. {
  290. return allowOperationBy(PERM_MOVE, agent, group);
  291. }
  292. bool LLPermissions::allowModifyBy(const LLUUID& agent) const
  293. {
  294. return allowOperationBy(PERM_MODIFY, agent, LLUUID::null);
  295. }
  296. bool LLPermissions::allowCopyBy(const LLUUID& agent) const
  297. {
  298. return allowOperationBy(PERM_COPY, agent, LLUUID::null);
  299. }
  300. bool LLPermissions::allowMoveBy(const LLUUID& agent) const
  301. {
  302. return allowOperationBy(PERM_MOVE, agent, LLUUID::null);
  303. }
  304. bool LLPermissions::allowTransferTo(const LLUUID &agent_id) const
  305. {
  306. if (mIsGroupOwned)
  307. {
  308. return allowOperationBy(PERM_TRANSFER, mGroup, mGroup);
  309. }
  310. else
  311. {
  312. return ((mOwner == agent_id) ? TRUE : allowOperationBy(PERM_TRANSFER, mOwner));
  313. }
  314. }
  315. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  316. // Class LLAggregatePermissions
  317. //
  318. // Class which encapsulates object and inventory permissions,
  319. // ownership, etc. Currently, it only aggregates PERM_COPY,
  320. // PERM_MODIFY, and PERM_TRANSFER.
  321. //
  322. // Usually you will construct an instance and hand the object several
  323. // permissions masks to aggregate the copy, modify, and
  324. // transferability into a nice trinary value.
  325. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  326. class LLAggregatePermissions
  327. {
  328. public:
  329. enum EValue
  330. {
  331. AP_EMPTY = 0x00,
  332. AP_NONE = 0x01,
  333. AP_SOME = 0x02,
  334. AP_ALL = 0x03
  335. };
  336. // construct an empty aggregate permissions
  337. LLAggregatePermissions();
  338. // pass in a PERM_COPY, PERM_TRANSFER, etc, and get out a EValue
  339. // enumeration describing the current aggregate permissions.
  340. EValue getValue(PermissionBit bit) const;
  341. // returns the permissions packed into the 6 LSB of a U8:
  342. // 00TTMMCC
  343. // where TT = transfer, MM = modify, and CC = copy
  344. // LSB is to the right
  345. U8 getU8() const;
  346. // return TRUE is the aggregate permissions are empty, otherwise FALSE.
  347. BOOL isEmpty() const ;
  348. // pass in a PERM_COPY, PERM_TRANSFER, etc, and an EValue
  349. // enumeration to specifically set that value. Not implemented
  350. // because I'm not sure it's a useful api.
  351. //void setValue(PermissionBit bit, EValue);
  352. // Given a mask, aggregate the useful permissions.
  353. void aggregate(PermissionMask mask);
  354. // Aggregate aggregates
  355. void aggregate(const LLAggregatePermissions& ag);
  356. // message handling
  357. void packMessage(LLMessageSystem* msg, const char* field) const;
  358. void unpackMessage(LLMessageSystem* msg, const char* block, const char *field, S32 block_num = 0);
  359. static const LLAggregatePermissions empty;
  360. friend std::ostream& operator<<(std::ostream &s, const LLAggregatePermissions &perm);
  361. protected:
  362. enum EPermIndex
  363. {
  364. PI_COPY = 0,
  365. PI_MODIFY = 1,
  366. PI_TRANSFER = 2,
  367. PI_END = 3,
  368. PI_COUNT = 3
  369. };
  370. void aggregateBit(EPermIndex idx, BOOL allowed);
  371. void aggregateIndex(EPermIndex idx, U8 bits);
  372. static EPermIndex perm2PermIndex(PermissionBit bit);
  373. // structure used to store the aggregate so far.
  374. U8 mBits[PI_COUNT];
  375. };
  376. // These functions convert between structured data and permissions as
  377. // appropriate for serialization. The permissions are a map of things
  378. // like 'creator_id', 'owner_id', etc, with the value copied from the
  379. // permission object.
  380. LLSD ll_create_sd_from_permissions(const LLPermissions& perm);
  381. LLPermissions ll_permissions_from_sd(const LLSD& sd_perm);
  382. #endif