2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Persons import Person, Persons
6 from PLC.Sites import Site, Sites
7 from PLC.Auth import Auth
9 class SetPersonPrimarySite(Method):
11 Makes the specified site the person's primary site. The person
12 must already be a member of the site.
14 Admins may update anyone. All others may only update themselves.
17 roles = ['admin', 'pi', 'user', 'tech']
21 Mixed(Person.fields['person_id'],
22 Person.fields['email']),
23 Mixed(Site.fields['site_id'],
24 Site.fields['login_base'])
27 returns = Parameter(int, '1 if successful')
29 object_type = 'Person'
31 def call(self, auth, person_id_or_email, site_id_or_login_base):
32 # Get account information
33 persons = Persons(self.api, [person_id_or_email])
35 raise PLCInvalidArgument, "No such account"
38 if person['peer_id'] is not None:
39 raise PLCInvalidArgument, "Not a local account"
41 # Authenticated function
42 assert self.caller is not None
44 # Non-admins can only update their own primary site
45 if 'admin' not in self.caller['roles'] and \
46 self.caller['person_id'] != person['person_id']:
47 raise PLCPermissionDenied, "Not allowed to update specified account"
49 # Get site information
50 sites = Sites(self.api, [site_id_or_login_base])
52 raise PLCInvalidArgument, "No such site"
55 if site['peer_id'] is not None:
56 raise PLCInvalidArgument, "Not a local site"
58 if site['site_id'] not in person['site_ids']:
59 raise PLCInvalidArgument, "Not a member of the specified site"
61 person.set_primary_site(site)