Beispiel #1
0
    def get(self):
        try:
            if g.user:
                username = g.user.username
            else:
                logerror.error("session:%s;errmsg:g.user Not Found" %
                               str(session))
                clear_user_cookie(session)
                return jsonify({"code": 40001, "msg": _("user not login")})

            url = "%s/api/tag/get_taglist_plus/" % current_app.config.get(
                "URL", "")
            if username in current_app.config.get("ADMINLIET", []):
                userType = "superadmin"
            else:
                userType = "admin"

            params = {"user": username, "userType": userType}

            res, data, msg = Requests(url=url, method="GET", params=params)
            if res:
                return jsonify({
                    "code": 20000,
                    "msg": _("success"),
                    "data": data["data"]
                })
            else:
                return jsonify({"code": 40004, "msg": msg})
        except Exception, e:
            logerror.error("session:%s;errmsg:%s" % (str(session), e))
            clear_user_cookie(session)
            return jsonify({"code": 40001, "msg": _("user not login")})
Beispiel #2
0
def app_before():
    loginfo.info("session:%s"%(str(session)))
    g.user = get_usertoken_from_session(session)
    path = request.path
    if not g.user and not path.endswith("/auth/login"):
        logerror.error("session:%s;errmsg:g.user Not Found" % str(session))
        return jsonify({"code": 40001, "msg": _("user not login")})
Beispiel #3
0
 def post(self):
     try:
         g.user = None
         clear_user_cookie(session)
         return jsonify({"code": 20000, "msg": _("success")})
     except Exception, e:
         logmsg = "errormsg:%s" % e
         logerror.error(logmsg)
         return jsonify({"code": 50000, "msg": _("Internal Server Error")})
Beispiel #4
0
    def sendSms(self, tos="", content=""):
        sms_url = self.conf.get('Sms', 'url')
        if not sms_url:
            errmsg = _("SMS configuration error")
            logerror.error("url:%s,errmsg:%s" % (str(sms_url), errmsg))
            return (False, errmsg)

        if not tos or not content:
            errmsg = _("The recipient or the content is empty")
            logerror.error("tos:%s,content:%s,errmsg:%s" %
                           (tos, content, errmsg))
            return (False, errmsg)

        return (False, _("This function is not implemented temporarily"))
Beispiel #5
0
    def sendMail(self, tos="", subject="", content="", content_type="plain"):
        try:
            mail_server = self.conf.get('Mail', 'server')
            mail_user = self.conf.get('Mail', 'user')
            mail_password = self.conf.get('Mail', 'password')

            if not mail_server or not mail_user or not mail_password:
                errmsg = _("Mailbox configuration error")
                logerror.error("server:%s,user:%s,password:%s,errmsg:%s" %
                               (mail_server, mail_user, mail_password, errmsg))
                return (False, errmsg)
        except Exception, e:
            errmsg = _("Mailbox configuration error")
            logerror.error("errmsg:%s,errinfo:%s" % (errmsg, e))
            return (False, errmsg)
 def post(self, tag=None):
     args = parser.parse_args()
     ipList = args.get("ipList")
 
     if not tag or not ipList:
         return jsonify({"code": 40000, "msg": _("invalid params")})
     print ipList
     try:
         import time
         time.sleep(3)
         return jsonify({"code": 20000, "msg": "success"})
         
     except Exception, e:
         logmsg = "errormsg:%s" % e
         logerror.error(logmsg)
         return jsonify({"code": 50000, "msg": _("Internal Server Error")})
Beispiel #7
0
 def get(self):
     try:
         if g.user:
             username = g.user.username
             if username in current_app.config.get("ADMINLIET", []):
                 admin = True
             else:
                 admin = False
             return jsonify({"code":20000,"msg":_("success"),"data":{"userName":username,"admin":admin}})
         else:
             logerror.error("session:%s;errmsg:g.user Not Found" % str(session))
             clear_user_cookie(session)
             return jsonify({"code": 40001, "msg": _("user not login")})
     except Exception,e:
         logerror.error("session:%s;errmsg:%s" % (str(session),e))
         clear_user_cookie(session)
         return jsonify({"code": 40001, "msg": _("user not login")})
Beispiel #8
0
    def post(self):
        args = parser.parse_args()
        username = args.get("username")
        password = args.get("password")

        if not username or not password:
            return jsonify({"code": 40000, "msg": _("invalid params")})
        try:
            res, data = self.ld.ldap_user_vaild(username, password)
            self.ld.close()
            if not res:
                return jsonify({"code": 40002, "msg": data})
            else:
                user = User.query.filter_by(username=username).first()
                if not user:
                    user = User(
                        username=username,
                        password=password,
                        mobile=data["mobile"],
                        cnname=data["displayName"],
                        userdn=data["userdn"],
                    )
                    db.session.add(user)
                    db.session.commit()
                g.user = user
                set_user_cookie(user, session)
                return jsonify({
                    "code": 20000,
                    "msg": _("success"),
                    "data": {
                        "userName": username
                    }
                })
        except Exception, e:
            logmsg = "username:%s;errormsg:%s" % e
            logerror.error(logmsg)
            return jsonify({"code": 50000, "msg": _("Internal Server Error")})
 def get(self, tag=None):
     args = parser.parse_args()
     page = args.get("page")
     limit = args.get("limit")
     search = args.get("search")
     
     if not tag:
         return jsonify({"code": 40000, "msg": _("invalid params")})
     try:
         _d = {
             "total": 0,
             "dataSource": [],
         }
         if search:
             return jsonify({"code": 20000, "msg": "success", "data": _d})
         res, data, msg = getDeviceInfo(tag=tag)
         if not res:
             return jsonify({"code": 40004, "msg": msg, "data": _d})
         
         dataSource = data["data"]
         dataSource.sort(key=self.foo)
         p = pagination_or_not(dataSource, page, limit, sql=False)
         ip_list = [i["ip"] for i in p.object_list]
         AgentDevice_objlist = AgentDevice.query.filter(AgentDevice.ip.in_(ip_list)).all()
         ipInfo  = {}
         for AgentDevice_obj in AgentDevice_objlist:
             ipInfo[AgentDevice_obj.ip] = {
                 "version": AgentDevice_obj.version,
                 "uploadTime": AgentDevice_obj.uploadTime,
             }
         _d["total"] = p.paginator.count
         _d["dataSource"] = self._get_info(p.object_list,ipInfo)
         return jsonify({"code": 20000, "msg": _("success"), "data": _d})
     except Exception, e:
         logmsg = "errormsg:%s" % e
         logerror.error(logmsg)
         return jsonify({"code": 50000, "msg": _("Internal Server Error")})
Beispiel #10
0
def get_usertoken_from_session(session_):
    if config.SITE_COOKIE in session_:
        cookies = session_[config.SITE_COOKIE]
        if not cookies:
            logerror.error("session_:%s;key:%s;" %
                           (str(session_), config.SITE_COOKIE))
            return None

        s = Serializer(config.SECRET_KEY)
        try:
            data = s.loads(cookies)
            try:
                user = User.query.get(data['id'])
                g.user = user
                return user
            except Exception, e:
                logerror.error("data:%s;errmsg:%s;" % (str(data), e))
                return None
        except Exception, e:
            logerror.error("data:%s;errmsg:%s;" % (str(s), e))
            return None
Beispiel #11
0
class Sender(object):
    def __init__(self):

        cf = ConfigParser.ConfigParser()
        if not os.path.exists("sender.cfg"):
            # Mail config
            cf.add_section("Mail")
            cf.set("Mail", "server", "smtp.qq.com")
            cf.set("Mail", "user", "*****@*****.**")
            cf.set("Mail", "password", "*****")

            # Sms config
            cf.add_section("Sms")
            cf.set("Sms", "url", "http://www.****.com/api/v1/sender/sms")

            cf.write(open("sender.cfg", "w"))

        cf.read("sender.cfg")
        self.conf = cf

    def sendMail(self, tos="", subject="", content="", content_type="plain"):
        try:
            mail_server = self.conf.get('Mail', 'server')
            mail_user = self.conf.get('Mail', 'user')
            mail_password = self.conf.get('Mail', 'password')

            if not mail_server or not mail_user or not mail_password:
                errmsg = _("Mailbox configuration error")
                logerror.error("server:%s,user:%s,password:%s,errmsg:%s" %
                               (mail_server, mail_user, mail_password, errmsg))
                return (False, errmsg)
        except Exception, e:
            errmsg = _("Mailbox configuration error")
            logerror.error("errmsg:%s,errinfo:%s" % (errmsg, e))
            return (False, errmsg)

        if not tos or not content or not subject:
            errmsg = _("The recipient or the content is empty")
            logerror.error("tos:%s,content:%s,subject:%s,errmsg:%s" %
                           (tos, content, subject, errmsg))
            return (False, errmsg)

        server = smtplib.SMTP()
        try:
            server.connect(mail_server, 587)  # 连接服务器
            server.starttls()
            server.login(mail_user, mail_password)  # 登录操作
            to_list = tos.split(",")
            msg = MIMEMultipart('related')
            msgAlternative = MIMEMultipart('alternative')
            msg.attach(msgAlternative)
            msgText = MIMEText(content, content_type, _charset='utf-8')
            msgAlternative.attach(msgText)
            msg['Subject'] = subject
            msg['From'] = mail_user
            msg['To'] = ";".join(to_list)  # 将收件人列表以‘;’分隔
            server.sendmail(mail_user, to_list, msg.as_string())
            server.quit()
            loginfo.info("tos:%s,content:%s,subject:%s" %
                         (tos, content, subject))
            return (True, _("success"))
        except Exception, e:
            logerror.error("tos:%s,content:%s,subject:%s,errmsg:%s" %
                           (tos, content, subject, e))
            return (False, "sendMail error:%s" % e)