def login_log(user, client): ''' 登录日志操作 :param user: 用户对象实例 :return: ''' # 更新登录日志 login_info = { 'time': time.time(), 'ip': request.remote_addr, 'geo': reader_city(request.remote_addr), 'client':client } user_login_log = mdb_user.db.user_login_log.find_one({'user_id': user.str_id}) if user_login_log and "login_info" in user_login_log: login_infos = user_login_log["login_info"] else: login_infos = [] login_infos.append(login_info) than_num = len(login_infos) - get_config("weblogger", "SING_IN_LOG_KEEP_NUM") if than_num > 0: del login_infos[0:than_num] mdb_user.db.user_login_log.update_one({'user_id': user.str_id}, {"$set": {"pass_error": 0, "login_info": login_infos} }, upsert=True) # 检查登录地区是否异常 anl = AbnormalLogin(login_infos[0:-1], login_info["geo"]) abr = anl.area() if abr == "abnormal": # 发送邮件 subject = gettext("Abnormal login") try: location = "{}/{}/{}".format( login_info["geo"]["subdivisions"]["name"], login_info["geo"]["country"]["name"], login_info["geo"]["continent"]["name"] ) except: location = None if location: body = gettext("<b>Abnormal login</b><br> Your account <a>{}</a>, is logined in " "<span style='color:#483D8B'>{}</span> " "on {} [UTC Time].<br>").format( user.email, location, time_to_utcdate(tformat="%Y-%m-%d %H:%M:%S") ) data = {"title": subject, "body": body, "other_info": gettext("End"), } html = get_email_html(data) send_email(subject=subject, recipients=[user.email], html_msg=html)
def login_log(user, client): """ 登录日志操作 :param user: 用户对象实例 :return: """ # 更新登录日志 login_info = { 'time': time.time(), 'ip': request.remote_addr, 'geo': reader_city(request.remote_addr), 'client': client } user_login_log = mdbs["user"].db.user_login_log.find_one( {'user_id': user.str_id}) if user_login_log and "login_info" in user_login_log: login_infos = user_login_log["login_info"] else: login_infos = [] login_infos.append(login_info) than_num = len(login_infos) - \ get_config("weblogger", "SING_IN_LOG_KEEP_NUM") if than_num > 0: del login_infos[0:than_num] mdbs["user"].db.user_login_log.update_one({'user_id': user.str_id}, {"$set": {"pass_error": 0, "login_info": login_infos} }, upsert=True) # 检查登录地区是否异常 anl = AbnormalLogin(login_infos[0:-1], login_info["geo"]) abr = anl.area() if abr == "abnormal": # 发送邮件 subject = gettext("Abnormal login") try: location = "{}/{}/{}".format( login_info["geo"]["subdivisions"]["name"], login_info["geo"]["country"]["name"], login_info["geo"]["continent"]["name"] ) except BaseException: location = None if location: body = [ gettext("Abnormal login"), gettext("Your account {} , is logined in {} on {} [UTC Time].").format( user.email, location, time_to_utcdate(tformat="%Y-%m-%d %H:%M:%S") ) ] data = { "title": subject, "username": user["username"], "body": body, "site_url": get_config("site_config", "SITE_URL") } html = get_email_html(data) msg = { "subject": subject, "recipients": [user["email"]], "html_msg": html } send_email(msg=msg, ctype="nt")