def test_intersect_mixed_usecase_3(self):
        """
            Given some users are dropped on ldap
            And some users are added on ldap
            And no one is individually assigned
            And some users are group assigned
            When i calculate the subscriptions/unsubscriptions to perform
            Then new users are in the subscriptions list, except for the group assigned (already subscribed)
            And dropped users are in the unsubscriptions list except for the group assigned
        """
        from ulearnhub.models.utils import intersect_users

        ldap_users = [1, 2, 3, 4]
        ldap_users.remove(3)
        ldap_users.remove(4)
        ldap_users = ldap_users + [5, 6]
        community_group_users = [3, 5]
        community_users = []
        context_users = [1, 2, 3, 4, 5]

        subscriptions, unsubscriptions = intersect_users(
            ldap_users, community_group_users, community_users, context_users)

        self.assertItemsEqual(subscriptions, [6])
        self.assertItemsEqual(unsubscriptions, [4])
    def test_intersect_mixed_usecase_4(self):
        """
            Given some users are dropped on ldap
            And some users are added on ldap
            And some users are individually assigned
            And some users are group assigned
            When i calculate the subscriptions/unsubscriptions to perform
            Then new users are in the subscriptions list, except for the group assigned (already subscribed)
            And dropped users are in the unsubscriptions list except for the group assigned
        """
        from ulearnhub.models.utils import intersect_users

        other_group_1 = [1, 4, 8]
        other_group_2 = [2, 6, 9]

        previous_ldap_users = [0, 1, 2, 3, 4, 5, 6]
        ldap_users = previous_ldap_users + [
            7,  # 7 is not on other groups nor individually assigned --> added
            8,  # 8 is on group 1 and already subscribed --> pass
            9,  # 9 is on group 2 and individually assigned, and already subscribed --> pass
            10,  # 10 is individually assigned and already subscribed  --> pass
            11  # 11 is individually assigned but not subscribed (inconsistency) -- added>
        ]
        ldap_users.remove(
            3
        )  # 3 is not on other groups nor individually assigned --> removed
        ldap_users.remove(4)  # 4 is on group 1 --> preserved
        ldap_users.remove(5)  # 5 is individually assigned --> preserved
        ldap_users.remove(
            6)  # 6 is in group 2 and individually assigned --> preserved
        ldap_users.remove(
            0
        )  # 0 is individually assigned but not subscribed (inconsistency) --> pass

        community_group_users = other_group_1 + other_group_2
        community_users = [0, 5, 6, 9, 10, 11]
        context_users = [1, 2, 3, 4, 5, 6, 8, 9, 10]

        subscriptions, unsubscriptions = intersect_users(
            ldap_users, community_group_users, community_users, context_users)

        self.assertItemsEqual(subscriptions, [7, 11])
        self.assertItemsEqual(unsubscriptions, [3])
    def test_intersect_drop_users_individually_and_group_assigned(self):
        """
            Given a user is dropped on ldap
            And the user is individually assigned
            And the user is group assigned
            When i calculate the subscriptions/unsubscriptions to perform
            Then no one is in the subscriptions list
            And no one is in the unsubscriptions list
        """
        from ulearnhub.models.utils import intersect_users

        ldap_users = [1, 2, 3, 4]
        ldap_users.remove(4)
        community_group_users = [4, 3]
        community_users = [4]
        context_users = [1, 2, 3, 4]

        subscriptions, unsubscriptions = intersect_users(
            ldap_users, community_group_users, community_users, context_users)

        self.assertItemsEqual(subscriptions, [])
        self.assertItemsEqual(unsubscriptions, [])
    def test_intersect_drop_user(self):
        """
            Given a user is dropped on ldap
            And there are no individually assigned users
            And there are no grup assigned users
            When i calculate the subscriptions/unsubscriptions to perform
            Then no one is in the subscriptions list
            And the dropped user is in the unsubscriptions list
        """
        from ulearnhub.models.utils import intersect_users

        ldap_users = [1, 2, 3]
        ldap_users.remove(3)
        community_group_users = []
        community_users = []
        context_users = [1, 2, 3]

        subscriptions, unsubscriptions = intersect_users(
            ldap_users, community_group_users, community_users, context_users)

        self.assertItemsEqual(subscriptions, [])
        self.assertItemsEqual(unsubscriptions, [3])
    def test_intersect_new_user_individually_and_other_group_subscribed(self):
        """
            Given new users in ldap
            And the new user is individually assigned
            And the new user is also subscribed via another group
            When i calculate the subscriptions/unsubscriptions to perform
            Then no one is in the subscriptions list
            And no one is in the unsubscriptions list
        """
        from ulearnhub.models.utils import intersect_users

        new_users = [3]
        ldap_users = [1, 2] + new_users
        community_group_users = [3, 2]
        community_users = [3]
        context_users = [1, 2, 3]

        subscriptions, unsubscriptions = intersect_users(
            ldap_users, community_group_users, community_users, context_users)

        self.assertItemsEqual(subscriptions, [])
        self.assertItemsEqual(unsubscriptions, [])
    def test_intersect_new_users(self):
        """
            Given new users in ldap
            And there are no individually assigned users
            And there are no grup assigned users
            When i calculate the subscriptions/unsubscriptions to perform
            Then the new users are in the subscriptions list
            And no one in in the unsubscriptions list
        """
        from ulearnhub.models.utils import intersect_users

        new_users = [3, 4]
        ldap_users = [1, 2] + new_users
        community_group_users = []
        community_users = []
        context_users = [1, 2]

        subscriptions, unsubscriptions = intersect_users(
            ldap_users, community_group_users, community_users, context_users)

        self.assertItemsEqual(subscriptions, [3, 4])
        self.assertItemsEqual(unsubscriptions, [])
Exemple #7
0
    def handle_rabbitmq(self, request, *args, **kwargs):
        """
            Update a groups user subscription on directory changes.

            For any community found using the group contained in the
            request, the full list of users is extracted and a minimum
            set of taskjs (SUB or UNSUB) is generated per-context. At the
            end, any added or removed user in the group will be synced so
            the subscriptions of that context match the users on the group.

            Format of request to this service is as follows:

            {
                "component": {
                    "type": "ldap",
                    "id": ""
                }
                "group": "cn=groupname,dc.....",
                "ignore_grants_and_vetos": true,
            }

        """
        data = request.json
        target_group = data['group']
        ldapserver = self.__parent__

        group_users = GroupUsersRetriever(ldapserver)

        # Expand users
        ldapserver.server.connect()
        ldap_group_current_users = group_users.load(target_group)

        # Disconnect ldap server, we won't need it outside here
        ldapserver.server.disconnect()

        # Search all domains that are using this **exact**
        # component instance
        domains = root_factory(request)['domains']
        target_domains = []
        for domain in domains.values():
            candidate = domain.get_component(ldapserver.__class__)
            if candidate is not None and candidate is ldapserver:
                target_domains.append(domain)

        processed_communities = []
        for domain in target_domains:
            # Get required components to process this domain
            rabbitserver = domain.get_component(RabbitServer)
            communities_site = domain.get_component(ULearnCommunities)
            maxserver = domain.get_component(MaxServer)
            maxclient = maxserver.maxclient
            authenticated_username, authenticated_token, scope = request.auth_headers
            maxclient.setActor(authenticated_username)
            maxclient.setToken(authenticated_token)

            if communities_site in processed_communities:
                continue

            # Get all communities that have the target group assigned
            # on any created_community
            target_communities = communities_site.get_communities_with_group(
                target_group)
            for context_data in target_communities:
                context_url = context_data['url']
                policy_granted_permissions, max_subscriptions = get_context_data(
                    maxclient, context_url)

                community_assigned_users = context_data['users']
                community_assigned_groups = context_data['groups']
                community_assigned_groups_users = group_users.load(
                    *community_assigned_groups)
                context_subscribed_users = max_subscriptions.keys()

                subscriptions, unsubscriptions = intersect_users(
                    ldap_group_current_users, community_assigned_groups_users,
                    community_assigned_users, context_subscribed_users)

                # Generate and queue subcribe/unsubscribe actions
                client = rabbitserver.notifications

                for username in subscriptions:
                    client.sync_acl(domain.name, context_url, username,
                                    {"subscribe": True})
                    gevent.sleep()

                for username in unsubscriptions:
                    client.sync_acl(domain.name, context_url, username,
                                    {"unsubscribe": True})
                    gevent.sleep()

                gevent.sleep(0.1)

        processed_communities.append(communities_site)
        return {}