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