forked from noderod/greyfish
-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_user.py
80 lines (62 loc) · 2.29 KB
/
new_user.py
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
#!/usr/bin/env python3
"""
BASICS
Creates a new user, specified by their token
If the user already exists, an error result is returned
"""
import os, shutil
from flask import Flask, request
import base_functions as bf
app = Flask(__name__)
GREYFISH_FOLDER = os.environ['greyfish_path']+"/sandbox/"
hugo = bf.idb_writer('greyfish')
# toktok (str): User token
@app.route("/grey/create_user/<gkey>/<toktok>")
def create_user(toktok, gkey):
# Gets the IP address
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
# Records all failed logins
bf.failed_login(gkey, IP_addr, toktok, "create-new-user")
return "INVALID key, cannot create a new user"
try:
os.makedirs(GREYFISH_FOLDER+'DIR_'+str(toktok))
if bf.influx_logs:
hugo.write_points([{
"measurement":"signup",
"tags":{
"id":toktok,
},
"time":bf.timformat(),
"fields":{
"client-IP":IP_addr
}
}])
return "Greyfish cloud storage now available"
except:
return "User already has an account"
# Deletes an entire user directory
@app.route("/grey/delete_user/<gkey>/<toktok>")
def delete_user(toktok, gkey):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "delete-user")
return "INVALID key, cannot create a new user"
try:
shutil.rmtree(GREYFISH_FOLDER+'DIR_'+str(toktok))
if bf.influx_logs:
hugo.write_points([{
"measurement":"delete_account",
"tags":{
"id":toktok,
},
"time":bf.timformat(),
"fields":{
"client-IP":IP_addr
}
}])
return "User files and data have been completely deleted"
except:
return "User does not exist"
if __name__ == '__main__':
app.run()