imported adminsearch (and required plc_objects) from plcwww as-is
[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-sites";
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   plc_debug('POST',$_POST);
60   return;
61  }
62
63 switch ($action) {
64
65  case 'add-person-to-site': {
66    $site_id = $_POST['site_id'];
67    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
68    header( "location: " . l_person($person_id));
69    exit();
70  }
71
72  case 'remove-person-from-sites': {
73    $site_ids = $_POST['site_ids'];
74    if ( ! $site_ids) {
75      drupal_set_message("action=$action - No site selected");
76      return;
77    }
78    foreach ( $site_ids as $site_id ) {
79      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
80    }
81    header( "location: " . l_person($person_id));
82    exit();
83  }
84
85  case 'remove-roles-from-person' : {
86    $role_ids=$_POST['role_ids'];
87    if ( ! $role_ids) {
88      drupal_set_message("action=$action - No role selected");
89      return;
90    }
91    foreach( $role_ids as $role_id)  {
92      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
93    }
94    header( "location: " . l_person($person_id));
95    exit();
96  }
97      
98  case 'add-role-to-person' : {
99    $role_id=$_POST['role_id'];
100    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
101    header( "location: " . l_person($person_id));
102    exit();
103  }
104
105  case 'enable-person' : {
106    $fields = array( "enabled"=>true );
107    $api->UpdatePerson( intval( $person_id ), $fields );
108    header( "location: " . l_person($person_id));
109    exit();
110  }
111
112  case 'disable-person' : {
113    $fields = array( "enabled"=>false );
114    $api->UpdatePerson( intval( $person_id ), $fields );
115    header( "location: " . l_person($person_id));
116    exit();
117  }
118
119  case 'become-person' : {
120    $plc->BecomePerson (intval($person_id));
121    header ("location: " . l_persons());
122    exit();
123  }
124
125  case 'delete-person' : {
126   $api->DeletePerson( intval( $person_id ) );
127   header( "location: " . l_persons() );
128   exit();
129  }
130
131  case 'delete-keys' : {
132    $key_ids=$_POST['key_ids'];
133    if ( ! $key_ids) {
134      drupal_set_message("action=$action - No key selected");
135      return;
136    }
137    foreach( $key_ids as $key_id ) {
138      $api->DeleteKey( intval( $key_id ) );
139    }
140    header( "location: " . l_person($person_id));
141    exit();
142  }
143
144  case 'upload-key' : {
145    if ( ! isset( $_FILES['key'] ) ) {
146      drupal_set_message ("action=$action, no key file set");
147      return;
148    }
149    
150    $key_file= $_FILES['key']['tmp_name'];
151    if ( ! $key_file ) {
152      plc_error("Please select a valid SSH key file to upload");
153      return;
154    } 
155    $fp = fopen( $key_file, "r" );
156    $key = "";
157    if( ! $fp ) {
158      plc_error("Unable to open key file $key_file");
159      return;
160    }
161    // opened the key file, read the one line of contents
162    // The POST operation always creates a file even if the filename
163    // the user specified was garbage.  If there was some problem
164    // with the source file, we'll get a zero length read here.
165    $key = fread($fp, filesize($key_file));
166    fclose($fp);
167    
168    $key_id= $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
169    
170    if ( ! $key_id ) {
171      $error=  $api->error();
172      plc_error("$error");
173      plc_error("Please verify your SSH  file content");
174      return;
175    }
176    header( "location: " . l_person($person_id));
177    exit();
178  }
179
180  case 'debug': {
181    plc_debug('GET',$_GET);
182    plc_debug('POST',$_POST);
183    plc_debug('FILES',$_FILES);
184    return;
185  }
186
187  default: {
188    plc_error ("Unknown action $action in person_actions.php");
189    return;
190  }
191
192  }
193
194 ?>