Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles

Reply
 
Thread Tools
Function to retrieve the forums a user can access
Admin's Avatar
Admin
Join Date: Oct 2023
Posts: 1

Admin

Server
Show Printable Version Email this Page Subscription
Admin Admin is offline 08-04-2002, 10:00 PM

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;

Reply With Quote
  #2  
Old 08-05-2002, 10:55 AM
Lesane's Avatar
Lesane Lesane is offline
 
Join Date: Oct 2001
Location: The Netherlands
Posts: 1,149
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Great function, very usefull indeed. Thanks.
Reply With Quote
  #3  
Old 08-08-2002, 08:42 AM
Dark_Wizard Dark_Wizard is offline
 
Join Date: Nov 2001
Location: North Carolina
Posts: 1,251
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Wow...very useful I agree. Thank you!
Reply With Quote
  #4  
Old 08-08-2002, 09:15 AM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I should mention that the function only issues 3 queries, one of which VERY fast.
Reply With Quote
  #5  
Old 08-11-2002, 01:48 AM
JJR512's Avatar
JJR512 JJR512 is offline
 
Join Date: Oct 2001
Location: Glen Burnie, MD, USA
Posts: 710
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #6  
Old 08-11-2002, 12:44 PM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Try the new version.
Reply With Quote
  #7  
Old 08-11-2002, 03:36 PM
MrLister's Avatar
MrLister MrLister is offline
 
Join Date: Oct 2001
Posts: 434
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

great, thank you!
Reply With Quote
  #8  
Old 08-16-2002, 09:36 PM
tHE DSS's Avatar
tHE DSS tHE DSS is offline
 
Join Date: Jun 2002
Location: UK
Posts: 113
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #9  
Old 08-17-2002, 02:24 PM
tHE DSS's Avatar
tHE DSS tHE DSS is offline
 
Join Date: Jun 2002
Location: UK
Posts: 113
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #10  
Old 08-18-2002, 06:25 AM
TECK's Avatar
TECK TECK is offline
 
Join Date: Nov 2001
Location: Canada
Posts: 4,182
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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

Thread Tools

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 10:33 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.05415 seconds
  • Memory Usage 2,321KB
  • Queries Executed 23 (?)
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
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)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_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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete