plugins/admin-flatfile/admin-users.sp
changeset 2052 138936f11ab5
parent 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- a/plugins/admin-flatfile/admin-users.sp	Tue May 13 08:22:49 2008 +0000
     1.2 +++ b/plugins/admin-flatfile/admin-users.sp	Wed May 14 05:31:14 2008 +0000
     1.3 @@ -36,10 +36,14 @@
     1.4  #define USER_STATE_INADMIN		2
     1.5  
     1.6  static Handle:g_hUserParser = INVALID_HANDLE;
     1.7 -static AdminId:g_CurUser = INVALID_ADMIN_ID;
     1.8  static g_UserState = USER_STATE_NONE;
     1.9  static String:g_CurAuth[64];
    1.10  static String:g_CurIdent[64];
    1.11 +static String:g_CurName[64];
    1.12 +static String:g_CurPass[64];
    1.13 +static Handle:g_GroupArray;
    1.14 +static g_CurFlags;
    1.15 +static g_CurImmunity;
    1.16  
    1.17  public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    1.18  {
    1.19 @@ -54,16 +58,25 @@
    1.20  		if (StrEqual(name, "Admins"))
    1.21  		{
    1.22  			g_UserState = USER_STATE_ADMINS;
    1.23 -			g_CurUser = INVALID_ADMIN_ID;
    1.24 -		} else {
    1.25 +		}
    1.26 +		else
    1.27 +		{
    1.28  			g_IgnoreLevel++;
    1.29  		}
    1.30 -	} else if (g_UserState == USER_STATE_ADMINS) {
    1.31 +	}
    1.32 +	else if (g_UserState == USER_STATE_ADMINS)
    1.33 +	{
    1.34  		g_UserState = USER_STATE_INADMIN;
    1.35 -		g_CurUser = CreateAdmin(name);
    1.36 +		strcopy(g_CurName, sizeof(g_CurName), name);
    1.37  		g_CurAuth[0] = '\0';
    1.38  		g_CurIdent[0] = '\0';
    1.39 -	} else {
    1.40 +		g_CurPass[0] = '\0';
    1.41 +		ClearArray(g_GroupArray);
    1.42 +		g_CurFlags = 0;
    1.43 +		g_CurImmunity = 0;
    1.44 +	}
    1.45 +	else
    1.46 +	{
    1.47  		g_IgnoreLevel++;
    1.48  	}
    1.49  	
    1.50 @@ -76,28 +89,22 @@
    1.51  									bool:key_quotes, 
    1.52  									bool:value_quotes)
    1.53  {
    1.54 -	if (g_UserState != USER_STATE_INADMIN
    1.55 -		|| g_IgnoreLevel
    1.56 -		|| g_CurUser == INVALID_ADMIN_ID)
    1.57 +	if (g_UserState != USER_STATE_INADMIN || g_IgnoreLevel)
    1.58  	{
    1.59  		return SMCParse_Continue;
    1.60  	}
    1.61  	
    1.62 -	new bool:auth = false;
    1.63 -	
    1.64  	if (StrEqual(key, "auth"))
    1.65  	{
    1.66 -		auth = true;
    1.67  		strcopy(g_CurAuth, sizeof(g_CurAuth), value);
    1.68  	}
    1.69  	else if (StrEqual(key, "identity"))
    1.70  	{
    1.71 -		auth = true;
    1.72  		strcopy(g_CurIdent, sizeof(g_CurIdent), value);
    1.73  	}
    1.74  	else if (StrEqual(key, "password")) 
    1.75  	{
    1.76 -		SetAdminPassword(g_CurUser, value);
    1.77 +		strcopy(g_CurPass, sizeof(g_CurPass), value);
    1.78  	} 
    1.79  	else if (StrEqual(key, "group")) 
    1.80  	{
    1.81 @@ -105,39 +112,30 @@
    1.82  		if (id == INVALID_GROUP_ID)
    1.83  		{
    1.84  			ParseError("Unknown group \"%s\"", value);
    1.85 -		} else if (!AdminInheritGroup(g_CurUser, id)) {
    1.86 -			ParseError("Unable to inherit group \"%s\"", value);
    1.87  		}
    1.88 +
    1.89 +		PushArrayCell(g_GroupArray, id);
    1.90  	} 
    1.91  	else if (StrEqual(key, "flags")) 
    1.92  	{
    1.93  		new len = strlen(value);
    1.94  		new AdminFlag:flag;
    1.95  		
    1.96 -		for (new i=0; i<len; i++)
    1.97 +		for (new i = 0; i < len; i++)
    1.98  		{
    1.99  			if (!FindFlagByChar(value[i], flag))
   1.100  			{
   1.101  				ParseError("Invalid flag detected: %c", value[i]);
   1.102  			}
   1.103 -			SetAdminFlag(g_CurUser, flag, true);
   1.104 +			else
   1.105 +			{
   1.106 +				g_CurFlags |= FlagToBit(flag);
   1.107 +			}
   1.108  		}
   1.109  	} 
   1.110  	else if (StrEqual(key, "immunity")) 
   1.111  	{
   1.112 -		new level = StringToInt(value);
   1.113 -		SetAdminImmunityLevel(g_CurUser, level);
   1.114 -	}
   1.115 -
   1.116 -	if (auth && g_CurIdent[0] && g_CurAuth[0])
   1.117 -	{
   1.118 -		if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
   1.119 -		{
   1.120 -			g_CurAuth[0] = '\0';
   1.121 -			g_CurIdent[0] = '\0';
   1.122 -		} else {
   1.123 -			ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
   1.124 -		}
   1.125 +		g_CurImmunity = StringToInt(value);
   1.126  	}
   1.127  	
   1.128  	return SMCParse_Continue;
   1.129 @@ -153,9 +151,48 @@
   1.130  	
   1.131  	if (g_UserState == USER_STATE_INADMIN)
   1.132  	{
   1.133 +		/* Dump this user to memory */
   1.134 +		if (g_CurIdent[0] != '\0' && g_CurAuth[0] != '\0')
   1.135 +		{
   1.136 +			decl AdminFlag:flags[26];
   1.137 +			new AdminId:id, i, num_groups, num_flags;
   1.138 +			
   1.139 +			if ((id = FindAdminByIdentity(g_CurAuth, g_CurIdent)) == INVALID_ADMIN_ID)
   1.140 +			{
   1.141 +				id = CreateAdmin(g_CurName);
   1.142 +				if (!BindAdminIdentity(id, g_CurAuth, g_CurIdent))
   1.143 +				{
   1.144 +					ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
   1.145 +				}
   1.146 +			}
   1.147 +			
   1.148 +			num_groups = GetArraySize(g_GroupArray);
   1.149 +			for (i = 0; i < num_groups; i++)
   1.150 +			{
   1.151 +				AdminInheritGroup(id, GetArrayCell(g_GroupArray, i));
   1.152 +			}
   1.153 +			
   1.154 +			SetAdminPassword(id, g_CurPass);
   1.155 +			if (GetAdminImmunityLevel(id) < g_CurImmunity)
   1.156 +			{
   1.157 +				SetAdminImmunityLevel(id, g_CurImmunity);
   1.158 +			}
   1.159 +			
   1.160 +			num_flags = FlagBitsToArray(g_CurFlags, flags, sizeof(flags));
   1.161 +			for (i = 0; i < num_flags; i++)
   1.162 +			{
   1.163 +				SetAdminFlag(id, flags[i], true);
   1.164 +			}
   1.165 +		}
   1.166 +		else
   1.167 +		{
   1.168 +			ParseError("Failed to create admin: did you forget either the auth or identity properties?");
   1.169 +		}
   1.170 +		
   1.171  		g_UserState = USER_STATE_ADMINS;
   1.172 -		g_CurUser = INVALID_ADMIN_ID;
   1.173 -	} else if (g_UserState == USER_STATE_ADMINS) {
   1.174 +	}
   1.175 +	else if (g_UserState == USER_STATE_ADMINS)
   1.176 +	{
   1.177  		g_UserState = USER_STATE_NONE;
   1.178  	}
   1.179  	
   1.180 @@ -179,6 +216,8 @@
   1.181  					   ReadUsers_KeyValue,
   1.182  					   ReadUsers_EndSection);
   1.183  		SMC_SetRawLine(g_hUserParser, ReadUsers_CurrentLine);
   1.184 +		
   1.185 +		g_GroupArray = CreateArray();
   1.186  	}
   1.187  }
   1.188  
   1.189 @@ -199,7 +238,9 @@
   1.190  		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   1.191  		{
   1.192  			ParseError("%s", buffer);
   1.193 -		} else {
   1.194 +		}
   1.195 +		else
   1.196 +		{
   1.197  			ParseError("Fatal parse error");
   1.198  		}
   1.199  	}