Programming Theory
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesProgramming Theory

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old March 15th, 2006, 05:32 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
implementing priveleges

i'm just curious how other people implement priveleges in their code. do you do a simple conditional, such as
php Code:
Original - php Code
  1.  
  2. if(isset($_SESSION['user'])) {
  3.     echo '<a href="members.php">members page</a>';
  4. } else {
  5.     echo 'you must login to access the members page';
  6. }
  7.  
  8. // or...
  9.  
  10. echo isset($_SESSION['user']) ? '<a href="members.php">members page</a>' : 'you must login to access the members page';

or what? the above is how i've always done it, but the code really starts to get sloppy after building up a lot of links like that, especially with more than one conditional per link.. does anyone have a more, managable way of going about it?

Reply With Quote
  #2  
Old March 15th, 2006, 10:34 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,727 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 1 h 4 m 45 sec
Reputation Power: 6
RE: implementing priveleges

The most I have had is 4 levels of privs (guest 1, user 3, admin 7, super 9) - and I just did a switch. something like:
php Code:
Original - php Code
  1.  
  2. if($_SESSION['log']) === TRUE)
  3. {
  4.   switch($_SESSION['level'])
  5.   {
  6.     case 1:
  7.     case 3:
  8.       $host  = $_SERVER['HTTP_HOST'];
  9.       $uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
  10.       $extra = 'error.php?errMes="Insufficent Priviledges"';
  11.       header("Location: http://$host$uri/$extra");
  12.       exit;
  13.       // just in case
  14.       break;
  15.     case 7:
  16.       $access = 'lim';
  17.       break;
  18.     case 9:
  19.       $access = 'full';
  20.       break;
  21.     default:
  22.       $host  = $_SERVER['HTTP_HOST'];
  23.       $uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
  24.       $extra = 'login.php?errMes="Please Log In"';
  25.       header("Location: http://$host$uri/$extra");
  26.       exit;
  27.   }
  28. }
  29. else
  30. {
  31.   $host  = $_SERVER['HTTP_HOST'];
  32.   $uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
  33.   $extra = 'login.php?errMes="Please Log In"';
  34.   header("Location: http://$host$uri/$extra");
  35.   exit;
  36. }
  37. // rest of page

And obviously the error and login pages have to be able to handle the passing of error messages.

Reply With Quote
  #3  
Old March 15th, 2006, 10:44 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,727 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 1 h 4 m 45 sec
Reputation Power: 6
RE: implementing priveleges

Hmm - now that I think about it - should have done the access as an int. Then i could have done a simple coditional to see if some areas were displayed (Ex: if $access>=2) Then I wouldn't of had all those &&/|| checks... hmm. need to remember that.

Reply With Quote
  #4  
Old March 29th, 2006, 06:44 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: implementing priveleges

speaking of priveleges, does anyone know of any good tutorials on the subject? i've searched everywhere and found only one which is pretty good:
http://www.sitepoint.com/article/anthology-2-1-access-control

i'm suprised i can't find any others... might be a good one for someone on codewalkers (not me) to write. ;)

Reply With Quote
  #5  
Old March 30th, 2006, 05:29 PM
Andrew's Avatar
Andrew Andrew is offline
Moderator
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,943 Andrew User rank is Private First Class (20 - 50 Reputation Level)Andrew User rank is Private First Class (20 - 50 Reputation Level)  Folding Points: 2429 Folding Title: Novice Folder
Time spent in forums: 4 Days 5 h 8 m 21 sec
Reputation Power: 3
RE: implementing priveleges

I could write it i suppose (was in the process of writing a ssh tutorial, how to use it fairly securely in php. But i kinda ran out of energy with it).

I generally create a function like the one described below (havent got an example to hand)

Code:
check_auth($user_id,$user_ip,$zone_id)

Check_auth look's up the user permissions for the user on a certain IP (help's to prevent session hijacking but thats not important here i dont think) then it look's up the permissions needed to view a certain zone (zone_id) from a zone table. If the user has insufficient permissions then they get forwarded back to the login page.



Thats how i do it and then just drop that in either static or dynamic pages (at the top) so they are either allowed to view the page or kicked back to the login page with an error.

Reply With Quote
  #6  
Old March 30th, 2006, 09:23 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,727 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 1 h 4 m 45 sec
Reputation Power: 6
RE: implementing priveleges

+1 for you Andrew

Reply With Quote
  #7  
Old April 14th, 2006, 12:26 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: implementing priveleges

Using directly $_SESSION for handling authentication is a security issue and should be avoided.In my practice the best way to handle permissions is to create and serialize a proper class and pass it through different pages. This way you can keep an eye on all events and to garantee stable authentication handling.
Also for permissions I would suggest an idea with a bit of code. The code is just for example.

<?
/**
@abstract :
Bit comparison base permissions class
@author : Angel Kostadinov(angel@support-24.com)
*/
class permissions
{
private $previleges;
public function __construct( $previleges = 0 )
{
$this->previleges = intval($previleges);
}
public function haveAccess($access = 0)
{
if ($access % 2) return false; // Only powers of 2 are allowed
if (intval($this->previleges) & intval($access)) return true;
else return false;
}
}
/**
@abstract : Basic Class User
*/
class user
{
public $access;
public function __construct(permissions $access)
{
if ($access instanceof permissions )
$this->access = $access;
else $this->access = new permissions(0); // Default no permissions to anywhere
}
}

/**
@example :
Creating user with permission to access pages
2,4,8 2+4+8 = 14
pages like 1,16,32 will not be accessible by that user
*/
$user = new user(new permissions(14));
if ($user->access->haveAccess(2)) print 'yes';
else print 'no';

Reply With Quote
  #8  
Old April 21st, 2006, 09:07 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: implementing priveleges

Quote:
Using directly $_SESSION for handling authentication is a security issue and should be avoided.

would you care to elaborate on that statement?

Reply With Quote
  #9  
Old April 22nd, 2006, 07:14 AM
pickleman78 pickleman78 is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Dallas,TX,USA
Posts: 582 pickleman78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to pickleman78
RE: implementing priveleges

Ya, I was wondering the same thing... There was an article in the in secure online magazine about sessions, and injecting session ID's and such, but I don't really know if thats any sort of relation.... And I guess that also makes me wonder how you serialize the class and pass it from page to page without sessions...

Reply With Quote
  #10  
Old July 13th, 2006, 12:18 AM
dschreck dschreck is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 44 dschreck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 53 sec
Reputation Power: 2
RE: implementing priveleges

I also use an auther Class to check the users group level.

usually like so:
php Code:
Original - php Code
  1.  
  2. <?php
  3. require_once('../includes/Auther.php');
  4. $auth = new Auther();
  5. if(!$auth->authenticate(3)) { /* run another check to see if they just lack access, or have been timed out */ }
  6.  
  7. /* load module and continue on with business */
  8. ?>


Usually have two tables, one with the access levels, and the other with the actual user's. This way everytime they hit up a new page they have to:
a) be currently authed
b) have proper access
c) their user account isnt suspended/deactivated midway through their session.

also allows for a 'waterfall' type of affect, so that users with access levels higher or lower could have different options or less.

The upside to this is that the $_SESSION information is validated everytime, so spoofing session data becomes more difficult. add in some extra security checks for validating that the information is in propper formating and to avoid sql injection attacks, and i'd call it a pretty sound security system (as far as online security goes... which is nill )

Reply With Quote
  #11  
Old July 13th, 2006, 04:57 AM
nazly nazly is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Colombo,SriLanka
Posts: 1,325 nazly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 18 sec
Reputation Power: 3
Send a message via Yahoo to nazly
RE: implementing priveleges

Quote:
or what? the above is how i've always done it, but the code really starts to get sloppy after building up a lot of links like that, especially with more than one conditional per link.. does anyone have a more, managable way of going about it?


The method I use is something close to the tutorial u posted. Having all the links and access levels within the code tends to get really messy and its difficult to modify the code for each new application I start to write. I use MySQL tables to sort out the issues and the code uses sessions to identify the user.

I use six tables to sort this out. I won't go indetail. Will try to give u the basic idea.

usmgt_application
Stores the Applications that uses this database. Can have multiple applications using the same auth system.

usmgt_activity
Stores all the activities, simply the links. Here there is a field to identify which application the activity belongs to.

usmgt_user
Stores all the username, password etc..

usmgt_group
Stores all the group names

usmgt_usergroup
User <-> Group relationship table

usmgt_groupactivity
Group <-> Activity relationship table

So this is what the code does.
I have a login script that authenticates the user by querying the usmgt_user table. Upon authentication the code checks which groups he belongs to and the it grabs the activities that the user is allowed to access. So I generate the links using the allowed activity list. So when they click a link I pass the activity id via GET and do a validation whether that id is an allowed activity to make sure that the user doesn't play around with the GET variable. If its an allowed activity I simply include the php file which will be in the name of 'activity_<id goes here>' stored in a seperate directory called 'includes'.

This works really well for me coz if I want to add a new activity I don't have to mess with the code. Simply add the activity to the usmgt_activity table and which application it belongs to. Add the groups that will be allowed to access this activity in the usmgt_groupactivity table. Then I have to save the php script for this activity in the includes directory with a prefix 'activity' folowed by an underscore and the activity id. This way in these activity scripts I don't need to write auth checks on top. Maintenance wise really easy.

The best part is it allows multiple users as well. Adding a user is simply easy. Add the user to the usmgt_user table and add the groups he belongs to on the usmgt_usergroup. I guess that gives a basic idea.

Quote:
might be a good one for someone on codewalkers (not me) to write.

If you guys feel that this method is worth writing a tutorial let me know. I'm not really a good writer though I feel its worth having a go at it. There might be better methods than this but it works for me really efficiently.

Quote:
Using directly $_SESSION for handling authentication is a security issue and should be avoided.

Really haven't looked into this matter. I'm also bit confused after hearing this. More information on this will be great..

Reply With Quote
  #12  
Old July 14th, 2006, 06:02 PM
dschreck dschreck is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 44 dschreck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 53 sec
Reputation Power: 2
RE: implementing priveleges

if you wanna read more about session security, the magazine php | architect has a good right up on it in their secruity corner. It's volume 5, issue 5. Talks about accessing _SESSION data directly etc etc. Read particually the last section titled 'Invisible Session Theft'