PHP Classes

File: examples.php

Recommend this page to a friend!
  Classes of Stefan Jibrail Froelich   User Roles   examples.php   Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: examples
Class: User Roles
Manage the roles of users stored as bit masks
Author: By
Last change:
Date: 12 years ago
Size: 4,035 bytes
 

Contents

Class file image Download
<pre>
< ?php


/*
    Before we start, we must create some roles. We need to know how roles
    are defined. Roles can be defined by name. This name is mostly the name
    of the activity that is done.
   
        Let us assume we have a blog in which we can write,
        read, edit and delete posts. This will be difined as
            array('read', 'write', 'edit', 'delete',);
   
    Also, certain roles can be combined with other roles to create new
    roles. These roles are usually named after the group and are defined as an
    associative array.
   
        Say ordinary users, moderators and admins. Each group
        is allowed to perform only certain tasks.
            array(
                'user' => array( 'read', 'write',),
                'mod' => array( 'read', 'write', 'edit',),
                'admin' => array( 'mod, 'delete',),
            );
               
        The final array will be
                array(
                    'read',
                    'write',
                    'edit',
                    'delete'
                    'user' => array( 'read', 'write',),
                    'mod' => array( 'read', 'write', 'edit',),
                    'admin' => array( 'mod', 'delete',),
                );
               
    There are a few things to note here. Within the role combos, you can
    list only and any roles that have come before it. This includes single
    roles like 'read' and 'write', and role combos defined before it. Like
    in admin, we have mod as a role. It could easily be " 'read', 'write',
    'edit', " instead.
   
    This array can be put in 'userroles_config.php' or passed to the
    constructor, whichever you wish.
*/
        //include the file
        include 'userroles.php';

        //create an instance
        //this tries to include 'userroles_config.php' and use the
        //roles defined in it
        $role = new UserRoles;
       
        //this includes the config file and merges the array
        //which has been passed to the constructor
        //any roles in the array will overwrite those defined in
        //the config file
        $role = new UserRoles(array('read'));
       
        //this passes the roles array and asks that the config
        //file not be used by passing 'FALSE' as the second parameter
        $role = new UserRoles(
                    array(
                            'read', 'write', 'edit', 'delete',
                            'user' => array( 'read', 'write', ),
                            'mod' => array( 'read', 'write', 'edit', ),
                            'admin' => array( 'mod', 'delete', ),
                        ), FALSE);
       
        //we need to get the role called 'mod' to assign to a user
        echo 'mod : ' . $role->get_role('mod') . '<br>';
        //assuming we need to add a new role called 'teach'
        $role->set_role('teach');
        //now we need a new role where a 'mod' can teach
        echo 'mod + teach : ' . $mod_teach = $role->get_role_combo(array('mod', 'teach')) , '<br>';
        //maybe we need to save the new role
        //we just pass the role value as the second param to UserRoles::set_role()
        //please do not pass random values since this can cause serious problems
        //if your values are not well calculates. make sure you use only vals suppliesd
        //by the class
        $role->set_role('mod_teach', $mod_teach);
        //let us assume you gave a user the ability to 'read', 'write' and 'edit' posts
        $user_role = $role->get_role_combo( array('read', 'write', 'edit',) );
        //you late on want to check if the user has the role/permission to edit a post
        echo 'User has an "edit" role : ' , ($role->has_role($user_role, 'edit')) ? 'true' : 'false' , '<br>';
        //you later on want to check if the user has the role/permission to delete a post
        echo 'User has a "delete" role : ' , ($role->has_role($user_role, 'delete')) ? 'true' : 'false', '<br>';
       
        //instead of some complicated database role system, you have a very simple bitmask based one
        //and all you need to do now is create a single column for the user where you store his given role
       
        ##//..please note that you can only check if a user can perform a single role.
        ##//..trying to check with a role combo will lead to unpredictable results.
        ##//..the ability to save combos is only for the ease it allows in retrieval but not for
        ##//..crosschecking.
       
        ##//..the limit on the number of single roles is unknown to me.
        ##//..it might be around 16 - 32, but I am unsure.