fix
[plewww.git] / planetlab / persons / person_action.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 //print header
11 require_once 'plc_drupal.php';
12 //set default
13 drupal_set_title('Persons');
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_sorts.php';
19
20 // find person roles
21 $_person= $plc->person;
22 $_roles= $_person['role_ids'];
23
24
25 // get person id
26 if( $_POST['person_id'] )
27   $person_id= $_POST['person_id'];
28
29
30 // if site_id submitted add the person to that site_id
31 if( $_POST['site_add'] ) {
32   $site_id= $_POST['site_add'];
33
34   $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
35   header( "location: index.php?id=$person_id" );
36   exit();
37
38 }
39
40 if ( $_POST['Remove_Sites']){
41   if( $_POST['rem_site'] ) {
42     foreach( $_POST['rem_site'] as $site_id ) {
43       $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
44     }
45     header( "location: index.php?id=$person_id" );
46     exit();
47   }else{
48     echo "<h3><span class='plc-warning'>Please select one or more Sites to  remove.<br /></h3> </span>\n";
49     echo "<br /><hr /><p><a href='/db/persons/index.php?id=$person_id'>Back to person page</a></div>";
50   }
51 }
52
53 // remove role
54 if ( $_POST['Remove_Roles']){
55   if($_POST['rem_role']) {
56     $rem_ids= $_POST['rem_role'];
57     foreach( $rem_ids as $role_id ) {
58       $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
59     }
60     header( "location: index.php?id=$person_id" );
61     exit();
62   }else{
63     echo "<h3><span class='plc-warning'>Please select one or more Roles to  remove.<br /></h3> </span>\n";
64     echo "<br /><hr /><p><a href='/db/persons/index.php?id=$person_id'>Back to person page</a></div>";
65   }
66 }
67
68 // add roles
69 if( $_POST['add_role'] ) {
70   $role_id= $_POST['add_role'];
71
72   $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
73
74   header( "location: index.php?id=$person_id" );
75   exit();
76 }
77
78 // enable person
79 if( $_GET['enab_id'] ) {
80   $per_id= $_GET['enab_id'];
81
82   $fields= array( "enabled"=>true );
83
84   $api->UpdatePerson( intval( $per_id ), $fields );
85
86   header( "location: index.php?id=$per_id" );
87   exit();
88
89 }
90
91 // disable person
92 if( $_GET['dis_id'] ) {
93   $per_id= $_GET['dis_id'];
94
95   $fields= array( "enabled"=>false );
96
97   $api->UpdatePerson( intval( $per_id ), $fields );
98
99   header( "location: index.php?id=$person_id" );
100   exit();
101   
102 }
103
104 // if action exists figure out what to do
105 if( $_POST['actions'] ) {
106
107   // depending on action, run function
108   switch( $_POST['actions'] ) {
109     case "delete":
110       header( "location: person_action.php?del_id=$person_id" );
111       exit();
112       break;
113     case "disable":
114       header( "location: person_action.php?dis_id=$person_id" );
115       exit();
116       break;
117     case "enable":
118       header( "location: person_action.php?enab_id=$person_id" );
119       exit();
120       break;
121     case "su":
122       plc_debug('plc',$plc);
123       $plc->BecomePerson (intval($person_id));
124       header ( "location: index.php" );
125       break;
126   }
127
128 }
129
130 // delete person
131 if( $_GET['per_id'] ) {
132   $person_id= $_GET['per_id'];
133
134   $api->DeletePerson( intval( $person_id ) );
135
136   header( "location: index.php" );
137   exit();
138  }
139
140 //delete a key
141 if ( $_POST['Remove_keys'] ){
142   if( $_POST['rem_key'] ) {
143     $key_ids= $_POST['rem_key'];
144     
145     foreach( $key_ids AS $key_id ) {
146       $api->DeleteKey( intval( $key_id ) );
147     }
148     header( "location: index.php?id=$person_id" );
149     exit();
150   }else{
151     echo "<h3><span class='plc-warning'>Please select one or more keys to remove.<br /></h3> </span>\n";
152     echo "<br /><hr /><p><a href='/db/persons/index.php?id=$person_id'>Back to person page</a></div>";
153   }
154  }
155
156 // upload a key if the user submitted one
157 if ( $_POST['Upload']){
158   if( isset( $_FILES['key'] ) ) {
159     $key_file= $_FILES['key']['tmp_name'];
160     if( $key_file ){
161       $fp = fopen( $key_file, "r" );
162       $key = "";
163       if( $fp ) {
164         // opened the key file, read the one line of contents
165         // The POST operation always creates a file even if the filename
166         // the user specified was garbage.  If there was some problem
167         // with the source file, we'll get a zero length read here.
168         $key = fread($fp, filesize($key_file));
169         fclose($fp);
170         
171         $key_id= $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
172         
173         if (!$key_id){
174           $error=  $api->error();
175           echo "<h3><span class='plc-warning'> Please verify your SSH  file content.<br /></h3> </span>\n";
176           print '<br /><div class="messages error">' . $error . '</div>';
177           echo "<br /><hr /><p><a href='/db/persons/index.php?id=$person_id'>Back to person page</a></div>";
178         }
179         else{
180           header( "location: index.php?id=$person_id" );
181           exit();
182         }
183       }else {
184         $error= "Unable to open key file.";
185         print '<div class="messages error">' . $error . '</div>';
186       }
187     }else{
188       echo "<h3><span class='plc-warning'>Please select a valid SSH key file to upload.<br /></h3> </span>\n";
189       echo "<br /><hr /><p><a href='/db/persons/index.php?id=$person_id'>Back to person page</a></div>";
190     }
191   }
192  }
193
194 // delete person confimation
195 if( $_GET['del_id'] ) {
196   $person_id= $_GET['del_id'];
197
198   // get person info from API
199   $person_info= $api->GetPersons( array( intval( $person_id ) ), array( "first_name", "last_name", "email", "roles" ) );
200
201   // start form
202   echo "<form action='person_action.php?per_id=$person_id' method=post>\n";
203
204   // show delete confirmation
205   echo "<h2>Delete ". $person_info[0]['first_name'] ." ". $person_info[0]['last_name'] ."</h2>\n";
206   echo "<p>Are you sure you want to delete this user?\n";
207
208   echo "<table><tbody>\n";
209   echo "<tr><th>Email: </th><td> ". $person_info[0]['email'] ."</td></tr>\n";
210   echo "<tr><th>Roles: </th><td> ";
211
212   foreach( $person_info[0]['roles'] as $role ) {
213     echo "$role<br />\n";
214   }
215
216   echo "</td></tr>\n";
217
218   echo "</tbody></table>\n";
219   echo "<p><input type=submit value='Delete User' name='delete'>\n";
220   echo "</form>\n";
221
222
223 }
224
225 // Print footer
226 include 'plc_footer.php';
227
228
229 ?>