vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   Function to retrieve the forums a user can access (https://vborg.vbsupport.ru/showthread.php?t=41934)

Admin 08-04-2002 10:00 PM

Function to retrieve the forums a user can access
 
This is something I once wrote for someone (think it was merk?), and kept it in a file, and finally got around to clean it up and wrap it in a function.

Very useful for some hacks, hope this helps at least some of you.
Here is the full function, along with some basic instructions:

PHP Code:

/***********************************************************************
 * Parameters:
 *    1.    The name of the permission you want to check for.
 *        Alternatively, it can take an array of permissions.
 *        Defaults to 'canview'.
 *    2.    The user ID of the user you are checking permissions for.
 *        Defaults to -1 (i.e current viewer).
 *    3.    The separator for the list of forum ID's.
 * Return:
 *    1.    If the first parameter is a string, the function will
 *        return the list of forum ID's.
 *    2.    If the first parameter is an array, the function will
 *        return an associative array of permission => forum ID's.
 * Example usage:
 *    1.    get_allowed_forums();
 *            Will return the list of forums the current user can view:
 *            1,5,6,8
 *    2.    get_allowed_forums(array('canview', 'canpostnew'), 2);
 *            Will return an array that contains two keys, canview and
 *            canpostnew, each referring to the list of forums user
 *            number 2 can access:
 *            Array
 *            (
 *                [canview] => 3,5,7,8
 *                [canpostnew] => 3,7
 *            )
 **********************************************************************/
function get_allowed_forums ($checkfor 'canview'$userid = -1$separator ',') {
    global 
$permissions$DB_site$bbuserinfo$enableaccess$hideprivateforums;

    if (
$userid == -1) {
        
$userinfo $bbuserinfo;
    } else {
        
$userinfo getuserinfo($userid);
    }

    if (
is_array($checkfor)) {
        
$select implode(', '$checkfor);
    } else {
        
$select $checkfor;
    }
    
$forumpermissions $DB_site->query("
        SELECT forumid, 
$select
        FROM forumpermission
        WHERE usergroupid = 
$userinfo[usergroupid]
    "
);
    while (
$forumpermission $DB_site->fetch_array($forumpermissions)) {
        
$forumpermscache[$forumpermission['forumid']] = $forumpermission;
    }

    if (
$userinfo['userid'] != and $enableaccess) {
        
$accesspermissions $DB_site->query("
            SELECT forumid, accessmask
            FROM access
            WHERE userid = 
$userinfo[userid]
        "
);
        while (
$accesspermission $DB_site->fetch_array($accesspermissions)) {
            
$accesscache[$accesspermission['forumid']] = $accesspermission;
        }

        if (
is_array($checkfor)) {
            foreach (
$checkfor as $permname) {
                
$usergroupdefault[$permname] = $permissions[$permname];
                
$nopermissions[$permname] = 0;
            }
        } else {
            
$usergroupdefault = array(
                
$checkfor => $permissions[$checkfor]
            );
            
$nopermissions = array(
                
$checkfor => 0
            
);
        }
    }

    
// (This is a really really fast query since it only
    //  uses the index and not the actual table.)
    
$forums $DB_site->query('SELECT forumid FROM forum');
    while (
$forum $DB_site->fetch_array($forums)) {
        if (
$enableaccess and is_array($accesscache[$forum['forumid']])) {
            if (
$accesscache[$forum['forumid']]['accessmask'] == 1) {
                
$forumperms $usergroupdefault;
            } else {
                
$forumperms $nopermissions;
            }
        } elseif (
is_array($forumpermscache[$forum['forumid']])) {
            
$forumperms $forumpermscache[$forum['forumid']];
        } else {
            
$forumperms $permissions;
        }

        if (
is_array($checkfor)) {
            foreach (
$checkfor as $permname) {
                if ((!
$hideprivateforums and $permname == 'canview') or $forumperms[$permname]) {
                    if (
$allowedforums[$permname]) {
                        
$allowedforums[$permname] .= $separator;
                    }
                    
$allowedforums[$permname] .= $forum['forumid'];
                }
            }
        } else {
            if ((!
$hideprivateforums and $checkfor == 'canview') or $forumperms[$checkfor]) {
                if (
$allowedforums) {
                    
$allowedforums .= $separator;
                }
                
$allowedforums .= $forum['forumid'];
            }
        }
    }

    return 
$allowedforums;



Lesane 08-05-2002 10:55 AM

Great function, very usefull indeed. Thanks.

Dark_Wizard 08-08-2002 08:42 AM

Wow...very useful I agree. Thank you!

Admin 08-08-2002 09:15 AM

I should mention that the function only issues 3 queries, one of which VERY fast. :)

JJR512 08-11-2002 01:48 AM

I seem to be having some problems with this.

I've added the entire function to functions.php. I added it as the second-to-last function, above doshutdown.

I tried to use the function from global.php (the forums, not the admin). Right above the part of global.php that parses the css (headinclude), header, and footer.

I've tried $jjr512=get_allowed_forums();. I've tried $jjr512=get_allowed_forums('canview',-1,',');. I've tried a lot of things. But whenever I try to use the function, I get this error: "Warning: Bad arguments to implode() in /home/jjr512/public_html/testvb/admin/functions.php on line 2289" Now as I have it in my functions.php, Line 2289 is:
Code:

SELECT forumid, '.iif(is_array($checkfor), implode(', ', $checkfor), $checkfor)."
What am I doing wrong?

Admin 08-11-2002 12:44 PM

Try the new version.

MrLister 08-11-2002 03:36 PM

great, thank you!

tHE DSS 08-16-2002 09:36 PM

This is a great function, and i'm using it right now for some custom statistics.

Thing is though, Guests and normal members and stuff get permissions returned that they don't have "canview" permissions to.

Any thoughts?

tHE DSS 08-17-2002 02:24 PM

I think what is happening, is if a forum is using permissions from it's parent forum, then it doesn't get taken into account.

If you know what I mean?

TECK 08-18-2002 06:25 AM

very usefull, as lesane said before. thanks for posting it.


All times are GMT. The time now is 02:41 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.01531 seconds
  • Memory Usage 1,771KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete