registration pages reviewed and nicer - check for existing email
[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));
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']) {
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       $api->UpdateSite ($site_id,$site);
238       $api_error=$api->error();
239       if (!empty($api_error)) {
240         $error .= $api->error();
241         $messages [] = "Could not enable site";
242       } else {
243         $messages[] = "Site " . $site['name'] . " enabled";
244       }
245       
246       if (empty ($error)) {
247         // Update Address
248         $api->UpdateAddress($address_id,$address);
249         $api_error=$api->error();
250         if ( ! empty($api_error)) {
251           $error .= $api->error();
252           $messages [] = "Could not update address";
253         }
254         
255         foreach ( array("Billing","Shipping") as $address_type) {
256           $api->AddAddressTypeToAddress($address_type,$address_id);
257           $api_error=$api->error();
258           if ( ! empty($api_error)) {
259             $error .= $api->error();
260             $messages [] = "Could not add address type " . $address_type;
261           }
262         }
263           
264         // Update pi, and enable him
265         $api->UpdatePerson ($pi_id,$pi);
266         if ( $pi ['enabled' ] ) {
267           $messages [] = "PI already enabled";
268         } else {
269           $api->UpdatePerson ($pi_id,array("enabled"=>True));
270           $api_error=$api->error();
271           if (empty($api_error)) {
272             $messages[] = "Enabled PI as " . $pi['email'] ;
273             notify_enabled_pi ($api, $pi_id,$pi,$site_id, $site);
274             $messages[] = "Notified PI by email";
275           } else {
276             $error .= $api->error();
277             $messages [] = "Could not update PI";
278           }
279         }
280
281         if ($pi_id != $tech_id) {
282           // Update tech, and VerifyPerson him if needed
283           $api->UpdatePerson ($tech_id,$tech);
284           if ( $tech [ 'enabled' ] ) {
285             $messages [] = "Tech already enabled";
286           } else {
287             $api->VerifyPerson($tech_id);
288             $api_error=$api->error();
289             if (empty($api_error)) {
290               $messages[] = "Created account registration for Tech as " . $tech['email'];
291             } else {
292               $error .= $api->error();
293               $messages [] = "Could not verify Tech";
294             }
295           }
296         }
297       }
298
299       break;
300     }
301     default: {
302       $error .= '<div class-"plc-warning">Internal error - unexpected request</div>';
303       break;
304     }
305
306     } // end switch
307   }
308
309   // Show messages
310   if (!empty($messages)) {
311     print '<div class="messages status"><ul>';
312     foreach ($messages as $line) {
313       print "<li> $line";
314     }
315     print "</ul></div>";
316   }
317         
318   // Show errors if any
319   if (!empty($error)) {
320     print '<div class="messages error">' . $error . '</div>';
321     drupal_set_title('Join Request - Review');
322     render_join_request_review($api, $_POST['site_id']);    
323   } else {
324     drupal_set_title('All currently pending join requests');
325     render_all_join_requests($api);
326   }
327
328  }
329  else // list all pending requests
330 {
331
332     drupal_set_title('All currently pending join requests');
333     render_all_join_requests($api);
334 }
335
336 // Print footer
337 include 'plc_footer.php';
338
339 ?>