Exemplo n.º 1
0
    def post(self):
        req = request.get_json(force=True)
        require_fields(req, ('name', 'email'))

        if '@' not in req['email']:
            abort(400, message='Bad email address.')
        name, domain = req['email'].split('@', 1)

        if domain.lower() in blacklist or domain.lower() == 'qq.com':
            abort(400, message='Bad email address.')

        user = models.User(org=self.current_org,
                           name=req['name'],
                           email=req['email'],
                           is_invitation_pending=True,
                           group_ids=[self.current_org.default_group.id])

        try:
            models.db.session.add(user)
            models.db.session.commit()
        except IntegrityError as e:
            if "email" in e.message:
                abort(400, message='Email already taken.')
            abort(500)

        self.record_event({
            'action': 'create',
            'object_id': user.id,
            'object_type': 'user'
        })

        should_send_invitation = 'no_invite' not in request.args
        return invite_user(self.current_org, self.current_user, user, send_email=should_send_invitation)
Exemplo n.º 2
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'query_id'))

        alert = models.Alert.create(name=req['name'],
                                    query=req['query_id'],
                                    user=self.current_user,
                                    options=req['options'])

        record_event.delay({
            'user_id': self.current_user.id,
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        # TODO: should be in model?
        models.AlertSubscription.create(alert=alert, user=self.current_user)

        record_event.delay({
            'user_id': self.current_user.id,
            'action': 'subscribe',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        return alert.to_dict()
Exemplo n.º 3
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'query_id'))

        alert = models.Alert.create(
            name=req['name'],
            query=req['query_id'],
            user=self.current_user,
            options=req['options']
        )

        record_event.delay({
            'user_id': self.current_user.id,
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        # TODO: should be in model?
        models.AlertSubscription.create(alert=alert, user=self.current_user)

        record_event.delay({
            'user_id': self.current_user.id,
            'action': 'subscribe',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        return alert.to_dict()
Exemplo n.º 4
0
    def post(self):
        req = request.get_json(force=True)
        require_fields(req, ('name', 'email'))

        user = models.User(org=self.current_org,
                           name=req['name'],
                           email=req['email'],
                           group_ids=[self.current_org.default_group.id])

        try:
            models.db.session.add(user)
            models.db.session.commit()
        except IntegrityError as e:
            if "email" in e.message:
                abort(400, message='Email already taken.')

            abort(500)

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': user.id,
            'object_type': 'user'
        })

        if request.args.get('no_invite') is not None:
            invite_url = invite_link_for_user(user)
        else:
            invite_url = invite_user(self.current_org, self.current_user, user)

        d = user.to_dict()
        d['invite_link'] = invite_url

        return d
Exemplo n.º 5
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'query_id'))

        query = models.Query.get_by_id_and_org(req['query_id'], self.current_org)
        require_access(query.groups, self.current_user, view_only)

        alert = models.Alert.create(
            name=req['name'],
            query=query,
            user=self.current_user,
            options=req['options']
        )

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        # TODO: should be in model?
        models.AlertSubscription.create(alert=alert, user=self.current_user)

        self.record_event({
            'action': 'subscribe',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        return alert.to_dict()
Exemplo n.º 6
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ("options", "name", "query_id"))

        query = models.Query.get_by_id_and_org(req["query_id"],
                                               self.current_org)
        require_access(query, self.current_user, view_only)

        alert = models.Alert(
            name=req["name"],
            query_rel=query,
            user=self.current_user,
            rearm=req.get("rearm"),
            options=req["options"],
        )

        models.db.session.add(alert)
        models.db.session.flush()
        models.db.session.commit()

        self.record_event({
            "action": "create",
            "object_id": alert.id,
            "object_type": "alert"
        })

        return serialize_alert(alert)
Exemplo n.º 7
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'query_id'))

        query = models.Query.get_by_id_and_org(req['query_id'],
                                               self.current_org)
        require_access(query.groups, self.current_user, view_only)

        alert = models.Alert.create(name=req['name'],
                                    query=query,
                                    user=self.current_user,
                                    options=req['options'])

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        # TODO: should be in model?
        models.AlertSubscription.create(alert=alert, user=self.current_user)

        self.record_event({
            'action': 'subscribe',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        return alert.to_dict()
Exemplo n.º 8
0
    def post(self):
        # TODO: send invite.
        req = request.get_json(force=True)
        require_fields(req, ('name', 'email', 'password'))

        user = models.User(org=self.current_org, name=req['name'], email=req['email'],
                           groups=[self.current_org.default_group.id])
        user.hash_password(req['password'])

        try:
            user.save()
        except IntegrityError as e:
            if "email" in e.message:
                abort(400, message='Email already taken.')

            abort(500)

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': user.id,
            'object_type': 'user'
        })

        return user.to_dict()
Exemplo n.º 9
0
    def post(self):
        req = request.get_json(force=True)
        require_fields(req, ("name", "email"))

        if "@" not in req["email"]:
            abort(400, message="Bad email address.")
        name, domain = req["email"].split("@", 1)

        if domain.lower() in blacklist or domain.lower() == "qq.com":
            abort(400, message="Bad email address.")

        user = models.User(
            org=self.current_org,
            name=req["name"],
            email=req["email"],
            is_invitation_pending=True,
            group_ids=[self.current_org.default_group.id],
        )

        try:
            models.db.session.add(user)
            models.db.session.commit()
        except IntegrityError as e:
            if "email" in str(e):
                abort(400, message="Email already taken.")
            abort(500)

        self.record_event(
            {"action": "create", "object_id": user.id, "object_type": "user"}
        )

        should_send_invitation = "no_invite" not in request.args
        return invite_user(
            self.current_org, self.current_user, user, send_email=should_send_invitation
        )
Exemplo n.º 10
0
    def post(self):
        req = request.get_json(force=True)
        require_fields(req, ("name", "email"))

        if "@" not in req["email"]:
            abort(400, message="非法的电子邮箱。")
        require_allowed_email(req["email"])

        user = models.User(
            org=self.current_org,
            name=req["name"],
            email=req["email"],
            is_invitation_pending=True,
            group_ids=[self.current_org.default_group.id],
        )

        try:
            models.db.session.add(user)
            models.db.session.commit()
        except IntegrityError as e:
            if "email" in str(e):
                abort(400, message="电子邮箱已占用。")
            abort(500)

        self.record_event(
            {"action": "create", "object_id": user.id, "object_type": "user"}
        )

        should_send_invitation = "no_invite" not in request.args
        return invite_user(
            self.current_org, self.current_user, user, send_email=should_send_invitation
        )
Exemplo n.º 11
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'type'))

        schema = get_configuration_schema_for_destination_type(req['type'])
        if schema is None:
            abort(400)

        config = ConfigurationContainer(req['options'], schema)
        if not config.is_valid():
            abort(400)

        destination = models.NotificationDestination(org=self.current_org,
                                                     name=req['name'],
                                                     type=req['type'],
                                                     options=config,
                                                     user=self.current_user)

        try:
            models.db.session.add(destination)
            models.db.session.commit()
        except IntegrityError as e:
            if 'name' in e.message:
                abort(400, message=u"Alert Destination with the name {} already exists.".format(req['name']))
            abort(500)

        return destination.to_dict(all=True)
Exemplo n.º 12
0
    def post(self):
        # 忽视mimetype类型,强制为Json类型

        req = request.get_json(True)

        # 判断请求参数
        require_fields(req, ('options', 'name', 'query_id'))

        #### 根据哪个query_id创建的alert
        query = Query.get_by_id_and_org(req['query_id'], self.current_org)

        ## 权限系统??????????????
        require_access(query.groups, self.current_user, view_only)

        # query_rel 是 relationShip, 传对方的行对象,需要提供
        # 但是不用提供外键对应的列
        alert = Alert(name=req['name'],
                      query_rel=query,
                      user=self.current_user,
                      rearm=req.get('rearm'),
                      options=req['options'])

        session.add(alert)
        session.flush()
        session.commit()

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        ### RESETFUL 规范, 不能只是响应个200,就完事了, 最好把创建好的东西返回给前端
        return serialize_alert(alert)
Exemplo n.º 13
0
    def post(self):
        req = request.get_json(force=True)
        require_fields(req, ('name', 'email'))

        user = models.User(org=self.current_org,
                           name=req['name'],
                           email=req['email'],
                           group_ids=[self.current_org.default_group.id])

        try:
            models.db.session.add(user)
            models.db.session.commit()
        except IntegrityError as e:
            if "email" in e.message:
                abort(400, message='Email already taken.')

            abort(500)

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': user.id,
            'object_type': 'user'
        })

        if request.args.get('no_invite') is not None:
            invite_url = invite_link_for_user(user)
        else:
            invite_url = invite_user(self.current_org, self.current_user, user)

        d = user.to_dict()
        d['invite_link'] = invite_url

        return d
Exemplo n.º 14
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ("options", "name", "type"))

        schema = get_configuration_schema_for_destination_type(req["type"])
        if schema is None:
            abort(400)

        config = ConfigurationContainer(req["options"], schema)
        if not config.is_valid():
            abort(400)

        destination = models.NotificationDestination(
            org=self.current_org,
            name=req["name"],
            type=req["type"],
            options=config,
            user=self.current_user,
        )

        try:
            models.db.session.add(destination)
            models.db.session.commit()
        except IntegrityError as e:
            if "name" in str(e):
                abort(
                    400,
                    message="Alert Destination with the name {} already exists."
                    .format(req["name"]),
                )
            abort(500)

        return destination.to_dict(all=True)
Exemplo n.º 15
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ("name", ))
        name = req["name"]

        if models.Application.get_by_name_and_org(
                name, self.current_org) is not None:
            abort(400, message="Name already token")

        application = models.Application(
            name=name,
            icon_url=req.get("icon_url"),
            active=req.get("active", True),
            description=req.get("description"),
            created_by_id=self.current_user.id,
            org=self.current_org,
        )

        models.db.session.add(application)
        models.db.session.commit()

        self.record_event({
            "action": "create",
            "object_id": application.id,
            "object_type": "application",
        })

        return application.to_dict(hide_token=False)
Exemplo n.º 16
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'query_id'))

        query = models.Query.get_by_id_and_org(req['query_id'],
                                               self.current_org)
        require_access(query, self.current_user, view_only)

        alert = models.Alert(
            name=req['name'],
            query_rel=query,
            user=self.current_user,
            rearm=req.get('rearm'),
            options=req['options'],
        )

        models.db.session.add(alert)
        models.db.session.flush()
        models.db.session.commit()

        self.record_event({
            'action': 'create',
            'object_id': alert.id,
            'object_type': 'alert'
        })

        return serialize_alert(alert)
Exemplo n.º 17
0
    def post(self):
        req = request.get_json(force=True)
        require_fields(req, ('name', 'email'))

        if '@' not in req['email']:
            abort(400, message='Bad email address.')
        name, domain = req['email'].split('@', 1)

        # if domain.lower() in blacklist or domain.lower() == 'qq.com':
        if domain.lower() in blacklist:
            abort(400, message='Bad email address.')

        user = models.User(org=self.current_org,
                           name=req['name'],
                           email=req['email'],
                           is_invitation_pending=True,
                           group_ids=[self.current_org.default_group.id])

        try:
            models.db.session.add(user)
            models.db.session.commit()
        except IntegrityError as e:
            if "email" in e.message:
                abort(400, message='Email already taken.')
            abort(500)

        self.record_event({
            'action': 'create',
            'object_id': user.id,
            'object_type': 'user'
        })

        should_send_invitation = 'no_invite' not in request.args
        return invite_user(self.current_org, self.current_user, user, send_email=should_send_invitation)
Exemplo n.º 18
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'query_id'))

        query = models.Query.get_by_id_and_org(req['query_id'],
                                               self.current_org)
        require_access(query.groups, self.current_user, view_only)

        alert = models.Alert(name=req['name'],
                             query_rel=query,
                             user=self.current_user,
                             options=req['options'])

        models.db.session.add(alert)
        models.db.session.flush()
        models.db.session.commit()

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        return alert.to_dict()
Exemplo n.º 19
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'query_id'))

        query = models.Query.get_by_id_and_org(req['query_id'],
                                               self.current_org)
        require_access(query.groups, self.current_user, view_only)

        alert = models.Alert(
            name=req['name'],
            query_rel=query,
            user=self.current_user,
            rearm=req.get('rearm'),
            options=req['options']
        )

        models.db.session.add(alert)
        models.db.session.flush()
        models.db.session.commit()

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        return serialize_alert(alert)
Exemplo n.º 20
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'type'))

        schema = get_configuration_schema_for_query_runner_type(req['type'])
        if schema is None:
            abort(400)

        config = ConfigurationContainer(filter_none(req['options']), schema)
        # from IPython import embed
        # embed()
        if not config.is_valid():
            abort(400)

        try:
            datasource = models.DataSource.create_with_group(org=self.current_org,
                                                             name=req['name'],
                                                             type=req['type'],
                                                             options=config)

            models.db.session.commit()
        except IntegrityError as e:
            if req['name'] in e.message:
                abort(400, message="Data source with the name {} already exists.".format(req['name']))

            abort(400)

        self.record_event({
            'action': 'create',
            'object_id': datasource.id,
            'object_type': 'datasource'
        })

        return datasource.to_dict(all=True)
Exemplo n.º 21
0
    def post(self):
        # TODO: send invite.
        req = request.get_json(force=True)
        require_fields(req, ("name", "email", "password"))

        user = models.User(name=req["name"], email=req["email"])
        user.hash_password(req["password"])
        try:
            user.save()
        except IntegrityError as e:
            if "email" in e.message:
                abort(400, message="Email already taken.")

            abort(500)

        record_event.delay(
            {
                "user_id": self.current_user.id,
                "action": "create",
                "timestamp": int(time.time()),
                "object_id": user.id,
                "object_type": "user",
            }
        )

        return user.to_dict()
Exemplo n.º 22
0
    def post(self, application_id):
        req = request.get_json(True)
        require_fields(req, ("dashboard_id", ))

        Application = get_object_or_404(models.Application.get_by_id_and_org,
                                        application_id, self.current_org)

        models.ApplicationDashboard.add_dashboard_to_application(
            req["dashboard_id"], application_id, self.current_user)

        self.record_event({
            "action": "add_dashboard_to_application",
            "object_id": application_id,
            "object_type": "application",
            "member_id": req["dashboard_id"],
        })
Exemplo n.º 23
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('trigger', 'description', 'snippet'))

        snippet = models.QuerySnippet.create(trigger=req['trigger'],
                                             description=req['description'],
                                             snippet=req['snippet'],
                                             user=self.current_user,
                                             org=self.current_org)

        self.record_event({
            'action': 'create',
            'object_id': snippet.id,
            'object_type': 'query_snippet'
        })

        return snippet.to_dict()
Exemplo n.º 24
0
    def post(self, dashboard_id):
        req = request.get_json(True)
        require_fields(req, ("application_id", ))

        dashboard = get_object_or_404(models.Dashboard.get_by_id_and_org,
                                      dashboard_id, self.current_org)
        require_admin_or_owner(dashboard.user_id)

        models.ApplicationDashboard.add_dashboard_to_application(
            dashboard_id, req["application_id"], self.current_user)

        self.record_event({
            "action": "add_dashboard_to_application",
            "object_id": dashboard_id,
            "object_type": "dashboard",
            "member_id": req["application_id"],
        })
Exemplo n.º 25
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ("options", "name", "type"))

        schema = get_configuration_schema_for_query_runner_type(req["type"])
        if schema is None:
            abort(400)

        config = ConfigurationContainer(filter_none(req["options"]), schema)
        if not config.is_valid():
            abort(400)

        try:
            datasource = models.DataSource.create_with_group(
                org=self.current_org,
                name=req["name"],
                type=req["type"],
                description=req["description"] if "description" in req else "",
                options=config,
            )

            models.db.session.commit()

            # Refresh the stored schemas when a new data source is added to the list
            refresh_schema.delay(datasource.id)

        except IntegrityError as e:
            models.db.session.rollback()
            if req["name"] in str(e):
                abort(
                    400,
                    message="Data source with the name {} already exists.".
                    format(req["name"]),
                )

            abort(400)

        self.record_event({
            "action": "create",
            "object_id": datasource.id,
            "object_type": "datasource",
        })

        return datasource.to_dict(all=True)
Exemplo n.º 26
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('trigger', 'description', 'snippet'))

        snippet = models.QuerySnippet.create(
            trigger=req['trigger'],
            description=req['description'],
            snippet=req['snippet'],
            user=self.current_user,
            org=self.current_org
        )

        self.record_event({
            'action': 'create',
            'object_id': snippet.id,
            'object_type': 'query_snippet'
        })

        return snippet.to_dict()
Exemplo n.º 27
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ('options', 'name', 'type'))

        schema = get_configuration_schema_for_destination_type(req['type'])
        if schema is None:
            abort(400)

        config = ConfigurationContainer(req['options'], schema)
        if not config.is_valid():
            abort(400)

        destination = models.NotificationDestination(org=self.current_org,
                                                     name=req['name'],
                                                     type=req['type'],
                                                     options=config,
                                                     user=self.current_user)

        models.db.session.add(destination)
        models.db.session.commit()
        return destination.to_dict(all=True)
Exemplo n.º 28
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ("trigger", "description", "snippet"))

        snippet = models.QuerySnippet(
            trigger=req["trigger"],
            description=req["description"],
            snippet=req["snippet"],
            user=self.current_user,
            org=self.current_org,
        )

        models.db.session.add(snippet)
        models.db.session.commit()

        self.record_event({
            "action": "create",
            "object_id": snippet.id,
            "object_type": "query_snippet",
        })

        return snippet.to_dict()
Exemplo n.º 29
0
    def post(self):
        req = request.get_json(True)
        require_fields(req, ("options", "name", "type"))

        schema = get_configuration_schema_for_query_runner_type(req["type"])
        if schema is None:
            abort(400)

        config = ConfigurationContainer(filter_none(req["options"]), schema)
        if not config.is_valid():
            abort(400)

        try:
            datasource = models.DataSource.create_with_group(
                org=self.current_org,
                name=req["name"],
                type=req["type"],
                options=config)

            models.db.session.commit()
        except IntegrityError as e:
            if req["name"] in str(e):
                abort(
                    400,
                    message="同名数据源 {} 已经存在。".format(req["name"]),
                )

            abort(400)

        self.record_event({
            "action": "create",
            "object_id": datasource.id,
            "object_type": "datasource",
        })

        return datasource.to_dict(all=True)