~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Class that implements the IPersonRoles interface."""

__metaclass__ = type
__all__ = ['PersonRoles']

from zope.component import (
    adapts,
    getUtility,
    )
from zope.interface import implements
from zope.security.proxy import removeSecurityProxy

from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.bugs.interfaces.bugsupervisor import IHasBugSupervisor
from lp.bugs.interfaces.securitycontact import IHasSecurityContact
from lp.registry.interfaces.person import IPerson
from lp.registry.interfaces.role import (
    IHasDrivers,
    IPersonRoles,
    )


class PersonRoles:
    implements(IPersonRoles)
    adapts(IPerson)

    def __init__(self, person):
        self.person = person
        self._celebrities = getUtility(ILaunchpadCelebrities)
        # Use an unproxied inTeam() method for security checks.
        self.inTeam = removeSecurityProxy(self.person).inTeam

    def __getattr__(self, name):
        """Handle all in_* attributes."""
        prefix = 'in_'
        errortext = "'PersonRoles' object has no attribute '%s'" % name
        if not name.startswith(prefix):
            raise AttributeError(errortext)
        attribute = name[len(prefix):]
        try:
            return self.inTeam(getattr(self._celebrities, attribute))
        except AttributeError:
            raise AttributeError(errortext)

    @property
    def id(self):
        return self.person.id

    def isOwner(self, obj):
        """See IPersonRoles."""
        return self.inTeam(obj.owner)

    def isBugSupervisor(self, obj):
        """See IPersonRoles."""
        return (IHasBugSupervisor.providedBy(obj)
                and self.inTeam(obj.bug_supervisor))

    def isSecurityContact(self, obj):
        """See IPersonRoles."""
        return (IHasSecurityContact.providedBy(obj)
                and self.inTeam(obj.security_contact))

    def isDriver(self, obj):
        """See IPersonRoles."""
        return self.inTeam(obj.driver)

    def isOneOfDrivers(self, obj):
        """See IPersonRoles."""
        if not IHasDrivers.providedBy(obj):
            return self.isDriver(obj)
        for driver in obj.drivers:
            if self.inTeam(driver):
                return True
        return False

    def isOneOf(self, obj, attributes):
        """See IPersonRoles."""
        for attr in attributes:
            role = getattr(obj, attr)
            if self.inTeam(role):
                return True
        return False