Access resource data from snippet - Resource Group

Good evening.

I’m using a form that after submit take the user to a page with a snippet.
in this snippet I’m trying to retrieve some information from an specific page (resource), I need to read some fields but the most important is the resource group where that page belongs to assign the same resource group to the user that is filling the form, so that resource can be accessed.

SNIPPET:

$page = $modx->getObject(‘modResource’, $courseId);
$resources = $page->getOne(‘ResourceGroupResources’);
$resId = $resources->get(‘document_group’);
$user->joinGroup($resId);

The problem is that I can’t access the resource data of the page because it belongs to another resource group, but I need the data to assign the resource group to the user to access the resource.

This is a dichotomy, what can I do? any advice please?

Thanks!

$user->joinGroup() will add the user to a User Group, not a Resource Group. In fact, users can’t ever be added to Resource Groups.

I’m not sure I understand what you’re trying to do, but I think what you want is to add the User to the User Group that has access to the Resource Group.

Sorry for the mess.

I have an user with access permission: Member
That user has access to a page with resource Group: Member
In that page (Page 1) there is a form, the user fill the form and send it, after submit it’s redirected to a page with empty template where exist an snippet to make some processes.
One of the process is read data from another page (page 2) that belong to a Resource Group called: Level2 (it does not belong to Member).
The problem is that the snippet is not reading the data from page 2, because the user has not permissions to access that page.
the question is, how can I read the data from the page 2 via snippet?

hope that is clear Bob, thanks for your help!

To resume, How I can’t get data from a resource that belong to an specific resource group? using a anonymous user or an user without permissions to access that resource?

If in theory this is just a request to the database, why I can’t access to the resource data?:

$page = $modx->getObject('modResource', $courseId);

I’m trying to print the error but I’m not getting anything.

try{
    $page = $modx->getObject('modResource', $courseId);
} catch(Exception $e) {
    $modx->log(modX::LOG_LEVEL_ERROR, $e);
    return false;
}

What am I missing?. Thanks!

You can probably circumvent the permission check with your own query

$query = $modx->newQuery('modResource');
$query->where(array('id' => $courseId));
$query->select(array('content' => 'content'));
if ($query->prepare() && $query->stmt->execute()) {
    $result = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
    if ($result && count($result) == 1){
        $content = $result[0]['content'];
    }
}

Not sure though, why it has to be protected at all then…

The getObject() method doesn’t throw an error or an exception if the object isn’t found. It returns null.

 $page = $modx->getObject('modResource', $courseId);

if (!$page) {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Resource Not found with Id: ' . $courseId);
    return false;
}

OK, I’m getting the error:

[2020-06-04 19:17:10] (ERROR @ /home/user/public_html/core/cache/includes/elements/modsnippet/83.include.cache.php : 54) Resource Not found with Id: 18

Obviously it’s because the Resource Group where this resource belong.
How can I access to the Resource then?

I though there was not restrictions to access via xpdo database request.

can you please help me?
Thanks!

Are you absolutely sure that there’s a resource with the ID 18?

Yea off course.
If I remove all the Resource Group from the ID 18
Or I Add the same Access Permission that the Resource have in Resource Group to the user it works.

The question is, why I can;t access the resource via Xpdo if the logged user has not access to the Resource Group?

When you call $modx->getObject() then modAccessibleObject::checkPolicy() is called to checked your permissions. So you can’t use getObject() if your user hasn’t the rights to access the resource.

Because xPDO enforces the permissions on the objects. That’s doing exactly what it’s supposed to do.

You can try giving anonymous/other usergroups load permission for the resource group so that it can be accessed through xPDO. If I recall correctly, being able of seeing the resource in the front-end requires list and view permissions, so load permissions allows the object to be loaded without exposing the resource itself to visitors.

Excellent mate, problem solve adding load only on the Resource group access.

I just can;t find any documentation where the xpdo is restrictec by permissions, but problem solve!

Thanks you very much!

1 Like

It can also be bypassed by not using the getObject, but that might be risky with permissions etc

$modx->query("SELECT id, pagetitle, parent, menuindexFROMmodx_site_contentASmodResourceWHERE (modResource.published = 1 ) ORDER BY pagetitle ASC");

see, what @halftrainedharry has posted above

That would be better

1 Like

That would be better indeed, completely overlooked that one :grimacing: