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