fix person & keys
[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
13 // Common functions
14 require_once 'plc_functions.php';
15
16 $known_actions=array();
17 ////////////////////////////////////////////////////////////
18 // interface :
19 // (*) use POST 
20 // (*) set 'action' to one of the following
21 $known_actions []= "add-person-to-site";
22 //        expects:      person_id & site_id
23 $known_actions []= "remove-person-from-site";
24 //        expects:      person_id & site_ids
25 $known_actions []= "remove-roles-from-person";
26 //        expects:      person_id & role_ids
27 $known_actions []= "add-role-to-person";
28 //        expects:      role_person_id & id
29 $known_actions []= "enable-person";
30 //        expects:      person_id
31 $known_actions []= "disable-person";
32 //        expects:      person_id
33 $known_actions []= "become-person";
34 //        expects:      person_id
35 $known_actions []= "delete-person";
36 //        expects:      person_id
37 $known_actions []= "delete-keys";
38 //        expects:      key_ids & person_id (for redirecting to the person's page)
39 $known_actions []= "upload-key";
40 //        expects:      person_id & $_FILES['key']
41
42 //////////////////////////////
43 // sometimes we don't set 'action', but use the submit button name instead
44 // so if 'action' not set, see if $_POST has one of the actions as a key
45 if ($_POST['action']) 
46   $action=$_POST['action'];
47 else 
48   foreach ($known_actions as $known_action) 
49     if ($_POST[$known_action]) {
50       $action=$known_action;
51       break;
52     }
53
54 //
55 $person_id = $_POST['person_id'];       // usually needed
56
57 if ( ! $action ) {
58   drupal_set_message ("person_actions.php: action not set");
59   return;
60  }
61
62 switch ($action) {
63
64  case 'add-person-to-site': {
65    $site_id = $_POST['site_id'];
66    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
67    header( "location: " . l_person($person_id));
68    exit();
69  }
70
71  case 'remove-person-from-sites': {
72    $site_ids = $_POST['site_ids'];
73    if ( ! $site_ids) {
74      drupal_set_message("action=$action - No site selected");
75      return;
76    }
77    foreach ( $site_ids as $site_id ) {
78      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
79    }
80    header( "location: " . l_person($person_id));
81    exit();
82  }
83
84  case 'remove-roles-from-person' : {
85    $role_ids=$_POST['role_ids'];
86    if ( ! $role_ids) {
87      drupal_set_message("action=$action - No role selected");
88      return;
89    }
90    foreach( $role_ids as $role_id)  {
91      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
92    }
93    header( "location: " . l_person($person_id));
94    exit();
95  }
96      
97  case 'add-role-to-person' : {
98    $role_id=$_POST['role_id'];
99    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
100    header( "location: " . l_person($person_id));
101    exit();
102  }
103
104  case 'enable-person' : {
105    $fields = array( "enabled"=>true );
106    $api->UpdatePerson( intval( $person_id ), $fields );
107    header( "location: " . l_person($person_id));
108    exit();
109  }
110
111  case 'disable-person' : {
112    $fields = array( "enabled"=>false );
113    $api->UpdatePerson( intval( $person_id ), $fields );
114    header( "location: " . l_person($person_id));
115    exit();
116  }
117
118  case 'become-person' : {
119    $plc->BecomePerson (intval($person_id));
120    header ("location: " . l_persons());
121    exit();
122  }
123
124  case 'delete-person' : {
125   $api->DeletePerson( intval( $person_id ) );
126   header( "location: " . l_persons() );
127   exit();
128  }
129
130  case 'delete-keys' : {
131    $key_ids=$_POST['key_ids'];
132    if ( ! $key_ids) {
133      drupal_set_message("action=$action - No key selected");
134      return;
135    }
136    foreach( $key_ids as $key_id ) {
137      $api->DeleteKey( intval( $key_id ) );
138    }
139    header( "location: " . l_person($person_id));
140    exit();
141  }
142
143  case 'upload-key' : {
144    if ( ! isset( $_FILES['key'] ) ) {
145      drupal_set_message ("action=$action, no key file set");
146      return;
147    }
148    
149    $key_file= $_FILES['key']['tmp_name'];
150    if ( ! $key_file ) {
151      plc_error("Please select a valid SSH key file to upload");
152      return;
153    } 
154    $fp = fopen( $key_file, "r" );
155    $key = "";
156    if( ! $fp ) {
157      plc_error("Unable to open key file $key_file");
158      return;
159    }
160    // opened the key file, read the one line of contents
161    // The POST operation always creates a file even if the filename
162    // the user specified was garbage.  If there was some problem
163    // with the source file, we'll get a zero length read here.
164    $key = fread($fp, filesize($key_file));
165    fclose($fp);
166    
167    $key_id= $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
168    
169    if ( ! $key_id ) {
170      $error=  $api->error();
171      plc_error("$error");
172      plc_error("Please verify your SSH  file content");
173      return;
174    }
175    header( "location: " . l_person($person_id));
176    exit();
177  }
178
179  case 'debug': {
180    plc_debug('GET',$_GET);
181    plc_debug('POST',$_POST);
182    plc_debug('FILES',$_FILES);
183    return;
184  }
185
186  default: {
187    plc_error ("Unknown action $action in person_actions.php");
188    return;
189  }
190
191  }
192
193 ?>