function check($Permission, $UserID = null)
    {
        if ($UserID === null)
            throw new \RbacUserNotProvidedException ("\$UserID is a required argument.");

        // convert permission to ID
        if (is_numeric ( $Permission ))
        {
            $PermissionID = $Permission;
        }
        else
        {
            if (substr ( $Permission, 0, 1 ) == "/")
                $PermissionID = $this->Permissions->pathId ( $Permission );
            else
                $PermissionID = $this->Permissions->titleId ( $Permission );
        }

        // if invalid, throw exception
        if ($PermissionID === null)
            throw new RbacPermissionNotFoundException ( "The permission '{$Permission}' not found." );

        if ($this->isSQLite())
        {
            $LastPart="AS Temp ON ( TR.ID = Temp.RoleID)
 							WHERE
 							TUrel.UserID=?
 							AND
 							Temp.ID=?";
        }
        else //mysql
        {
            $LastPart="ON ( TR.ID = TRel.RoleID)
 							WHERE
 							TUrel.UserID=?
 							AND
 							TPdirect.ID=?";
        }
        $Res=Jf::sql ( "SELECT COUNT(*) AS Result
            FROM
            {$this->tablePrefix()}userroles AS TUrel

            JOIN {$this->tablePrefix()}roles AS TRdirect ON (TRdirect.ID=TUrel.RoleID)
            JOIN {$this->tablePrefix()}roles AS TR ON ( TR.Lft BETWEEN TRdirect.Lft AND TRdirect.Rght)
            JOIN
            (	{$this->tablePrefix()}permissions AS TPdirect
            JOIN {$this->tablePrefix()}permissions AS TP ON ( TPdirect.Lft BETWEEN TP.Lft AND TP.Rght)
            JOIN {$this->tablePrefix()}rolepermissions AS TRel ON (TP.ID=TRel.PermissionID)
            ) $LastPart",
            $UserID, $PermissionID );

        return $Res [0] ['Result'] >= 1;
    }