Back to vBulletin 3 Articles

Function To Retrieve Usergroup Forum CANVIEW Permissions
by tHE DSS 02 Sep 2002

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

http://www.vbulletin.org/forum/showthread.php?s=&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.

vblts.ru supports vBulletin®, 2022-2024