def get_plot_data(user_id=None, token=None): """ :param: category of analysis ie blocked/(unblocked/warned) :parameter: user_id: the user making the request :parameter: token: the user token for each request :rtype: object """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response(jsonify({"error": 'Invalid User id.', "data": False}), 200) ips = get_plots().find({}) if ips: ibl_list = [] for x in ips: x['id'] = x['_id'] del x['_id'] ibl_list.append(x) create_log(find_user(user_id), request, "retrieved analysis data", LOG_USER_TYPE) return make_response(jsonify({"error": False, "token": get_request_token(user_id), "data": json.dumps(ibl_list, cls=JSONEncoder)}), 200) return make_response(jsonify({"error": False, "token": get_request_token(user_id), "data": False}), 200)
def logout(user_id=None, token=None): f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response( jsonify({ "error": 'Invalid User id.', "data": False }), 200) if not isinstance(user_id, ObjectId): if not user_id or not len(user_id) == 24: return make_response( jsonify({ "error": 'Invalid User id.', "data": False }), 200) sessions = get_sessions() result = sessions.delete_many({"data.user_id": ObjectId(user_id)}) session.clear() if result and result.deleted_count < 1: return make_response( jsonify({ "error": "Could not log you out. The possible cause is due to invalid request data", "data": False })) return make_response(jsonify({"error": False, "data": True}))
def delete_logs(user_id, log_type="user", retain=None, token=None): """ :param token: :param user_id :param log_type :param retain :rtype: object """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response( jsonify({ "error": 'Invalid User id.', "data": False }), 200) logs = get_logs() try: """ To change this so that deleting is limited to certain date to use retain """ data = logs.delete_many({ 'type': log_type, "lastAccessTime": { "$lt": "2018-04-03 19:33:53" } }) if not data or data.deleted_count < 1: return make_response( jsonify({ "error": 'Unable to delete records. No records found ', "token": get_request_token(user_id), "data": False })) user = find_user(_id=user_id) if user: create_log(user, request, "accessed " + log_type + " logs", LOG_SYSTEM_TYPE) except IndexError: return make_response( jsonify({ "error": 'No data found for deletion', "token": get_request_token(user_id), "data": False })) return make_response( jsonify({ "error": False, "token": get_request_token(user_id), "data": data.deleted_count }))
def fetch_logs(user_id=None, log_type="user", token=None): """ :param token: :param user_id :param log_type :return: """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response( jsonify({ "error": 'Invalid User id.', "data": False }), 200) logs = get_logs() try: user = find_user(_id=user_id) if user: create_log(user, request, "accessed " + log_type + " logs", LOG_SYSTEM_TYPE) data = logs.find({ 'type': log_type }, { '_id': 0 }).sort("lastAccessTime", -1) if not data or data.count() < 1: return make_response( jsonify({ "error": 'No logs found..', "token": get_request_token(user_id), "data": False })) data = [x for x in data] except IndexError: return make_response( jsonify({ "error": 'No data found', "token": get_request_token(user_id), "data": False })) return make_response( jsonify({ "error": False, "token": get_request_token(user_id), "data": data }))
def block_by_country(country=None, user_id=None, token=None): f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response(jsonify({"error": 'Invalid User id.', "data": False}), 200) if not country or not len(country.strip()) == 2: return make_response(jsonify({"error": 'Incorrect data sent to server', "token": get_request_token(user_id), "data": False}), 200) block_all(by_country=country) return make_response(jsonify({"error": False, "token": get_request_token(user_id), "data": "Country `{0}` has been black-listed ".format(country)}), 200)
def fetch_single_user(user_email_id, user_id=None, token=None): """ :parameter: user_id: the user making the request :parameter: token: the user token for each request :param: user_email_id: user id to update :rtype: object """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response( jsonify({ "error": 'Invalid User id.', "data": False }), 200) if not user_email_id or not len(user_email_id) == 24: return make_response( jsonify({ "error": 'A valid User id is required', "token": get_request_token(user_id), "data": False }), 200) user = find_user(user_email_id) if user: user['id'] = user['_id'] del user['_id'] del user['password'] create_log(find_user(user_id), request, "retrieved user data", LOG_USER_TYPE) return make_response( jsonify({ "error": False, "token": get_request_token(user_id), "data": json.dumps(user, cls=JSONEncoder) }), 200) return make_response( jsonify({ "error": "Could not find the requested user.", "token": get_request_token(user_id), "data": False }), 200)
def deactivate_user(email, user_id=None, token=None): """ :parameter: user_id: the user making the request :parameter: email: the email of user to deactivate :parameter: token: the user token for each request :rtype: object """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response( jsonify({ "error": 'Invalid User id.', "data": False }), 200) email = email.replace('-', '@') if not validate_email(email): return make_response( jsonify({ "error": "Email is invalid. ex. [email protected]", "token": get_request_token(user_id), "data": False }), 200) if deactivate_user_status(email): create_log(find_user(user_id), request, "deactivated an account of {0}".format(email), LOG_USER_TYPE) return make_response( jsonify({ "error": False, "token": get_request_token(user_id), "data": "User has been deactivated successfully" }), 200) return make_response( jsonify({ "error": "Sorry. Could not deactivate user. Something happened", "token": get_request_token(user_id), "data": False }), 200)
def get_global_notifications(user_id=None, token=None): """ :param: category of analysis ie blocked/(unblocked/warned) :parameter: user_id: the user making the request :parameter: token: the user token for each request :rtype: object """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response(jsonify({"error": 'Invalid User id.', "data": False}), 200) ips = get_new_global_alerts() return make_response(jsonify({"error": False, "token": get_request_token(user_id), "data": json.dumps(ips, cls=JSONEncoder)}), 200)
def blocked_or_warned(category=None, order=None, user_id=None, token=None): """ :param token: :param user_id: :param category: category of analysis ie blocked/(unblocked/warned) :param order: order of the category: ie network or ip :parameter: user_id: the user making the request :parameter: token: the user token for each request :rtype: object """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response(jsonify({"error": 'Invalid User id.', "data": False}), 200) if not category or not order: return make_response(jsonify({"error": False, "token": get_request_token(user_id), "data": False}), 200) ips = None if category == "warned" or category == "unblocked": if order == "ip": ips = get_warned_by_ip().find({}) elif order == "network": ips = get_warned_by_network().find({}) elif category == "blocked": if order == "ip": ips = get_blocked_by_ip().find({}) elif order == "network": ips = get_blocked_by_network().find({}) if ips: ibl = ips.sort([("block_date", DESCENDING), ("attempts", ASCENDING)]) ibl_list = [] for x in ibl: x['id'] = x['_id'] del x['_id'] ibl_list.append(x) create_log(find_user(user_id), request, "retrieved analysis data", LOG_USER_TYPE) return make_response(jsonify({"error": False, "token": get_request_token(user_id), "data": json.dumps(ibl_list, cls=JSONEncoder)}), 200) return make_response(jsonify({"error": False, "token": get_request_token(user_id), "data": False}), 200)
def fetch_all_users(user_id=None, token=None): """ :parameter: user_id: the user making the request :parameter: token: the user token for each request :rtype: object """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response( jsonify({ "error": 'Invalid User id.', "data": False }), 200) users = get_users().find({}) if users: users = users.sort([("status", ASCENDING), ("first_name", ASCENDING), ("last_name", ASCENDING)]) user_list = [] for x in users: x['id'] = x['_id'] del x['_id'] del x['password'] user_list.append(x) create_log(find_user(user_id), request, "retrieved user data", LOG_USER_TYPE) return make_response( jsonify({ "error": False, "token": get_request_token(user_id), "data": json.dumps(user_list, cls=JSONEncoder) }), 200) return make_response( jsonify({ "error": False, "token": get_request_token(user_id), "data": False }), 200)
def fetch_domains(user_id, token): """ :param: category of analysis ie blocked/(unblocked/warned) :parameter: user_id: the user making the request :parameter: token: the user token for each request :rtype: object """ f = do_get_auth(user_id, token) if not isinstance(f, bool): return f else: if f is not True: return make_response(jsonify({"error": 'Invalid User id or token', "data": False}), 200) ips = get_domains().find() data = [] for d in ips: data.append(d) return make_response(jsonify({"error": False, "token": get_request_token(user_id), "data": json.dumps(data, cls=JSONEncoder)}), 200)