~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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Security adapters for the bugs module."""

__metaclass__ = type
__all__ = []

from zope.component import getUtility

from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.app.security import (
    AnonymousAuthorization,
    AuthorizationBase,
    DelegatedAuthorization,
    )
from lp.bugs.interfaces.bug import IBug
from lp.bugs.interfaces.bugattachment import IBugAttachment
from lp.bugs.interfaces.bugbranch import IBugBranch
from lp.bugs.interfaces.bugnomination import IBugNomination
from lp.bugs.interfaces.bugsubscription import IBugSubscription
from lp.bugs.interfaces.bugsubscriptionfilter import IBugSubscriptionFilter
from lp.bugs.interfaces.bugsupervisor import IHasBugSupervisor
from lp.bugs.interfaces.bugtask import IBugTaskDelete
from lp.bugs.interfaces.bugtracker import IBugTracker
from lp.bugs.interfaces.bugwatch import IBugWatch
from lp.bugs.interfaces.hasbug import IHasBug
from lp.bugs.interfaces.structuralsubscription import IStructuralSubscription
from lp.registry.interfaces.role import IHasOwner
from lp.services.messages.interfaces.message import IMessage


class EditBugNominationStatus(AuthorizationBase):
    permission = 'launchpad.Driver'
    usedfor = IBugNomination

    def checkAuthenticated(self, user):
        return self.obj.canApprove(user.person)


class EditBugTask(AuthorizationBase):
    """Permission checker for editing objects linked to a bug.

    Allow any logged-in user to edit objects linked to public
    bugs. Allow only explicit subscribers to edit objects linked to
    private bugs.
    """
    permission = 'launchpad.Edit'
    usedfor = IHasBug

    def checkAuthenticated(self, user):
        # Delegated entirely to the bug.
        return self.obj.bug.userCanView(user)


class DeleteBugTask(AuthorizationBase):
    permission = 'launchpad.Delete'
    usedfor = IBugTaskDelete

    def checkAuthenticated(self, user):
        """Check that a user may delete a bugtask.

        A user may delete a bugtask if:
         - project maintainer
         - task creator
         - bug supervisor
        """
        if user is None:
            return False

        # Admins can always delete bugtasks.
        if user.inTeam(getUtility(ILaunchpadCelebrities).admin):
            return True

        bugtask = self.obj
        owner = None
        if IHasOwner.providedBy(bugtask.pillar):
            owner = bugtask.pillar.owner
        bugsupervisor = None
        if IHasBugSupervisor.providedBy(bugtask.pillar):
            bugsupervisor = bugtask.pillar.bug_supervisor
        return (
            user.inTeam(owner) or
            user.inTeam(bugsupervisor) or
            user.inTeam(bugtask.owner))


class AdminDeleteBugTask(DeleteBugTask):
    """Launchpad admins can also delete bug tasks."""
    permission = 'launchpad.Admin'


class PublicToAllOrPrivateToExplicitSubscribersForBugTask(AuthorizationBase):
    permission = 'launchpad.View'
    usedfor = IHasBug

    def checkAuthenticated(self, user):
        return self.obj.bug.userCanView(user.person)

    def checkUnauthenticated(self):
        """Allow anonymous users to see non-private bugs only."""
        return not self.obj.bug.private


class EditPublicByLoggedInUserAndPrivateByExplicitSubscribers(
    AuthorizationBase):
    permission = 'launchpad.Edit'
    usedfor = IBug

    def checkAuthenticated(self, user):
        """Allow any logged in user to edit a public bug, and only
        explicit subscribers to edit private bugs. Any bug that can be
        seen can be edited.
        """
        return self.obj.userCanView(user)

    def checkUnauthenticated(self):
        """Never allow unauthenticated users to edit a bug."""
        return False


class PublicToAllOrPrivateToExplicitSubscribersForBug(AuthorizationBase):
    permission = 'launchpad.View'
    usedfor = IBug

    def checkAuthenticated(self, user):
        """Allow any user to see non-private bugs, but only explicit
        subscribers to see private bugs.
        """
        return self.obj.userCanView(user.person)

    def checkUnauthenticated(self):
        """Allow anonymous users to see non-private bugs only."""
        return not self.obj.private


class EditBugBranch(EditPublicByLoggedInUserAndPrivateByExplicitSubscribers):
    permission = 'launchpad.Edit'
    usedfor = IBugBranch

    def __init__(self, bug_branch):
        # The same permissions as for the BugBranch's bug should apply
        # to the BugBranch itself.
        super(EditBugBranch, self).__init__(bug_branch.bug)


class ViewBugAttachment(DelegatedAuthorization):
    """Security adapter for viewing a bug attachment.

    If the user is authorized to view the bug, he's allowed to view the
    attachment.
    """
    permission = 'launchpad.View'
    usedfor = IBugAttachment

    def __init__(self, bugattachment):
        super(ViewBugAttachment, self).__init__(
            bugattachment, bugattachment.bug)


class EditBugAttachment(DelegatedAuthorization):
    """Security adapter for editing a bug attachment.

    If the user is authorized to view the bug, he's allowed to edit the
    attachment.
    """
    permission = 'launchpad.Edit'
    usedfor = IBugAttachment

    def __init__(self, bugattachment):
        super(EditBugAttachment, self).__init__(
            bugattachment, bugattachment.bug)


class ViewBugSubscription(AnonymousAuthorization):

    usedfor = IBugSubscription


class EditBugSubscription(AuthorizationBase):
    permission = 'launchpad.Edit'
    usedfor = IBugSubscription

    def checkAuthenticated(self, user):
        """Check that a user may edit a subscription.

        A user may edit a subscription if:
         - They are the owner of the subscription.
         - They are the owner of the team that owns the subscription.
         - They are an admin of the team that owns the subscription.
        """
        if self.obj.person.is_team:
            return (
                self.obj.person.teamowner == user.person or
                user.person in self.obj.person.adminmembers)
        else:
            return user.person == self.obj.person


class ViewBugMessage(AnonymousAuthorization):

    usedfor = IMessage


class ViewBugTracker(AnonymousAuthorization):
    """Anyone can view a bug tracker."""
    usedfor = IBugTracker


class EditBugTracker(AuthorizationBase):
    permission = 'launchpad.Edit'
    usedfor = IBugTracker

    def checkAuthenticated(self, user):
        """Any logged-in user can edit a bug tracker."""
        return True


class AdminBugTracker(AuthorizationBase):
    permission = 'launchpad.Admin'
    usedfor = IBugTracker

    def checkAuthenticated(self, user):
        return (
            user.in_janitor or
            user.in_admin or
            user.in_launchpad_developers)


class AdminBugWatch(AuthorizationBase):
    permission = 'launchpad.Admin'
    usedfor = IBugWatch

    def checkAuthenticated(self, user):
        return (
            user.in_admin or
            user.in_launchpad_developers)


class EditStructuralSubscription(AuthorizationBase):
    """Edit permissions for `IStructuralSubscription`."""
    permission = "launchpad.Edit"
    usedfor = IStructuralSubscription

    def checkAuthenticated(self, user):
        """Subscribers can edit their own structural subscriptions."""
        return user.inTeam(self.obj.subscriber)


class EditBugSubscriptionFilter(AuthorizationBase):
    """Bug subscription filters may only be modified by the subscriber."""
    permission = 'launchpad.Edit'
    usedfor = IBugSubscriptionFilter

    def checkAuthenticated(self, user):
        return (
            self.obj.structural_subscription is None or
            user.inTeam(self.obj.structural_subscription.subscriber))