2388be8b58c2454830570e7cb4562605325a183e
[tpg/opendispense2.git] / src / cokebank_basic / main.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * cokebank.c - Coke-Bank management
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
8  * for full details.
9  */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <pwd.h>
13 #include <string.h>
14
15 // === IMPORTS ===
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);
22
23 // === PROTOTYPES ===
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);
30
31 // === CODE ===
32 /**
33  * \brief Load the cokebank database
34  */
35 void Init_Cokebank(void)
36 {
37         
38 }
39
40 /**
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
47  */
48 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
49 {
50         if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
51                 return 1;
52         if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) )
53                 return 1;
54         Bank_AlterUserBalance(DestUser, Ammount);
55         Bank_AlterUserBalance(SourceUser, -Ammount);
56         return 0;
57 }
58
59 /**
60  * \brief Get the balance of the passed user
61  */
62 int GetBalance(int User)
63 {
64         return 0;
65 }
66
67 /**
68  * \brief Return the name the passed user
69  */
70 char *GetUserName(int User)
71 {
72         return NULL;
73 }
74
75 /**
76  * \brief Get the User ID of the named user
77  */
78 int GetUserID(const char *Username)
79 {
80         struct passwd   *pwd;
81          int    ret;
82
83         // Get user ID
84         pwd = getpwnam(Username);
85         if( !pwd ) {
86                 return -1;
87         }
88
89         // Get internal ID (or create new user)
90         ret = Bank_GetUserByUnixID(pwd->pw_uid);
91         if( ret == -1 ) {
92                 ret = Bank_AddUser(pwd->pw_uid);
93         }
94
95         return ret;
96 }
97
98 /**
99  * \brief Authenticate a user
100  * \return User ID, or -1 if authentication failed
101  */
102 int GetUserAuth(const char *Username, const char *Password)
103 {
104         if( strcmp(Username, "test") == 0 )
105                 return Bank_GetUserByName("test");
106         return -1;
107 }
108

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