Abstraced updates to the cokebank file to a function
[tpg/opendispense2.git] / src / cokebank_basic / bank.c
index abf5763..56db422 100644 (file)
@@ -27,6 +27,19 @@ tUser        *gaBank_Users;
 FILE   *gBank_File;
 
 // === CODE ===
+static int Bank_int_WriteEntry(int ID)
+{
+       if( ID < 0 || ID >= giBank_NumUsers ) {
+               return -1;
+       }
+       
+       // Commit to file
+       fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
+       fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
+       
+       return 0;
+}
+
 int Bank_GetUserByName(const char *Username)
 {
         int    i, uid;
@@ -55,17 +68,15 @@ int Bank_GetUserFlags(int ID)
 {
        if( ID < 0 || ID >= giBank_NumUsers )
                return -1;
-               
-       // TODO: Implement checking the PAM groups and status instead, then
-       // fall back on the database. (and update if there is a difference)
 
        // root
        if( gaBank_Users[ID].UnixID == 0 ) {
-               gaBank_Users[ID].Flags &= ~USER_FLAG_TYPEMASK;
-               gaBank_Users[ID].Flags |= USER_TYPE_WHEEL;
+               gaBank_Users[ID].Flags |= USER_FLAG_WHEEL|USER_FLAG_COKE;
        }
 
        #if USE_UNIX_GROUPS
+       // TODO: Implement checking the PAM groups and status instead, then
+       // fall back on the database. (and update if there is a difference)
        if( gaBank_Users[ID].UnixID > 0 )
        {
                struct passwd   *pwd;
@@ -80,10 +91,9 @@ int Bank_GetUserFlags(int ID)
                if( grp ) {
                        for( i = 0; grp->gr_mem[i]; i ++ )
                        {
-                               if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 )
-                               {
-                                       gaBank_Users[ID].Flags &= ~USER_FLAG_TYPEMASK;
-                                       gaBank_Users[ID].Flags |= USER_TYPE_COKE;
+                               if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) {
+                                       gaBank_Users[ID].Flags |= USER_FLAG_COKE;
+                                       break ;
                                }
                        }
                }
@@ -93,10 +103,9 @@ int Bank_GetUserFlags(int ID)
                if( grp ) {
                        for( i = 0; grp->gr_mem[i]; i ++ )
                        {
-                               if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 )
-                               {
-                                       gaBank_Users[ID].Flags &= ~USER_FLAG_TYPEMASK;
-                                       gaBank_Users[ID].Flags |= USER_TYPE_WHEEL;
+                               if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) {
+                                       gaBank_Users[ID].Flags |= USER_FLAG_WHEEL;
+                                       break ;
                                }
                        }
                }
@@ -115,8 +124,10 @@ int Bank_SetUserFlags(int ID, int Mask, int Value)
        // Silently ignore changes to root and meta accounts
        if( gaBank_Users[ID].UnixID <= 0 )      return 0;
        
-       gaBank_Users[ID].Flags &= Mask;
+       gaBank_Users[ID].Flags &= ~Mask;
        gaBank_Users[ID].Flags |= Value;
+
+       Bank_int_WriteEntry(ID);
        
        return 0;
 }
@@ -130,42 +141,33 @@ int Bank_AlterUserBalance(int ID, int Delta)
        // Update
        gaBank_Users[ID].Balance += Delta;
 
-       // Commit
-       fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
-       fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
-       
-       return 0;
-}
-
-int Bank_SetUserBalance(int ID, int Value)
-{
-       // Sanity
-       if( ID < 0 || ID >= giBank_NumUsers )
-               return -1;
-
-       // Update
-       gaBank_Users[ID].Balance = Value;
-       
-       // Commit
-       fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
-       fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
+       Bank_int_WriteEntry(ID);
        
        return 0;
 }
 
 int Bank_GetMinAllowedBalance(int ID)
 {
+        int    flags;
        if( ID < 0 || ID >= giBank_NumUsers )
                return 0;
 
-       switch( Bank_GetUserFlags(ID) & USER_FLAG_TYPEMASK )
-       {
-       case USER_TYPE_NORMAL:  return      0;
-       case USER_TYPE_COKE:    return  -2000;
-       case USER_TYPE_WHEEL:   return -10000;
-       case USER_TYPE_GOD:     return INT_MIN;
-       default:        return 0;
-       }
+       flags = Bank_GetUserFlags(ID);
+
+       // Internal accounts have no limit
+       if( (flags & USER_FLAG_INTERNAL) )
+               return INT_MIN;
+
+       // Wheel is allowed to go to -$100
+       if( (flags & USER_FLAG_WHEEL) )
+               return -10000;
+       
+       // Coke is allowed to go to -$20
+       if( (flags & USER_FLAG_COKE) )
+               return -2000;
+
+       // For everyone else, no negative
+       return 0;
 }
 
 /**
@@ -187,18 +189,19 @@ int Bank_AddUser(const char *Username)
        gaBank_Users[giBank_NumUsers].Flags = 0;
        
        if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {
-               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+               gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL;
+       }
+       else if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) {
+               gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL;
        }
        else if( strcmp(Username, "root") == 0 ) {
-               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+               gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_WHEEL|USER_FLAG_COKE;
        }
-       
-       // Commit to file
-       fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET);
-       fwrite(&gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File);
 
        // Increment count
        giBank_NumUsers ++;
+       
+       Bank_int_WriteEntry(giBank_NumUsers - 1);
 
        return 0;
 }

UCC git Repository :: git.ucc.asn.au