예제 #1
0
async def set_current_user(request, data, **kw):
    current_user = auth.current_user(request)
    if current_user is None:
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Phiên làm việc hết hạn, đăng nhập lại.'
            },
            status=523)

    uid = current_user['uid']
    if uid is None or uid.find(".") <= 0:
        auth.logout_user(request)
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Phiên làm việc hết hạn, đăng nhập lại.'
            },
            status=523)

    current_tenant_id = uid.split('.')[0]
    current_user_id = uid.split('.')[1]

    user_info = await motordb.db['user'].find_one({
        'tenant_id': current_tenant_id,
        'id': str(current_user_id)
    })
    if user_info is not None:
        data["owner_id"] = str(user_info['_id'])
예제 #2
0
async def user_logout(request):
    uid = auth.current_user(request)
    params = request.json
    # print(params)
    # user = db.session.query(User).filter(User.id == int(current_user)).first()
    user_update = db.session.query(User).filter(User.id == uid).first()
    if user_update is not None:
        user_update.group_last_access_id = params['group_last_access_id']
    db.session.commit()
    auth.logout_user(request)
    return json({})
예제 #3
0
async def user_logout(request):
    current_user = auth.current_user(request)
    user = db.session.query(User).filter(User.id == int(current_user)).first()
    try:
        set_logout_for_salary(user)
        # salary = db.session.query(Salary).filter(Salary.id_employee==17).first()
        # print("11111111111",salary)
        user.employee.status = 'offline'
        db.session.commit()
    except:
        pass
    auth.logout_user(request)
    return json({})
예제 #4
0
async def change_password(request):
    verify_access(request)

    current_tenant = get_current_tenant(request)
    if current_tenant is None or 'error_code' in current_tenant:
        return json(
            {
                'error_code': 'TENANT_UNKNOW',
                'error_message': 'Request Unknown'
            },
            status=523)

    current_user = auth.current_user(request)
    if current_user is None:
        auth.logout_user(request)
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Phiên làm việc hết hạn, đăng nhập lại.'
            },
            status=523)

    current_tenant_id = current_tenant.get('id')
    current_user_id = current_user['uid']
    if current_user_id is None:
        auth.logout_user(request)
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Phiên làm việc hết hạn, đăng nhập lại.'
            },
            status=523)

    user_info = db.session.query(User).filter(and_(User.tenant_id == current_tenant_id,\
                                                   User.id == current_user_id)).first()
    if user_info is None:
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Tài khoản không tồn tại.'
            },
            status=520)

    body_data = request.json
    current_password = body_data.get('current_password', None)
    new_password = body_data.get('new_password', None)

    # CHECK CURRENT PASSWORD CORRECT OR NOT
    if auth.verify_password(current_password, user_info.password,
                            user_info.salt) == False:
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Tên tài khoản hoặc mật khẩu không đúng'
            },
            status=523)

    user_info.password = auth.encrypt_password(new_password, user_info['salt'])
    user_info.updated_at = now_timestamp()
    db.session.commit()
    return json({'code': 'S200', 'message': 'Thành công'}, status=200)
예제 #5
0
async def current_user(request):
    current_user = auth.current_user(request)
    if current_user is None:
        auth.logout_user(request)
        return json(
            {
                "error_code": "E523",
                "error_message": "Phiên làm việc hết hạn!"
            },
            status=520)

    uid = current_user['uid']
    if uid is None:
        auth.logout_user(request)
        return json(
            {
                "error_code": "E523",
                "error_message": "Phiên làm việc hết hạn!"
            },
            status=520)

    current_user = db.session.query(User).filter(and_(User.id == uid)).first()

    if current_user is None:
        return json(
            {
                'error_code': 'NOT_EXIST',
                'error_message': 'User does not exist'
            },
            status=520)

    if current_user.tenant is None:
        return json(
            {
                'error_code': 'TENANT_UNKNOWN',
                'error_message': 'Request Unknown'
            },
            status=523)

    tenant_dict = to_dict(current_user.tenant)
    for key in exclude_attrs:
        if key in tenant_dict:
            del tenant_dict[key]

    current_user = to_dict(current_user)
    for key in exclude_attrs:
        if key in current_user:
            del current_user[key]

    request['session']['current_tenant_id'] = tenant_dict.get('id')
    current_user['tenant'] = tenant_dict

    return json({
        'id': str(current_user.get('id')),
        'display_name': current_user.get('display_name'),
        'phone': current_user.get('phone'),
        'email': current_user.get('email'),
        'gender': current_user.get('gender'),
        'avatar': current_user.get('avatar'),
        'tenant': current_user.get('tenant'),
        'current_tenant_id': tenant_dict.get('id')
    })

    auth.logout_user(request)
    return json(
        {
            "error_code": "SESSION_EXPIRED",
            "error_message": "Phiên làm việc hết hạn!"
        },
        status=520)
예제 #6
0
async def logout(request):
    try:
        auth.logout_user(request)
    except:
        pass
    return json({})
예제 #7
0
async def user_logout(request):
    auth.logout_user(request)
    return json({})
예제 #8
0
async def logout(request):
    auth.logout_user(request)
    return text("OK")