TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RBAC.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
40 #ifndef _RBAC_H
41 #define _RBAC_H
42 
43 #include "DatabaseEnv.h"
44 #include <string>
45 #include <set>
46 #include <map>
47 
48 namespace rbac
49 {
50 
52 {
59  // 7 - reuse
60  // 8 - reuse
61  // 9 - reuse
64  // 12 - reuse
104 
105  // Free space for core permissions (till 149)
106  // Roles (Permissions with delegated permissions) use 199 and descending
290  // 383 reuse
650  // 743 - 752 reuse
652  // 754 - 756 reuse
654  // 758 - 760 reuse
670  RBAC_PERM_COMMAND_DEBUG_PHASE = 776, // only 4.3.4
724  // 830 reuse
731 
732  // custom permissions 1000+
734 };
735 
737 {
744 };
745 
746 typedef std::set<uint32> RBACPermissionContainer;
747 
749 {
750  public:
751  RBACPermission(uint32 id = 0, std::string const& name = ""):
752  _id(id), _name(name), _perms() { }
753 
755  std::string const& GetName() const { return _name; }
757  uint32 GetId() const { return _id; }
758 
760  RBACPermissionContainer const& GetLinkedPermissions() const { return _perms; }
762  void AddLinkedPermission(uint32 id) { _perms.insert(id); }
764  void RemoveLinkedPermission(uint32 id) { _perms.erase(id); }
765 
766  private:
768  std::string _name;
769  RBACPermissionContainer _perms;
770 };
771 
784 {
785  public:
786  RBACData(uint32 id, std::string const& name, int32 realmId, uint8 secLevel = 255):
787  _id(id), _name(name), _realmId(realmId), _secLevel(secLevel),
788  _grantedPerms(), _deniedPerms(), _globalPerms() { }
789 
791  std::string const& GetName() const { return _name; }
793  uint32 GetId() const { return _id; }
794 
811  bool HasPermission(uint32 permission) const
812  {
813  return _globalPerms.find(permission) != _globalPerms.end();
814  }
815 
816  // Functions enabled to be used by command system
818  RBACPermissionContainer const& GetPermissions() const { return _globalPerms; }
820  RBACPermissionContainer const& GetGrantedPermissions() const { return _grantedPerms; }
822  RBACPermissionContainer const& GetDeniedPermissions() const { return _deniedPerms; }
823 
846  RBACCommandResult GrantPermission(uint32 permissionId, int32 realmId = 0);
847 
870  RBACCommandResult DenyPermission(uint32 permissionId, int32 realmId = 0);
871 
895  RBACCommandResult RevokePermission(uint32 permissionId, int32 realmId = 0);
896 
898  void LoadFromDB();
899  PreparedQueryResultFuture LoadFromDBAsync();
900  void LoadFromDBCallback(PreparedQueryResult result);
901 
904  {
905  _secLevel = id;
906  LoadFromDB();
907  }
908 
910  uint8 GetSecurityLevel() const { return _secLevel; }
911  private:
913  void SavePermission(uint32 role, bool granted, int32 realm);
915  void ClearData();
916 
926  void CalculateNewPermissions();
927 
928  int32 GetRealmId() const { return _realmId; }
929 
930  // Auxiliar private functions - defined to allow to maintain same code even
931  // if internal structure changes.
932 
934  bool HasGrantedPermission(uint32 permissionId) const
935  {
936  return _grantedPerms.find(permissionId) != _grantedPerms.end();
937  }
938 
940  bool HasDeniedPermission(uint32 permissionId) const
941  {
942  return _deniedPerms.find(permissionId) != _deniedPerms.end();
943  }
944 
946  void AddGrantedPermission(uint32 permissionId)
947  {
948  _grantedPerms.insert(permissionId);
949  }
950 
952  void RemoveGrantedPermission(uint32 permissionId)
953  {
954  _grantedPerms.erase(permissionId);
955  }
956 
958  void AddDeniedPermission(uint32 permissionId)
959  {
960  _deniedPerms.insert(permissionId);
961  }
962 
964  void RemoveDeniedPermission(uint32 permissionId)
965  {
966  _deniedPerms.erase(permissionId);
967  }
968 
970  void AddPermissions(RBACPermissionContainer const& permsFrom, RBACPermissionContainer& permsTo);
971 
973  void RemovePermissions(RBACPermissionContainer& permsFrom, RBACPermissionContainer const& permsToRemove);
974 
982  void ExpandPermissions(RBACPermissionContainer& permissions);
983 
985  std::string _name;
988  RBACPermissionContainer _grantedPerms;
989  RBACPermissionContainer _deniedPerms;
990  RBACPermissionContainer _globalPerms;
991 };
992 
993 }
994 
995 #endif
Definition: RBAC.h:398
Definition: RBAC.h:508
void SetSecurityLevel(uint8 id)
Sets security level.
Definition: RBAC.h:903
Definition: RBAC.h:502
Definition: RBAC.h:92
Definition: RBAC.h:349
Definition: RBAC.h:416
Definition: RBAC.h:514
Definition: RBAC.h:432
Definition: RBAC.h:164
Definition: RBAC.h:743
Definition: RBAC.h:728
Definition: RBAC.h:500
RBACPermissionContainer const & GetDeniedPermissions() const
Returns all the denied permissions.
Definition: RBAC.h:822
Definition: RBAC.h:404
Definition: RBAC.h:409
Definition: RBAC.h:392
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: QueryResult.h:107
Definition: RBAC.h:408
uint32 _id
Definition: RBAC.h:767
uint8 GetSecurityLevel() const
Returns the security level assigned.
Definition: RBAC.h:910
Definition: RBAC.h:424
Definition: RBAC.h:504
Definition: RBAC.h:400
Definition: RBAC.h:501
void AddDeniedPermission(uint32 permissionId)
Adds a new denied permission.
Definition: RBAC.h:958
Definition: RBAC.h:388
Definition: RBAC.h:691
void AddGrantedPermission(uint32 permissionId)
Adds a new granted permission.
Definition: RBAC.h:946
Definition: RBAC.h:421
Realm realm
Definition: World.cpp:3485
Definition: RBAC.h:379
Definition: RBAC.h:155
Definition: RBAC.h:167
Definition: RBAC.h:340
Definition: RBAC.h:53
Definition: RBAC.h:425
void LoadFromDB()
Definition: AddonMgr.cpp:40
Definition: RBAC.h:325
uint8 _secLevel
RealmId Affected
Definition: RBAC.h:987
Definition: RBAC.h:738
Definition: RBAC.h:390
RBACCommandResult
Definition: RBAC.h:736
Definition: RBAC.h:487
Definition: RBAC.h:664
Definition: RBAC.h:441
Definition: RBAC.h:341
Definition: RBAC.h:646
Definition: RBAC.h:666
std::string const & GetName() const
Gets the Name of the Object.
Definition: RBAC.h:755
Definition: RBAC.h:438
Definition: RBAC.h:337
Definition: RBAC.h:177
Definition: RBAC.h:649
Definition: RBAC.h:449
Definition: RBAC.h:427
bool HasGrantedPermission(uint32 permissionId) const
Checks if a permission is granted.
Definition: RBAC.h:934
Definition: RBAC.h:507
RBACPermissionContainer const & GetLinkedPermissions() const
Gets the Permissions linked to this permission.
Definition: RBAC.h:760
Definition: RBAC.h:219
Definition: AccountMgr.h:47
Definition: RBAC.h:319
Definition: RBAC.h:107
Definition: RBAC.h:439
int32 _realmId
Account name
Definition: RBAC.h:986
Definition: RBAC.h:292
Definition: RBAC.h:624
Definition: RBAC.h:783
RBACPermissionContainer _deniedPerms
Granted permissions
Definition: RBAC.h:989
Definition: RBAC.h:194
Definition: RBAC.h:288
Definition: RBAC.h:647
bool HasPermission(uint32 permission) const
Definition: RBAC.h:811
std::string _name
id of the object
Definition: RBAC.h:768
Definition: RBAC.h:280
Definition: RBAC.h:661
RBACPermissions
Definition: RBAC.h:51
Definition: RBAC.h:386
Definition: RBAC.h:403
RBACPermissionContainer _grantedPerms
Account SecurityLevel
Definition: RBAC.h:988
void RemoveGrantedPermission(uint32 permissionId)
Removes a granted permission.
Definition: RBAC.h:952
Definition: RBAC.h:395
Definition: RBAC.h:733
uint32 GetId() const
Gets the Id of the Object.
Definition: RBAC.h:793
bool HasDeniedPermission(uint32 permissionId) const
Checks if a permission is denied.
Definition: RBAC.h:940
std::string const & GetName() const
Gets the Name of the Object.
Definition: RBAC.h:791
Definition: RBAC.h:434
Definition: RBAC.h:207
Definition: RBAC.h:442
Definition: RBAC.h:247
Definition: RBAC.h:370
Definition: RBAC.h:413
Definition: RBAC.h:198
void RemoveLinkedPermission(uint32 id)
Removes a linked Permission.
Definition: RBAC.h:764
Definition: RBAC.h:742
Definition: RBAC.h:510
Definition: RBAC.h:663
Definition: RBAC.h:450
Definition: RBAC.h:57
Definition: RBAC.h:159
Definition: RBAC.h:169
Definition: RBAC.h:459
Definition: RBAC.h:625
Definition: RBAC.h:255
Definition: RBAC.h:405
Definition: RBAC.h:175
Definition: RBAC.h:166
Definition: RBAC.h:443
Definition: RBAC.h:176
Definition: RBAC.h:402
Definition: RBAC.h:411
void AddLinkedPermission(uint32 id)
Adds a new linked Permission.
Definition: RBAC.h:762
Definition: RBAC.h:645
Definition: RBAC.h:415
Definition: RBAC.h:348
Definition: RBAC.h:668
Definition: RBAC.h:662
Definition: RBAC.h:278
Definition: RBAC.h:336
Definition: RBAC.h:412
Definition: RBAC.h:180
uint32 GetId() const
Gets the Id of the Object.
Definition: RBAC.h:757
Definition: RBAC.h:739
Definition: RBAC.h:168
RBACPermission(uint32 id=0, std::string const &name="")
Definition: RBAC.h:751
Definition: RBAC.h:477
int32_t int32
Definition: Define.h:146
Definition: RBAC.h:433
uint32_t uint32
Definition: Define.h:150
Definition: RBAC.h:146
Definition: RBAC.h:279
Definition: RBAC.h:151
std::string _name
Account id
Definition: RBAC.h:985
Definition: RBAC.h:418
RBACPermissionContainer const & GetPermissions() const
Returns all the granted permissions (after computation)
Definition: RBAC.h:818
Definition: RBAC.h:423
Definition: RBAC.h:248
Definition: RBAC.h:748
Definition: RBAC.h:308
Definition: RBAC.h:108
Definition: RBAC.h:741
Definition: RBAC.h:246
Definition: RBAC.h:617
Definition: RBAC.h:509
Definition: RBAC.h:294
Definition: RBAC.h:162
Definition: RBAC.h:665
Definition: RBAC.h:282
int32 GetRealmId() const
Definition: RBAC.h:928
Definition: RBAC.h:274
Definition: RBAC.h:343
Definition: RBAC.h:397
RBACPermissionContainer const & GetGrantedPermissions() const
Returns all the granted permissions.
Definition: RBAC.h:820
Definition: RBAC.h:315
Definition: RBAC.h:436
Definition: RBAC.h:445
Definition: RBAC.h:211
Definition: RBAC.h:378
Definition: RBAC.h:435
Definition: RBAC.h:139
Definition: RBAC.h:430
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: Callback.h:26
Definition: RBAC.h:281
Definition: RBAC.h:420
Definition: RBAC.h:451
Definition: RBAC.h:672
Definition: RBAC.h:324
Definition: RBAC.h:174
Definition: RBAC.h:293
RBACPermissionContainer _globalPerms
Denied permissions
Definition: RBAC.h:990
#define TC_GAME_API
Definition: Define.h:134
RBACPermissionContainer _perms
name of the object
Definition: RBAC.h:769
Definition: RBAC.h:179
Definition: RBAC.h:63
Definition: RBAC.h:644
Definition: RBAC.h:414
Definition: RBAC.h:124
uint8_t uint8
Definition: Define.h:152
Definition: RBAC.h:429
Definition: RBAC.h:202
Definition: RBAC.h:446
void RemoveDeniedPermission(uint32 permissionId)
Removes a denied permission.
Definition: RBAC.h:964
uint32 _id
Definition: RBAC.h:984
Definition: RBAC.h:316
Definition: RBAC.h:671
Definition: RBAC.h:692
std::set< uint32 > RBACPermissionContainer
Definition: RBAC.h:746
Definition: RBAC.h:195
Definition: RBAC.h:339
Definition: RBAC.h:287
Definition: RBAC.h:399
Definition: RBAC.h:406
Definition: RBAC.h:284
RBACData(uint32 id, std::string const &name, int32 realmId, uint8 secLevel=255)
Definition: RBAC.h:786
Definition: RBAC.h:165
Definition: RBAC.h:667
Definition: RBAC.h:422
Definition: RBAC.h:426
Definition: RBAC.h:95
Definition: RBAC.h:375
Definition: RBAC.h:478
Definition: RBAC.h:149
Definition: RBAC.h:55
Definition: RBAC.h:345
Definition: RBAC.h:407
Definition: RBAC.h:113
Definition: RBAC.h:655
Definition: RBAC.h:431
Definition: RBAC.h:440
Definition: RBAC.h:417
Definition: RBAC.h:54
Definition: RBAC.h:377
Definition: RBAC.h:369
Definition: RBAC.h:56