示例#1
0
def _update_groups(user, group_ids, user_change_logger):
    groups_updated = user.set_groups(group_ids)
    if user_change_logger and groups_updated:
        groups = []
        if group_ids:
            groups = [
                Group.wrap(doc) for doc in get_docs(Group.get_db(), group_ids)
            ]
        user_change_logger.add_info(UserChangeMessage.groups_info(groups))
示例#2
0
def log_user_groups_change(domain, request, user, group_ids=None):
    groups = []
    # no groups assigned would be group ids as []
    # so if group ids were NOT passed or if some were passed, get groups for user
    if group_ids is None or group_ids:
        groups = Group.by_user_id(user.get_id)
    log_user_change(
        by_domain=domain,
        for_domain=domain,  # Groups are bound to a domain, so use domain
        couch_user=user,
        changed_by_user=request.couch_user,
        changed_via=USER_CHANGE_VIA_WEB,
        change_messages=UserChangeMessage.groups_info(groups))
示例#3
0
    def update_user_groups(self, domain_info, group_names):
        """
        Add/remove user from groups without save and return change message for changes, if any
        """
        old_group_ids = set()
        for group in domain_info.group_memoizer.by_user_id(self.user.user_id):
            old_group_ids.add(group.get_id)
            if group.name not in group_names:
                group.remove_user(self.user)
                domain_info.group_memoizer.updated_groups.add(group.get_id)

        new_groups = {}
        for group_name in group_names:
            group = domain_info.group_memoizer.by_name(group_name)
            new_groups[group.get_id] = group
            if group.add_user(self.user, save=False):
                domain_info.group_memoizer.group_updated(group.get_id)

        if set(new_groups) != old_group_ids:
            return UserChangeMessage.groups_info(list(new_groups.values()))
示例#4
0
    def test_update(self):

        user = CommCareUser.create(domain=self.domain.name,
                                   username="******",
                                   password="******",
                                   created_by=None,
                                   created_via=None,
                                   phone_number="50253311398")
        group = Group({"name": "test"})
        group.save()

        self.addCleanup(user.delete, self.domain.name, deleted_by=None)
        self.addCleanup(group.delete)

        user_json = {
            "first_name": "test",
            "last_name": "last",
            "email": "*****@*****.**",
            "language": "pol",
            "phone_numbers": ["+50253311399", "50253314588"],
            "groups": [group._id],
            "user_data": {
                PROFILE_SLUG: self.profile.id,
                "chw_id": "13/43/DFA"
            },
            "password": "******"
        }

        backend_id = user._id
        response = self._assert_auth_post_resource(
            self.single_endpoint(backend_id),
            json.dumps(user_json),
            content_type='application/json',
            method='PUT')
        self.assertEqual(response.status_code, 200, response.content)
        self.assertEqual(1, len(CommCareUser.by_domain(self.domain.name)))
        modified = CommCareUser.get(backend_id)
        self.assertEqual(modified.username, "test")
        self.assertEqual(modified.first_name, "test")
        self.assertEqual(modified.last_name, "last")
        self.assertEqual(modified.email, "*****@*****.**")
        self.assertEqual(modified.language, "pol")
        self.assertEqual(modified.get_group_ids()[0], group._id)
        self.assertEqual(modified.metadata["chw_id"], "13/43/DFA")
        self.assertEqual(modified.metadata[PROFILE_SLUG], self.profile.id)
        self.assertEqual(modified.metadata["imaginary"], "yes")
        self.assertEqual(modified.default_phone_number, "50253311399")

        # test user history audit
        user_history = UserHistory.objects.get(
            action=UserModelAction.UPDATE.value, user_id=user._id)
        self.assertDictEqual(
            user_history.changes, {
                'email': '*****@*****.**',
                'language': 'pol',
                'last_name': 'last',
                'first_name': 'test',
                'user_data': {
                    'chw_id': '13/43/DFA',
                    'commcare_profile': self.profile.id,
                    'commcare_project': 'qwerty'
                }
            })
        self.assertTrue(
            "50253311398" in user_history.change_messages['phone_numbers']
            ['remove_phone_numbers']['phone_numbers'])
        self.assertTrue(
            "50253311399" in user_history.change_messages['phone_numbers']
            ['add_phone_numbers']['phone_numbers'])
        self.assertTrue(
            "50253314588" in user_history.change_messages['phone_numbers']
            ['add_phone_numbers']['phone_numbers'])
        self.assertEqual(user_history.change_messages['groups'],
                         UserChangeMessage.groups_info([group])['groups'])
        self.assertEqual(user_history.change_messages['password'],
                         UserChangeMessage.password_reset()['password'])
        self.assertEqual(user_history.changed_via, USER_CHANGE_VIA_API)