Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmSetPersonEnabled.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 AdmSetPersonEnabled(Method):
8     """
9     Enables or disables a person.
10
11     Users and techs can only update themselves. PIs can only update
12     themselves and other non-PIs at their sites. Admins can update
13     anyone.
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin', 'pi']
19
20     accepts = [
21         PasswordAuth(),
22         Mixed(Person.fields['person_id'],
23               Person.fields['email']),
24         Person.fields['enabled']
25         ]
26
27     returns = Parameter(int, '1 if successful')
28
29     def call(self, auth, person_id_or_email, enabled):
30         # Get account information
31         persons = Persons(self.api, [person_id_or_email])
32         if not persons:
33             raise PLCInvalidArgument, "No such account"
34
35         person = persons.values()[0]
36
37         # Authenticated function
38         assert self.caller is not None
39
40         # Check if we can update this account
41         if not self.caller.can_update(person):
42             raise PLCPermissionDenied, "Not allowed to enable specified account"
43
44         person['enabled'] = enabled
45         person.flush()
46
47         return 1