Raised This Month: $51 Target: $400
 12% 

Parsing a string for admin flags


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrimsonGT
Veteran Member
Join Date: Oct 2007
Location: Gainesville, FL
Old 07-31-2009 , 02:53   Parsing a string for admin flags
Reply With Quote #1

I just wrote a small function that I am assuming should work for checking multiple flags in a string, but when I pass it something like "abz" it doesnt. I dont mess with flags often so im assuming I made a mistake somewhere.

PHP Code:
bool:HasFlag(clientString:flags[])
{
    if (
GetUserFlagBits(client) & ADMFLAG_ROOT)
    {
        return 
true;
    }

    new 
iFlags ReadFlagString(flags);

    if ((
GetUserFlagBits(client) & iFlags) == iFlags)
    {
        return 
true;
    }

    return 
false;

__________________
CrimsonGT is offline
TESLA-X4
Senior Member
Join Date: Dec 2008
Location: $Recycle.Bin
Old 07-31-2009 , 03:26   Re: Parsing a string for admin flags
Reply With Quote #2

Hmm from the ReadFlagString function description (and looking at its c++ code in AdminCache.cpp), it should be able to handle a string of flags (in any order), so I doubt that's the problem. But your code looks fine to me (assuming you require the client have all of the listed flags in order to pass).
TESLA-X4 is offline
BrutalGoerge
AlliedModders Donor
Join Date: Jul 2007
Old 07-31-2009 , 03:28   Re: Parsing a string for admin flags
Reply With Quote #3

edit
like this, if you pass the flags, and one of the flags, it's true

your way = client needs all the flags

both work just fine passing multiple flags.
PHP Code:
if (GetUserFlagBits(client) & iFlags
depends what you want it to do

like if a client has as flags (what my donators have), it will return true if you put in abz, false if they put in a bunch of flags they dont have (bcdefgz)

the original way was returning true no mater what gibberish i put.

i duno i think you wanted it to check for all them them

i fail


Edit never mind, your way works just fine for me, i forgot to take that root part out durr.

I set my flags to as, passing as returns true, passing a returns true, passing as returns true, passing asb returns false. Ive been wanting to add a convar where someone could just put in sm_flags "abcd" and
sm_flags_excusive 0/1 where they can say people with all these flags = admin, or people with 1 of these flags = admin.

neat stuff.

You obviously want an admin to have all flags for it to return true, *slaps self but hey i learned stuff thank you!!!!!!!!!!

rep++

ahawhhawhaw

Duno why you were having problems, seems to work fine depending how you set it up, a client needs just 1 of the flags, or all of the flags being passed, and it not mattering if you pass more than 1 flag.

Edit

not sure why im so hyper at 1:40 am, but wee
__________________
My Pluggies If you like, consider to me.

Last edited by BrutalGoerge; 07-31-2009 at 03:48.
BrutalGoerge is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 07-31-2009 , 04:21   Re: Parsing a string for admin flags
Reply With Quote #4

Try this out. I modified another stock that I wrote to handle flag strings instead of just bitflags.

PHP Code:
/**
 * Checks to see if a client has all of the specified admin flags
 *
 * @param client        Player's index.
 * @param flagString    String of flags to check for.
 * @return                True on admin having all flags, false otherwise.
 */
stock bool:CheckAdminFlagsByString(client, const String:flagString[])
{
    new 
AdminId:admin GetUserAdmin(client);
    if (
admin != INVALID_ADMIN_ID)
    {
        new 
countfoundflags ReadFlagString(flagString);
        for (new 
0<= 20i++)
        {
            if (
flags & (1<<i))
            {
                
count++;

                if (
GetAdminFlag(adminAdminFlag:i))
                {
                    
found++;
                }
            }
        }

        if (
count == found)
        {
            return 
true;
        }
    }

    return 
false;

bl4nk is offline
CrimsonGT
Veteran Member
Join Date: Oct 2007
Location: Gainesville, FL
Old 07-31-2009 , 04:43   Re: Parsing a string for admin flags
Reply With Quote #5

Cool thanks guys.
__________________
CrimsonGT is offline
Kevin_b_er
SourceMod Donor
Join Date: Feb 2009
Old 08-01-2009 , 14:29   Re: Parsing a string for admin flags
Reply With Quote #6

This is normally completely valid:
PHP Code:
(GetUserFlagBits(client) & iFlags) == iFlags 
Bitwise anding with a bitfield to confirm the flags is normally quite valid, because if you only want them to have those bits. All the flags but the required ones are lopped off by the bitwise and then tested for. If you didn't test for equality, all they would need would be 1 of those bits.


But lets be clear here, there's one big problem:
The root flag.

GetUserFlagBits for a root user will return only root, not all flags and root.

So if you were to test for 'abz', that's reservation, generic admin, and root. Its impossible to have all 3 of those bits at once.

As a result, ReadFlagString("abz") will probably return (1<<0) | (1<<1) | (1<<14), but no one will have ALL those bits set at once, as I said above.

Your function seems correct, just that having "abz" together is impossible. You've already covered the root user case. You should just be testing for "ab" not "abz". If they're root, it'll return true, else they need both ADMFLAG_RESERVATION and ADMFLAG_GENERIC at once. And if you want to test for root and root alone, you can just test for "z" and that should work.
Kevin_b_er is offline
retsam
Veteran Member
Join Date: Aug 2008
Location: so-cal
Old 08-01-2009 , 14:38   Re: Parsing a string for admin flags
Reply With Quote #7

Ive been using similar code for admin flag checks. I was not aware it could check for multiple flags. I had thought it was limited to just one flag? I guess it looks like thats what bl4nks post does.

Last edited by retsam; 08-01-2009 at 14:42.
retsam is offline
rodrigo286
Veteran Member
Join Date: Sep 2010
Location: Brazil, São Paulo
Old 11-17-2017 , 22:47   Re: Parsing a string for admin flags
Reply With Quote #8

Quote:
Originally Posted by bl4nk View Post
Try this out. I modified another stock that I wrote to handle flag strings instead of just bitflags.

PHP Code:
/**
 * Checks to see if a client has all of the specified admin flags
 *
 * @param client        Player's index.
 * @param flagString    String of flags to check for.
 * @return                True on admin having all flags, false otherwise.
 */
stock bool:CheckAdminFlagsByString(client, const String:flagString[])
{
    new 
AdminId:admin GetUserAdmin(client);
    if (
admin != INVALID_ADMIN_ID)
    {
        new 
countfoundflags ReadFlagString(flagString);
        for (new 
0<= 20i++)
        {
            if (
flags & (1<<i))
            {
                
count++;

                if (
GetAdminFlag(adminAdminFlag:i))
                {
                    
found++;
                }
            }
        }

        if (
count == found)
        {
            return 
true;
        }
    }

    return 
false;

This works nice on 18/11/2017 Stable and Dev Build...

Thanks bl4nk.

New API Syntax:

PHP Code:
/**
 * Checks to see if a client has all of the specified admin flags
 *
 * @param client        Player's index.
 * @param flagString    String of flags to check for.
 * @return                True on admin having all flags, false otherwise.
 * Original taken from https://forums.alliedmods.net/showpost.php?p=886345&postcount=4
 */
stock bool CheckAdminFlagsByString(int client, const char[] flagString)
{
    
AdminId admin view_as<AdminId>(GetUserAdmin(client));
    if (
admin != INVALID_ADMIN_ID){
        
int countfoundflags ReadFlagString(flagString);
        for (
int i 0<= 20i++){
            if (
flags & (1<<i))
            {
                
count++;

                if(
GetAdminFlag(adminview_as<AdminFlag>(i))){
                    
found++;
                }
            }
        }

        if (
count == found){
            return 
true;
        }
    }

    return 
false;

Regards.
__________________
My Plugins | VIEW HERE | I accept private requests, send me a PM.
Buy respawn | Uber Recall | Grenade drop | Damage Supperssor
Meet the Medic | Disguise Expert | Crazy Jet

CZSBrasil TEAM

Last edited by rodrigo286; 11-17-2017 at 22:49.
rodrigo286 is offline
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 -4. The time now is 08:31.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode