checkpoint
[plewww.git] / planetlab / persons / person.php
1 <?php
2
3 // $Id: index.php 11645 2009-01-21 23:09:49Z thierry $
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 include 'plc_header.php'; 
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_minitabs.php';
19 require_once 'plc_tables.php';
20 require_once 'plc_details.php';
21
22 // -------------------- 
23 // recognized URL arguments
24 $person_id=intval($_GET['id']);
25 if ( ! $person_id ) { plc_error('Malformed URL - id not set'); return; }
26
27 ////////////////////
28 // Get all columns as we focus on only one entry
29 $persons= $api->GetPersons( array($person_id));
30
31 if (empty($persons)) {
32   drupal_set_message ("Person " . $person_id . " not found");
33  } else {
34   $person=$persons[0];
35   
36   // vars from api
37   $first_name= $person['first_name'];
38   $last_name= $person['last_name'];
39   $title= $person['title'];
40   $url= $person['url'];
41   $phone= $person['phone'];
42   $email= $person['email'];
43   $enabled= $person['enabled'];
44   $peer_id=$person['peer_id'];
45   
46   // arrays from api
47   $role_ids= $person['role_ids'];
48   $roles= $person['roles'];
49   $site_ids= $person['site_ids'];
50   $slice_ids= $person['slice_ids'];
51   $key_ids= $person['key_ids'];
52   
53   // gets more data from API calls
54   $sites= $api->GetSites( $site_ids, array( "site_id", "name", "login_base" ) );
55   $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) );
56   $keys= $api->GetKeys( $key_ids );
57   
58   drupal_set_title("Details for account " . $first_name . " " . $last_name);
59
60   $plc_hash=plc_peer_get_hash($api);
61
62   $local_peer = plc_peer_block_start ($peer_hash,$peer_id);
63   $is_my_account = plc_my_person_id() == $person_id;
64   $privileges = plc_is_admin () || ( plc_in_site($site_id) && plc_is_pi());
65
66   $tabs=array();
67
68   if ($local_peer && $privileges) {
69     if (plc_is_admin())
70       $tabs['Events'] = array('url'=>l_events(),
71                               'values'=>array('type'=>'Person','person'=>$person_id),
72                               'bubble'=>'Related events',
73                               'image'=>'/planetlab/icons/event.png',
74                               'height'=>18);
75     if ($enabled) 
76       $tabs['Disable'] = array ('method'=>'POST',
77                                 'url'=>'/db/persons/person_actions.php',
78                                 'values'=> array ('person_id'=>$person_id,
79                                                   'action'=>'disable'),
80                                 'bubble'=>"Disable $first_name",
81                                 'confirm'=>"Are you sure you want to disable $first_name $last_name");
82     else 
83       $tabs['Enable'] = array ('method'=>'POST',
84                                 'url'=>'/db/persons/person_actions.php',
85                                 'values'=> array ('person_id'=>$person_id,
86                                                   'action'=>'enable'),
87                                 'bubble'=>"Enable $first_name");
88       $tabs['Delete'] = array ('method'=>'POST',
89                                 'url'=>'/db/persons/person_actions.php',
90                                 'values'=> array ('person_id'=>$person_id,
91                                                   'action'=>'delete'),
92                                'bubble'=>"Delete $first_name",
93                                'confirm'=>"Are you sure to delete $first_name $last_name");
94   }
95
96   if ($privileges || $is_my_account) 
97     $tabs['Update'] = array('url'=>'/db/persons/update.php',
98                             'values'=>array('id'=>$person_id),
99                             'bubble'=>"Update $first_name");
100
101   $tabs['All Users'] = array ('url'=>l_persons());
102
103   plc_tabs($tabs);
104     
105   plc_details_start();
106   plc_details_line("First Name",$first_name);
107   plc_details_line("Last Name",$last_name);
108   plc_details_line("Title",$title);
109   plc_details_line("Email",href("mailto:$email",$email));
110   plc_details_line("Phone",$phone);
111   plc_details_line("URL",$url);
112   plc_details_end();
113
114           
115   echo "<hr />\n";
116                 
117   // keys
118   $can_manage_keys = ( $local_peer && ( plc_is_admin() || $is_my_account) );
119   plc_table_title('Keys');
120   if ( empty( $key_ids ) ) {
121     echo "<span class='plc-warning'> This user has no known key</span>";
122   } else {
123     echo "<p><table border=0 width=450>\n";
124     echo "<thead><tr><th>Type</th><th>Key</th>";
125     if ( $can_manage_keys )
126       echo "<th>Remove</th>";
127     echo "</tr></thead><tbody>\n";
128     
129     foreach( $keys as $key ) {
130       $key_type= $key['key_type'];
131       $key_id= $key['key_id'];
132       $key_text= wordwrap( $key['key'], 70, "<br />\n", 1 );
133       echo "<tr><td>$key_type</td><td>$key_text";
134       
135       if ( $can_manage_keys ) 
136         echo "</td><td><input type=checkbox name='rem_key[]' value='$key_id'>";
137       
138       echo "</td></tr>\n";
139     }
140     
141     echo "</tbody></table>\n";
142     if ($can_manage_keys)
143       echo "<p><input type=submit name='Remove_keys' value='Remove keys'><br />\n";
144   }
145                 
146   if( $can_manage_keys ){
147     echo "<br /> Upload new key: <input type='file' name='key' size=30>\n
148         <input type='submit' name='Upload' value='Upload'>\n
149         <br /><hr />\n";
150   }
151
152   // sites
153   plc_table_title('Sites');
154   
155   // sites
156   if (empty( $sites ) ) {
157     plc_warning('This user is not affiliated with a site !!');
158     } else {
159     $columns=array();
160     $columns['Name']="string";
161     $columns['Login_base']="string";
162     $columns['Remove']="string";
163     $table_options = array('notes_area'=>false,'search_area'=>false);
164     plc_table_start ("person_slices",$columns,1,$table_options);
165     foreach( $sites as $site ) {
166       $site_name= $site['name'];
167       $site_id= $site['site_id'];
168       $login_base=$site['login_base'];
169       plc_table_row_start();
170       plc_table_cell (l_site_t($site_id,$site_name));
171       plc_table_cell ($login_base);
172       plc_table_cell ("<input id=" . $login_base . " type=checkbox name='rem_site[]' value=" . $site_id . ">");
173       plc_table_row_end ();
174     }
175     plc_table_end($table_options);
176   }
177
178   echo "<input type=submit name='Remove_Sites' value='Remove Sites'>\n";
179   
180         
181   // diplay site select list to add another site for user
182   if ($local_peer && plc_is_admin()) {
183     // get site info
184     $all_sites= $api->GetSites( NULL, array( "site_id", "name" ) );
185     
186     if( $sites )
187       $person_site= arr_diff( $all_sites, $sites );
188     else
189       $person_site= $all_sites;
190     
191     //    sort_sites( $person_site );
192     
193     echo "<p>Select a site to add this user to: ";
194     echo "<select name='site_add' onChange='submit()'>\n<option value=''>Choose a site to add:</option>\n";
195     
196     foreach( $person_site as $site ) {
197       echo "<option value=". $site['site_id'] .">". $site['name'] ."</option>\n";
198       
199     }
200     
201     echo "</select>";
202     
203   }
204   echo "<hr />\n";
205   
206   // roles
207   echo "<h3>Roles</h3>\n";
208   echo "<p><table>\n";
209   echo "<thead><tr><th>Role</th>";
210   if( plc_is_admin())
211     echo "<th>Remove</th>";
212   echo "</tr></thead><tbody>\n";
213   
214   // construct role array
215   for( $n=0; $n<count($roles); $n++ ) {
216     $proles[]= array( 'role_id'=>$role_ids[$n], 'name'=>$roles[$n] );
217   }
218     
219   $button_shown=0;
220   if ( !empty ($roles) ) {
221     foreach( $proles as $role ) {
222       $role_name= $role['name'];
223       $role_id= $role['role_id'];
224       
225       echo "<tr><td>$role_name";
226               
227
228       if( plc_is_admin()) {
229         echo "</td><td><input type=checkbox name='rem_role[]' value='$role_id'>";
230         if ( ! $button_shown ) {
231           $rowspan=count($roles);
232           echo "</td><td rowspan=$rowspan valign=center><input type=submit name='Remove_Roles' value='Remove Roles'></td></tr>\n";
233           $button_shown=1;
234         }
235       }
236       
237       echo "</td></tr>\n";
238     }
239   } else {
240     echo "<span class='plc-warning'> This user has no known role !!</span>";
241   }
242   echo "</tbody></table>\n";
243           
244   // if admin show roles to add
245   if( plc_is_admin()) {
246     $all_roles= $api->GetRoles();
247     $addable_roles= arr_diff( $all_roles, $proles );
248     ##when the proles array is empty strangely the method arr_diff($all_roles, $proles )
249     ##return an empty array and the scrolling roles list is not displayed in this case
250     ##assign to addablerole all the roles
251     if (count($proles)==0)
252       $addable_roles=$all_roles;
253     
254     if( !empty( $addable_roles ) ) {
255       echo "<p>Add role: <select name='add_role' onChange='submit()'>\n<option value=''>Choose a Role to add:</option>\n";
256       
257       foreach( $addable_roles as $arole ) {
258         echo "<option value=". $arole['role_id'] .">". $arole['name'] ."</option>\n";
259         
260       }
261       
262       echo "</select>\n";
263       
264     }
265   }
266   
267   echo "<hr />\n";
268   
269   // slices
270   plc_table_title('Slices');
271
272   if( empty( $slices ) ) {
273     drupal_set_message ("User has no slice");
274   } else {
275     $columns=array('Slice name'=>'string');
276     $table_options=array();
277     plc_table_start("person_slices",$columns,1,$table_options);
278
279     foreach( $slices as $slice ) {
280       $slice_name= $slice['name'];
281       $slice_id= $slice['slice_id'];
282       plc_table_row_start();
283       plc_table_cell(l_slice_t($slice_id,$slice_name));
284       plc_table_row_end();
285     }
286     plc_table_end($table_options);
287   }
288
289   plc_peer_block_end();
290   
291  }
292
293 // Print footer
294 include 'plc_footer.php';
295
296
297 ?>