3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * cokebank.c - Coke-Bank management
7 * This file is licenced under the 3-clause BSD Licence. See the file COPYING
16 int Bank_GetMinAllowedBalance(int ID);
17 int Bank_GetUserBalance(int ID);
18 int Bank_AlterUserBalance(int ID, int Delta);
19 int Bank_GetUserByUnixID(int UnixID);
20 int Bank_GetUserByName(const char *Name);
21 int Bank_AddUser(int UnixID);
24 void Init_Cokebank(void);
25 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
26 int GetBalance(int User);
27 char *GetUserName(int User);
28 int GetUserID(const char *Username);
29 int GetUserAuth(const char *Username, const char *Password);
33 * \brief Load the cokebank database
35 void Init_Cokebank(void)
41 * \brief Transfers money from one user to another
42 * \param SourceUser Source user
43 * \param DestUser Destination user
44 * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
45 * \param Reason Reason for the transfer (essentially a comment)
46 * \return Boolean failure
48 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
50 if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
52 if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) )
54 Bank_AlterUserBalance(DestUser, Ammount);
55 Bank_AlterUserBalance(SourceUser, -Ammount);
60 * \brief Get the balance of the passed user
62 int GetBalance(int User)
68 * \brief Return the name the passed user
70 char *GetUserName(int User)
76 * \brief Get the User ID of the named user
78 int GetUserID(const char *Username)
84 pwd = getpwnam(Username);
89 // Get internal ID (or create new user)
90 ret = Bank_GetUserByUnixID(pwd->pw_uid);
92 ret = Bank_AddUser(pwd->pw_uid);
99 * \brief Authenticate a user
100 * \return User ID, or -1 if authentication failed
102 int GetUserAuth(const char *Username, const char *Password)
104 if( strcmp(Username, "test") == 0 )
105 return Bank_GetUserByName("test");