Exemplo n.º 1
0
    def test_usergroup_instantiation(self):

        mock_client = mock.MagicMock()
        test_account = mkVscAccount(test_account_1)
        mock_client.account[test_account.vsc_id] = mock.MagicMock()
        mock_client.account[test_account.vsc_id].get.return_value = (
            200, test_account_1)
        mock_client.account[test_account.vsc_id].usergroup.get.return_value = (
            200, test_usergroup_1)

        accountpageuser = user.VscAccountPageUser(test_account.vsc_id,
                                                  rest_client=mock_client)

        self.assertEqual(accountpageuser.usergroup,
                         mkUserGroup(test_usergroup_1))

        mock_client.account[test_account.vsc_id].get.return_value = (
            200, test_account_2)
        mock_client.group[test_account_2].get.return_value = (
            200, test_admin_group_1)
        accountpageuser = user.VscAccountPageUser(test_account.vsc_id,
                                                  mock_client)

        self.assertEqual(accountpageuser.usergroup,
                         mkGroup(test_admin_group_1))
Exemplo n.º 2
0
    def usergroup(self):
        if not self._cache['usergroup']:
            if self.person.institute_login in ('x_admin', 'admin', 'voadmin'):
                # TODO to be removed when magic site admin usergroups are purged from code
                self._cache['usergroup'] = mkGroup((self.rest_client.group[self.user_id].get())[1])
            else:
                self._cache['usergroup'] = mkUserGroup((self.rest_client.account[self.user_id].usergroup.get()[1]))

        return self._cache['usergroup']
Exemplo n.º 3
0
    def sync_altered_accounts(self, last, dry_run=True):
        """
        Add new users to the LDAP and update altered users. This does not include usergroups.

        this does include pubkeys
        @type last: datetime
        @return: tuple (new, updated, error) that indicates what accounts were new, changed or could not be altered.
        """
        sync_accounts = [
            mkVscAccount(a)
            for a in self.client.account.modified[last].get()[1]
        ]
        accounts = {
            NEW: set(),
            UPDATED: set(),
            ERROR: set(),
        }

        logging.info("Found %d modified accounts in the range %s until %s" %
                     (len(sync_accounts),
                      datetime.fromtimestamp(last).strftime("%Y%m%d%H%M%SZ"),
                      self.now.strftime("%Y%m%d%H%M%SZ")))
        logging.debug("Modified accounts: %s",
                      [a.vsc_id for a in sync_accounts])

        for account in sync_accounts:
            try:
                usergroup = mkUserGroup(
                    self.client.account[account.vsc_id].usergroup.get()[1])
            except HTTPError:
                logging.error("No corresponding UserGroup for user %s" %
                              (account.vsc_id, ))
                continue
            gecos = ensure_ascii_string(account.person.gecos)

            logging.debug('fetching public key')
            public_keys = [
                ensure_ascii_string(x.pubkey)
                for x in self.client.get_public_keys(account.vsc_id)
            ]

            if not public_keys:
                public_keys = [ACCOUNT_WITHOUT_PUBLIC_KEYS_MAGIC_STRING]

            LDAP_STATE_MAPPER = {'forceinactive': 'inactive'}

            ldap_attributes = {
                'cn':
                str(account.vsc_id),
                'uidNumber': ["%s" % (account.vsc_id_number, )],
                'gecos': [gecos],
                'mail': [str(account.email)],
                'institute': [str(account.person.institute['name'])],
                'instituteLogin': [str(account.person.institute_login)],
                'uid': [str(account.vsc_id)],
                'homeDirectory': [str(account.home_directory)],
                'dataDirectory': [str(account.data_directory)],
                'scratchDirectory': [str(account.scratch_directory)],
                'pubkey':
                public_keys,
                'gidNumber': [str(usergroup.vsc_id_number)],
                'loginShell': [str(account.login_shell)],
                'researchField': [str(account.research_field[0])],
                'status': [
                    LDAP_STATE_MAPPER.get(str(account.status),
                                          str(account.status))
                ],
                'homeQuota': ["1"],
                'dataQuota': ["1"],
                'scratchQuota': ["1"],
            }
            logging.debug('fetching quota')
            quotas = self.client.account[account.vsc_id].quota.get()[1]
            for quota in quotas:
                for stype in ['home', 'data', 'scratch']:
                    # only gent sets filesets for vo's, so not gvo is user. (other institutes is empty or "None"
                    if quota['storage']['storage_type'] == stype and not quota[
                            'fileset'].startswith('gvo'):
                        ldap_attributes['%sQuota' %
                                        stype] = ["%d" % quota["hard"]]

            result = self.add_or_update(VscLdapUser, account.vsc_id,
                                        ldap_attributes, dry_run)
            accounts[result].add(account.vsc_id)

        return accounts