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