Xataface Switch User Module 0.1
Allow fast user switching for administrators of Xataface applications
/Applications/XAMPP/xamppfiles/htdocs/dtgapp/modules/switch_user/actions/switch_user.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Xataface Switch User Module
00004  * Copyright (C) 2011  Steve Hannah <steve@weblite.ca>
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301, USA.
00020  *
00021  */
00022  
00049 class actions_switch_user {
00050         function handle($params){
00051                 if ( @$_POST['--restore'] ){
00052                         if ( @$_SESSION['original_user'] ){
00053                                 $_SESSION['UserName'] = $_SESSION['original_user'];
00054                                 unset($_SESSION['original_user']);
00055                                 $this->response(array(
00056                                         'code'=>200,
00057                                         'msg'=>'Successfully restored user to '.$_SESSION['UserName']
00058                                 ));
00059                                 exit;
00060                         } else {
00061                                 $this->response(array(
00062                                         'code'=>500,
00063                                         'msg'=>'Failed to restore user because there was no original user to restore to.'
00064                                 ));
00065                                 exit;
00066                         }
00067                 } else {
00068                         $del = Dataface_Application::getInstance()->getDelegate();
00069                         if ( !(isset($del) and method_exists($del, 'canSwitchUser') and $del->canSwitchUser()) ){
00070                                 $this->response(array(
00071                                         'code'=>500,
00072                                         'msg'=>'Failed to change to different user because this action is reserved for administrators only.'
00073                                 ));
00074                         }
00075                         
00076                         if ( !@$_POST['--username'] ){
00077                                 $this->response(array(
00078                                         'code'=>500,
00079                                         'msg'=>'Failed to change to different user because no username was included in the request.'
00080                                 ));
00081                         }
00082                         
00083                         if ( @$_SESSION['original_user'] ){
00084                                 $this->response(array(
00085                                         'code'=>500,
00086                                         'msg'=>'Please return to your original user account before changing to a different account.'
00087                                 ));
00088                         }
00089                         
00090                         $_SESSION['original_user'] = $_SESSION['UserName'];
00091                         $_SESSION['UserName'] = $_POST['--username'];
00092                         $this->response(array(
00093                                 'code'=>200,
00094                                 'msg'=>'Successfully changed user to '.$_POST['--username']
00095                         ));
00096                 }
00097         }
00098         
00099         function response($params){
00100                 $params['username'] = $_SESSION['UserName'];
00101                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00102                 echo json_encode($params);
00103         }
00104 }
 All Data Structures Files Functions