case ROLE_MANAGER:
{
cout << "MANAGER" << endl;
//Managers have all the rights of admins except delete rights
rights = RIGHT_ALL;
rights = (Rights)(rights | (int)~RIGHT_USER_REMOVE);
break;
};
Basically the code is doing some bitwise operation. Most likely the permission is say stored in a byte of 8 bits and each bit represent some rights. It is like our Linux file permission concept e.g rwxrwxrwx
So in order to understand, the poster must look at the source code and hopefully there is some documentation on how each bit represent what. A simple clue would be RIGHT_ALL and RIGTH_USER_REMOVE should be #define or const int to some hard-coded value.
//Determine the user's role
Roles role = ROLE_UNKNOWN;
if (userName == "bob")
role = ROLE_ADMIN;
elseif (userName == "sue")
role = ROLE_MANAGER;
elseif (userName == "jane")
role = ROLE_USER;
elseif (userName == "pam")
role = ROLE_MANAGER;
else
{
DisplayAccessDeniedMessage();
cout << endl << "Press ENTER to quit" << endl;
cin.ignore();
cin.get();
exit(0);
}
//Determine the user's rights based upon their roles
Rights rights;
switch (role)
{
case ROLE_ADMIN:
{
cout << "ADMINISTRATOR" << endl;
rights = RIGHT_ALL;
break;
};
case ROLE_MANAGER:
{
cout << "MANAGER" << endl;
//Managers have all the rights of admins except delete rights
rights = RIGHT_ALL;
rights = (Rights) (~RIGHT_USER_REMOVE); //CR4 fix bitwise operation and removed or (int)
rights = (Rights) (~RIGHT_ROLE_REMOVE); //CR4 added Role remove to keep managers from removing roles
break;
};
security.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//Defines the rights defined in the system
enum Rights
{
RIGHT_NONE = 0,
RIGHT_USER_ADD = 0x00000001,
RIGHT_USER_MODIFY = 0x00000002,
RIGHT_USER_REMOVE = 0x00000004,
RIGHT_ROLE_ADD = 0x00000010,
RIGHT_ROLE_REMOVE = 0x00000040,
RIGHT_ALL = 0xFFFFFFFF,
};