Get URI of resource which is not accessible for the logged in user

I have certain pages that require a login, before they should be accessed. The login form itself (from the login extra) comes with a returnUrl field which redirects to a given uri after successful login.

The links to those pages are like this:

 <a href="[[!Personalize? &yesChunk=`@CODE:[[~1619]]` &noChunk=`@CODE:[[~6? &ref=`1619`]]`]]">

So, logged-in users go directly to resource 1619, while not logged-in users get redirected to the login form on resource 6, where the returnUrl is also sent to via the url parameter.

Now, I’m writing a custom snippet that retrieves the id from the url parameter and returns the uri, which then gets placed inside the returnUrl field of the login form.

The issue is the resources, which require a login, are protected through ACLs and it seems like I can’t get the resource object in the snippet. This of course makes sense in some way, but is there a different way to tell the login form to which page to redirect?

Here’s the relevant snippet code:

if (isset($_GET[$name])) {
  $value = filter_var($_GET[$name], FILTER_SANITIZE_STRING);
  
  if (preg_match('/^\d+$/', $value)) {
  	$resourceId = intval($value);
  	$resource = $modx->getObject('modResource', $resourceId);
  	
  	if ($resource) {
	  $uri = $resource->get('uri');
  	}
  }
}

MODX 2.8.4
PHP 8.2

Maybe you can give “load” permission to everyone for the protected resources. (Only “load” and not “view”.)

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.