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