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