AccessHandler.php

<?php

namespace Phad\Test;

/**
 * Access class used for testing Phad
 *
 */
class AccessHandler implements \Phad\AccessInterface {

    /** should be callable */
    public mixed $item_initialized;
    /** should be callable */
    public mixed $user_has_role;
    /** should be callable */
    public mixed $can_read_row;

    public bool $allow_edit_blog;

    public function user_has_role(string $role): bool {
        if (isset($this->user_has_role))return ($this->user_has_role)($role);

        /** should accept a single string representing the role (or roles) being checked. 
        * As a suggestion, roles should be like 'guest' or 'admin|moderator|vip' for multiple */
        if (isset($_GET['user'])&&$role == $_GET['user'])return true;
        return false;
    }

    public function main_msg($ItemInfo){
        echo "This is my custom deletion response. I don't care if deletion succeeded. Id was ".$ItemInfo->args['id'];
        return false;
    }

    public function never_allow($ItemInfo){
        return false;
    }

    public function can_submit($ItemInfo, $ItemRow){
        if (isset($_GET['deny_access'])&&$_GET['deny_access']=='true')return false;
        return true;
    }

    public function permit_me($ItemInfo, $data_node){
        if ($_GET['permit_me']=='true')return true;
        return false;
    }

    public function can_read_row(array $ItemRow, \Phad\ItemInfo $ItemInfo, string $ItemName): bool {
        //TODO: This method is required
        if (isset($this->can_read_row))return ($this->can_read_row)($ItemInfo);
        if (!isset($_GET['title']))return true;

        if ($ItemRow['title'] == $_GET['title']){
            return true;
        }

        return false;
    }

    /** should accept a single ItemInfo object  */
    public function item_initialized(\Phad\ItemInfo $ItemInfo): void{
        if (isset($this->item_initialized)){
            ($this->item_initialized)($ItemInfo);
        }
    }

    public function allow_edit_blog(){
        return $this->allow_edit_blog;
    }
    /**
     * When `call:some_method`, this method is called
     *
     * @param $args in a statement like `call:some_thing`, this is the portion to the right of the colon, so it is `some_thing`
     * @param $args_list is the rest of the arguments, and different callers invoke this differently.
     * 
     */
    public function call(\Phad\Caller $caller, string $what, ?\Phad\ItemInfo $ItemInfo, ...$args_list): bool {
        if (method_exists($this, $what))return $this->$what($ItemInfo, ...$args_list);
        else throw new \BadMethodCallException("Access Handler `".get_class($this)."` has no handler for '$what'");
    }


}