Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    short of permission

    i wanna write permission table like vbulletin (with 6 level) how can i write following shorter.contions1-7 is in db. it is read from db like vbulletin
    PHP Code:
    if (condition1){
        if (
    condition2){
            if (
    condition3){
                if (
    condition4){
                    if (
    condition5){
                        if (
    condition6){
                            if (
    condition7){
                            
    ////////////////
                            ///and  codes //
                            //////////////
                            
    }
                        }
                    }
                }
            }
        }


  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This isn't how the vbulletin tracks permissions. Long ago, the common approach was a single field represents a single privilege:
    PHP Code:
    if ($user['canedit']) ...
    if (
    $user['candelete'])...
    etc 
    But long ago we changed this approach for a more dynamic one using bitwise values. PHP's maximum integer size will limit you to 31 possible privileges assuming 0 is invalid. This allows you to dynamically change you're privilege sets to add / modify / remove whats available easily, as well as group available options:
    PHP Code:
    // We'll just use some defines, customize as you see fit:
    define('CAN_EDIT'1);
    define('CAN_DELETE'2);
    define('CAN_MODERATE'4);

    // $user is extracted from the db, 'permissions' is an bitwise integer combination
    if (($user['permissions'] & (CAN_EDIT CAN_MODERATE)) <> 0)
    // Indicates that the user has the privilege to edit or they can moderate (or both) 
    The above would indicate is a user has the privilege to EITHER edit OR moderate they can do so. With you're nested if statements, you would instead check using bitwise XOR instead of AND in conjunction with all permissions that need to be satisfied:
    PHP Code:
    if (($user['permissions'] ^ (CAN_EDIT CAN_DELETE CAN_MODERATE)) <> 0)
    {
        
    // Indicates that a user is privileged for CAN_EDIT AND CAN_DELETE AND CAN_MODERATE

    The trickiest part about this approach is the limitation in the number of bits available. As mentioned, PHP is capped at 2^31 bits for its integer size since there is no way to control unsigned integers, so that limits you to 31 privileges. Control it with grouping; have a group for what the user can change in their own control panel, another group for global control, another one for say, news posting privileges, etc.

    For more information on bitwise operations, refer to this page: http://php.net/manual/en/language.operators.bitwise.php
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Location
    127.0.0.1
    Posts
    348
    Thanks
    26
    Thanked 40 Times in 39 Posts
    Quote Originally Posted by Fou-Lu View Post
    But long ago we changed this approach for a more dynamic one using..
    really!! OMG! God Emperor!
    Blog Charity:Water
    WhatisWrongWith.me/tagnu - Send me anonymous feedback.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •