* @copyright 2004 by gERD Schaufelberger * @package wombat * @subpackage admin */ /** * admin interface * * * @version 1.1.0 * @package wombat * @subpackage Admin */ class wbAdminApp_Login extends wbAdminApp { /** * default values for request variables * @access protected * @var array $_requestDefaults * @see $_request */ var $_requestDefaults = array( 'login' => '_no_login_set_', 'action' => '_no_action_set' ); /** * name of this application * @var string $appName */ var $_appName = 'Login'; /** * fullscreen? * @var boolean $_fullScreen */ var $_fullScreen = true; /** * authentication object * @var object $_auth */ var $_auth; /** * account manager * @var object $_acc */ var $_acc; /** * run admin interface * * @access public * @return boolean true on success */ function process( $path ) { // process path if( is_array( $path ) ) { $pathCurrent = array_shift( $path ); } $params = array( 'flags' => $this->_config['auth']['flags'] ); $this->_auth =& wbFactory::singleton( 'wbAuth', $params ); // try to login if( $this->_request['login'] == 'login' ) { // look for requiered vars if( !isset( $this->_request['login_user'] ) || !isset( $this->_request['login_passwd'] ) ) { $this->_loadTemplates( 'page' ); $this->_loadTemplates( 'page_login' ); return true; } // try to log in $res = $this->_auth->login( $this->_request['login_user'], $this->_request['login_passwd'] ); if( patErrorManager::isError( $res ) ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Login attempt results in error! User "'. $this->_request['login_user'] .'"', 'Login'); wbLog::err( 'admin', array( $this->_visitId, 'admin', 'Auth: Error', 'User: ' . $this->_request['login_user'] ) ); // error during login - stop here! return patErrorManager::raiseError( 'wbAdminApp:Login:1', 'Processing error', 'Login failed caused by unexpected error.' ); } // login failed if( !$res ) { $this->_loadTemplates( 'page' ); $this->_loadTemplates( 'page_login' ); $this->_tmpl->setAttribute( 'login_failed', 'visibility', 'visible' ); $this->_tmpl->addGlobalVar( 'login_user', $this->_request['login_user'] ); wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Login attempt failed! User "'. $this->_request['login_user'] .'"', 'Login' ); wbLog::info( 'admin', array( $this->_visitId, 'admin', 'Auth: Failed', 'User: ' . $this->_request['login_user'] ) ); return true; } // login successful - add some template variables... $sess =& wbFactory::singleton( 'patSession' ); $globals = array(); $globals['sess_name'] = $sess->getName(); $globals['sess_id'] = $sess->getId(); $globals['self'] = $_SERVER['PHP_SELF'] . '?' . $sess->getQueryString(); $userData = $this->_auth->getUserData(); $this->_tmpl->addGlobalVars( $globals ); $this->_tmpl->addGlobalVars( $userData, 'user_' ); // figure out session language if( !empty( $userData['langadm'] ) ) { $controller =& wbFactory::singleton( 'wbAdmin' ); $controller->initLang( $userData['langadm'] ); } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Login succeeded! User "'. $this->_request['login_user'] .'"', 'Login'); wbLog::info( 'admin', array( $this->_visitId, 'admin', 'Auth: Login', 'User: ' . $this->_request['login_user'] ) ); } // From here, user must be logged in! if( !$this->_auth->isAuthenticated() ) { // load screen $this->_loadTemplates( 'page' ); // these parts are available for non authorised users! switch( $pathCurrent ) { // renew forgotten password case 'Renew': $this->_loadAccountManager(); $this->_loadTemplates( 'page_renew' ); $content = $this->_acc->renewPassword(); $this->_tmpl->addVar( 'content', 'content', $content ); break; case 'Help': $this->_loadTemplates( 'page_help' ); break; case 'Login': default: $this->_loadTemplates( 'page_login' ); break; } return true; } // switch off fullscreen mode $this->_fullScreen = false; switch( $pathCurrent ) { case 'Logout': $this->_fullScreen = true; $this->_loadTemplates( 'page' ); $this->_loadTemplates( 'page_logout' ); $userData = $this->_auth->getUserData(); $res = $this->_auth->logoff(); if( patErrorManager::isError( $res ) ) { return patErrorManager::raiseError( 'wbAdminApp:Login:2', 'Processing error', 'Logout failed caused by unexpected error.' ); } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'User "'. $userData['user'] .'" logged out!', 'Login' ); wbLog::info( 'admin', array( $this->_visitId, 'admin', 'Auth: Logout' ) ); break; // edit contact defails case 'Contact': $this->_loadAccountManager(); $this->_acc->editAccountData(); break; // change my password case 'Password': $this->_loadAccountManager(); $this->_acc->changePassword(); break; // ticket system case 'Ticket': $result = $this->_processTicketManager( $path ); if( patErrorManager::isError( $result ) ) { return $result; } break; // display welcome screen case 'Welcome': default: $this->_loadTemplates( 'welcome' ); break; } return true; } /** * process ticket actions * * @access private * @param array $path current path * @return boolean true on success, patError on error */ function _processTicketManager( $path ) { $tm =& wbFactory::singleton( 'wbTicket' ); $userId = $this->_auth->getUserId(); $action = 'List'; if( count( $path ) <= 1 ) { $action = array_shift( $path ); } else { $ticket = array_shift( $path ); $action = array_shift( $path ); $result = $tm->loadTicketById( $ticket ); if( patErrorManager::isError( $result ) ) { return $result; } } switch( $action ) { case 'View': // get basic ticket data $ticData = $tm->getData(); // create comment form $formDef = array( 'comment' => array( 'type' => 'Text', 'attributes' => array( 'required' => 'yes', 'display' => 'yes', 'edit' => 'yes', 'label' => _('Comment'), 'title' => _('Comment'), 'description' => _('Add a user\'s comment to the ticket.'), 'minlength' => '3', ) ) ); $result = wbFactory::includeClass( 'patForms' ); if( patErrorManager::isError( $result ) ) { return $result; } $this->_loadTemplates( 'formErrors', false ); $form =& patForms::createForm( $formDef, array( 'name' => 'form' ) ); // process data if( $this->_request['action'] == 'save' ) { $form->setSubmitted( true ); if( $form->validateForm() ) { $data = $form->getValues(); // save comment if( !$this->_checkReload() ) { $tm->addComment( $this->_auth->getUserId(), $data['comment'] ); } // go to edit page! $this->_addMsg( _( 'Comment save!' ) ); } else { $this->_displayFormErrors( $form ); } } // the user manager is required to figure out more about ticket's / comment's owner $userManerger =& wbFactory::singleton( 'wbUser_Admin' ); $userKey = $userManerger->getPrimaryKey( 'user' ); $ticUser = array(); if( isset( $ticData[$userKey] ) && $ticData[$userKey]) { $ticUser = $userManerger->getUserData( $ticData[$userKey] ); if( $userId == $ticUser[$userKey] ) { $ticData['myticket'] = 'yes'; } } // add comments $ticCmt = $tm->getComments(); $ticData['comments'] = count( $ticCmt ); for( $i = 0; $i < count( $ticCmt ); ++$i ) { $cmtUser = $userManerger->getUserData( $ticCmt[$i][$userKey] ); foreach( $cmtUser as $key => $value ) { $ticCmt[$i]['user_' . $key] = $value; } } // add output to template engine $this->_loadTemplates( 'ticket_view' ); $this->_tmpl->addGlobalVar( 'id', $ticket ); unset( $ticData['link'] ); $this->_tmpl->addGlobalVars( $ticData, 'ticket_' ); $this->_tmpl->addGlobalVars( $ticUser, 'ticket_user_' ); $this->_tmpl->addRows( 'comment_entry', $ticCmt ); $renderer =& patForms::createRenderer( 'Array' ); $form->setRenderer( $renderer ); $elements = $form->renderForm(); $this->_tmpl->addRows( 'form_entry', $elements ); break; case 'Add': break; case 'Adopt': $this->_addMsg( _( 'Ticket accepted' ) ); $tm->assign( $userId ); return $this->_processTicketManager( array( 'List' ) ); break; case 'Solve': $this->_addMsg( _( 'Ticket solved' ) ); $tm->solve( $ticket ); return $this->_processTicketManager( array( 'List' ) ); break; case 'Reject': $this->_addMsg( _( 'Ticket rejected' ) ); $tm->reject( $ticket ); return $this->_processTicketManager( array( 'List' ) ); break; default: case 'List': // tickets assigned to current user $idMy = $tm->getAssignedTickets( $userId ); if( patErrorManager::isError( $idMy ) ) { return $idMy; } $ticMy = $tm->getTicketList( $idMy ); if( patErrorManager::isError( $ticMy ) ) { return $ticMy; } // tickets to be observerd $idWatch = $tm->getTickets4Rcpt( $userId ); if( patErrorManager::isError( $idWatch ) ) { return $idWatch ; } $ticWatch = $tm->getTicketList( $idWatch ); if( patErrorManager::isError( $ticWatch ) ) { return $ticWatch; } // add to template engine $this->_loadTemplates( 'ticket_list' ); if( !empty( $ticMy ) ) { for( $i = 0; $i < count( $ticMy ); ++$i ) { unset( $ticMy[$i]['link'] ); } $this->_tmpl->addGlobalVar( 'ticket_my_count', count( $ticMy ) ); $this->_tmpl->addRows( 'myticket_entry', $ticMy ); } if( !empty( $ticWatch ) ) { for( $i = 0; $i < count( $ticWatch ); ++$i ) { unset( $ticWatch[$i]['link'] ); } $this->_tmpl->addGlobalVar( 'ticket_watch_count', count( $ticWatch ) ); $this->_tmpl->addRows( 'watchticket_entry', $ticWatch ); } break; } return true; } /** * load account manager * * @access private * @return boolean true on success, patError on error */ function _loadAccountManager() { $baseDir = wbFactory::getParam( 'systemDir' ) . '/admin/templates'; $conf =& wbFactory::singleton( 'patConfiguration' ); $conf->loadConfig( 'contact.xml' ); $contact = $conf->getConfigValue(); $conf->clearConfigValue(); $from = $contact['default']['from']; if( isset( $contact['admin'] ) && isset( $contact['admin']['from'] ) ) { $from = $contact['admin']['from']; } $this->_acc =& wbFactory::singleton( 'wbAccount' ); if( patErrorManager::isError( $this->_acc ) ) { return $this->_acc; } $this->_acc->setEmailHeader( 'from', $from ); $this->_acc->setTemplateDir( $this->_appName, $baseDir ); $this->_acc->setRequest( $this->_request ); return true; } /** * get form errors from form and translate them to template * * @access private * @param object $form patForm object * @return boolean true */ function _displayFormErrors( &$form ) { $errors = $form->getValidationErrors(); if( empty( $errors ) ) { return true; } $errorRows = array(); foreach( $errors as $elName => $elErrors ) { $row = array(); $el =& $form->getElement( $elName ); $atts = $el->getAttributes(); foreach( $atts as $key => $att ) { if( is_array( $att ) ) { continue; } $row['field_'.$key] = $att; } foreach( $elErrors as $err ) { foreach( $err as $key => $value ) { $row['error_' . $key] = $value; } } array_push( $errorRows, $row ); } $this->_tmpl->addRows( 'formErrors_entry', $errorRows ); $this->_tmpl->setAttribute( 'formErrors' , 'visibility', 'visible' ); return true; } } ?>