• I am using UAM to restrict access to some bbPress forums to only logged-on users.
    I send an email notification of new topics or replies, which includes a link to the topic or reply. Snag is, if the user follows the link to a restricted topic when they are not logged on, they get a 404 (correctly). I want, instead, to invite them to log in and then send them on to the topic.

    I have the following code:

    function ovni_force_login() {
           // force login if trying to access something requiring user to be logged in.
    	global $post;
    
    	echo 'Posts: $post ID ' . $post->ID . ' Type: ' . $post->post_type . ' access: ' . checkObjectAccess($post->post_type, $post->ID);
    	return;  // just during debug
    
    	if (!is_user_logged_in() && checkObjectAccess($post->post_type, $post->ID)){
    		auth_redirect();  // invites login and then takes them to the page
    		}
    	}
     add_action('get_header', 'ovni_force_login');

    The echo statement is for debugging. Before I added the checkObjectAccess bit, it was displaying the post ID and post type correctly, for the page I am landing on.

    I have now added the call to checkObjectAccess, which is a public function within UAM, in an attempt to check whether the page is accessible. If user is not logged in and the page is not accessible, I would use auth_direct() to get them to log on and then take them to the page.

    This code gives Fatal error: Call to undefined function checkObjectAccess().

    My research suggests I may need to call this as a method of an object?
    $this->checkObjectAccess() or some such. But I am not up to speed on OOP and classes.

    Can SKS help me out here please? I think I am close to a solution to my problem after days of working on it. Thanks.

    https://www.ads-software.com/plugins/user-access-manager/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Antipole

    (@antipole)

    I have a good-enough solution to my need, which I copy below for information. With this, if any not-logged-in user attempts to access a page not available because of UAM, they get the login form and, after log in, get sent on to the requested page. If that user not authorised for that page, get 404.

    Only downside is that any casual attempt to reach a random page gets the login invite first, rather than an immediate 404.

    function ovni_force_login() {
     	// if user is not logged on and target page not reachable, force login on the way
     	global $post;
    
    	if (!is_user_logged_in() && !$post->ID) auth_redirect();
    	}
    add_action('get_header', 'ovni_force_login');
    Thread Starter Antipole

    (@antipole)

    I have discovered that the above code invokes a login request whenever accessing a virtual page. This prevents a user registering. The following version contains an array of pages which are protected from the demand too login and which allows registration.

    function ovni_force_login() {
     	// if user is not logged on and target page not reachable, force login on the way
     	global $post;
    
     	$let_in_unlogged = array(	// array of pages to be allowed access even when not logged in
    	'/user-registration/',		// registering
    	'/activate'				// following activation link
    	);
    
    	if (is_user_logged_in() || $post->ID) return;	// if logged in or going to accessible page let them through
    	foreach ($let_in_unlogged as $thisone){
    		if (strpos($_SERVER['REQUEST_URI'], $thisone) !== false) return;	// let them past to these pages
    		}
    	auth_redirect();	// otherwise demand login and then send on to page
    	}
    add_action('get_header', 'ovni_force_login');

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Checking if can access post – help requested’ is closed to new replies.