Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Function To Retrieve Usergroup Forum CANVIEW Permissions
tHE DSS's Avatar
tHE DSS
Join Date: Jun 2002
Posts: 113

 

UK
Show Printable Version Email this Page Subscription
tHE DSS tHE DSS is offline 08-31-2002, 10:00 PM

This is a new, cleaner thread, holding my input to the following thread :

https://vborg.vbsupport.ru/showthrea...threadid=41934

I noticed that with the function in the above mentioned thread, though an excellent routine, if you use usergroup permissions inherited from parent forum permissions, inside the Forum Permissions in your AdminCP, that function can return false results.

I needed a function to tell my scripts which forums any usergroup has CANVIEW access to, and this is my solution.

This function handles forum CANVIEW permissions that are set in the Forum Permissions screen of the AdminCP, it does not cater for AccessMasks or anything, because we don't use accessmasks on our forums. This function also shouldn't be relied upon if you have several tiers of forums/sub-forums... this should only cater for those who have forum categories, and sub-forums of the category.... if your setup has sub-forums of sub-forums, do not rely on this function.

The restrictions of this function, as just mentioned, should not cause a problem to perhaps the majority of forum setups, as it's very rare that I see sub-forums of sub-forums (etc.) at vB boards.

Ok, i've cleaned this code up, and have done some more testing, so let's get on with the good stuff... here is the function :

PHP Code:
function getForumPermissionsDSS($usergroupID) {
    global 
$DB_site;
    
$forumsCannotView_strList "";
    
$forumCatPerm_result $DB_site->query("SELECT forumid FROM forumpermission WHERE canview=0 AND usergroupid=$usergroupID");
    if (!
$num_rows $DB_site->num_rows($forumCatPerm_result) <=0) {
        
$arrayIndex 0;
        while (
$row $DB_site->fetch_array($forumCatPerm_resultMYSQL_ASSOC)) {
            
$forumCatPerm_array[$arrayIndex++] = $row[forumid];
        }
        
$forumsCannotView_strList .= implode(","$forumCatPerm_array) . ",";
        if (isset(
$forumCatPerm_array)) {
            
$forumsCannotView_result $DB_site->query("SELECT forumid,parentlist FROM forum WHERE active=1");
            if (!
$num_rows $DB_site->num_rows($forumsCannotView_result) <=0) {
                
$arrayIndex 0;
                while (
$row $DB_site->fetch_array($forumsCannotView_resultMYSQL_ASSOC)) {
                    
$forumParentList_temp explode(','$row[parentlist]);
                    if (
$forumParentList_temp[1] != -1) {
                        if (
in_array($forumParentList_temp[1], $forumCatPerm_array)) {
                            
$forumsCannotView_array[$arrayIndex++] = $forumParentList_temp[0];
                        }
                    }
                }
                
$forumsCannotView_strList .= implode(","$forumsCannotView_array);
            }
        }
    }
    return 
$forumsCannotView_strList;

.. the naming part "DSS" at the end of the function name is there simply to ensure the function name doesn't clash with any others you may have.

Right, that's the function - you may place that in your 'admin/functions.php' file, if you wish.

Then, in any script that you intend to use the function, place this :

PHP Code:
$forumsCannotView_strList getForumPermissionsDSS($bbuserinfo[usergroupid]);
$whereClause_forumPermissions = ($forumsCannotView_strList != "") ? ("(forumid NOT IN ($forumsCannotView_strList)) AND") : (""); 
... after this :

PHP Code:
require ("global.php"); 
.. or, you could simply put the code right after the function, inside the 'admin/functions.php' file. That is a good idea if you intend to, or think that you may need the function regular.

What those two lines do, is get a string list of all forums that the current visitor has CANVIEW permissions to, and then generates a WHERE clause for you, ready for easy inclusion into an SQL query.

Unless you will need the string list for other reasons, you'll only be interested in this $whereClause_forumPermissions variable... which holds the WHERE clause.

Here is an example of how forums using permissions inherited from it's parent forum might look inside your AdminCP :

Code:
Example Forums
    (COPPA) Users Awaiting Moderation
    Administrators
    .... etc ....
    .... etc ....
    Registered
    .... etc ....

    Example Sub Forum, Of Above Category
        (COPPA) Users Awaiting Moderation
        Administrators
        .... etc ....
        .... etc ....
        Registered
        .... etc ....

    Example Sub Forum II, Of Above Category
        (COPPA) Users Awaiting Moderation
        Administrators
        .... etc ....
        .... etc ....
        Registered
        .... etc ....
... permissions have been set for the Example Forums parent forum (category forum) for each usergroup (custom permissions), and the forums inside (Example Sub Forum, Of Above Category and Example Sub Forum II, Of Above Category for example), inherit from those permissions.

This function will cater for this. The following is also allowable by this function :

Code:
Example Forums
    (COPPA) Users Awaiting Moderation
    Administrators
    .... etc ....
    .... etc ....
    Registered
    .... etc ....

    Example Sub Forum, Of Above Category
        (COPPA) Users Awaiting Moderation
        Administrators
        .... etc ....
        .... etc ....
        Registered
        .... etc ....

    Example Sub Forum II, Of Above Category
        (COPPA) Users Awaiting Moderation
        Administrators
        .... etc ....
        .... etc ....
        Registered
        .... etc ....
... basically, most of the usergroup permissions for the Example Sub Forum, Of Above Category and Example Sub Forum II, Of Above Category forums inherit from the Example Forum category, except for 2 usergroups, which are "etc." and "Registered"... they have custom permissions set.

Only one thing to be carful of, is not setting any permissions for a forum.... this function will not include that forum. A forum must have either custom permissions, or inherited permissions, (show in either RED or BLUE) or the forum will be classed as "viewable".

So, your "viewable by all" forums can be left without special permissions set, if you wish. You only need to set permissions for those forums where certain usergroups should not have CANVIEW access.

Right, the next thing to do, is show some examples of usage. We've already called the function, and setup the WHERE clause, so let's execute a query.... the following example, is so that you can test this out - it will display the list of forums which a usergroup can access :

PHP Code:
$testPermissions_result $DB_site->query("SELECT forumid,title FROM forum WHERE $whereClause_forumPermissions active=1 ORDER BY forumid ASC");
if (!
$num_rows $DB_site->num_rows($testPermissions_result) <=0) {
    while (
$row $DB_site->fetch_array($testPermissions_resultMYSQL_ASSOC)) {
        echo 
"<br><b>Forum ID : </b>$row[forumid]&nbsp;&nbsp;&nbsp;&nbsp;<b>Forum Title : </b>$row[title]";
    }

... notice the $whereClause_forumPermissions variable? That gets expanded by the PHP interpreter so our forums that get returned, are those only viewable by the current usergroup. Also notice the active=1 in the query? Basically, so that only active forums get returned too... that is, forums that are "open for business". There is no need to put an "AND" before that, as the $whereClause_forumPermissions variable will handle that, if it's needed.

During testing, you can subsitute the $bbuserinfo[usergroupid] in the calling of the function, for any usergroup number, if you wish... but be sure to put it back before going "live" with the function... example :

PHP Code:
$forumsCannotView_strList getForumPermissionsDSS(6); 
... usergroup ID 6 is the Admins... change the 6 to other usergroup IDs whilst testing, if you wish.

Another test, would be to return the most viewed thread stored in your database, but only threads in forums that are viewable by the current usergroup will be counted :

PHP Code:
$topViewedThread_result $DB_site->query_first("SELECT * 
    FROM thread 
        WHERE 
$whereClause visible=1 AND open=1 
            ORDER BY views DESC 
                LIMIT 1"
); 
... the "visible=1" and "open=1" simply state to the query, don't return any threads that are not available and locked.

That's the jist, it does the job for me, and I hope you may find it usfull too.

Our forums only use usergroup permissions, inherited from parent forums, and occasionally some custom permissions for usergroups, but, we don't use accessmasks, and we don't have sub-forums of sub-forums... this function works for us, and maybe for alot you too.

Biggup the "open source / community source".... it's saved me alot of time and headache at times.... as i'm sure i've saved others from the same.... again.... big it up.
Reply With Quote
  #2  
Old 10-20-2002, 10:52 AM
tHE DSS's Avatar
tHE DSS tHE DSS is offline
 
Join Date: Jun 2002
Location: UK
Posts: 113
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm back again to share some more. Sombody wanted a list of forums that a user can view, based on their usergroup permissions.

I'm doing exactly the same thing at our site, but, i'm outputting formatted links. The request was for a dropdown box... so, here is the gumph for a DD box.

Again, here is the function... you can place this in your 'functions.php' or 'global.php' if you wish :

PHP Code:
function getForumPermissions($usergroupID) {
    global 
$DB_forum;
    
$forumsCannotView_strList "";
    
$forumCatPerm_result $DB_forum->query("SELECT forumid FROM forumpermission WHERE canview=0 AND usergroupid=$usergroupID");
    if (!
$num_rows $DB_forum->num_rows($forumCatPerm_result) <=0) {
        
$arrayIndex 0;
        while (
$row $DB_forum->fetch_array($forumCatPerm_result$DB_content->ASSOC)) {
            
$forumCatPerm_array[$arrayIndex++] = $row[forumid];
        }
        
$forumsCannotView_strList .= implode(","$forumCatPerm_array) . ",";
        if (isset(
$forumCatPerm_array)) {
            
$forumsCannotView_result $DB_forum->query("SELECT forumid,parentlist FROM forum WHERE active=1");
            if (!
$num_rows $DB_forum->num_rows($forumsCannotView_result) <=0) {
                
$arrayIndex 0;
                while (
$row $DB_forum->fetch_array($forumsCannotView_result$DB_content->ASSOC)) {
                    
$forumParentList_temp explode(','$row[parentlist]);
                    if (
$forumParentList_temp[1] != -1) {
                        if (
in_array($forumParentList_temp[1], $forumCatPerm_array)) {
                            
$forumsCannotView_array[$arrayIndex++] = $forumParentList_temp[0];
                        }
                    }
                }
                
$forumsCannotView_strList .= implode(","$forumsCannotView_array);
            }
        }
    }
    return 
$forumsCannotView_strList;

In a page where you want to list forums a user can view, call the function to get a string list, and then setup the variable WHERE clause, like this :

PHP Code:
$forumsCannotView_strList getForumPermissions($bbuserinfo[usergroupid]);
$whereClause_permissions = ($forumsCannotView_strList != "") ? ("(forumid NOT IN ($forumsCannotView_strList)) AND") : (""); 
Then, fill a multi-dimensional array with all the details of each viewable forum :

PHP Code:
$mainLinks_result $DB_forum->query("SELECT forumid,title,description,parentlist FROM forum WHERE $whereClause_permissions active=1 ORDER BY parentid,displayorder ASC");
if (!
$num_rows $DB_forum->num_rows($mainLinks_result) <=0) {

    
// ####### Main Links Multi Dimensional Array Layout #######
    // this commented out code is just the a visible structure of the multi-array that is used in the actual code following. ;-)
    /*
    $mainLinks_multiArray = array(array(     "catID"  => ""
                    ,"catTitle" => ""
                    ,"forums_array" => array(array(     "forumID" => ""
                                    ,"forumTitle" => ""
                                    ,"description" => ""))));
    */
    // ####### end of Main Links Multi Dimensional Array Layout #######

    
while ($row $DB_forum->fetch_array($mainLinks_resultMYSQL_ASSOC)) {
        
$forumParentList_temp explode(','$row[parentlist]);
        
$thisCatKey $forumParentList_temp[1];
        
$thisForumID $forumParentList_temp[0];
        if (
$thisCatKey == "-1") {
            
$mainLinks_multiArray[$thisForumID][catID] = $thisForumID;
            
$mainLinks_multiArray[$thisForumID][catTitle] = $row[title];
        } else {
            
$mainLinks_multiArray[$thisCatKey][forums_array][$thisForumID][forumID] = $thisForumID;
            
$mainLinks_multiArray[$thisCatKey][forums_array][$thisForumID][forumTitle] = $row[title];
        }
    }
    
$forumLinksStatus 1;
}
// null the main links, to start from fresh
$mainLinks ""
... if you wish, you can make that into a function, and also place that into your 'functions.php' or 'global.php'. If so, don't forget to describe the global variables.... especially the 'global $DB_site;'.

Then using all the collected data, you can create a drop down box, with this :

PHP Code:
$mainLinks .= "<select name=\"viewable_forums\">";

foreach (
$mainLinks_multiArray as $key => $value) {
    
$title $mainLinks_multiArray[$key][catTitle];
    
$mainLinks .= "<option value=\"null\">$title</option>";
    foreach (
$mainLinks_multiArray[$key][forums_array] as $key2 => $value2) {
        
$forumID $mainLinks_multiArray[$key][forums_array][$key2][forumID];
        
$title $mainLinks_multiArray[$key][forums_array][$key2][forumTitle];
        
$mainLinks .= "<option value=\"$forumID\"> --- $title</option>";
    }
}

$mainLinks .= "</select>"
You then have the HTML code for a drop down box, stored inside the variable '$mainLinks', which you can use inside any of your templates.

Do remember that this does not support permissions where Access Masks are used. I reckon the majority of boards don't use Access Masks anyway.

Also, i'm using a multi database connection system on our site, where this thing is used, and I have changed the SQL object to a different name.

Just replace all occurances of '$DB_forum' with '$DB_site', and you'll be ok.

Hope it helps you, and I hope you can get it working.
Reply With Quote
  #3  
Old 07-15-2005, 12:26 PM
UnholyWa UnholyWa is offline
 
Join Date: Jul 2002
Posts: 2
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Execue me, is there an updated version for vbb 3.0.7 :nervous:
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:38 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03867 seconds
  • Memory Usage 2,299KB
  • Queries Executed 18 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (2)bbcode_code
  • (10)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (3)post_thanks_box
  • (3)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (3)post_thanks_postbit_info
  • (2)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete