forked from SDU-NetdiskTeam/seahub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseahub-admin.py
91 lines (79 loc) · 2.34 KB
/
seahub-admin.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
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# encoding: utf-8
# Copyright (c) 2012-2016 Seafile Ltd.
import sqlite3
import os
import sys
import time
import hashlib
import getpass
# Get .ccnet directory from argument or user input
if len(sys.argv) >= 2:
ccnet_dir = sys.argv[1]
else:
home_dir = os.path.join(os.path.expanduser('~'), '.ccnet')
ccnet_dir = input("Enter ccnet directory:(leave blank for %s) " % home_dir)
if not ccnet_dir:
ccnet_dir = home_dir
# Test usermgr.db exists
usermgr_db = os.path.join(ccnet_dir, 'PeerMgr/usermgr.db')
if not os.path.exists(usermgr_db):
print('%s DOES NOT exist. FAILED' % usermgr_db)
sys.exit(1)
# Connect db
conn = sqlite3.connect(usermgr_db)
# Get cursor
c = conn.cursor()
# Check whether admin user exists
sql = "SELECT email FROM EmailUser WHERE is_staff = 1"
try:
c.execute(sql)
except sqlite3.Error as e:
print("An error orrured:", e.args[0])
sys.exit(1)
staff_list = c.fetchall()
if staff_list:
print("Admin is already in database. Email as follows: ")
print('--------------------')
for e in staff_list:
print(e[0])
print('--------------------')
choice = input('Previous admin would be deleted, would you like to continue?[y/n] ')
if choice == 'y':
sql = "DELETE FROM EmailUser WHERE is_staff = 1"
try:
c.execute(sql)
except sqlite3.Error as e:
print("An error orrured:", e.args[0])
sys.exit(1)
else:
print('Previous admin is deleted.')
else:
conn.close()
sys.exit(0)
# Create admin user
choice = input('Would you like to create admin user?[y/n]')
if choice != 'y':
conn.close()
sys.exit(0)
username = input('E-mail address:')
passwd = getpass.getpass('Password:')
passwd2 = getpass.getpass('Password (again):')
if passwd != passwd2:
print("Two passwords NOT same.")
sys.exit(1)
mySha1 = hashlib.sha1()
mySha1.update(passwd.encode('utf-8'))
enc_passwd = mySha1.hexdigest()
sql = "INSERT INTO EmailUser(email, passwd, is_staff, is_active, ctime) VALUES ('%s', '%s', 1, 1, '%d');" % (username, enc_passwd, time.time()*1000000)
try:
c = conn.cursor()
c.execute(sql)
conn.commit()
except sqlite3.Error as e:
print("An error occured:", e.args[0])
sys.exit(1)
else:
print("Admin user created successfully.")
# Close db
conn.close()