Exemple #1
0
    def saved_list(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        dummy_data = {}
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        if es_check:
            res = BackendRequest.get_all_saved_search({
                "token": es_check["t"],
                'operator': es_check['u']
            })
            saved_searches = []
            permits = []
            if res['result']:
                for i in res['items']:
                    anonymous = i.get('anonymous', False)
                    if anonymous:
                        continue
                    saved_searches.append({
                        'savedsearch_id':
                        i['id'].encode('utf-8'),
                        'savedsearch_name':
                        i['name'].encode('utf-8'),
                        "related_alert_num":
                        i.get('alert_count', 0),
                        'query':
                        i['query'].encode('utf-8'),
                        "sourcegroup":
                        i['source_groups'].encode('utf-8'),
                        "owner":
                        i.get('owner_name', "").encode('utf-8'),
                        "rg_ids":
                        i.get("resource_group_ids", []),
                        "timerange":
                        i.get('time_range', "").encode('utf-8'),
                        "filters":
                        i.get('filters', "").encode('utf-8') if i.get(
                            'filters', "") else "",
                        "fav":
                        'yes' if i.get('like', True) else 'no'
                    })
                    permits.append({
                        "resource_id": int(i['id']),
                        "target": "SavedSearch",
                        "action": "Update"
                    })
                    permits.append({
                        "resource_id": int(i['id']),
                        "target": "SavedSearch",
                        "action": "Delete"
                    })
                permits.append({"target": "SavedSearch", "action": "Create"})
                permits.append({
                    "target": "DerelictResource",
                    "action": "Possess"
                })

                dummy_data["status"] = "1"
                dummy_data["totle"] = len(saved_searches)
                dummy_data["list"] = sorted(saved_searches,
                                            key=lambda x: x['fav'],
                                            reverse=True)
                param = {'token': es_check['t'], 'operator': es_check['u']}
                permit_param = {'permits': permits}
                permit_res = BackendRequest.batch_permit_can(
                    param, permit_param)
                if permit_res['result']:
                    dummy_data["permit_list"] = permit_res["short_permits"]
                else:
                    dummy_data["permit_list"] = []
            else:
                dummy_data = err_data.build_error(res)
        else:
            data = err_data.build_error({}, "auth error!")
            data["location"] = "/auth/login/"
            dummy_data = data
        bundle = self.build_bundle(obj=dummy_data,
                                   data=dummy_data,
                                   request=request)
        response_data = bundle
        resp = self.create_response(request, response_data)
        return resp
Exemple #2
0
    def saved_delete(self, request, **kwargs):
        self.method_check(request, allowed=['post'])
        savedsearch_id = kwargs['ssid']

        dummy_data = {}
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        if es_check:
            res = BackendRequest.delete_saved_search({
                "token": es_check["t"],
                "operator": es_check["u"],
                "id": savedsearch_id
            })
            if res["result"]:
                time.sleep(0.2)
                res_list = BackendRequest.get_all_saved_search({
                    "token":
                    es_check["t"],
                    'operator':
                    es_check['u']
                })
                saved_searches = []
                if res_list['result']:
                    for i in res_list['items']:
                        ana = i.get("anonymous", False)
                        if ana:
                            continue
                        saved_searches.append({
                            'savedsearch_id':
                            i['id'].encode('utf-8'),
                            'savedsearch_name':
                            i['name'].encode('utf-8'),
                            "related_alert_num":
                            i.get('alert_count', 0),
                            'query':
                            i['query'].encode('utf-8'),
                            "sourcegroup":
                            i['source_groups'].encode('utf-8'),
                            "owner":
                            i.get('owner_name', "").encode('utf-8'),
                            "timerange":
                            i.get('time_range', "").encode('utf-8'),
                            "filters":
                            i.get('filters', "").encode('utf-8'),
                            "fav":
                            'yes' if i.get('like', True) else 'no'
                        })
                dummy_data["status"] = "1"
                dummy_data["totle"] = len(saved_searches)
                dummy_data["list"] = saved_searches
            else:
                dummy_data = err_data.build_error(res)
        else:
            data = err_data.build_error({}, "auth error!")
            data["location"] = "/auth/login/"
            dummy_data = data
        bundle = self.build_bundle(obj=dummy_data,
                                   data=dummy_data,
                                   request=request)
        response_data = bundle
        resp = self.create_response(request, response_data)
        return resp