Exemple #1
0
 def count(self, read_from_cache=True, **kw):
     """Count of items in a container
     """
     principal = common.get_request_principal()
     if (read_from_cache and principal.id in self.tab_count_cache.keys() and
         (self.tab_count_cache[principal.id].timestamp +
          capi.workspace_tab_count_cache_refresh_time) > time.time()):
         return self.tab_count_cache[principal.id].count
     results, count = self._query(**kw)
     return count
 def count(self, read_from_cache=True, **kw):
     """Count of items in a container
     """
     principal = common.get_request_principal()
     if (read_from_cache and principal.id in self.tab_count_cache.keys() and
             (self.tab_count_cache[principal.id].timestamp +
                 capi.workspace_tab_count_cache_refresh_time) > time.time()
         ):
         return self.tab_count_cache[principal.id].count
     results, count = self._query(**kw)
     return count
Exemple #3
0
 def _query(self, **kw):
     principal = common.get_request_principal()
     roles = get_workspace_roles()
     group_roles_domain_status = self.item_status_filter(kw, roles)
     session = Session()
     results = []
     count = 0
     reverse = True if (kw.get("sort_dir", "desc") == "desc") else False
     for domain_class, status in group_roles_domain_status.iteritems():
         query = session.query(domain_class).filter(
             domain_class.status.in_(status)).enable_eagerloads(False)
         #filter on title
         query = self.filter_title(query, domain_class, kw)
         #filter on status_date
         query = self.filter_status_date(query, domain_class, kw)
         # Order results
         query = self.order_query(query, domain_class, kw, reverse)
         results.extend(query.all())
     for obj_role in ROLES_DIRECTLY_DEFINED_ON_OBJECTS:
         object_roles_domain_status = self.item_status_filter(
             kw, [obj_role])
         for domain_class, status in object_roles_domain_status.iteritems():
             query = session.query(domain_class).filter(
                 domain_class.status.in_(status)).enable_eagerloads(False)
             #filter on title
             query = self.filter_title(query, domain_class, kw)
             #filter on status_date
             query = self.filter_status_date(query, domain_class, kw)
             # Order results
             query = self.order_query(query, domain_class, kw, reverse)
             for obj in query.all():
                 if obj in results:
                     continue
                 prm = IPrincipalRoleMap(obj)
                 if (prm.getSetting(obj_role, principal.id) == Allow):
                     results.append(
                         contained(obj, self, self.string_key(obj)))
     results = [
         item for item in results
         if checkPermission(view_permission(item),
                            contained(item, self, self.string_key(item)))
     ]
     # Sort items
     if (kw.get("sort_on", None) and kw.get("sort_dir", None)):
         results.sort(key=lambda x: getattr(x, str(kw.get("sort_on"))),
                      reverse=reverse)
     count = len(results)
     if not (kw.get("filter_title", None) or kw.get("filter_type", None)
             or kw.get("filter_status", None)
             or kw.get("filter_status_date", None)):
         self.set_tab_count(principal.id, count)
     return (results, count)
Exemple #4
0
    def _query(self, **kw):
        # !+REWORK **kw to be explicit keywords, see context.query() in ui/workspace.py
        session = Session()
        results = []
        count = 0
        reverse = True if (kw.get("sort_dir", "desc") == "desc") else False
        principal_id = common.get_request_principal().id

        def extend_results_for_roles(roles):
            domain_status = self.item_status_filter(kw, roles)
            OBJECT_ROLES = [
                role for role in roles
                if role in ROLES_DIRECTLY_DEFINED_ON_OBJECTS
            ]
            for domain_class, status in domain_status.iteritems():
                query = session.query(domain_class).filter(
                    domain_class.status.in_(status)).enable_eagerloads(False)
                query = self.filter_group(query, domain_class, kw)
                query = self.filter_title(query, domain_class, kw)
                query = self.filter_status_date(query, domain_class, kw)
                query = self.order_query(query, domain_class, kw, reverse)
                for obj in query.all():
                    if obj in results:
                        continue
                    if OBJECT_ROLES:
                        prm = IPrincipalRoleMap(obj)
                        if not prm.getSetting(obj_role, principal_id) == Allow:
                            continue
                    results.append(contained(obj, self, self.string_key(obj)))

        # get results for roles
        extend_results_for_roles(get_workspace_roles())
        for obj_role in ROLES_DIRECTLY_DEFINED_ON_OBJECTS:
            extend_results_for_roles([obj_role])

        # filter
        results = [
            item for item in results
            if checkPermission(view_permission(item), item)
        ]

        # sort items
        if (kw.get("sort_on", None) and kw.get("sort_dir", None)):
            results.sort(key=lambda x: getattr(x, str(kw.get("sort_on"))),
                         reverse=reverse)
        count = len(results)
        if not (kw.get("filter_title", None) or kw.get("filter_type", None)
                or kw.get("filter_status", None)
                or kw.get("filter_status_date", None)):
            self.set_tab_count(principal_id, count)
        return (results, count)
 def _query(self, **kw):
     principal = common.get_request_principal()
     roles = get_workspace_roles()
     group_roles_domain_status = self.item_status_filter(kw, roles)
     session = Session()
     results = []
     count = 0
     reverse = True if (kw.get("sort_dir", "desc") == "desc") else False
     for domain_class, status in group_roles_domain_status.iteritems():
         query = session.query(domain_class).filter(domain_class.status.in_(status)).enable_eagerloads(False)
         # filter on title
         query = self.filter_title(query, domain_class, kw)
         # filter on status_date
         query = self.filter_status_date(query, domain_class, kw)
         # Order results
         query = self.order_query(query, domain_class, kw, reverse)
         results.extend(query.all())
     for obj_role in ROLES_DIRECTLY_DEFINED_ON_OBJECTS:
         object_roles_domain_status = self.item_status_filter(kw, [obj_role])
         for domain_class, status in object_roles_domain_status.iteritems():
             query = session.query(domain_class).filter(domain_class.status.in_(status)).enable_eagerloads(False)
             # filter on title
             query = self.filter_title(query, domain_class, kw)
             # filter on status_date
             query = self.filter_status_date(query, domain_class, kw)
             # Order results
             query = self.order_query(query, domain_class, kw, reverse)
             for obj in query.all():
                 if obj in results:
                     continue
                 prm = IPrincipalRoleMap(obj)
                 if prm.getSetting(obj_role, principal.id) == Allow:
                     results.append(contained(obj, self, self.string_key(obj)))
     results = [
         item
         for item in results
         if checkPermission(view_permission(item), contained(item, self, self.string_key(item)))
     ]
     # Sort items
     if kw.get("sort_on", None) and kw.get("sort_dir", None):
         results.sort(key=lambda x: getattr(x, str(kw.get("sort_on"))), reverse=reverse)
     count = len(results)
     if not (
         kw.get("filter_title", None)
         or kw.get("filter_type", None)
         or kw.get("filter_status", None)
         or kw.get("filter_status_date", None)
     ):
         self.set_tab_count(principal.id, count)
     return (results, count)
 def _query(self, **kw):
     # !+REWORK **kw to be explicit keywords, see context.query() in ui/workspace.py
     session = Session()
     results = []
     count = 0
     reverse = True if (kw.get("sort_dir", "desc") == "desc") else False
     principal_id = common.get_request_principal().id
     
     def extend_results_for_roles(roles):
         domain_status = self.item_status_filter(kw, roles)
         OBJECT_ROLES = [ 
             role for role in roles if role in ROLES_DIRECTLY_DEFINED_ON_OBJECTS ]
         for domain_class, status in domain_status.iteritems():
             query = session.query(domain_class).filter(
                 domain_class.status.in_(status)).enable_eagerloads(False)
             query = self.filter_group(query, domain_class, kw)
             query = self.filter_title(query, domain_class, kw)
             query = self.filter_status_date(query, domain_class, kw)
             query = self.order_query(query, domain_class, kw, reverse)
             for obj in query.all():
                 if obj in results:
                     continue
                 if OBJECT_ROLES:
                     prm = IPrincipalRoleMap(obj)
                     if not prm.getSetting(obj_role, principal_id) == Allow:
                         continue
                 results.append(contained(obj, self, self.string_key(obj)))
     # get results for roles
     extend_results_for_roles(get_workspace_roles())
     for obj_role in ROLES_DIRECTLY_DEFINED_ON_OBJECTS:
         extend_results_for_roles([obj_role])
     
     # filter 
     results = [ item for item in results 
         if checkPermission(view_permission(item), item) ]
     
     # sort items
     if (kw.get("sort_on", None) and kw.get("sort_dir", None)):
         results.sort(key=lambda x: getattr(x, str(kw.get("sort_on"))),
             reverse=reverse)
     count = len(results)
     if not (kw.get("filter_title", None) or
             kw.get("filter_type", None) or
             kw.get("filter_status", None) or
             kw.get("filter_status_date", None)
         ):
         self.set_tab_count(principal_id, count)
     return (results, count)
def get_workspace_roles():
    """Returns all the roles that the current principal has that are 
    relevant to the workspace configuration.
    """
    principal = common.get_request_principal()
    session = bungeni.alchemist.Session()
    roles = set()
    user = bungeni.models.utils.get_login_user()
    groups = (
        session.query(bungeni.models.domain.Group)
        .filter(bungeni.models.domain.Group.principal_name.in_(principal.groups.keys()))
        .all()
    )
    principal_groups = [delegate.login for delegate in bungeni.models.delegation.get_user_delegations(user)] + [
        principal.id
    ]
    pg = []
    Allow = zope.securitypolicy.settings.Allow
    for group in groups:
        context = bungeni.core.workflows.utils.get_group_context(group)
        prm = zope.component.queryAdapter(context, IPrincipalRoleMap, default=None)
        if prm:
            pg = principal_groups + [group.principal_name]
            for principal_id in pg:
                l_roles = prm.getRolesForPrincipal(principal_id)
                for role in l_roles:
                    if role[1] == Allow:
                        roles.add(role[0])
    group_memberships = (
        session.query(bungeni.models.domain.GroupMembership)
        .filter(bungeni.models.domain.GroupMembership.user_id == user.user_id)
        .all()
    )
    group_membership_roles = []
    for group_membership in group_memberships:
        for sub_role in group_membership.sub_roles:
            group_membership_roles.append(sub_role.role_id)
    for group_membership_role in group_membership_roles:
        roles.add(group_membership_role)
    return list(roles)
Exemple #8
0
def get_workspace_roles():
    """Returns all the roles that the current principal has that are 
    relevant to the workspace configuration.
    """
    principal = common.get_request_principal()
    session = bungeni.alchemist.Session()
    roles = set()
    user = bungeni.models.utils.get_login_user()
    groups = session.query(bungeni.models.domain.Group).filter(
        bungeni.models.domain.Group.principal_name.in_(
            principal.groups.keys())).all()
    principal_groups = [
        delegate.login
        for delegate in bungeni.models.delegation.get_user_delegations(user)
    ] + [principal.id]
    pg = []
    Allow = zope.securitypolicy.settings.Allow
    for group in groups:
        context = bungeni.core.workflows.utils.get_group_context(group)
        prm = zope.component.queryAdapter(context,
                                          IPrincipalRoleMap,
                                          default=None)
        if prm:
            pg = principal_groups + [group.principal_name]
            for principal_id in pg:
                l_roles = prm.getRolesForPrincipal(principal_id)
                for role in l_roles:
                    if role[1] == Allow:
                        roles.add(role[0])
    group_memberships = session.query(
        bungeni.models.domain.GroupMembership).filter(
            bungeni.models.domain.GroupMembership.user_id ==
            user.user_id).all()
    group_membership_roles = []
    for group_membership in group_memberships:
        for sub_role in group_membership.sub_roles:
            group_membership_roles.append(sub_role.role_id)
    for group_membership_role in group_membership_roles:
        roles.add(group_membership_role)
    return list(roles)