644826b676acad9359589665ddbce3ba7f3abd53
[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     object_type = 'Person'
29
30     def call(self, auth, person_id_or_email, site_id_or_login_base):
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         # Non-admins can only update their own primary site
44         if 'admin' not in self.caller['roles'] and \
45            self.caller['person_id'] != person['person_id']:
46             raise PLCPermissionDenied, "Not allowed to update specified account"
47
48         # Get site information
49         sites = Sites(self.api, [site_id_or_login_base])
50         if not sites:
51             raise PLCInvalidArgument, "No such site"
52         site = sites[0]
53
54         if site['peer_id'] is not None:
55             raise PLCInvalidArgument, "Not a local site"
56
57         if site['site_id'] not in person['site_ids']:
58             raise PLCInvalidArgument, "Not a member of the specified site"
59
60         person.set_primary_site(site)
61
62         return 1