Add password reset to Crafty Console

This commit is contained in:
amcmanu3 2022-05-16 15:39:10 -04:00
parent 7d8515321b
commit a2b711b5ac
3 changed files with 33 additions and 4 deletions

View File

@ -69,11 +69,13 @@ class UsersController:
elif key == "password":
if user_data["password"] is not None and user_data["password"] != "":
up_data["password"] = self.helper.encode_pass(user_data["password"])
elif key == "lang":
up_data["lang"] = user_data["lang"]
elif key == "hints":
up_data["hints"] = user_data["hints"]
elif base_data[key] != user_data[key]:
up_data[key] = user_data[key]
up_data["last_update"] = self.helper.get_time_as_string()
up_data["lang"] = user_data["lang"]
up_data["hints"] = user_data["hints"]
logger.debug(f"user: {user_data} +role:{added_roles} -role:{removed_roles}")
for role in added_roles:
HelperUsers.get_or_create(user_id=user_id, role_id=role)

View File

@ -3,6 +3,7 @@ import cmd
import time
import threading
import logging
import getpass
from app.classes.shared.console import Console
from app.classes.shared.import3 import Import3
@ -11,11 +12,13 @@ logger = logging.getLogger(__name__)
class MainPrompt(cmd.Cmd):
def __init__(self, helper, tasks_manager, migration_manager):
def __init__(self, helper, tasks_manager, migration_manager, main_controller):
super().__init__()
self.helper = helper
self.tasks_manager = tasks_manager
self.migration_manager = migration_manager
self.controller = main_controller
# overrides the default Prompt
self.prompt = f"Crafty Controller v{self.helper.get_version_string()} > "
@ -49,6 +52,30 @@ class MainPrompt(cmd.Cmd):
else:
Console.info("Unknown migration command")
def do_set_passwd(self, line):
try:
username = str(line).lower()
user_id = self.controller.users.get_id_by_name(username)
except Exception as e:
Console.error(f"User: {line} Not Found")
return False
new_pass = getpass.getpass(prompt=f"NEW password for: {username} > ")
new_pass_conf = getpass.getpass(prompt="Re-enter your password: > ")
if new_pass != new_pass_conf:
Console.error("Passwords do not match. Please try again.")
return False
if len(new_pass) > 512:
Console.warning("Password Too Long")
return False
if len(new_pass) < 6:
Console.warning("Password Too Short")
return False
self.controller.users.update_user(user_id, {"password": new_pass})
@staticmethod
def do_threads(_line):
for thread in threading.enumerate():

View File

@ -168,7 +168,7 @@ if __name__ == "__main__":
if not controller.check_system_user():
controller.add_system_user()
Crafty = MainPrompt(helper, tasks_manager, migration_manager)
Crafty = MainPrompt(helper, tasks_manager, migration_manager, controller)
project_root = os.path.dirname(__file__)
controller.set_project_root(project_root)