Support Forum
Hi I need to dynamically allow permission to a forum and it's subforums if certain conditions are met.
Actually I want to assign the user membership to a group and have the permissions for that group get used with respect to forum listings, forums, and accessing topics.
In SP 4, I did this by manipulating variables in $sfglobals and testing conditions in $sfvars.
and settting
$current_user->sfaccess=1;
But I can't figure out how in sp 5
I am able to access and manipulate some variables like so
$spuser = & sp_get_user($current_user->ID);
array_push($spuser->memberships, array ('usergroup_id' => $myid, 'usergroup_name' => $mygroupname, 'usergroup_desc' =>'' ));
and successfully change values in memory
But the forums I want show are not shown after manipulating these values.
Do I have to change the auth value? for the forum? If so how do I reliably access the index of the relevant permission?
I also see some apply_filters in the function sp_can_view but I can't figure out how to use the code. If apply_filters is the way to go are there any code examples you can refer me to that would allow for dynamically assigning permissions to view forum in listings, topics etc.
Well first question is have you checked out the Data Inspector in the forum admin Toolbox? This will allow you to display - to the screen - a whole host of available data. The equivalent of the old $current_user is $spThisUser which now has more data available. You can also view the various view data objects, $sfglobals, $sfvars etc.
The second question has to really by why? Are not the permissions/user groups themselves powerful enough to do what you want? If they are not I would be interested to know why in case there is something there we should perhaps add in. A more detailed explanation of the requirement would help is any case to try and point you to the best way of achieving what it is you are trying to do.
YELLOW
SWORDFISH
|
Thanks as always for your quick response!
I didn't know about that data inspector. VERY COOL!
The use case is that we have a pretty complicated set of criteria that are temporary to each session and the the session is time limited and the criteria is shared with other non sp content. A cookie is read. If the cookie has valid info, then read only access to a certain forum is given. There is a user group that has no members. In the past v.4 I have been able to dynamically assign the group to the user when conditions are met.
I tried using spThisUser which provides better access instead of using a reference.
But the result seems to be the same. I can set add the usergroup to the spThisUser array and it sticks but and even if I also set all auths for the forum in question =1 I still don't see the forum/subforum in the listing.
The temporary approach I am thinking of using is to call
sp_remove_membership($user_group, $current_user->ID);
edit: fixed code changed $forum to $user_group
... on forum load for all users when they access the forum
and then
sp_add_membership($user_group, $current_user->ID);
edit: fixed code changed $forum to $user_group
when the conditions are met.
The dynamic approach used with the old sp 4 allowed me to avoid calls to the database ...The first one would be for each each user at each access of the forum. The second would be each time the conditions are met.
Hopefully this makes sense.
I will need to study this some more. I sort of understand the requirement - tricky as it is! Not sure I totally follow the logic yet and I am a little pushed for time immediately but can look closer later on in the day.
What I would say first off though is that you clearly need to make any data changes early on so I would suggest you attack the $spThisUser object when it is first created. Are you familiar with WP action and filter hooks? Hopefully so and there is an action (NOT a filter) you can use named 'sph_current_user_class' that passes in the $spThisUser object. This is passed by reference so any changes you make should stick.
The place to use this action would be in the forum themes spFunctions.php (in the theme /templates folder).
So - I assume you are looking at the 'auths' array within the object as it seems to me this is what you need to manipulate. Do you understand the data layout in there - i.e., the forum array key and the numeric auths elements - which map to the auths map available in $sfglobals?
YELLOW
SWORDFISH
|
Thanks a bunch.
I added this to spFunctions.php ... it sticks but the forums still don't show. I don't quite understand the auth mapping yet, therefore I am just setting everything =1 until I understand.
function test_ppp($a) {
array_push($a->memberships, array ('usergroup_id' => $UG, 'usergroup_name' => $UG_TITLE, 'usergroup_desc' =>'', 'usergroup_join' => 0 ));
foreach( $a->auths[$aForum] as $key => $value) $a->auths[$aForum][$key]=1; // just for testing, don't do this in production
foreach( $a->auths[$aSubForum] as $key => $value) $a->auths[$aSubForum][$key]=1; // just for testing, don't do this in production
// echo print_r($a);
}
add_action('sph_current_user_class', 'test_ppp');
OK that's good.
I see no reason why that should not work.
if you look at the function sp_get_auth() (sp-api/sp-api-auths.php) you will quite clearly see that this is the central processing of all auth checks. I will have tp ponder this and also see if Steve can see any reason that should not work.
YELLOW
SWORDFISH
|
what is not showing? entire forum? some posts? access denied? maybe a screenshot... and is this as guest, user or admin? or all of above? as admin, you wouldnt want to set them all to 1, a couple need to be zero...
Visit Cruise Talk Central and Mr Papa's World
1 Guest(s)