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