コード例 #1
0
ファイル: LockItem.py プロジェクト: www3838438/Zope
    def __init__(self, creator, owner='', depth=0, timeout='Infinite',
                 locktype='write', lockscope='exclusive', token=None):
        errors = []
        # First check the values and raise value errors if outside of contract
        if not getattr(creator, 'getUserName', None):
            errors.append("Creator not a user object")
        if str(depth).lower() not in ('0', 'infinity'):
            errors.append("Depth must be 0 or infinity")
        if locktype.lower() != 'write':
            errors.append("Lock type '%s' not supported" % locktype)
        if lockscope.lower() != 'exclusive':
            errors.append("Lock scope '%s' not supported" % lockscope)

        timeout, e = validateTimeout(timeout)
        errors = errors + e

        # Finally, if there were errors, report them ALL to on high
        if errors:
            raise ValueError(errors)

        # AccessControl.owner.ownerInfo returns the id of the creator
        # and the path to the UserFolder they're defined in
        self._creator = ownerInfo(creator)

        self._owner = owner
        self._depth = depth
        self._timeout = timeout
        self._locktype = locktype
        self._lockscope = lockscope
        self._modifiedtime = time.time()

        if token is None:
            self._token = generateLockToken()
        else:
            self._token = token
コード例 #2
0
    def import_rows(self):
        if hasattr(self.context, 'openDataFile'):
            csvfile = self.context.openDataFile(self.filename)
            if csvfile is None:
                return
        else:
            datafile = self.context.readDataFile(self.filename)
            if datafile is None:
                return
            csvfile = tempfile.TemporaryFile()
            csvfile.write(datafile)
            csvfile.seek(0)
        reader = csv.DictReader(csvfile)

        getPrincipalById = getattr(
            self.acl_users,
            'get{}ById'.format(self.principal_type.capitalize()))
        rows = {}
        for row in reader:
            dest_id = row.get('Destination ID')
            source_id = row['Source ID']
            if not dest_id or source_id == dest_id:
                continue
            rows[source_id] = dest_id
            source_principal = getPrincipalById(source_id)

            groupmakers = self.plugins.listPlugins(IGroupsPlugin)
            for groupmaker_id, groupmaker in groupmakers:
                if not hasattr(groupmaker, 'addPrincipalToGroup'):
                    continue
                groups = groupmaker.getGroupsForPrincipal(source_principal)
                for group in groups:
                    logger.info('Changing group %r member from %r to %r',
                                group, source_principal.getId(), dest_id)
                    groupmaker.addPrincipalToGroup(dest_id, group)
                    groupmaker.removePrincipalFromGroup(
                        source_principal.getId(), group)

        acl_users_path = owner.ownerInfo(self.plugins)[0]

        def import_ofs_obj(obj,
                           path=None,
                           acl_users_path=acl_users_path,
                           rows=rows,
                           getPrincipalById=getPrincipalById):
            userdb_path, user_id = obj.getOwnerTuple()

            creators = getattr(obj, 'listCreators', [])
            if callable(creators):
                orig_creators = creators()
                creators = list(orig_creators)
            contributors = getattr(obj, 'listContributors', [])
            if callable(contributors):
                orig_contributors = contributors()
                contributors = list(orig_contributors)

            for source_id, dest_id in rows.iteritems():
                # ownership
                if (acl_users_path, source_id) == (userdb_path, user_id):
                    logger.info('Changing %r owner from %r to %r', obj,
                                source_id, dest_id)
                    dest_principal = getPrincipalById(dest_id)
                    obj.changeOwnership(dest_principal)

                # local roles
                local_roles = obj.get_local_roles_for_userid(source_id)
                if local_roles:
                    logger.info('Changing %r local roles %r from %r to %r',
                                obj, local_roles, source_id, dest_id)
                    obj.manage_addLocalRoles(dest_id, local_roles)
                    obj.manage_delLocalRoles([source_id])

                # CMF creators and contributors
                if source_id in creators:
                    creators[creators.index(source_id)] = dest_id
                if source_id in contributors:
                    contributors[contributors.index(source_id)] = dest_id

            creators = tuple(creators)
            if creators != orig_creators:
                logger.info('Changing %r creators from %r to %r', obj,
                            orig_creators, creators)
                obj.setCreators(creators)
            contributors = tuple(contributors)
            if contributors != orig_contributors:
                logger.info('Changing %r contributors from %r to %r', obj,
                            orig_contributors, contributors)
                obj.setContributors(contributors)

        if rows:
            self.site.ZopeFindAndApply(self.site, apply_func=import_ofs_obj)