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