Create add_user.py

This commit is contained in:
Saifeddine ALOUI 2024-01-16 09:45:46 +01:00 committed by GitHub
parent 91b0d1698a
commit 19500578f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import sys
import random
from getpass import getuser
from pathlib import Path
def generate_key(length=10):
"""Generate a random key of given length"""
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+[]{}|;:,.<>?/~'
return ''.join(random.choice(chars) for _ in range(length))
def add_user(users_list=None):
"""Add a new user to the users list file"""
user_name = input('Enter your username: ')
key = generate_key()
print(f'Your key is: {key}')
if not users_list or not users_list.exists():
users_list = Path(users_list) if users_list else Path('authorized_users.txt')
users_list.touch(exist_ok=True)
with open(users_list, 'a') as f:
f.write(f'{user_name}: {key}\n')
print(f'User {user_name} added to the authorized users list')
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--users_list':
add_user(Path(sys.argv[2]))
else:
add_user()