Esempio n. 1
0
    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)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
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
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
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)
Esempio n. 9
0
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)
Esempio n. 10
0
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)
Esempio n. 11
0
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
Esempio n. 12
0
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
Esempio n. 13
0
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)