17c3c993beee82eab1afd528e96067d16420f12d
[plcapi.git] / PLC / Methods / AddPersonToSite.py
1 from PLC.Faults import *
2 from PLC.Auth import Auth
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Sites import Site, Sites
6 from PLC.Persons import Person, Persons
7 from PLC.PersonTags import PersonTags, PersonTag
8 from PLC.Namespace import email_to_hrn
9 from PLC.TagTypes import TagTypes
10
11 from PLC.Debug import log
12
13 class AddPersonToSite(Method):
14     """
15     Adds the specified person to the specified site. If the person is
16     already a member of the site, no errors are returned. Does not
17     change the person's primary site.
18
19     Returns 1 if successful, faults otherwise.
20     """
21
22     roles = ['admin']
23
24     accepts = [
25         Auth(),
26         Mixed(Person.fields['person_id'],
27               Person.fields['email']),
28         Mixed(Site.fields['site_id'],
29               Site.fields['login_base'])
30         ]
31
32     returns = Parameter(int, '1 if successful')
33
34     def call(self, auth, person_id_or_email, site_id_or_login_base):
35         # Get account information
36         persons = Persons(self.api, [person_id_or_email])
37         if not persons:
38             raise PLCInvalidArgument, "No such account"
39         person = persons[0]
40
41         if person['peer_id'] is not None:
42             raise PLCInvalidArgument, "Not a local account"
43
44         # Get site information
45         sites = Sites(self.api, [site_id_or_login_base])
46         if not sites:
47             raise PLCInvalidArgument, "No such site"
48         site = sites[0]
49
50         if site['peer_id'] is not None:
51             raise PLCInvalidArgument, "Not a local site"
52
53         if site['site_id'] not in person['site_ids']:
54             site.add_person(person)
55
56         # Logging variables
57         self.event_objects = {'Site': [site['site_id']],
58                               'Person': [person['person_id']]}
59         self.message = 'Person %d added to site %d' % \
60                        (person['person_id'], site['site_id'])
61
62         # maintain person's hrn
63         # only if at this point we have a single site 
64         # which means, there was no site attached to person upon entering this call
65         try:
66             had_no_site= (len (person['site_ids']) == 0)
67             if had_no_site: 
68                 login_base=site['login_base']
69                 root_auth = self.api.config.PLC_HRN_ROOT
70                 hrn=email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
71                 tagname='hrn'
72                 tag_type = TagTypes(self.api,{'tagname':tagname})[0]
73                 person_tags=PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
74                 if not person_tags:
75                     person_tag = PersonTag(self.api)
76                     person_tag['person_id'] = person['person_id']
77                     person_tag['tag_type_id'] = tag_type['tag_type_id']
78                     person_tag['tagname']  = tagname
79                     person_tag['value'] = hrn
80                     person_tag.sync()
81                 else:
82                     person_tag = person_tags[0]
83                     person_tag['value'] = hrn
84                     person_tag.sync() 
85         except Exception,e:
86             print >> log, "BEG Warning, cannot maintain person's hrn, %s"%e
87             import traceback
88             traceback.print_exc(file=log)
89             print >> log, "END Warning, cannot maintain person's hrn, %s"%e
90                 
91
92         return 1