From e09f2c9bebc7b865881a6a1298da228f98df6c30 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 17 Jan 2021 09:17:46 +0100 Subject: [PATCH] Added Password change on cli --- cps/cli.py | 5 ++++- cps/ub.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cps/cli.py b/cps/cli.py index 65a4185a..07b719d2 100644 --- a/cps/cli.py +++ b/cps/cli.py @@ -110,7 +110,10 @@ if ipadress: sys.exit(1) # handle and check user password argument -user_password = args.s or None +user_credentials = args.s or None +if user_credentials and ":" not in user_credentials: + print("No valid username:password format") + sys.exit(3) # Handles enableing of filepicker filepicker = args.f or None diff --git a/cps/ub.py b/cps/ub.py index 03720387..f342e356 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -43,10 +43,11 @@ from sqlalchemy import Column, ForeignKey from sqlalchemy import String, Integer, SmallInteger, Boolean, DateTime, Float, JSON from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm.attributes import flag_modified +from sqlalchemy.sql.expression import func from sqlalchemy.orm import backref, relationship, sessionmaker, Session, scoped_session from werkzeug.security import generate_password_hash -from . import constants, logger +from . import constants, logger, cli log = logger.create() @@ -680,6 +681,21 @@ def init_db(app_db_path): create_admin_user(session) create_anonymous_user(session) + if cli.user_credentials: + username, password = cli.user_credentials.split(':') + user = session.query(User).filter(func.lower(User.nickname) == username.lower()).first() + if user: + user.password = generate_password_hash(password) + if session_commit() == "": + print("Password for user '{}' changed".format(username)) + sys.exit(0) + else: + print("Failed changing password") + sys.exit(3) + else: + print("Username '{}' not valid, can't change password".format(username)) + sys.exit(3) + def dispose(): global session