コード例 #1
0
    def test_resource_users_for_any_perm(self, db_session):
        self.maxDiff = 99999
        self.set_up_user_group_and_perms(db_session)
        perms = ResourceService.users_for_perm(
            self.resource, "__any_permission__", db_session=db_session
        )
        second = [
            PermissionTuple(
                self.user, "group_perm", "group", self.group, self.resource, False, True
            ),
            PermissionTuple(
                self.user, "test_perm2", "user", None, self.resource, False, True
            ),
            PermissionTuple(
                self.user, "foo_perm", "user", None, self.resource, False, True
            ),
            PermissionTuple(
                self.user4,
                "group_perm",
                "group",
                self.group2,
                self.resource,
                False,
                True,
            ),
        ]

        check_one_in_other(perms, second)
コード例 #2
0
    def test_resource_users_for_any_perm_excluding_group_perms(self, db_session):
        self.maxDiff = 99999
        self.set_up_user_group_and_perms(db_session)
        user6 = add_user(db_session, 6, "user 6")
        user7 = add_user(db_session, 7, "user 7")
        perm2 = GroupResourcePermission(
            perm_name="group_perm2", resource_id=self.resource.resource_id
        )
        self.group.resource_permissions.append(perm2)
        self.group.users.append(user6)
        self.group.users.append(user7)
        perms = ResourceService.users_for_perm(
            self.resource,
            "__any_permission__",
            limit_group_permissions=True,
            skip_group_perms=True,
            db_session=db_session,
        )
        second = [
            PermissionTuple(
                self.user, "test_perm2", "user", None, self.resource, False, True
            ),
            PermissionTuple(
                self.user, "foo_perm", "user", None, self.resource, False, True
            ),
        ]

        check_one_in_other(perms, second)
コード例 #3
0
ファイル: event.py プロジェクト: vinodpandey/appenlight
 def send_alerts(self, request=None, resource=None, db_session=None):
     """" Sends alerts to applicable channels """
     db_session = get_db_session(db_session)
     db_session.flush()
     if not resource:
         resource = ResourceService.by_resource_id(self.resource_id)
     if not request:
         request = get_current_request()
     if not resource:
         return
     users = set(
         [p.user for p in ResourceService.users_for_perm(resource, "view")])
     for user in users:
         for channel in user.alert_channels:
             matches_resource = not channel.resources or resource in [
                 r.resource_id for r in channel.resources
             ]
             if (not channel.channel_validated or not channel.send_alerts
                     or not matches_resource):
                 continue
             else:
                 try:
                     channel.notify_alert(resource=resource,
                                          event=self,
                                          user=user,
                                          request=request)
                 except IntegrationException as e:
                     log.warning("%s" % e)
コード例 #4
0
    def test_resource_users_for_any_perm(self, db_session):
        self.maxDiff = 99999
        self.set_up_user_group_and_perms(db_session)
        perms = ResourceService.users_for_perm(self.resource,
                                               "__any_permission__",
                                               db_session=db_session)
        second = [
            PermissionTuple(self.user, "group_perm", "group", self.group,
                            self.resource, False, True),
            PermissionTuple(self.user, "test_perm2", "user", None,
                            self.resource, False, True),
            PermissionTuple(self.user, "foo_perm", "user", None, self.resource,
                            False, True),
            PermissionTuple(
                self.user4,
                "group_perm",
                "group",
                self.group2,
                self.resource,
                False,
                True,
            ),
        ]

        check_one_in_other(perms, second)
コード例 #5
0
    def test_resource_users_limited_group_ownage(self, db_session):
        self.maxDiff = 9999
        self.set_up_user_group_and_perms(db_session)
        resource = ResourceTestobjB(resource_id=99,
                                    resource_name="other",
                                    owner_user_id=self.user2.id)
        group3 = add_group(db_session, "group 3")
        user2_permission = UserResourcePermission(perm_name="foo_perm",
                                                  user_id=self.user2.id)
        group3_permission = GroupResourcePermission(perm_name="group_perm",
                                                    group_id=group3.id)
        resource.group_permissions.append(group3_permission)
        resource.user_permissions.append(user2_permission)
        group3.users.append(self.user3)
        self.user.resources.append(resource)
        self.group2.resources.append(resource)
        db_session.flush()
        perms = ResourceService.users_for_perm(resource,
                                               "__any_permission__",
                                               db_session=db_session)
        second = [
            PermissionTuple(self.user2, "foo_perm", "user", None, resource,
                            False, True),
            PermissionTuple(self.user, ALL_PERMISSIONS, "user", None, resource,
                            True, True),
            PermissionTuple(self.user4, ALL_PERMISSIONS, "group", self.group2,
                            resource, True, True),
            PermissionTuple(self.user3, "group_perm", "group", group3,
                            resource, False, True),
        ]

        check_one_in_other(perms, second)
コード例 #6
0
    def test_resource_users_for_any_perm_excluding_group_perms(
            self, db_session):
        self.maxDiff = 99999
        self.set_up_user_group_and_perms(db_session)
        user6 = add_user(db_session, 6, "user 6")
        user7 = add_user(db_session, 7, "user 7")
        perm2 = GroupResourcePermission(perm_name="group_perm2",
                                        resource_id=self.resource.resource_id)
        self.group.resource_permissions.append(perm2)
        self.group.users.append(user6)
        self.group.users.append(user7)
        perms = ResourceService.users_for_perm(
            self.resource,
            "__any_permission__",
            limit_group_permissions=True,
            skip_group_perms=True,
            db_session=db_session,
        )
        second = [
            PermissionTuple(self.user, "test_perm2", "user", None,
                            self.resource, False, True),
            PermissionTuple(self.user, "foo_perm", "user", None, self.resource,
                            False, True),
        ]

        check_one_in_other(perms, second)
コード例 #7
0
def check_user_report_notifications(resource_id):
    since_when = datetime.utcnow()
    try:
        request = get_current_request()
        application = ApplicationService.by_id(resource_id)
        if not application:
            return
        error_key = REDIS_KEYS["reports_to_notify_per_type_per_app"].format(
            ReportType.error, resource_id)
        slow_key = REDIS_KEYS["reports_to_notify_per_type_per_app"].format(
            ReportType.slow, resource_id)
        error_group_ids = Datastores.redis.smembers(error_key)
        slow_group_ids = Datastores.redis.smembers(slow_key)
        Datastores.redis.delete(error_key)
        Datastores.redis.delete(slow_key)
        err_gids = [int(g_id) for g_id in error_group_ids]
        slow_gids = [int(g_id) for g_id in list(slow_group_ids)]
        group_ids = err_gids + slow_gids
        occurence_dict = {}
        for g_id in group_ids:
            key = REDIS_KEYS["counters"]["report_group_occurences"].format(
                g_id)
            val = Datastores.redis.get(key)
            Datastores.redis.delete(key)
            if val:
                occurence_dict[g_id] = int(val)
            else:
                occurence_dict[g_id] = 1
        report_groups = ReportGroupService.by_ids(group_ids)
        report_groups.options(sa.orm.joinedload(ReportGroup.last_report_ref))

        ApplicationService.check_for_groups_alert(
            application,
            "alert",
            report_groups=report_groups,
            occurence_dict=occurence_dict,
        )
        users = set([
            p.user
            for p in ResourceService.users_for_perm(application, "view")
        ])
        report_groups = report_groups.all()
        for user in users:
            UserService.report_notify(
                user,
                request,
                application,
                report_groups=report_groups,
                occurence_dict=occurence_dict,
            )
        for group in report_groups:
            # marks report_groups as notified
            if not group.notified:
                group.notified = True
    except Exception as exc:
        print_traceback(log)
        raise
コード例 #8
0
    def test_resource_users_for_perm(self, db_session):
        self.set_up_user_group_and_perms(db_session)
        perms = ResourceService.users_for_perm(self.resource,
                                               "foo_perm",
                                               db_session=db_session)
        second = [
            PermissionTuple(self.user, "foo_perm", "user", None, self.resource,
                            False, True)
        ]

        check_one_in_other(perms, second)
コード例 #9
0
    def test_resource_users_for_perm(self, db_session):
        self.set_up_user_group_and_perms(db_session)
        perms = ResourceService.users_for_perm(
            self.resource, "foo_perm", db_session=db_session
        )
        second = [
            PermissionTuple(
                self.user, "foo_perm", "user", None, self.resource, False, True
            )
        ]

        check_one_in_other(perms, second)
コード例 #10
0
def comment_create(request):
    """
    Creates user comments for report group, sends email notifications
    of said comments
    """
    report_group = request.context.report_group
    application = request.context.resource
    form = forms.CommentForm(MultiDict(request.unsafe_json_body),
                             csrf_context=request)
    if request.method == "POST" and form.validate():
        comment = ReportComment(owner_id=request.user.id,
                                report_time=report_group.first_timestamp)
        form.populate_obj(comment)
        report_group.comments.append(comment)
        perm_list = ResourceService.users_for_perm(application, "view")
        uids_to_notify = []
        users_to_notify = []
        for perm in perm_list:
            user = perm.user
            if ("@{}".format(user.user_name) in comment.body
                    and user.id not in uids_to_notify):
                uids_to_notify.append(user.id)
                users_to_notify.append(user)

        commenters = ReportGroupService.users_commenting(
            report_group, exclude_user_id=request.user.id)
        for user in commenters:
            if user.id not in uids_to_notify:
                uids_to_notify.append(user.id)
                users_to_notify.append(user)

        for user in users_to_notify:
            email_vars = {
                "user": user,
                "commenting_user": request.user,
                "request": request,
                "application": application,
                "report_group": report_group,
                "comment": comment,
                "email_title": "AppEnlight :: New comment",
            }
            UserService.send_email(
                request,
                recipients=[user.email],
                variables=email_vars,
                template="/email_templates/new_comment_report.jinja2",
            )
        request.session.flash(_("Your comment was created"))
        return comment.get_dict()
    else:
        return form.errors
コード例 #11
0
    def test_resource_users_limited_group_other_user_3(self, db_session):
        self.maxDiff = 9999
        self.set_up_user_group_and_perms(db_session)
        perms = ResourceService.users_for_perm(
            self.resource2,
            "__any_permission__",
            user_ids=[self.user3.id],
            db_session=db_session,
        )
        second = [
            PermissionTuple(self.user3, "test_perm", "user", None,
                            self.resource2, False, True)
        ]

        check_one_in_other(perms, second)
コード例 #12
0
    def test_resource_users_limited_group_other_user_3(self, db_session):
        self.maxDiff = 9999
        self.set_up_user_group_and_perms(db_session)
        perms = ResourceService.users_for_perm(
            self.resource2,
            "__any_permission__",
            user_ids=[self.user3.id],
            db_session=db_session,
        )
        second = [
            PermissionTuple(
                self.user3, "test_perm", "user", None, self.resource2, False, True
            )
        ]

        check_one_in_other(perms, second)
コード例 #13
0
    def users_for_perm(self, perm_name, user_ids=None, group_ids=None,
                       limit_group_permissions=False, skip_group_perms=False,
                       db_session=None):
        """

        .. deprecated:: 0.8

        :param perm_name:
        :param user_ids:
        :param group_ids:
        :param limit_group_permissions:
        :param skip_group_perms:
        :param db_session:
        :return:
        """
        db_session = get_db_session(db_session, self)
        return ResourceService.users_for_perm(
            self, perm_name, user_ids=user_ids, group_ids=group_ids,
            limit_group_permissions=limit_group_permissions,
            skip_group_perms=skip_group_perms, db_session=db_session)
コード例 #14
0
def assigned_users(request):
    """
    Returns list of users a specific report group is assigned for review
    """
    report_group = request.context.report_group
    application = request.context.resource
    users = set(
        [p.user for p in ResourceService.users_for_perm(application, "view")])
    currently_assigned = [u.user_name for u in report_group.assigned_users]
    user_status = {"assigned": [], "unassigned": []}
    # handle users
    for user in users:
        user_dict = {
            "user_name": user.user_name,
            "gravatar_url": UserService.gravatar_url(user),
            "name": "%s %s" % (user.first_name, user.last_name),
        }
        if user.user_name in currently_assigned:
            user_status["assigned"].append(user_dict)
        elif user_dict not in user_status["unassigned"]:
            user_status["unassigned"].append(user_dict)
    return user_status
コード例 #15
0
    def test_resource_users_limited_group_ownage(self, db_session):
        self.maxDiff = 9999
        self.set_up_user_group_and_perms(db_session)
        resource = TestResourceB(
            resource_id=99, resource_name="other", owner_user_id=self.user2.id
        )
        group3 = add_group(db_session, "group 3")
        user2_permission = UserResourcePermission(
            perm_name="foo_perm", user_id=self.user2.id
        )
        group3_permission = GroupResourcePermission(
            perm_name="group_perm", group_id=group3.id
        )
        resource.group_permissions.append(group3_permission)
        resource.user_permissions.append(user2_permission)
        group3.users.append(self.user3)
        self.user.resources.append(resource)
        self.group2.resources.append(resource)
        db_session.flush()
        perms = ResourceService.users_for_perm(
            resource, "__any_permission__", db_session=db_session
        )
        second = [
            PermissionTuple(
                self.user2, "foo_perm", "user", None, resource, False, True
            ),
            PermissionTuple(
                self.user, ALL_PERMISSIONS, "user", None, resource, True, True
            ),
            PermissionTuple(
                self.user4, ALL_PERMISSIONS, "group", self.group2, resource, True, True
            ),
            PermissionTuple(
                self.user3, "group_perm", "group", group3, resource, False, True
            ),
        ]

        check_one_in_other(perms, second)
コード例 #16
0
def daily_digest():
    """
    Sends daily digest with top 50 error reports
    """
    request = get_current_request()
    apps = Datastores.redis.smembers(REDIS_KEYS["apps_that_had_reports"])
    Datastores.redis.delete(REDIS_KEYS["apps_that_had_reports"])
    since_when = datetime.utcnow() - timedelta(hours=8)
    log.warning("Generating daily digests")
    for resource_id in apps:
        resource_id = resource_id.decode("utf8")
        end_date = datetime.utcnow().replace(microsecond=0, second=0)
        filter_settings = {
            "resource": [resource_id],
            "tags": [{
                "name": "type",
                "value": ["error"],
                "op": None
            }],
            "type": "error",
            "start_date": since_when,
            "end_date": end_date,
        }

        reports = ReportGroupService.get_trending(
            request, filter_settings=filter_settings, limit=50)

        application = ApplicationService.by_id(resource_id)
        if application:
            users = set([
                p.user
                for p in ResourceService.users_for_perm(application, "view")
            ])
            for user in users:
                user.send_digest(request,
                                 application,
                                 reports=reports,
                                 since_when=since_when)
コード例 #17
0
    def test_resource_users_limited_group_other_user_4(self, db_session):
        self.maxDiff = 9999
        self.set_up_user_group_and_perms(db_session)
        perms = ResourceService.users_for_perm(
            self.resource,
            "__any_permission__",
            user_ids=[self.user4.id],
            group_ids=[self.group2.id],
            db_session=db_session,
        )
        second = [
            PermissionTuple(
                self.user4,
                "group_perm",
                "group",
                self.group2,
                self.resource,
                False,
                True,
            )
        ]

        check_one_in_other(perms, second)
コード例 #18
0
    def test_resource_users_limited_group_other_user_4(self, db_session):
        self.maxDiff = 9999
        self.set_up_user_group_and_perms(db_session)
        perms = ResourceService.users_for_perm(
            self.resource,
            "__any_permission__",
            user_ids=[self.user4.id],
            group_ids=[self.group2.id],
            db_session=db_session,
        )
        second = [
            PermissionTuple(
                self.user4,
                "group_perm",
                "group",
                self.group2,
                self.resource,
                False,
                True,
            )
        ]

        check_one_in_other(perms, second)
コード例 #19
0
    def get_patch(self):
        request = self.request
        resource = self.request.context.resource

        breadcrumbs = tree_service.path_upper(
            resource.resource_id, db_session=self.request.dbsession)

        user_permission_form = UserResourcePermissionForm(
            request.POST, context={'request': request})

        group_permission_form = GroupResourcePermissionForm(
            request.POST, context={'request': request})
        groups = GroupService.base_query(db_session=request.dbsession)
        group_permission_form.group_id.choices = [(g.id, g.group_name)
                                                  for g in groups]

        choices = [(p, p) for p in resource.__possible_permissions__]
        user_permission_form.perm_name.choices = choices
        group_permission_form.perm_name.choices = choices

        permissions = ResourceService.users_for_perm(
            resource, perm_name=ANY_PERMISSION, limit_group_permissions=True)
        user_permissions = sorted([p for p in permissions if p.type == 'user'],
                                  key=lambda x: x.user.user_name)
        group_permissions = sorted(
            [p for p in permissions if p.type == 'group'],
            key=lambda x: x.group.group_name)

        user_permissions_grid = ResourceUserPermissionsGrid(user_permissions,
                                                            request=request)
        group_permissions_grid = ResourceGroupPermissionsGrid(
            group_permissions, request=request)

        parent_id_choices = get_possible_parents(self.request)
        resource_form = EntryCreateForm(request.POST,
                                        obj=resource,
                                        context={
                                            'request': request,
                                            'modified_obj': resource
                                        })
        resource_form.parent_id.choices = parent_id_choices

        if request.method == "POST" and resource_form.validate():
            parent_id = resource_form.data.get('parent_id', noop)
            position = resource_form.data.get('ordering', noop)
            # we need to not change_parent_id or order of element
            # to not confuse tree manager
            self.shared.populate_instance(
                resource,
                resource_form.data,
                exclude_keys=['parent_id', 'ordering'])
            into_new_parent = parent_id != resource.parent_id and \
                              parent_id is not noop
            if position is not noop or into_new_parent:
                if not position and into_new_parent:
                    position = tree_service.count_children(
                        parent_id, db_session=self.request.dbsession) + 1
                tree_service.move_to_position(
                    resource_id=resource.resource_id,
                    new_parent_id=parent_id,
                    to_position=position,
                    db_session=self.request.dbsession)

        return {
            'breadcrumbs': breadcrumbs,
            'resource': resource,
            'resource_form': resource_form,
            'user_permission_form': user_permission_form,
            'group_permission_form': group_permission_form,
            'group_permissions_grid': group_permissions_grid,
            'user_permissions_grid': user_permissions_grid
        }
コード例 #20
0
ファイル: resource.py プロジェクト: vinodpandey/appenlight
 def user_permissions_list(self):
     return [
         permission_tuple_to_dict(perm)
         for perm in ResourceService.users_for_perm(
             self, "__any_permission__", limit_group_permissions=True)
     ]