]> Softwares of Agnibho - librevax.git/blob - userman
Database dump
[librevax.git] / userman
1 #! /bin/env python3
2
3 # LibreVax
4 # Copyright (C) 2024 Dr. Agnibho Mondal
5 # This file is part of LibreVax.
6 # LibreVax is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
7 # LibreVax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8 # You should have received a copy of the GNU General Public License along with LibreVax. If not, see <https://www.gnu.org/licenses/>.
9
10 import sqlite3, sys, bcrypt, base64
11 from getpass import getpass
12
13 try:
14
15 connection=sqlite3.connect("data/database.db")
16 cur=connection.cursor()
17
18 if sys.argv[1]=="list":
19 record=cur.execute("SELECT * FROM users")
20 print("User\tAuth")
21 print("----\t----")
22 for r in record.fetchall():
23 print(r[0]+"\t"+r[1])
24 elif sys.argv[1]=="create":
25 try:
26 print("Creating user: "+sys.argv[2])
27 auth=input("Authorisation (all/none):")
28 pwd=getpass()
29 pwd=base64.b64encode(bcrypt.hashpw(pwd.encode("utf-8"), bcrypt.gensalt()))
30 cur.execute("INSERT INTO users (user, auth, hash) VALUES (?, ?, ?)", (sys.argv[2], auth, pwd))
31 except IndexError:
32 print("Username not provided")
33 elif sys.argv[1]=="update":
34 try:
35 print("Updating user: "+sys.argv[2])
36 auth=input("Authorisation (all/none):")
37 pwd=getpass()
38 pwd=base64.b64encode(bcrypt.hashpw(pwd.encode("utf-8"), bcrypt.gensalt()))
39 cur.execute("UPDATE users SET auth=?, hash=? WHERE user=?", (auth, pwd, sys.argv[2]))
40 if(cur.rowcount==0):
41 print("No record to update")
42 except IndexError:
43 print("Username not provided")
44 elif sys.argv[1]=="delete":
45 try:
46 print("Deleting user: "+sys.argv[2])
47 cur.execute("DELETE FROM users WHERE user=?", (sys.argv[2],))
48 if(cur.rowcount==0):
49 print("No record to delete")
50 except IndexError:
51 print("Username not provided")
52
53 connection.commit()
54 connection.close()
55
56 except IndexError:
57 print("Allowed commands: list, create, update, delete")