86e96c9ed466c77f62aee3d784cbe6d31adad1e4
[plcapi.git] / PLC / Methods / SetPersonPrimarySite.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.Sites import Site, Sites
6 from PLC.Auth import Auth
7
8 class SetPersonPrimarySite(Method):
9     """
10     Makes the specified site the person's primary site. The person
11     must already be a member of the site.
12
13     Admins may update anyone. All others may only update themselves.
14     """
15
16     roles = ['admin', 'pi', 'user', 'tech']
17
18     accepts = [
19         Auth(),
20         Mixed(Person.fields['person_id'],
21               Person.fields['email']),
22         Mixed(Site.fields['site_id'],
23               Site.fields['login_base'])
24         ]
25
26     returns = Parameter(int, '1 if successful')
27
28     def call(self, auth, person_id_or_email, site_id_or_login_base):
29         # Get account information
30         persons = Persons(self.api, [person_id_or_email])
31         if not persons:
32             raise PLCInvalidArgument, "No such account"
33         person = persons[0]
34
35         if person['peer_id'] is not None:
36             raise PLCInvalidArgument, "Not a local account"
37
38         # Authenticated function
39         assert self.caller is not None
40
41         # Non-admins can only update their own primary site
42         if 'admin' not in self.caller['roles'] and \
43            self.caller['person_id'] != person['person_id']:
44             raise PLCPermissionDenied, "Not allowed to update specified account"
45
46         # Get site information
47         sites = Sites(self.api, [site_id_or_login_base])
48         if not sites:
49             raise PLCInvalidArgument, "No such site"
50         site = sites[0]
51
52         if site['peer_id'] is not None:
53             raise PLCInvalidArgument, "Not a local site"
54
55         if site['site_id'] not in person['site_ids']:
56             raise PLCInvalidArgument, "Not a member of the specified site"
57
58         person.set_primary_site(site)
59
60         return 1