Page 1 of 1

Restricting URI access based on ccap in functions.php ?

PostPosted: September 6th, 2010, 7:09 am
by BoweFrankema
Hi Jason,

I'm really digging into S2Member now, and I want to use it for another project of mine. I've decided that Custom Capabilities are the best solution for my needs, and now I want to use it to protect specific BuddyPress Groups (where support and downloads can be found). So my idea was to do this:

Make a new Custom Capability for every new Premium Group which add that tag that I would like to use to define that membership.

Then go into my themes functions.php file and restrict the access to that group to the member with that custom capability added, like you showed in the video. But since I can't write any PHP I have no idea how to write the code that allows me to block someone from a hard coded URL. In the video example you use Image
for restricting access based on tags or categories.

So what I would like to do is create levels that allows access to a certain group URL only.
but I have no idea to modify it to replace tags/categories with an hardcoded url or a BuddyPress conditonal tag. Any ideas how to achieve this?

Thanks in advance,
Bowe

Re: Restricting URI access based on ccap in functions.php ?

PostPosted: September 7th, 2010, 10:14 pm
by Jason Caldwell
Hi Bowe. Thanks for the great question.

Here is how you would integrate BuddyPress Conditional Tags:
Code: Select all
<?php
add_action 
("template_redirect", "my_custom_capabilities", 1);
function my_custom_capabilities ()
    {
        if (bp_is_group_forum () && !current_user_can ("access_s2member_ccap_forums"))
            {
                header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
                exit ();
            }
    }
?>

So you're just swapping out a native WordPress® Conditional tag like has_tag(), and instead using a specialized BuddyPress Conditional Tag, like: bp_is_group_forum(). Other BuddyPress Conditional Tags are documented here: http://codex.buddypress.org/developer-d ... late-tags/

--------------------------------

Now, if instead; you wanted to bypass all Conditional Tags, and go straight to the URI itself, you would test word fragments against the $_SERVER["REQUEST_URI"]. So something like this:
Code: Select all
<?php
add_action 
("template_redirect", "my_custom_capabilities", 1);
function my_custom_capabilities ()
    {
        if (fnmatch ("/activity*", $_SERVER["REQUEST_URI"]) && !current_user_can ("access_s2member_ccap_activity"))
            {
                header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
                exit ();
            }
        else if (fnmatch ("/forums*", $_SERVER["REQUEST_URI"]) && !current_user_can ("access_s2member_ccap_forums"))
            {
                header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
                exit ();
            }
    }
?>

So here we are testing word fragments against $_SERVER["REQUEST_URI"].

Important to note:
- A URL looks like this: http://domain/path/to/file.php
- A URI is just the path: /path/to/file.php ( and this is what $_SERVER["REQUEST_URI"] represents ).

The function fnmatch() is documented here, and it's very simple.
The asterisk (*) represents a wildcard.
http://php.net/manual/en/function.fnmatch.php

Re: Restricting URI access based on ccap in functions.php ?

PostPosted: September 9th, 2010, 12:14 pm
by BoweFrankema
Jason thank you thank you thank you :D

This work perfectly! I'm working on a Premium BuddyPress Theme with S2Member integration and some blog posts on how to achieve BuddyPress specific things with it.. Seriously this plugin is so powerful it blows my mind.

When the theme is released I'll send you a copy so you can take a look :)

Thanks again!

Re: Restricting URI access based on ccap in functions.php ?

PostPosted: September 9th, 2010, 10:59 pm
by Jason Caldwell
You are VERY welcome Bowe.
Thanks for the kudos!

Re: Restricting URI access based on ccap in functions.php ?

PostPosted: November 26th, 2010, 9:01 pm
by startasocialnetwork
Hey Jason,

I know since this post (and the video) you've added custom capabilities fields to pages/posts to make using ccaps easier. Is the above stratgey still the only way to work with ccaps and URIs, or has this also been improved?

Thanks
Ric

Re: Restricting URI access based on ccap in functions.php ?

PostPosted: November 27th, 2010, 12:48 pm
by startasocialnetwork
Also, can these two strategies be combined?

That is, Restrict access by URI, but also use the buddypress conditionals for fine-tuning (like only letting some people create new groups)?

Re: Restricting URI access based on ccap in functions.php ?

PostPosted: December 1st, 2010, 8:18 am
by svenl77
Hi, I needed to add current_user_can('level_10'), otherwise the admin has no access to custom capability groups.

function my_custom_capabilities ()
{
if(current_user_can('level_10'))
{
return false;
}
...

Re: Restricting URI access based on ccap in functions.php ?

PostPosted: August 25th, 2011, 2:31 am
by peeld
Just digging through S2 and it looks like the above URI example is the way I'm going to need to go; I need to restrict certain BuddyPress groups so users have access only to the group they've paid for (a classroom scenario). When I put that code in my theme's function.php file (theme = custom community pro), it either breaks it or doesn't work :(

Any new suggestions on what I could be doing wrong? I'm happy to submit my functions.php file...

Daisy

Re: Restricting URI access based on ccap in functions.php ?

PostPosted: August 25th, 2011, 5:20 pm
by peeld
Just an update, I've got it all working GREAT, thanks to forum posters for their solutions.

Here's the solution: viewtopic.php?f=36&t=1405

EDIT 11/6/2011: As of BP 1.5 for some reason my code doesn't work anymore, FYI. Trying to troubleshoot.