fixes to test scripts, added filter option to command line tool
[sfa.git] / changes / AddPersonToSite.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Persons import Person, Persons
5 from PLC.Sites import Site, Sites
6 from PLC.Auth import Auth
7 import sys              ###########################soner
8 sys.path.append('../../../../util')
9 from pl_to_geni import *
10 from util import *
11 from db import *
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         #insert the record into GENI tables        ###################soner
63         (global_sr_tree, global_cr_tree) = get_tree_globals()
64         (site_id, site_hrn) = site_to_auth(site_id_or_login_base)
65         dbinfo = determine_dbinfo(site_hrn, global_sr_tree)
66         if dbinfo == None:
67                 raise PLCInvalidArgument, "No GENI authority corresponding to the site "+site['name']
68         cnx = dbinfo[0]
69         tablename = dbinfo[1]
70         
71         new_hrn = person_to_user(person['email'])
72         existing = cnx.query("SELECT * FROM "+tablename+" WHERE hrn = '"+new_hrn+"'; ")
73         if existing != None:
74                 new_hrn = person_to_user(person['email'], 1)
75                 existing = cnx.query("SELECT * FROM "+tablename+" WHERE hrn = '"+new_hrn+"'; ")
76                 if existing != None:
77                         new_hrn = person_to_user(person['email'], 2)
78         
79         geni_record = {'hrn':''}
80         geni_record["hrn"] = new_hrn
81         geni_record["type"] = 'user'
82         geni_record['pointer'] = person['person_id']
83         
84         querystr = generate_querystr('INSERT', tablename, geni_record)
85         cnx.query(querystr)
86         
87         return 1