~landscape/zope3/ztk-1.1.3

« back to all changes in this revision

Viewing changes to src/zope/app/zopeappgenerations/evolve3.py

  • Committer: Sidnei da Silva
  • Date: 2010-07-05 21:07:01 UTC
  • Revision ID: sidnei.da.silva@canonical.com-20100705210701-zmqhqrbzad1mhzsl
- Reduce deps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
##############################################################################
2
 
#
3
 
# Copyright (c) 2004 Zope Corporation and Contributors.
4
 
# All Rights Reserved.
5
 
#
6
 
# This software is subject to the provisions of the Zope Public License,
7
 
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
 
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
 
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
 
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
 
# FOR A PARTICULAR PURPOSE.
12
 
#
13
 
##############################################################################
14
 
"""Evolve existing PAU group folders.
15
 
 
16
 
They should be used as contained plugins rather than registered plugins.
17
 
 
18
 
$Id: evolve3.py 85358 2008-04-14 20:30:57Z fdrake $
19
 
"""
20
 
__docformat__ = "reStructuredText"
21
 
 
22
 
from zope import component
23
 
 
24
 
from zope.app.component.interfaces import ISite
25
 
from zope.app.zopeappgenerations import getRootFolder
26
 
 
27
 
from zope.app.generations.utility import findObjectsProviding
28
 
 
29
 
from zope.app.component import registration
30
 
import zope.app.authentication.interfaces
31
 
from zope.app.authentication import groupfolder
32
 
from zope.copypastemove.interfaces import IObjectMover
33
 
 
34
 
generation = 3
35
 
 
36
 
def evolve(context):
37
 
    """Evolve existing PAUs and group folders.
38
 
 
39
 
    - Group folders should no longer be registered.
40
 
 
41
 
    - PAUs that use group folders should use their contents name, not their
42
 
      (formerly) registered name.
43
 
 
44
 
    Group folders used by multiple PAUs were not supported, and are not
45
 
    supported with this evolution.
46
 
    """
47
 
    root = getRootFolder(context)
48
 
 
49
 
    for site in findObjectsProviding(root, ISite):
50
 
        sm = site.getSiteManager()
51
 
        for pau in findObjectsProviding(
52
 
            sm, zope.app.authentication.interfaces.IPluggableAuthentication):
53
 
            for nm, util in component.getUtilitiesFor(
54
 
                zope.app.authentication.interfaces.IAuthenticatorPlugin,
55
 
                context=pau):
56
 
                if groupfolder.IGroupFolder.providedBy(util):
57
 
                    if util.__parent__ is not pau:
58
 
                        raise RuntimeError(
59
 
                            "I don't know how to migrate your database: "
60
 
                            "each group folder should only be within the "
61
 
                            "Pluggable Authentication utility that uses it")
62
 
                    # we need to remove this registration
63
 
                    regs = registration.Registered(util).registrations()
64
 
                    if len(regs) != 1:
65
 
                        raise RuntimeError(
66
 
                            "I don't know how to migrate your database: "
67
 
                            "you should only have registered your group "
68
 
                            "folder as an IAuthenticatorPlugin, but it looks "
69
 
                            "like it's registered for something additional "
70
 
                            "that I don't expect")
71
 
                    r = regs[0]
72
 
                    r.registry.unregisterUtility(
73
 
                       util,
74
 
                       zope.app.authentication.interfaces.IAuthenticatorPlugin,
75
 
                       nm)
76
 
                    if r.name in pau.authenticatorPlugins:
77
 
                        if util.__name__ != r.name: # else no-op
78
 
                            plugins = list(pau.authenticatorPlugins)
79
 
                            if util.__name__ in pau.authenticatorPlugins:
80
 
                                # argh! another active plugin's name is
81
 
                                # the same as this group folder's
82
 
                                # __name__.  That means we need to choose
83
 
                                # a new name that is also not in
84
 
                                # authenticatorPlugins and not in
85
 
                                # pau.keys()...
86
 
                                ct = 0
87
 
                                nm = '%s_%d' % (util.__name__, ct)
88
 
                                while (nm in pau.authenticatorPlugins or
89
 
                                       nm in pau):
90
 
                                    ct += 1
91
 
                                    nm = '%s_%d' % (util.__name__, ct)
92
 
                                IObjectMover(util).moveTo(pau, nm)
93
 
                            plugins[plugins.index(r.name)] = util.__name__
94
 
                            pau.authenticatorPlugins = tuple(plugins)
95
 
            for k, r in pau.registrationManager.items():
96
 
                if groupfolder.IGroupFolder.providedBy(r.component):
97
 
                    del pau.registrationManager[k]
98