-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.cgi
executable file
·268 lines (257 loc) · 8.9 KB
/
admin.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/local/bin/python
# change top line to the path of the Python interpreter on your system
""" admin.cgi This file handles input from the account administrators
form used to create/delete/view/mark inactive accounts.
Richard Kay last changed: 21 Feb 2003
Changes: 21 Feb 2003: added facility to modify member details
"""
# uncomment next 3 lines to debug script
#import sys
#sys.stderr=sys.stdout
#print "Content-type: text/plain\n"
import cgiutils
import tablcgi
from table_details import btab,mtab
# globals
members=None
def main():
cgiutils.html_header(title="LETS Accounting administration results")
if len(cgiutils.keys()) == 0:
# if no keys authentication data missing
cgiutils.html_end(error="Missing authentication details.")
return
elif not cgiutils.has_required(["ac_id","pin","user_ac_id"]):
cgiutils.html_end(error="Missing account identifier or PIN.")
return
# check if has required keys and of correct format
ac_id=cgiutils.firstval("ac_id")
pinentry=cgiutils.firstval("pin")
user_ac_id=cgiutils.firstval("user_ac_id")
# only do work for the admin account
if not cgiutils.is_valid(ac_id,allowed='^admin$'):
cgiutils.html_end(error="Only the administrator is allowed to do this.")
return
if not cgiutils.is_valid(pinentry,allowed='^[1-9][0-9]{3,3}$'):
cgiutils.html_end(error="Invalid PIN format.")
return
# check user account no./id
if not cgiutils.is_valid(user_ac_id,max_leng=8,allowed='^[\w]+$'):
cgiutils.html_end(error="Invalid user account id/no.")
return
# check pin
members=tablcgi.table(mtab)
ismember=members.has_key(ac_id)
if not ismember:
cgiutils.html_end(error="admin account not found.installation error.")
return
else:
index=members.find(ac_id)
pin=members.data[index]["pin"]
if int(pinentry) != int(pin):
cgiutils.html_end(error="Incorrect PIN entered.")
return
# which submit button was pressed ?
option="none"
for word in ["add","modify","inactive","active","delete","view"]:
if cgiutils.has_key(word):
option=word
break
if option=="none":
cgiutils.html_end(error="Invalid submit button value.")
return
# If we've got this far, there should either be a valid option and PIN OK
if option == "add": # need details for new account
if not cgiutils.has_required(["user_name","email","start_bal"]):
cgiutils.html_end(error="Missing user_name, email or starting balance.")
return
# validate fields
email=cgiutils.firstval("email")
if not cgiutils.is_email(email):
cgiutils.html_end(error="Invalid email address.")
return
user_name=cgiutils.firstval("user_name")
if not cgiutils.is_valid(user_name,max_leng=60,
allowed='^[A-Za-z][\w\'\- ]+$'):
cgiutils.html_end(error="Invalid name.")
return
# check account doesn't exist
if members.has_key(user_ac_id):
cgiutils.html_end(error="Account number/id exists. Can't add account.")
return
sbal=cgiutils.stof("start_bal")
if type(sbal) == type(None): # wasn't valid float
cgiutils.html_end(error="Invalid starting balance.")
return
if sbal > 1000000.0 or sbal < -1000000.0: # unreasonable balance
cgiutils.html_end(error="starting balance too high or low.")
return
# if still here, generate new PIN, create and add row to members
# and balances tables
newpin=cgiutils.make_pin()
row={}
row["ac_id"]=user_ac_id
row["name"]=user_name
row["email"]=email
row["pin"]=newpin
row["active"]='Y'
members.addrow(row)
brow={}
brow["ac_id"]=user_ac_id
brow["balance"]=sbal
brow["cfwd"]=sbal
brow["in"]=0.0
brow["out"]=0.0
brow["turnover"]=0.0
balance=tablcgi.table(btab)
balance.addrow(brow)
print "<p>new member added"
cgiutils.html_end(received=1)
return
elif option == "modify": # modify details for account
modifications=0 # counts number of fields changed
# check account does exist
if not members.has_key(user_ac_id):
error="Account id/no. doesn't exist. Can't modify."
cgiutils.html_end(error)
return
mindex=members.find(user_ac_id)
balance=tablcgi.table(btab)
bindex=balance.find(user_ac_id)
# check email address
if cgiutils.has_key("email"):
# validate field
email=cgiutils.firstval("email")
if not cgiutils.is_email(email):
cgiutils.html_end(error="Invalid email address.")
return
members.data[mindex]["email"]=email
modifications+=1
# check account name
if cgiutils.has_key("user_name"):
# validate field
user_name=cgiutils.firstval("user_name")
if not cgiutils.is_valid(user_name,max_leng=60,
allowed='^[A-Za-z][\w\'\- ]+$'):
cgiutils.html_end(error="Invalid name.")
return
members.data[mindex]["name"]=user_name
modifications+=1
if modifications: # save changes to members table if any
members.save()
# check CFWD or start balance
if cgiutils.has_key("start_bal"):
# validate field
sbal=cgiutils.stof("start_bal")
if type(sbal) == type(None): # wasn't valid float
cgiutils.html_end(error="Invalid starting balance.")
return
if sbal > 1000000.0 or sbal < -1000000.0: # unreasonable balance
cgiutils.html_end(error="starting balance too high or low.")
return
balance=tablcgi.table(btab)
old_cfwd=balance.data[bindex]["cfwd"]
difference=sbal-old_cfwd
balance.data[bindex]["cfwd"]=sbal
balance.data[bindex]["balance"]+=difference
balance.save()
modifications+=1
if not modifications: # all changeable fields were blank ?
cgiutils.html_end(error="No modified field values submitted.")
return
else:
print "<p> %d field/s modified.</p>" % modifications
cgiutils.html_end(received=1)
return
elif option == "inactive":
# check account does exist
if not members.has_key(user_ac_id):
error="Account id/no. doesn't exist. Can't make inactive."
cgiutils.html_end(error)
return
mindex=members.find(user_ac_id)
members.data[mindex]["active"]='N'
members.save()
cgiutils.html_end(received=1)
return
elif option == "active":
# check account does exist
if not members.has_key(user_ac_id):
error="Account id/no. doesn't exist. Can't reactivate."
cgiutils.html_end(error)
return
mindex=members.find(user_ac_id)
members.data[mindex]["active"]='Y'
members.save()
cgiutils.html_end(received=1)
return
elif option == "delete":
if not members.has_key(user_ac_id):
error="Account id/no. doesn't exist. Can't delete."
cgiutils.html_end(error)
return
mindex=members.find(user_ac_id)
balance=tablcgi.table(btab)
if not balance.has_key(user_ac_id):
error="""Account id/no. has member record but no balance record.
please inform webmaster."""
cgiutils.html_end(error)
return
bindex=balance.find(user_ac_id)
bal=balance.data[bindex]["balance"]
if bal > 0.001 or bal < -0.001:
error="Account has non-zero balance. Unwise to delete it. Clear it first."
cgiutils.html_end(error)
return
balance.delrow(user_ac_id)
members.delrow(user_ac_id)
cgiutils.html_end(received=1)
return
elif option == "view":
if not members.has_key(user_ac_id):
error="Account id/no. doesn't exist. Can't view it."
cgiutils.html_end(error)
return
mindex=members.find(user_ac_id)
balance=tablcgi.table(btab)
if not balance.has_key(user_ac_id):
error="""Account id/no. has member record but no balance record.
please inform webmaster."""
cgiutils.html_end(error)
return
bindex=balance.find(user_ac_id)
bal=balance.data[bindex]["balance"]
# create members table view object
mtabvo=tablcgi.table_def(mtab)
mtabvo.file=None
mtabvo.meta["keys"].append("cfwd")
mtabvo.meta["keys"].append("balance")
mtabvo.meta["keys"].append("in")
mtabvo.meta["keys"].append("out")
mtabvo.meta["keys"].append("turnover")
mtabvo.meta["colheads"].append("CFWD")
mtabvo.meta["colheads"].append("Balance")
mtabvo.meta["colheads"].append("In")
mtabvo.meta["colheads"].append("Out")
mtabvo.meta["colheads"].append("Turnover")
mtabvo.meta["formats"].append("%.2f")
mtabvo.meta["formats"].append("%.2f")
mtabvo.meta["formats"].append("%.2f")
mtabvo.meta["formats"].append("%.2f")
mtabvo.meta["formats"].append("%.2f")
mtv=tablcgi.table(mtabvo)
row={}
for key in ["ac_id","pin","name","email","active"]:
row[key]=members.data[mindex][key]
for key in ["cfwd","balance","in","out","turnover"]:
row[key]=balance.data[bindex][key]
mtv.addrow(row)
mtv.tab2html()
cgiutils.html_end(received=1)
return
if __name__ == "__main__":
try:
main()
except:
import traceback
print "error detected in admin.cgi main()"
traceback.print_exc()