138 lines
5.6 KiB
Python
138 lines
5.6 KiB
Python
|
from os import getcwd as pwd
|
||
|
from getpass import getpass
|
||
|
from common.common import clear
|
||
|
import csv
|
||
|
|
||
|
def fin_03():
|
||
|
"""You need to create a program that will store the user ID and passwords
|
||
|
for the users of a system. It should display the following menu:
|
||
|
1) Create a new User ID
|
||
|
2) Change a password
|
||
|
3) Display all User IDs
|
||
|
4) Quit
|
||
|
|
||
|
Enter selection:
|
||
|
If the user selects 1, it should ask them to enter a user ID. It should check
|
||
|
if the user ID is already in the list. If it is, the program should display
|
||
|
a suitable message and ask them to select another user ID. Once a suitable
|
||
|
user ID has been entered it should ask for a password. Passwords should be
|
||
|
scored with 1 point for each of the following:
|
||
|
- it should have at least 8 characters.
|
||
|
- it should include uppercase letters.
|
||
|
- it should include lower case letters.
|
||
|
- it should include numbers.
|
||
|
- it should include at least one special character such as
|
||
|
!, £, $, %, &, <, * or @.
|
||
|
If the password scores only 1 or 2 it should be rejected with a message
|
||
|
saying it is a weak password; if it scores 3 or 4 tell them that "This
|
||
|
password could be improved." Ask them if they want to try again. If it
|
||
|
scores 5 tell them they have selected a strong password. Only acceptable
|
||
|
user IDs and passwords should be added to the end of the .csv file. If they
|
||
|
select 2 from the menu they will need to enter a user ID, check to see if
|
||
|
the user ID exists in the list, and if it does, allow the user to change the
|
||
|
password and save the changes to the .csv file. Make sure the program only
|
||
|
alters the existing password and does not create a new record. If the user
|
||
|
selects 3 from the menu, display all the user IDs but not the passwords. If
|
||
|
the user selects 4 from the menu it should stop the program."""
|
||
|
file_path = f"{pwd()}/final/files/users.csv"
|
||
|
menu = """
|
||
|
Administrador de Usuarios
|
||
|
|
||
|
1) Crear nuevo ID de usuario
|
||
|
2) Cambiar una contraseña
|
||
|
3) Ver todos los ID de usuario
|
||
|
s) Salir
|
||
|
"""
|
||
|
def create_pass():
|
||
|
points = 0
|
||
|
while points < 3:
|
||
|
points = 0
|
||
|
password = getpass("Ingresa la contraseña: ")
|
||
|
if len(password) >= 8:
|
||
|
points += 1
|
||
|
x = len([1 for p in password if p.isupper()])
|
||
|
points += 1 if x > 0 else 0
|
||
|
x = len([1 for p in password if p.islower()])
|
||
|
points += 1 if x > 0 else 0
|
||
|
x = len([1 for p in password if p.isdigit()])
|
||
|
points += 1 if x > 0 else 0
|
||
|
x = len([1 for p in password if p in '!£$%&<*@'])
|
||
|
points += 1 if x > 0 else 0
|
||
|
if points < 3:
|
||
|
print("La contraseña es demasiado debil")
|
||
|
continue
|
||
|
elif 2 < points < 5:
|
||
|
resp = input("El password podría ser mejor, ¿quieres mejorarlo? (S|n): ").lower()
|
||
|
if resp != 'n':
|
||
|
points = 0
|
||
|
continue
|
||
|
repass = getpass("Verificar la contraseña:")
|
||
|
if repass != password:
|
||
|
points = 0
|
||
|
return password
|
||
|
|
||
|
def create_user():
|
||
|
user = input("Ingresa el nombre de usuario: ").strip()
|
||
|
temp_users = get_users()
|
||
|
if temp_users is None or user not in temp_users:
|
||
|
password = create_pass()
|
||
|
with open(file_path, '+a') as file:
|
||
|
file.write(f"{user},{password}\n")
|
||
|
input("Usuario creado, presiona Enter para continuar")
|
||
|
else:
|
||
|
input("Usuario ya existe, presiona Enter para continuar")
|
||
|
|
||
|
def change_pass():
|
||
|
user = input("Ingresa el nombre del usuario a modificar contraseña: ")
|
||
|
temp_users = get_users()
|
||
|
if temp_users is not None and user in temp_users:
|
||
|
password = create_pass()
|
||
|
temp_data = []
|
||
|
with open(file_path, 'r') as file:
|
||
|
reader = csv.reader(file)
|
||
|
for line in reader:
|
||
|
if user in line[0]:
|
||
|
temp_data.append([line[0],password])
|
||
|
else:
|
||
|
temp_data.append([line[0],line[1]])
|
||
|
with open(file_path, 'w') as file:
|
||
|
writer = csv.writer(file)
|
||
|
writer.writerows(temp_data)
|
||
|
print(f"Password actualizado exitosamente")
|
||
|
else:
|
||
|
print("El usuario ingresado no existe")
|
||
|
input("Presiona Enter para continuar")
|
||
|
|
||
|
def get_users():
|
||
|
temp_users = []
|
||
|
try:
|
||
|
with open(file_path, 'r') as file:
|
||
|
read = csv.reader(file)
|
||
|
for r in read:
|
||
|
temp_users.append(r[0])
|
||
|
except FileNotFoundError:
|
||
|
pass
|
||
|
return temp_users
|
||
|
|
||
|
while True:
|
||
|
clear()
|
||
|
print(f"Ruta archivo:\n{file_path}")
|
||
|
print(menu)
|
||
|
sel = input("Ingresa una opción: ")
|
||
|
match sel:
|
||
|
case "1":
|
||
|
create_user()
|
||
|
case "2":
|
||
|
change_pass()
|
||
|
case "3":
|
||
|
users = get_users()
|
||
|
print("\n Usuarios \n----------")
|
||
|
for user in users:
|
||
|
print(" "+user)
|
||
|
input("\nPresiona Enter para continuar")
|
||
|
case "s":
|
||
|
break
|
||
|
case _:
|
||
|
print("Debes ingresar una opción válida")
|
||
|
input("Presiona enter para continuar")
|