399a7c1d1c768d0f6286fb04ed0df8955123c7e5
[plcapi.git] / PLC / Methods / DeletePerson.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Persons import Person, Persons
7 from PLC.Auth import Auth
8 from PLC.SFA import SFA
9
10 class DeletePerson(Method):
11     """
12     Mark an existing account as deleted.
13
14     Users and techs can only delete themselves. PIs can only delete
15     themselves and other non-PIs at their sites. ins can delete
16     anyone.
17
18     Returns 1 if successful, faults otherwise.
19     """
20
21     roles = ['admin', 'pi', 'user', 'tech']
22
23     accepts = [
24         Auth(),
25         Mixed(Person.fields['person_id'],
26               Person.fields['email'])
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
31     def call(self, auth, person_id_or_email):
32         # Get account information
33         persons = Persons(self.api, [person_id_or_email])
34         if not persons:
35             raise PLCInvalidArgument, "No such account"
36         person = persons[0]
37
38         if person['peer_id'] is not None:
39             raise PLCInvalidArgument, "Not a local account"
40
41         # Authenticated function
42         assert self.caller is not None
43
44         # Check if we can update this account
45         if not self.caller.can_update(person):
46             raise PLCPermissionDenied, "Not allowed to delete specified account"
47
48         person.delete()
49         
50         # Logging variables
51         self.event_objects = {'Person': [person['person_id']]}
52         self.message = 'Person %d deleted' % person['person_id']
53
54         sfa = SFA(self.api)
55         sfa.delete_record(person, 'person')
56
57         return 1