initial import from onelab svn codebase
[plewww.git] / planetlab / sites / join_request.php
1 <?php
2 // $Id: join_request.php 776 2007-08-22 17:28:29Z thierry $
3 //
4 // page for administration of pending site registration requests
5 //
6
7 // Require login
8 require_once 'plc_login.php';
9
10 // Get session and API handles
11 require_once 'plc_session.php';
12 global $plc, $api;
13
14 // Common functions
15 require_once 'plc_functions.php';
16 require_once 'plc_sorts.php';
17
18 // Print header
19 require_once 'plc_drupal.php';
20 include 'plc_header.php';
21
22 include 'site_form.php';
23
24 ////////////////////////////////////////
25 function render_all_join_requests($api) {
26   $sites = $api->GetSites( array("enabled" => False, "peer_id" => NULL));
27   if (!empty($sites)) {
28     print("<table width=100%>");
29     print("<tr><th>Site Name</th><th>site_id</th><th>Submitted</th></tr>");
30     foreach($sites as $site) {
31       printf("<tr><td><a href=/db/sites/join_request.php?review=t&site_id=%d> %s </a> </td>", $site['site_id'], $site['name']);
32       printf("<td><a href=/db/sites/join_request.php?review=t&site_id=%d> %d </a> </td>", $site['site_id'], $site['site_id']);
33       printf("<td> %s </td> </tr>", date("d F Y, G:i",$site['date_created']));
34     }
35     print("</table>");
36   } else {
37     print("<p> No open join requests </p>");
38   }
39 }
40
41 function render_join_request_review($api, $site_id) {
42   $sites = $api->GetSites( array(intval($site_id)) );
43   if (empty($sites)) {
44       print("<p class='plc-warning'> Invalid request with site_id=$site_id</p> ");
45       return;
46   }
47   $site = $sites[0];
48   if ($site['enabled']) {
49     print("<p class='plc-warning'> This site is already enabled </p>");
50     return;
51   }
52   $addresses = $api->GetAddresses ($site['address_ids']);
53   if (empty ($addresses)) {
54       print("<p class='plc-warning'> No address found for site_id=$site_id</p> ");
55       return ;
56   }
57   $address = $addresses[0];
58   $address_id=$address['address_id'];
59 # just in case there is no person attached yet
60   if (empty ($site['person_ids'])) {
61     $persons=array();
62   } else {
63     $person_ids = $site['person_ids'];
64     $persons = $api->GetPersons( $person_ids, array( "person_id", "role_ids", "first_name", "last_name", "email" , "phone") );
65   }
66   $tech = Null;
67   $pi = Null;
68   foreach($persons as $person) {
69     if ( in_array('20',  $person['role_ids']) ) {
70       $pi = $person;
71     }
72     if ( in_array('40',  $person['role_ids']) ) {
73       $tech = $person;
74     }
75   }
76   $pi_id = $pi['person_id'];
77   $tech_id = $tech['person_id'];
78     
79   print <<< EOF
80 <p> Please review the join request below.</p>
81     <p> <b> Warning:</b> the PI email address that was provided in this form will <b> not be checked</b> automatically. We expect that as part of the handshake with the site, the support team has had an opportunity to use this address so it can be considered safe. Please check it manually if this is not the case.</p>
82 <form name="join_request" method="post" action="/db/sites/join_request.php">
83 <input type="hidden" name="pi_id" value="$pi_id">
84 <input type="hidden" name="address_id" value="$address_id">
85 <input type="hidden" name="tech_id" value="$tech_id">
86 <input type="hidden" name="site_id" value="$site_id">
87
88 <table border="0" width="100%" cellspacing="0" cellpadding="3">
89 EOF;
90
91   $site_form = build_site_form(FALSE);
92   $input = array ('site' => $site, 'address'=> $address, 'pi' => $pi, 'tech' => $tech);
93   
94   // display the buttons 
95   print <<< EOF
96     <tr>
97     <td colspan=2>
98     <table width="100%" border=0 cellspacing="0" cellpadding="5"> <tr> 
99     <td align=center><input type="submit" name="submitted" value="Delete"></td>
100     <td align=center><input type="submit" name="submitted" value="Update"></td>
101     <td align=center><input type="submit" name="submitted" value="Approve"></td>
102     </tr> </table>
103     </tr>
104 EOF;
105
106   // render the form - not supposed to be empty
107   form_render_table2 ($site_form, $input, TRUE);
108
109   // display the buttons 
110   print <<< EOF
111     <tr>
112     <td colspan=2>
113     <table width="100%" border=0 cellspacing="0" cellpadding="5"> <tr> 
114     <td align=center><input type="submit" name="submitted" value="Delete"></td>
115     <td align=center><input type="submit" name="submitted" value="Update"></td>
116     <td align=center><input type="submit" name="submitted" value="Approve"></td>
117     </tr> </table>
118     </tr>
119 EOF;
120
121   print "</table></form>";
122 }
123
124 function notify_enabled_pi ($api, $pi_id, $pi, $site_id, $site) {
125   // notify the PI
126   $template= <<<EOF
127 You have filed a site registration with the %s platform.
128
129 This registration has been approved, and your account was enabled
130 You were granted a PI role, meaning that you will be responsible 
131 for managing personal accounts and slices for your site
132
133 You can now enter the system at
134 https://%s:%d/
135 with %s as a login, 
136 and the password that you provided at registration-time
137
138 You can directly access your site information at
139 https://%s:%d/db/sites/index.php?id=%d
140
141 Please start with adding nodes for this site with
142 https://%s:%d/db/nodes/add_node.php
143
144 Our support team will be glad to answer any question that you might have
145 They are reachable at mailto:%s
146 EOF;
147  
148  $body=sprintf($template,
149                PLC_NAME,
150                PLC_WWW_HOST,PLC_WWW_SSL_PORT,
151                $pi['email'],
152                PLC_WWW_HOST,PLC_WWW_SSL_PORT,$site_id,
153                PLC_WWW_HOST,PLC_WWW_SSL_PORT,
154                PLC_MAIL_SUPPORT_ADDRESS);
155    
156  $subject="Site registration for " . $site['name'] . " has been approved by " . PLC_NAME;
157  $api->NotifyPersons(array($pi_id),$subject,$body);
158 }
159
160
161 // find person roles
162 $_person= $plc->person;
163 $_roles= $_person['role_ids'];
164
165 // only admins are allowed to view this page
166 if( !in_array( '10', $_roles ) ) {
167
168     print("<p> not allowed to view this page </p>");
169 }
170 else if ($_GET['review'])
171 {
172
173     //print review page
174     drupal_set_title('Join Request - Review');
175     render_join_request_review($api, $_GET['site_id']);
176         
177 }
178 else if ($_POST['submitted'] )
179 {
180
181   // parse the form
182   $site_form = build_site_form(FALSE);
183   $input = parse_form ($site_form, $_REQUEST, $input);
184   // xxx should not happen ?
185   $empty_form = $input['is_empty'];
186   $error = "";
187   $messages= array();
188   if ( $empty_form ) {
189     $error .= '<div class-"plc-warning">Internal error - empty form !?!</div>';
190   }
191   if (empty ($error)) {
192     $site=$input['site'];
193     $address=$input['address'];
194     $pi=$input['pi'];
195     $tech=$input['tech'];
196
197     // Look for missing/blank entries
198     $error .= form_check_required ($site_form, $input);
199   }
200
201   if (empty($error)) {
202     // get objects id from the request
203     $site_id = intval(trim($_POST['site_id']));
204     $address_id = intval(trim($_POST['address_id']));
205     $pi_id = intval(trim($_POST['pi_id']));
206     $tech_id = intval(trim($_POST['tech_id']));
207
208     switch ($_POST['submitted']) {
209     case 'Delete': {
210       $api->DeleteSite ($site_id);
211       $api_error=$api->error();
212       if (!empty($api_error)) {
213         $error .= $api->error();
214       } else {
215         $messages [] = "Site " . $site['name'] . " deleted";
216       }
217       break;
218     }
219     case 'Update': {
220       $api->begin();
221       $api->UpdateSite($site_id,$site);
222       $api->UpdateAddress($address_id,$address);
223       $api->UpdatePerson($pi_id,$pi);
224       $api->UpdatePerson($tech_id,$tech);
225       $api->commit();
226       $api_error=$api->error();
227       if (!empty($api_error)) {
228         $error .= $api->error();
229       } else {
230         $messages [] = "Join request updated for site " . $site['name'] ;
231       }
232       
233       break;
234     }
235     case 'Approve': {
236       // Thierry - august 22 2007
237       // keep it simple - the admin who approves is now supposed to check 
238       // the PI's email address, which makes the whole thing *a lot* simpler
239       // enable the site, enable the PI, and VerifyPerson the thec if different from the PI
240       $site['enabled'] = True;
241       $api->UpdateSite ($site_id,$site);
242       $api_error=$api->error();
243       if (!empty($api_error)) {
244         $error .= $api->error();
245         $messages [] = "Could not enable site";
246       } else {
247         $messages[] = "Site " . $site['name'] . " enabled";
248       }
249       
250       if (empty ($error)) {
251         // Update Address
252         $api->UpdateAddress($address_id,$address);
253         $api_error=$api->error();
254         if ( ! empty($api_error)) {
255           $error .= $api->error();
256           $messages [] = "Could not update address";
257         }
258         
259         foreach ( array("Billing","Shipping") as $address_type) {
260           $api->AddAddressTypeToAddress($address_type,$address_id);
261           $api_error=$api->error();
262           if ( ! empty($api_error)) {
263             $error .= $api->error();
264             $messages [] = "Could not add address type " . $address_type;
265           }
266         }
267           
268         // Update pi, and enable him
269         $api->UpdatePerson ($pi_id,$pi);
270         if ( $pi ['enabled' ] ) {
271           $messages [] = "PI already enabled";
272         } else {
273           $api->UpdatePerson ($pi_id,array("enabled"=>True));
274           $api_error=$api->error();
275           if (empty($api_error)) {
276             $messages[] = "Enabled PI as " . $pi['email'] ;
277             notify_enabled_pi ($api, $pi_id,$pi,$site_id, $site);
278             $messages[] = "Notified PI by email";
279           } else {
280             $error .= $api->error();
281             $messages [] = "Could not update PI";
282           }
283         }
284
285         if ($pi_id != $tech_id) {
286           // Update tech, and VerifyPerson him if needed
287           $api->UpdatePerson ($tech_id,$tech);
288           if ( $tech [ 'enabled' ] ) {
289             $messages [] = "Tech already enabled";
290           } else {
291             $api->VerifyPerson($tech_id);
292             $api_error=$api->error();
293             if (empty($api_error)) {
294               $messages[] = "Created account registration for Tech as " . $tech['email'];
295             } else {
296               $error .= $api->error();
297               $messages [] = "Could not verify Tech";
298             }
299           }
300         }
301       }
302
303       break;
304     }
305     default: {
306       $error .= '<div class-"plc-warning">Internal error - unexpected request</div>';
307       break;
308     }
309
310     } // end switch
311   }
312
313   // Show messages
314   if (!empty($messages)) {
315     print '<div class="messages status"><ul>';
316     foreach ($messages as $line) {
317       print "<li> $line";
318     }
319     print "</ul></div>";
320   }
321         
322   // Show errors if any
323   if (!empty($error)) {
324     print '<div class="messages error">' . $error . '</div>';
325     drupal_set_title('Join Request - Review');
326     render_join_request_review($api, $_POST['site_id']);    
327   } else {
328     drupal_set_title('All currently pending join requests');
329     render_all_join_requests($api);
330   }
331
332  }
333  else // list all pending requests
334 {
335
336     drupal_set_title('All currently pending join requests');
337     render_all_join_requests($api);
338 }
339
340 // Print footer
341 include 'plc_footer.php';
342
343 ?>