To make accounts Admin
- RogueTomboy
- Posts: 61
- Joined: Sun May 22, 2011 3:42 pm
To make accounts Admin
What would be the best way of going about making certain accounts Admin accounts for a game?
Like I would want my account and my partners to be the BIG BOSS accounts with lots of lovely powers and then we could assign moderators later on. I'm not sure how to go about making it so upon log in it would recognize that so and so is a certain rank.
			
			
									
						
							Like I would want my account and my partners to be the BIG BOSS accounts with lots of lovely powers and then we could assign moderators later on. I'm not sure how to go about making it so upon log in it would recognize that so and so is a certain rank.
Need art?  Commissions are always open!
http://www.ladytankstudios.com
Currently addicted to:

Collect & Explore for pets
			
						http://www.ladytankstudios.com
Currently addicted to:

Collect & Explore for pets
Re: To make accounts Admin
Add a field in your database users table - call it "admin" or something.  Set it to "True" or "Yes" on the people that you want to be admins.  Then have your code check to see if a user is an admin before letting them do something critical.
			
			
									
						
										
						Re: To make accounts Admin
I'd make a field called 'rights' in your users table, type char and add one to the length for every section in your system, this would depend on how complex your system needs to be.
The database then contains a string for the rights of every user.
For every section you use either letters or numbers.
The default would be 'u' or 1 for a user.
For a moderator a 'm' or 2.
For an admin 'a' or 3. etc
Then I would define which character number in this string complies to which section in my system.
So if I had four sections, a user system, a forum system, a chat system and a clan system. The first character would comply to people who have what control over the user system, the second over the forum system, the third over the chat and the fourth over the clan system.
If I wanted to make one of my users a moderator over the forum system I would change the value of his row to:
umuu
If I were a super admin, meaning I had rights to everything, my string would look like:
aaaa
whereas if I were a user in every system it would only be
uuuu
this also makes it easier to ban people from certain areas. Like say I don't want a user to access the chat system anymore, I'd set his string to
uubu
An alternative would be to make a separate table, with pretty much the same principle, only relate the row to a users id, this would help save storage in your database, but would be a lot of extra work. If a user is not in this table he would simply be a user.
Hope this makes sense
			
			
									
						
							The database then contains a string for the rights of every user.
For every section you use either letters or numbers.
The default would be 'u' or 1 for a user.
For a moderator a 'm' or 2.
For an admin 'a' or 3. etc
Then I would define which character number in this string complies to which section in my system.
So if I had four sections, a user system, a forum system, a chat system and a clan system. The first character would comply to people who have what control over the user system, the second over the forum system, the third over the chat and the fourth over the clan system.
If I wanted to make one of my users a moderator over the forum system I would change the value of his row to:
umuu
If I were a super admin, meaning I had rights to everything, my string would look like:
aaaa
whereas if I were a user in every system it would only be
uuuu
this also makes it easier to ban people from certain areas. Like say I don't want a user to access the chat system anymore, I'd set his string to
uubu
An alternative would be to make a separate table, with pretty much the same principle, only relate the row to a users id, this would help save storage in your database, but would be a lot of extra work. If a user is not in this table he would simply be a user.
Hope this makes sense
Fighting for peace is declaring war on war. If you want peace be peaceful.
			
						Re: To make accounts Admin
Just to expand on this idea (this is the kind of thing i would do aswell)Chris wrote:I'd make a field called 'rights' in your users table, type char and add one to the length for every section in your system, this would depend on how complex your system needs to be.
The database then contains a string for the rights of every user.
For every section you use either letters or numbers.
The default would be 'u' or 1 for a user.
For a moderator a 'm' or 2.
For an admin 'a' or 3. etc
Then I would define which character number in this string complies to which section in my system.
So if I had four sections, a user system, a forum system, a chat system and a clan system. The first character would comply to people who have what control over the user system, the second over the forum system, the third over the chat and the fourth over the clan system.
If I wanted to make one of my users a moderator over the forum system I would change the value of his row to:
umuu
If I were a super admin, meaning I had rights to everything, my string would look like:
aaaa
whereas if I were a user in every system it would only be
uuuu
this also makes it easier to ban people from certain areas. Like say I don't want a user to access the chat system anymore, I'd set his string to
uubu
An alternative would be to make a separate table, with pretty much the same principle, only relate the row to a users id, this would help save storage in your database, but would be a lot of extra work. If a user is not in this table he would simply be a user.
Hope this makes sense
If you store privilages as a long string, deliminate them with something (a comma, or pipe etc) this was you can explode the data for easier access to it all)
New Site Coming Soon! Stay tuned 
			
						
Re: To make accounts Admin
It might be easier on the eye, but no will not be easier on the server. Exploding things into an array will take up a lot more memory, much needed memory if you have over 1000 users online at one time. Using a simple substr() function would be much quicker. Or as we only need to select one character in PHP 5 we can simply just select the character in the string using square brackets.
Here's an example using explode:
Here's an example using substr:
Or in PHP 5+:
			
			
									
						
							Here's an example using explode:
Code: Select all
function getRights($sys, $str)
{
    $arr = explode(',', $str); // generating an unnecessary array
    return $arr[$sys];
}
echo getRights(0, 'a,u,u,u'); //a
 Code: Select all
function getRights($sys, $str)
{
    return substr($str, $sys, 1);
}
echo getRights(0, 'auuu'); //a
 Code: Select all
function getRights($sys, $str)
{
    return $str[$sys];
}
echo getRights(0, 'auuu'); //a
 Fighting for peace is declaring war on war. If you want peace be peaceful.
			
						Re: To make accounts Admin
Ok fair point, it might be easier on the load, but its not as flexable as far as i can tell.
If you want to edit the permissions of a user without messing about to much. I could be wrong however.
You can explode the string, change the appropriate permission then implode the string again before resaving it.
If you know an effective way without exploding it, might be worth showing that aswell
			
			
									
						
							If you want to edit the permissions of a user without messing about to much. I could be wrong however.
You can explode the string, change the appropriate permission then implode the string again before resaving it.
If you know an effective way without exploding it, might be worth showing that aswell

New Site Coming Soon! Stay tuned 
			
						
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: To make accounts Admin
You just have to be careful, and don't spell out exactly what it is in the database. I would not actually add an "admin" Boolean field to the database. The reason is because if someone gets access to your database, that is going to be the first thing they go after. But if the field is called something like "status" and the fields contain either a senseless number or a string like Chris suggested, they would also need access to your scripts to see what it is used for. Obviously they could still cause plenty of damage in your database, but it would likely be immediately noticeable (and thus immediately reversible), whereas someone secretly changing themselves to an admin would be much worse because the damage may not be caught for weeks.
			
			
									
						
							The indelible lord of tl;dr
			
						Re: To make accounts Admin
again, sorry but I'd have to disagree:Torniquet wrote:If you want to edit the permissions of a user without messing about to much. I could be wrong however.
Exploding and imploding
Code: Select all
function updateRights($sys, $to, $str)
{
    $arr = explode(',', $str); // again unnecessary load
    $arr[$sys] = $to;
    return implode(',', $arr); // and looping
}
echo updateRights(0, 'a', 'u,u,u,u'); //a,u,u,u
 Code: Select all
function updateRights($sys, $to, $str)
{
    return substr_replace($str, $to, $sys, 1);
}
echo updateRights(0, 'a', 'uuuu'); //auuu
 Code: Select all
function updateRights($sys, $to, $str)
{
    $str[$sys] = $to;
    return $str;
}
echo updateRights(0, 'a', 'uuuu'); //auuu
 Fighting for peace is declaring war on war. If you want peace be peaceful.
			
						Re: To make accounts Admin
fair play chris.
I never thought about it like that
			
			
									
						
							I never thought about it like that

New Site Coming Soon! Stay tuned 
			
						
Re: To make accounts Admin
Yeah, well exploding still isn't a bad idea, I prefer it to matching when I have to deal with strings that contain information that needs to be separated but have unpredictable lengths.
For example (I'm in no way advising to do things this way), if you wanted to make it as eye friendly as possible, by storing the information in words:
This would best be exploded. But then you might as well go ahead and store your information as JSON or a serialized array.
			
			
									
						
							For example (I'm in no way advising to do things this way), if you wanted to make it as eye friendly as possible, by storing the information in words:
Code: Select all
admin,user,user,moderator
Fighting for peace is declaring war on war. If you want peace be peaceful.
			
						



