Advertisement
i1last

future code, homework, 4.1.1. Access Control

May 16th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <string>
  2. #include <unordered_map>
  3. #include <unordered_set>
  4.  
  5. class AccessControl {
  6. private:
  7.     std::unordered_map<int, std::unordered_set<std::string>> userRoles;
  8.     std::unordered_set<std::string> registeredRoles;
  9.  
  10. public:
  11.     bool HasRole(int id, const std::string& roleName) const {
  12.         auto userIt = userRoles.find(id);
  13.         if (userIt != userRoles.end()) {
  14.             return userIt->second.find(roleName) != userIt->second.end();
  15.         }
  16.         return false;
  17.     }
  18.  
  19.     bool GrantRole(int id, const std::string& roleName) {
  20.         if (registeredRoles.find(roleName) == registeredRoles.end()) {
  21.             return false;
  22.         }
  23.         return userRoles[id].insert(roleName).second;
  24.     }
  25.  
  26.     bool RemoveRole(int id, const std::string& roleName) {
  27.         auto userIt = userRoles.find(id);
  28.         if (userIt != userRoles.end() && userIt->second.find(roleName) != userIt->second.end()) {
  29.             userIt->second.erase(roleName);
  30.             return true;
  31.         }
  32.         return false;
  33.     }
  34.  
  35.     void RegisterRole(const std::string& roleName) {
  36.         registeredRoles.insert(roleName);
  37.     }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement