Exemplo n.º 1
0
 def create_user(session, name, login, password) -> int:
     user = Users(name=name,
                  login=login,
                  password=generate_password_hash(password))
     session.add(user)
     session.commit()
     return user.id
Exemplo n.º 2
0
async def validate_update_user_form(conn, form):
    username = form.get('username')
    first_name = form.get('first_name')
    last_name = form.get('last_name')
    old_password = form.get('old_password')
    new_password = form.get('new_password')
    permission = form.get('permission')
    city = form.get('city')
    user_id = form.get('user_id')

    if not username:
        return 'username is required'
    if not permission:
        return 'permission is required'
    
    if not city or city == 'None':
        city = ''
    
    user = await models.get_user_by_id(conn, user_id)

    if old_password and new_password:
        if check_password_hash(old_password, user['password']):
            password = generate_password_hash(new_password)
        else:
            return 'invalid password'
    else:
        password = None
    
    await models.update_user(conn, user_id, username, first_name, last_name, permission, city, password)
Exemplo n.º 3
0
    async def post(self):
        data = await self.json()

        required = ['login', 'password']
        if not all(key in data for key in required):
            return web.json_response({
                "status": "error",
                "error": "Missing values"
            })

        user_key = User.get_user_by_key(self.app['db'], node_identifier)

        if not user_key:
            password_hash = generate_password_hash(data['password'])
            create_user = await User.create_user(self.app['db'],
                                                 node_identifier,
                                                 data['login'], password_hash)

            response = {
                "status": "User created",
                "key": node_identifier,
                "login": data['login']
            }
            return web.json_response(response)
        else:
            return web.json_response({"error": "User already exists"})
Exemplo n.º 4
0
def create_sample_data(target_config=None):
    engine = get_engine(target_config)

    with engine.connect() as conn:
        conn.execute(users.insert(), [
            {
                'key': str(generate_key('-', '')),
                'login': '******',
                'password': generate_password_hash('leo')
            },
            {
                'key': str(generate_key('-', '')),
                'login': '******',
                'password': generate_password_hash('richard')
            },
        ])
Exemplo n.º 5
0
def create_admin(engine):
    username = '******'
    password_hash = generate_password_hash('admin')
    role = 'admin'
    with engine.connect() as conn:
        conn.execute(users.insert().values(username=username,
                                           password_hash=password_hash,
                                           role=role))
Exemplo n.º 6
0
def create_sample_data(target_config=None):
    engine = get_engine(target_config)

    with engine.connect() as conn:
        conn.execute(users.insert(), [
            {'username': '******',
             'email': '*****@*****.**',
             'password_hash': generate_password_hash('adam')},
            {'username': '******',
             'email': '*****@*****.**',
             'password_hash': generate_password_hash('bob')},
        ])
        conn.execute(posts.insert(), [
            {'user_id': 1, 'body': 'Lovely day'},
            {'user_id': 2, 'body': 'Roses are red'},
            {'user_id': 2, 'body': 'Lorem ipsum'},
        ])
Exemplo n.º 7
0
async def create_user(conn, username, password, email, role='user'):
    password_hash = generate_password_hash(password)
    result = await conn.execute(
        users.insert().values(username=username,
                              password_hash=password_hash,
                              email=email,
                              role=role))
    user = await result.first()
    return dict(user)
Exemplo n.º 8
0
def create_admin(engine):
    username = os.environ.get('3DEPOSIT_ADMIN_USERNAME')
    password_hash = generate_password_hash(
        os.environ.get('3DEPOSIT_ADMIN_PASSWORD'))
    role = 'admin'
    email = os.environ.get('3DEPOSIT_ADMIN_EMAIL')
    with engine.connect() as conn:
        conn.execute(users.insert().values(username=username,
                                           password_hash=password_hash,
                                           role=role,
                                           email=email))
Exemplo n.º 9
0
async def register(request):
    """
    Register new user.

    Args:
        request: instance of request.

    Returns:
        json with information about new user
    """
    db = request.app['db']
    user = await request.json()

    user_id = await return_last_id(db) + 1

    if user['login'] in await return_all_users_login(db):
        return web.json_response(
            {
                'error': 'User already exist',
            },
            status=web.HTTPUnauthorized.status_code,
        )

    access_token, refresh_token, expires_in = await generate_tokens(user_id)

    if len(user['password']) < 8:
        return web.json_response(
            {
                'error': 'Password is to short',
            },
            status=web.HTTPBadRequest.status_code,
        )

    await db.user.insert_one(
        {
            'id': user_id,
            'login': user['login'],
            'refresh_token': refresh_token.decode('utf-8'),
            'pw_hash': generate_password_hash(user['password']),
        },
    )

    return web.json_response(
        {
            'id': user_id,
            'login': user['login'],
            'accessToken': access_token.decode('utf-8'),
            'refreshToken': refresh_token.decode('utf-8'),
            'expires_in': expires_in,
        },
        status=web.HTTPCreated.status_code,
    )
Exemplo n.º 10
0
def generate_users(session: Session, count: int) -> list:
    faker = Faker('ru_RU')
    default_pass = generate_password_hash('skytrack')
    result = []
    for i in range(count):
        fake = faker.simple_profile(sex=None)
        user = Users(name=fake['name'],
                     login=fake['username'],
                     password=default_pass)
        result.append(user)
        log.info(f"Generate user: {user}")
        session.add(user)
    session.commit()
    for i in result:
        session.refresh(i)
    return result
Exemplo n.º 11
0
async def validate_create_user_form(conn, form):
    username = form.get('username')
    first_name = form.get('first_name')
    last_name = form.get('last_name')
    permission = form.get('permission')
    password = form.get('password')
    confirm_password = form.get('confirm_password')

    if not username:
        return 'username is required'
    if not password:
        return 'password is required'

    result = await conn.execute(models.users.select().where(models.users.c.username==username))
    copy_user = await result.first()
    if copy_user:
        return 'this username is already exists'
    
    if password != confirm_password:
        return 'the passwords are different'
    
    password_hash = generate_password_hash(password)
    await models.create_user(conn, username, first_name, last_name, password_hash, permission)
Exemplo n.º 12
0
async def generate_user(conn, **kw):
    # u = User(name='root', password_hash=generate_password_hash('password'), admin=1)
    u = User(name='kedaduck',
             password_hash=generate_password_hash('wqh930129'),
             admin=1)
    await u.save(conn)
Exemplo n.º 13
0
def create_sample_data(target_config=None):
    engine = get_engine(target_config)

    with engine.connect() as conn:
        conn.execute(users.insert(),
                     [{
                         'name': 'Nikita',
                         'secondname': 'Russkih',
                         'email': '*****@*****.**',
                         'phone': '89999999999',
                         'birthday': '22.10.1999',
                         'occupation': 'student',
                         'city': 'SP',
                         'password': generate_password_hash('8888')
                     }, {
                         'name': 'Marat',
                         'secondname': 'Chuchalov',
                         'email': '*****@*****.**',
                         'phone': '89999999990',
                         'birthday': '13.04.1999',
                         'occupation': 'stud',
                         'city': 'Kazan',
                         'password': generate_password_hash('123')
                     }])

        conn.execute(projects.insert(), [
            {
                'name': 'Двигатели',
                'company': 'Lada',
                'author_id': 1,
                'description': 'Создание машин',
                'presentation': 'None',
                'deadline': datetime.utcnow(),
                'member': 0,
                'gift': '100000',
                'total': False
            },
            {
                'name': 'Лодки',
                'company': 'BMW',
                'author_id': 2,
                'description': 'Создание лодок',
                'presentation': 'None',
                'deadline': datetime.utcnow(),
                'member': 0,
                'gift': '1000000',
                'total': False
            },
        ])

        conn.execute(projects_user.insert(), [{
            'user_id': 1,
            'project_id': 2
        }, {
            'user_id': 2,
            'project_id': 1
        }])

        conn.execute(answer_user.insert(), [{
            'user_id': 1,
            'project_id': 2,
            'answer': 'None'
        }, {
            'user_id': 2,
            'project_id': 1,
            'answer': 'None'
        }])

        conn.execute(results.insert(), [{
            'answer_id': 1,
            'score': 5,
            'gift': '0'
        }, {
            'answer_id': 2,
            'score': 100,
            'gift': '10000000'
        }])