Restrict page access

This topic contains 9 replies, has 3 voices, and was last updated by  Alex Howes 6 years, 1 month ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #429733

    Alex Howes
    Expired Member
    Post count: 175

    Hello,

    I have a site where workers can create cv listings which employers can look through. “Worker” and “Employer” are both roles with different permissions.

    I don’t want workers to be able to see other workers’ cv listings, only their own. Is there an option for this? Ordinarily I have a plugin which enables me to restrict page access based on role, but this doesn’t work because of the way geodirectory creates pages dynamically.

    The membership plugin I’m using is usersWP.

    Thanks 🙂

    #429740

    Alex Rollin
    Moderator
    Post count: 27815

    Hello,

    GD V1 does not have any features for hide/show content based on roles.

    UsersWP does not yet have these features either. Please feel free to show your interest with a post to UsersWP forum.

    https://userswp.io

    Some members have reported success when using S2 Members for page access.

    #429756

    Alex Howes
    Expired Member
    Post count: 175

    Thanks Alex

    I’m using the following code (found here https://userswp.io/support/topic/restrict-users-from-seeing-each-others-profiles/) to restrict access to only allow users to view their own profile.

    
    
    function _uwp_custom_profile_access( $access, $user ) {
        if ( ! ( current_user_can('manage_options') || ( !empty( $user ) && get_current_user_id() == $user->ID ) ) ) {
            $access = false;
        }
    
        return $access;
    }
    add_filter( 'uwp_profile_access', '_uwp_custom_profile_access', 10, 2 );
    
    function _uwp_custom_profile_access_denied( $user ) {
        ?>
        <div><p><?php echo __( 'You are not allowed to view this profile!' ); ?></p></div>
        <?php
    }
    add_action( 'uwp_profile_access_denied', '_uwp_custom_profile_access_denied', 10, 1 );

    I was wondering if there are hooks equivalent to _uwp_profile_access and _uwp_profile_access_denied that I can use to restrict listing pages in a similar way?

    #429877

    Patrik
    Moderator
    Post count: 1971

    Hi Alex Howes,

    Thet is not possible without customization and there are no such hooks in GD but maybe you can try to use ‘template_redirect’ hook to redirect users on the home page or 404 page when they try to access the listings you specify.

    More info about hook at https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect

    You can override the listing detail page in the theme and put required conditions in that template.

    Let me know if this works for you.

    Regards,
    Patrik

    #429888

    Alex Howes
    Expired Member
    Post count: 175

    Thanks for the suggestion but I’m not sure that would work since I can’t specify a specific url to redirect from. I rather want it to redirect on every listing detail page and since I have lots of them (and they’re created by users) I can’t specify each one.

    #429898

    Patrik
    Moderator
    Post count: 1971

    You can check if the current user is listing owner then allow to view details else redirect to home or 404 page.

    #429900

    Alex Howes
    Expired Member
    Post count: 175

    Yes I understand that bit but is there a way to check if the current page is a geodirectory listing detail page without specifying the url?

    #429903

    Patrik
    Moderator
    Post count: 1971

    Yes, you can use the following function to check if the current page is listing details page or not:

    geodir_is_page('detail')

    So you can use this function to check if the current page is listing detail page and then check if the logged in user is the owner of the current listing or not.

    Regards,
    Patrik

    #429924

    Alex Howes
    Expired Member
    Post count: 175

    Thanks! I’ve almost got it working. Is there a GD function to get the id of the listing owner?

    #429941

    Alex Howes
    Expired Member
    Post count: 175

    Sorry for the last question, I’ve got it working now 🙂 In case anyone else is interested in doing this I’ve posted my code below. Thanks for your help! 🙂

    The post type (CPT) I’m blocking is “gd_cv”, which I’m only allowing the logged in listing owner to view.

    
    
    function cv_detail_redirect()
        {
        $current_user = wp_get_current_user();
        $current_user_roles = $current_user->roles;
        global $post;
      
        if ($post) {
      
        $author_id = $post->post_author;
      
        if( geodir_is_page('detail') && ! is_user_logged_in() && geodir_get_current_posttype() == 'gd_cv')
        {
            wp_redirect( home_url( '/login/' ) );
            die;
        }
        elseif( geodir_is_page('detail') && is_user_logged_in() && geodir_get_current_posttype() == 'gd_cv')
        {
    	    if ( ! ( current_user_can('manage_options') || ( !empty( $author_id ) && get_current_user_id() == $author_id ) ) ) {
    		
            	wp_redirect( home_url( '' ) );
        	    die;
      }
        }
    	}
    }
    add_action( 'template_redirect', 'cv_detail_redirect' );
Viewing 10 posts - 1 through 10 (of 10 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket