def update_user(self, db, password, new_user, new_password=None): """ update username (and password) in BD """ if not new_password: new_password = password new_manager_obj = SQLAlchemyManager(db, self._user) logins = new_manager_obj.unit_obj.get_logins() logins_list = logins.get('logins') name_list = logins.get('name') for i in range(len(logins_list)): password_for_login = new_manager_obj.unit_obj.get_password( self._user, password, logins_list[i], name_list[i]) new_manager_obj.unit_obj \ .update_unit(new_user, new_password, logins_list[i], password_for_login=password_for_login, name=name_list[i]) if new_password: secret_password = new_user + new_password else: secret_password = new_user + password pass_hash = get_hash(secret_password.encode("utf-8")) self._session.query(User) \ .filter(User.user == self._user).update({"user": new_user, "password": pass_hash}) self._session.commit() log_and_print( f'User "{self._user}" updated. New username is "{new_user}"', level=INFO)
def udelete(user, password, db): """ delete user command """ manager_obj = SQLAlchemyManager(db, user) manager_obj.user_obj.del_user() log_and_print(f'User named "{user}" deleted', level=INFO)
def ushow(db): """ show users command """ manager_obj = SQLAlchemyManager(db) users = manager_obj.user_obj.all_users() for user in users: print(user) log_and_print(f'Show users command is done', print_need=False, level=INFO)
def show(ctx, user, password, category, db): """ show logins command """ manager_obj = SQLAlchemyManager(db, user) logins = manager_obj.unit_obj.get_logins(category) units_composition_obj = UnitsComposition(logins) units_composition_obj.prepare_data() res_str = units_composition_obj.make_str_logins(ctx.obj['FLAGS']) print(res_str) log_and_print(f'Show logins command is done', print_need=False, level=INFO)
def validate_new_user(ctx, param, value): """ Check new user name """ if not re.match('^[A-Za-z][A-Za-z0-9_-]*$', value): log_and_print( 'The user name must consist of English letters, ' 'numbers, and underscores. Start with a letter', level=ERROR) exit(-1) else: return value
def uadd(user, password, db): """ add user command """ manager_obj = SQLAlchemyManager(db, user) if manager_obj.user_obj.check_user(): log_and_print(f'User named "{user}" already exists', level=ERROR) exit(-1) else: manager_obj.user_obj.add_user(password) log_and_print(f'User named "{user}" created', level=INFO)
def delete(user, password, login, name, db): """ delete login and password command """ manager_obj = SQLAlchemyManager(db, user) if manager_obj.unit_obj.check_login(login, name): manager_obj.unit_obj.delete_unit(login, name) log_and_print(f'Login "{login}" deleted', level=INFO) else: log_and_print(f'login "{login}" with "{name}"' f' name not exists', level=ERROR)
def get(user, password, login, name, db): """ get password by login command """ manager_obj = SQLAlchemyManager(db, user) if manager_obj.unit_obj.check_login(login, name): pyperclip.copy( manager_obj.unit_obj.get_password(user, password, login, name)) log_and_print(f'Password is placed on the clipboard', level=INFO) else: log_and_print(f'login "{login}" with "{name}"' f' name not exists', level=ERROR)
def add(user, password, login, password_for_login, category, url, name, db): """ add login and password command """ manager_obj = SQLAlchemyManager(db, user) if manager_obj.unit_obj.check_login(login, name): log_and_print(f'login "{login}" with "{name}"' f' name already exists', level=ERROR) else: category = 'default' if category is None else category manager_obj.unit_obj\ .add_unit(user, password, login, password_for_login, name, category, url) log_and_print(f'Login "{login}" added', level=INFO)
def uupdate(user, password, new_username, new_password, db): """ update username (and password) command """ manager_obj = SQLAlchemyManager(db, user) new_password = None if new_password == '' else new_password if manager_obj.user_obj.check_user(new_username) and not new_password: log_and_print( f'User named "{new_username}" already exists ' f'and no new password is given', level=ERROR) else: manager_obj.user_obj.update_user(db, password, new_username, new_password)
def validate_password(ctx, param, value): """ Check password """ if 'USER' not in ctx.obj.keys(): ctx.obj['PASSWORD'] = value return value user = ctx.obj['USER'] manager_obj = SQLAlchemyManager(FILE_DB, user) if not manager_obj.user_obj.check_user_password(value): log_and_print(f'Incorrect password for user named "{user}"', level=ERROR) exit(-1) else: return value
def validate_user(ctx, param, value): """ Check user exists """ manager_obj = SQLAlchemyManager(FILE_DB, value) if not manager_obj.user_obj.check_user(): log_and_print(f'User named "{value}" not exists', level=ERROR) exit(-1) elif 'PASSWORD' in ctx.obj.keys() \ and not manager_obj.user_obj.check_user_password(ctx.obj['PASSWORD']): log_and_print(f'Incorrect password for user named "{value}"', level=ERROR) exit(-1) else: ctx.obj['USER'] = value return value
def update(user, password, login, name, new_login, new_name, password_for_login, new_category, url, db): """Update unit""" manager_obj = SQLAlchemyManager(db, user) new_login = login if new_login is None else new_login if not manager_obj.unit_obj.check_login(login, name): log_and_print(f'login "{login}" with "{name}"' f' name not exists', level=ERROR) elif manager_obj.unit_obj.check_login(new_login, new_name) \ and (login != new_login or name != new_name): log_and_print(f'login "{login}" with "{name}"' f' name already exists', level=ERROR) else: password_for_login = None if password_for_login == '' else password_for_login manager_obj.unit_obj\ .update_unit(user, password, login, name, new_login, password_for_login, new_category, url, new_name) log_and_print(f'Login "{login}" updated', level=INFO)