Saving permissions to multidimensional php array

OK, I'm just trying to improve the work with more free classes, etc. in PHP to improve your skills. I have a local test database on my computer, and for the user table, I have a column called "role". I am trying to create a function that is a general function for obtaining permissions for a user, so it does not depend on the specific task they are trying to do.

When a user tries to do something like creating a new forum section, etc., I want to query the database, and if the "role" is a certain value, save the permissions in a multidimensional array, for example:

$permissions = array(
    'forums' => array("create", "delete", "edit", "lock"),
    'users' => array("edit", "lock")
);

Then I want to be able to search this array for a specific permission without typing the following at the beginning of each PHP file after the user submits the form by checking isset ($ var). Therefore, if the user is trying to change the user, I want, if possible, to do something like the following using the class method

if (Class::get_permissions($userID),array($permissionType=>$permission))) {
   // do query
} else {
   // return error message
}

, - ? , , , . "admin", "user" .. , . , php script.

if (Class::get_permissions($userID) == "admin") {
   // allow query
} else {
   // return error
}

, , .

+5
2

, . , $permissions.

public static $permissions = array();

public static function setPermissions($perms)
{
    if (!is_array($perms)) {
        throw new Exception('$perms must be an array.');
    }
    self::$permissions = $perms;
}

public static function hasPermission($section, $action)
{
    if (array_key_exists($section, self::$permissions)
        && in_array($action, self::$permissions[$section])
    ) {
        return true;
    }

    return false;
}

, , var Class::$permissions :

Class::setPermissions($permissions);
// ...
if (Class::hasPermissions($page, $action)) {
    // user has permission
}

, , . , , - , . , $page - "forums", ( $action = 'edit'), Class::hasPermission() true.

+3

... .

@corey , , . LoginCommand, , , .

, . , . . , , , , .

. PHP

, , - , . , , , .

+2

All Articles