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 4 of 4
  1. #1
    MoD
    MoD is offline
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Permissions with bitwise operations

    Ok, users in my system can have ONE number in their permissions field.. Namely:

    SUPER_ADMIN = 0;
    ADMIN = 1;
    MODERATOR = 2;
    MENTOR = 3;
    USER = 4;
    GUEST = 5;

    Easy ha? Well... Not exactly.. Heres the entire class
    PHP Code:
    class user
    {
        
    // Rights constants
        
    const SUPER_ADMIN     0;
        const 
    ADMIN            1;
        const 
    MODERATOR     2;
        const 
    MENTOR        3;
        const 
    USER            4;
        const 
    GUEST            5;

        
    /**
        * @access public
        * Loads the user. Must be done in every script
        * that needs user access!
        */
        
    public function load()
        {
        }

        
    /**
        * Checks if the user is logged in...
        * @access public
        * @return bool
        */
        
    public function logged_in()
        {
            if(
    $_SESSION['auth'] === true && $_SESSION['user_id'] != '')
            {
                return 
    true;
            }
            else
            {
                return 
    false;
            }
        }

        
    /**
        * Checks if the user has a specific right
        * @access public
        * @param right - numerical
        * @return true if the user has right, false otherwise
        */
        
    public function is_right($value)
        {
            if(
    $this->rights == $value)
            {
                return 
    true;
            }
            else
            {
                return 
    false;
            }
        }

        
    /**
        * Checks for specifics
        * @access public
        * @return bool
        */
        
    public function is_sadmin()
        {
            if(
    $this->rights == SUPER_ADMIN)
            {
                return 
    true;
            }
            else
            {
                return 
    false;
            }
        }

        
    /**
        * Checks for specifics
        * @access public
        * @return bool
        */
        
    public function is_admin()
        {
            if(
    $this->rights == ADMIN || $this->rights == SUPER_ADMIN)
            {
                return 
    true;
            }
            else
            {
                return 
    false;
            }
        }

        
    /**
        * Checks for specifics
        * @access public
        * @return bool
        */
        
    public function is_mentor()
        {
            if(
    $this->rights == MENTOR || $this->rights == ADMIN || $this->rights == SUPER_ADMIN)
            {
                return 
    true;
            }
            else
            {
                return 
    false;
            }
        }

        
    /**
        * Checks for specifics
        * @access public
        * @return bool
        */
        
    public function is_user()
        {
            if(
    $this->rights == USER || $this->rights == ADMIN || $this->rights == SUPER_ADMIN
            
    || $this->rights == MENTOR)
            {
                return 
    true;
            }
            else
            {
                return 
    false;
            }
        }

    Dont bother about some unfinished functions. So, do you see the problem?

    Logically, if the USER has access to X, then the admins and mods will have access to there too.. Right? Basically, the problem is, the lower you go in the functions that check the user rights ( is_user , is_mod) the more OR's there are... I dont like that.

    Is there an easier method involving bitwise operations?

    Thanks!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    with top down authentication you only need check if the status <= than the required minimum level
    PHP Code:
    <?
        
    public function is_user()
        {
            if(
    $this->rights <= USER)
            {
                return 
    true;
            }
            else
            {
                return 
    false;
            } 
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    In case your access levels aren't as clean as top-down and thus you can't use the simple technique Firepages mentioned, then this thread may help you:

    http://www.codingforums.com/showthread.php?t=117414

  • #4
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what about using a switch statement



    PHP Code:
    switch($permissions) {

            case 
    "1":
            return 
    whatever;
            break;

            case 
    "2":
            
    dostuff();
            break;

            case 
    "3":
            
    dostuff();
            break;

    Last edited by Xer0-; 07-02-2007 at 08:43 AM.


  •  

    Posting Permissions

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