185fdc95bc3c5a9d93952bffd1292cf25cc1ba6e
[plcapi.git] / PLC / Methods / AddPersonToSite.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 AddPersonToSite(Method):
10     """
11     Adds the specified person to the specified site. If the person is
12     already a member of the site, no errors are returned. Does not
13     change the person's primary site.
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin']
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     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         # Get site information
41         sites = Sites(self.api, [site_id_or_login_base])
42         if not sites:
43             raise PLCInvalidArgument, "No such site"
44         site = sites[0]
45
46         if site['peer_id'] is not None:
47             raise PLCInvalidArgument, "Not a local site"
48
49         if site['site_id'] not in person['site_ids']:
50             site.add_person(person)
51
52         # Logging variables
53         self.event_objects = {'Site': [site['site_id']],
54                               'Person': [person['person_id']]}
55         self.message = 'Person %d added to site %d' % \
56                 (person['person_id'], site['site_id'])
57         return 1