permissions.py 5.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from nm2.lib.permissions import Permissions, Permission
from . import const


class PersonVisitorPermissions(Permissions):
    """
    Store NM-specific permissions
    """
    __slots__ = ("visitor", "person")

    edit_email = Permission(doc="the visitor can edit the person's preferred email address")
    edit_bio = Permission(doc="the visitor can edit the person's biography")
    update_keycheck = Permission(doc="the visitor can update keycheck results for this person")
    view_person_audit_log = Permission(doc="the visitor can view the audit logs for this person's data")
    request_new_status = Permission(doc="the visitor can request a new status for this person")
    edit_ldap = Permission(doc="the visitor can edit the person's LDAP-synced fields")
    edit_fpr = Permission(doc="the visitor can edit the person's key fingerprint")
    fd_comments = Permission(doc="the visitor can view the person's FD comments")
19
    endorse_key = Permission(doc="the visitor can endorse a FingerPrint")
20
    view_certificate = Permission(doc="the visitor can view the DPL certificate")
21
22
23
24
25
26
27
28
29

    def __init__(self, person, visitor, **kw):
        super(PersonVisitorPermissions, self).__init__(**kw)
        # Person being visited
        self.visitor = visitor
        self.person = person.person

        if self.visitor is None:
            pass
30
        elif self.visitor.is_superuser:
31
32
33
34
35
36
37
38
            self._compute_admin_perms()
        elif self.visitor == self.person:
            self._compute_own_perms()
        elif self.visitor.is_am:
            self._compute_active_am_perms()
        elif self.visitor.is_dd:
            self._compute_dd_perms()

39
40
41
42
43
44
45
46
    def _person_is_dm(self):
        """
        If the person is in an active role for the project, their key fpr can't
        be updated
        """

        return self.person.status in (const.STATUS_DM, const.STATUS_DM_GA)

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    def _person_has_ldap_record(self):
        """
        If the person is already in LDAP, then nobody can edit their LDAP
        info, since this database then becomes a read-only mirror of LDAP
        """
        return self.person.status not in (const.STATUS_DC, const.STATUS_DM)

    def _person_has_frozen_processes(self):
        """
        Return True if there are active processes currently frozen for review
        """
        import process.models as pmodels
        return pmodels.Process.objects.filter(
                person=self.person, frozen_by__isnull=False, closed_time__isnull=True).exists()

    def _compute_admin_perms(self):
        self.edit_email = True
        self.edit_bio = True
        self.update_keycheck = True
        self.view_person_audit_log = True
67
        self.endorse_key = True
68
69
        if self.person.status in (const.STATUS_DM, const.STATUS_DM_GA, const.STATUS_DD_NU, const.STATUS_DD_U):
            self.view_certificate = True
70
71
72
73
74
75
76
77
        if self.person.possible_new_statuses:
            self.request_new_status = True
        if not self._person_has_ldap_record():
            self.edit_ldap = True
            self.edit_fpr = True
        elif self.person.status in (const.STATUS_EMERITUS_DD, const.STATUS_REMOVED_DD):
            self.edit_fpr = True
        self.fd_comments = True
78
79
80

        if self.visitor == self.person:
            self.endorse_key = False
81
82
83
84

    def _compute_own_perms(self):
        self.edit_email = True
        self.update_keycheck = True
85
        self.endorse_key = False
86
87
        if self.person.status in (const.STATUS_DM, const.STATUS_DM_GA, const.STATUS_DD_NU, const.STATUS_DD_U):
            self.view_certificate = True
88
89
90
91
        if not self._person_has_frozen_processes():
            if not self.person.pending:
                if not self._person_has_ldap_record():
                    self.edit_ldap = True
92
93
                    if not self._person_is_dm():
                        self.edit_fpr = True
94
95
96
97
98
99
100
101
102
103
104
105
                elif self.person.status in (const.STATUS_EMERITUS_DD, const.STATUS_REMOVED_DD):
                    self.edit_fpr = True
            self.edit_bio = True
        if self.person.pending:
            return
        self.view_person_audit_log = True
        if self.person.possible_new_statuses:
            self.request_new_status = True

    def _compute_active_am_perms(self):
        self.update_keycheck = True
        self.view_person_audit_log = True
106
        self.endorse_key = True
107
108
109
110
        if not self._person_has_frozen_processes():
            self.edit_bio = True
            if not self._person_has_ldap_record():
                self.edit_ldap = True
111
112
                if not self._person_is_dm():
                    self.edit_fpr = True
113
114
115
            elif self.person.status in (const.STATUS_EMERITUS_DD, const.STATUS_REMOVED_DD):
                self.edit_fpr = True

116
117
118
        if self.visitor == self.person:
            self.endorse_key = False

119
120
121
    def _compute_dd_perms(self):
        self.update_keycheck = True
        self.view_person_audit_log = True
122
123
124
125
        self.endorse_key = True

        if self.visitor == self.person:
            self.endorse_key = False
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

    # TODO: advocate view audit log


class LegacyProcessVisitorPermissions(PersonVisitorPermissions):
    """
    Permissions for visiting old-style Processes
    """
    __slots__ = ("process",)

    view_mbox = Permission(doc="the visitor can view the mailbox of this process")

    def __init__(self, process, visitor):
        super().__init__(process.person, visitor)
        self.process = process

        if self.visitor is None:
            pass
144
        elif self.visitor.is_superuser:
145
146
147
148
149
150
151
            self.view_mbox = True
        elif self.visitor == self.person:
            self.view_mbox = True
        elif self.visitor.is_am:
            self.view_mbox = True
        elif self.process.advocates.filter(pk=self.visitor.pk).exists():
            self.view_mbox = True