Beispiel #1
0
def get_user_business_detail(username, bk_biz_id):
    """Get authorized business list for a exact username.

    :param object username: User username
    :param bool use_cache: (Optional)
    """

    user_info = _get_user_info(username)
    client = get_client_by_user(username)
    result = client.cc.search_business({
        "bk_supplier_account":
        user_info["bk_supplier_account"],
        "condition": {
            "bk_data_status": {
                "$in": ["enable", "disabled", None]
            },
            "bk_biz_id": bk_biz_id
        },
    })

    if result["result"]:
        data = result["data"]["info"]
    else:
        raise exceptions.APIError(system="cc",
                                  api="search_business",
                                  message=result["message"],
                                  result=result)

    if len(data) != 1:
        raise exceptions.APIError(system="cc",
                                  api="search_business",
                                  message=result["message"],
                                  result=result)

    return data[0]
Beispiel #2
0
def _get_user_info(request, use_cache=True):
    """
    获取用户基本信息
    @param request:
    @param use_cache:
    @return:
    """
    user = request.user
    cache_key = "%s_get_user_info_%s" % (CACHE_PREFIX, user.username)
    data = cache.get(cache_key)
    if not (use_cache and data):
        client = settings.ESB_GET_CLIENT_BY_REQUEST(request)
        auth = getattr(client, settings.ESB_AUTH_COMPONENT_SYSTEM)
        get_user_info = getattr(auth, settings.ESB_AUTH_GET_USER_INFO)
        userinfo = get_user_info({})
        userinfo.setdefault('code', -1)
        if userinfo['result']:
            data = userinfo['data']
            if data:
                cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        elif userinfo['code'] in ('20101', 20101):
            raise exceptions.Unauthorized(userinfo['message'])
        elif userinfo['code'] in ('20103', 20103, '20201', 20201, '20202',
                                  20202):
            raise exceptions.Forbidden(userinfo['message'])
        else:
            raise exceptions.APIError(
                settings.ESB_AUTH_COMPONENT_SYSTEM, 'get_user',
                userinfo.get('detail_message', userinfo['message']))
    return data
Beispiel #3
0
def _get_user_info(request, use_cache=True):
    """
    获取用户基本信息
    @param request:
    @param use_cache:
    @return:
    """
    user = request.user
    cache_key = "%s_get_user_info_%s" % (CACHE_PREFIX, user.username)
    data = cache.get(cache_key)
    if not (use_cache and data):
        userinfo = get_user_info(request)
        userinfo.setdefault('code', -1)
        if userinfo['result']:
            data = userinfo['data']
            if data:
                cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        elif userinfo.get('code') in ('20101', 20101):
            raise exceptions.Unauthorized(userinfo['message'])
        elif userinfo.get('code') in ('20103', 20103, '20201', 20201, '20202',
                                      20202):
            raise exceptions.Forbidden(userinfo['message'])
        else:
            raise exceptions.APIError(
                'bk_api', 'get_user_info',
                userinfo.get('detail_message', userinfo['message']))
    return data
Beispiel #4
0
def get_user_business_list(username, use_cache=True):
    """Get authorized business list for a exact username.

    :param object username: User username
    :param bool use_cache: (Optional)
    """
    cache_key = "%s_get_user_business_list_%s" % (CACHE_PREFIX, username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        user_info = _get_user_info(username)
        client = get_client_by_user(username)
        result = client.cc.search_business({
            "bk_supplier_account":
            user_info["bk_supplier_account"],
            "condition": {
                "bk_data_status": {
                    "$in": ["enable", "disabled", None]
                }
            },
        })

        if result["result"]:
            data = result["data"]["info"]
            cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        else:
            raise exceptions.APIError(system="cc",
                                      api="search_business",
                                      message=result["message"],
                                      result=result)

    return data
Beispiel #5
0
def get_user_business_list(username, use_cache=True):
    """Get authorized business list for a exact username.

    :param object username: User username
    :param bool use_cache: (Optional)
    """
    cache_key = "%s_get_user_business_list_%s" % (CACHE_PREFIX, username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        user_info = _get_user_info(username)
        client = get_client_by_user(username)
        result = client.cc.search_business({
            'bk_supplier_account':
            user_info['bk_supplier_account'],
            'condition': {
                'bk_data_status': {
                    '$in': ['enable', 'disabled', None]
                }
            }
        })

        if result['result']:
            data = result['data']['info']
            cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        else:
            raise exceptions.APIError(system='cc',
                                      api='search_business',
                                      message=result['message'],
                                      result=result)

    return data
Beispiel #6
0
def _get_user_business_list(request, use_cache=True):
    """Get authorized business list for a exact username.

    :param object request: django request object.
    :param bool use_cache: (Optional)
    """
    user = request.user
    cache_key = "%s_get_user_business_list_%s" % (CACHE_PREFIX, user.username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        client = settings.ESB_GET_CLIENT_BY_REQUEST(request)
        result = client.cc.get_app_by_user_role({
            'user_role':
            ','.join(roles.CC_ROLES),
            # 'user_role': roles.MAINTAINERS,
        })

        if result['result']:
            data = result['data']

            # 获取用户所属开发商信息
            user_info = _get_user_info(request)
            # 兼容多开发商和单开发商模式
            if user_info.get('company_list', []):
                owner_list = [
                    owner['company_code']
                    for owner in user_info['company_list']
                ]
            elif user_info.get('company_code'):
                owner_list = [user_info.get('company_code')]
            else:
                owner_list = []

            # 按照开发商过滤
            for role, biz_list in data.iteritems():
                temp_list = []
                for item in biz_list:
                    if item.get('LifeCycle') not in ['3', _(u"停运")]:
                        if owner_list:
                            if item['Owner'] in owner_list:
                                temp_list.append(item)
                        else:
                            temp_list.append(item)
                data.update({role: temp_list})
            cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        elif result['code'] in ('20101', 20101):
            raise exceptions.Unauthorized(result['message'])
        elif result['code'] in ('20103', 20103, '20201', 20201, '20202',
                                20202):
            raise exceptions.Forbidden(result['message'])
        else:
            raise exceptions.APIError(
                'cc', 'get_app_by_user_role',
                result.get('detail_message', result['message']))

    return data
Beispiel #7
0
def _get_user_business_list(request, use_cache=True):
    """Get authorized business list for a exact username.

    :param object request: django request object.
    :param bool use_cache: (Optional)
    """
    user = request.user
    cache_key = "%s_get_user_business_list_%s" % (CACHE_PREFIX, user.username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        user_info = _get_user_info(request)
        client = settings.ESB_GET_CLIENT_BY_USER(request.user.username)
        result = client.cc.search_business({
            'bk_supplier_account':
            user_info['bk_supplier_account'],
            'condition': {
                'bk_data_status': {
                    '$in': ['enable', 'disabled', None]
                },
                '$or': [{
                    'bk_biz_developer': {
                        "$regex": user.username
                    }
                }, {
                    'bk_biz_productor': {
                        "$regex": user.username
                    }
                }, {
                    'bk_biz_maintainer': {
                        "$regex": user.username
                    }
                }, {
                    'bk_biz_tester': {
                        "$regex": user.username
                    }
                }]
            }
        })

        if result['result']:
            data = result['data']['info']
            cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        elif result.get('code') in ('20101', 20101):
            raise exceptions.Unauthorized(result['message'])
        elif result.get('code') in ('20103', 20103, '20201', 20201, '20202',
                                    20202):
            raise exceptions.Forbidden(result['message'])
        else:
            raise exceptions.APIError(
                'cc', 'search_business',
                result.get('detail_message', result['message']))

    return data
Beispiel #8
0
def update_user_info(request, cc_id, use_cache=True):
    cache_key = "%s_update_user_info_%s" % (CACHE_PREFIX, cc_id)
    data = cache.get(cache_key)

    if not (use_cache and data):
        result = get_user_info(request)
        if result['result']:
            _update_user_info(result['data'])
        elif result['code'] in ('20101', 20101):
            raise exceptions.Unauthorized(result['message'])
        elif result['code'] in ('20103', 20103):
            raise exceptions.Forbidden(result['message'])
        else:
            raise exceptions.APIError(
                settings.ESB_AUTH_COMPONENT_SYSTEM, 'get_user',
                result.get('detail_message', result['message']))

        cache.set(cache_key, True, DEFAULT_CACHE_TIME_FOR_CC)
Beispiel #9
0
def _get_business_info(request, app_id, use_cache=True, use_maintainer=False):
    """Get detail infomations for a exact app_id.

    @param object request: django request object.
    @param int app_id: cc_id of core.business model.
    @param use_maintainer: 使用运维身份请求
    """
    username = request.user.username
    business = Business.objects.get(cc_id=app_id)
    cache_key = "%s_get_business_info_%s_%s" % (CACHE_PREFIX, app_id, username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        if use_maintainer:
            client = get_client_by_user_and_biz_id(username, app_id)
        else:
            client = settings.ESB_GET_CLIENT_BY_REQUEST(request)
        result = client.cc.search_business({
            'bk_supplier_account': business.cc_owner,
            'condition': {
                'bk_biz_id': int(app_id)
            }
        })

        if result['result']:
            if not result['data']['info']:
                raise exceptions.Forbidden()
            data = result['data']['info'][0]
        elif result.get('code') in ('20101', 20101):
            raise exceptions.Unauthorized(result['message'])
        elif result.get('code') in ('20103', 20103, '20201', 20201,
                                    '20202', 20202):
            raise exceptions.Forbidden(result['message'])
        else:
            raise exceptions.APIError(
                'cc',
                'get_app_by_id',
                result.get('detail_message', result['message'])
            )

        cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)

    return data
Beispiel #10
0
def get_all_business_list(use_cache=True):
    username = settings.SYSTEM_USE_API_ACCOUNT
    cache_key = "%s_get_all_business_list_%s" % (CACHE_PREFIX, username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        client = get_client_by_user(username)

        result = client.cc.search_business({'bk_supplier_account': 0})

        if result['result']:
            data = result['data']['info']
            cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        else:
            raise exceptions.APIError(system='cc',
                                      api='search_business',
                                      message=result['message'])

    return data
Beispiel #11
0
def _get_user_info(username, use_cache=True):
    """
    获取用户基本信息
    @param username:
    @param use_cache:
    @return:
    """
    cache_key = "%s_get_user_info_%s" % (CACHE_PREFIX, username)
    data = cache.get(cache_key)
    if not (use_cache and data):
        userinfo = get_user_info(username)
        userinfo.setdefault("code", -1)
        if userinfo["result"]:
            data = userinfo["data"]
            if data:
                cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        else:
            raise exceptions.APIError(
                "bk_api", "get_user_info",
                userinfo.get("detail_message", userinfo["message"]))
    return data
Beispiel #12
0
def _get_user_info(username, use_cache=True):
    """
    获取用户基本信息
    @param username:
    @param use_cache:
    @return:
    """
    cache_key = "%s_get_user_info_%s" % (CACHE_PREFIX, username)
    data = cache.get(cache_key)
    if not (use_cache and data):
        userinfo = get_user_info(username)
        userinfo.setdefault('code', -1)
        if userinfo['result']:
            data = userinfo['data']
            if data:
                cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        else:
            raise exceptions.APIError(
                'bk_api', 'get_user_info',
                userinfo.get('detail_message', userinfo['message']))
    return data
Beispiel #13
0
def get_all_business_list(use_cache=True):
    username = settings.SYSTEM_USE_API_ACCOUNT
    cache_key = "%s_get_all_business_list_%s" % (CACHE_PREFIX, username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        client = get_client_by_user(username)

        result = client.cc.search_business({
            "bk_supplier_account":
            EnvironmentVariables.objects.get_var(
                "BKAPP_DEFAULT_SUPPLIER_ACCOUNT", 0)
        })

        if result["result"]:
            data = result["data"]["info"]
            cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        else:
            raise exceptions.APIError(system="cc",
                                      api="search_business",
                                      message=result["message"])

    return data
Beispiel #14
0
def _get_business_info(request, app_id, use_cache=True, use_maintainer=False):
    """Get detail infomations for a exact app_id.

    @param object request: django request object.
    @param int app_id: cc_id of core.business model.
    @param use_maintainer: 使用运维身份请求
    """
    username = request.user.username
    cache_key = "%s_get_business_info_%s_%s" % (CACHE_PREFIX, app_id, username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        if use_maintainer:
            client = get_client_by_user_and_biz_id(username, app_id)
        else:
            client = settings.ESB_GET_CLIENT_BY_REQUEST(request)
        result = client.cc.get_app_by_id({
            'app_id':
            app_id,
            'uin_to_openid_column':
            ','.join(roles.CC_ROLES),
        })
        if result['result']:
            data = result['data'][0]
        elif result['code'] in ('20101', 20101):
            raise exceptions.Unauthorized(result['message'])
        elif result['code'] in ('20103', 20103, '20201', 20201, '20202',
                                20202):
            raise exceptions.Forbidden(result['message'])
        else:
            raise exceptions.APIError(
                'cc', 'get_app_by_id',
                result.get('detail_message', result['message']))

        cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)

    return data