noob question

how do i give all rights except user_remove

1
2
3
4
5
6
7
8
9
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;
		};
Im a bit of a noob myself but what are you trying to accomplish?

It looks like you are setting the same variable to a constant and then I have no idea what is going on in the second assignment to the "rights".

What does the single | do?

Sorry, but this is really interesting actually.
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.

Without more details it is hard to help out.

thnx for the hint of bitwise, i did some research and fixed the problem, code is posted
this is for IceThatJaw check out this bitwise website to better understand
http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
	//Determine the user's role
	Roles role = ROLE_UNKNOWN;
	if (userName == "bob")
		role = ROLE_ADMIN;
	else if (userName == "sue")
		role = ROLE_MANAGER;
	else if (userName == "jane")
		role = ROLE_USER;
	else if (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,
};
Last edited on
Topic archived. No new replies allowed.