File indexing completed on 2024-12-22 05:33:28

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Backend_UserController extends Local_Controller_Action_Backend
0024 {
0025 
0026     const RESULT_OK = "OK";
0027     const RESULT_ERROR = "ERROR";
0028     const DATA_ID_NAME = 'member_id';
0029 
0030     /** @var Default_Model_Member */
0031     protected $_model;
0032 
0033     protected $_modelName = 'Default_Model_Member';
0034     protected $_pageTitle = 'Manage Users';
0035 
0036     public function init()
0037     {
0038         $this->_model = new $this->_modelName();
0039 
0040         $this->view->pageTitle = $this->_pageTitle;
0041 
0042         parent::init();
0043     }
0044 
0045     public function indexAction()
0046     {
0047 
0048     }
0049 
0050     public function createAction()
0051     {
0052         $jTableResult = array();
0053         try {
0054             $newRow = $this->_model->createRow($this->getAllParams());
0055             $result = $newRow->save();
0056 
0057             $jTableResult['Result'] = self::RESULT_OK;
0058             $jTableResult['Record'] = $newRow->toArray();
0059         } catch (Exception $e) {
0060             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0061             $translate = Zend_Registry::get('Zend_Translate');
0062             $jTableResult['Result'] = self::RESULT_ERROR;
0063             $jTableResult['Message'] = $translate->_('Error while processing data.');
0064         }
0065 
0066         $this->_helper->json($jTableResult);
0067     }
0068 
0069     public function updateAction()
0070     {
0071         $jTableResult = array();
0072         try {
0073             $values = $this->getAllParams();
0074 
0075             foreach ($values as $key => $value) {
0076                 if ($value == '') {
0077                     $values[$key] = new Zend_Db_Expr('NULL');
0078                 }
0079             }
0080 
0081             $record = $this->_model->save($values);
0082 
0083             $jTableResult = array();
0084             $jTableResult['Result'] = self::RESULT_OK;
0085             $jTableResult['Record'] = $record->toArray();
0086         } catch (Exception $e) {
0087             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0088             $translate = Zend_Registry::get('Zend_Translate');
0089             $jTableResult['Result'] = self::RESULT_ERROR;
0090             $jTableResult['Message'] = $translate->_('Error while processing data.');
0091         }
0092 
0093         $this->_helper->json($jTableResult);
0094     }
0095 
0096     public function deleteAction()
0097     {
0098         if($this->hasParam(self::DATA_ID_NAME)) {
0099             $memberId = (int)$this->getParam(self::DATA_ID_NAME, null); 
0100         } else {
0101             $memberId = (int)$this->getParam('c', null); 
0102         }
0103 
0104         $this->_model->setDeleted($memberId);
0105 
0106         $identity = Zend_Auth::getInstance()->getIdentity();
0107 
0108         try {
0109             Default_Model_ActivityLog::logActivity($memberId, null, $identity->member_id, Default_Model_ActivityLog::BACKEND_USER_DELETE,
0110                 null);
0111         } catch (Exception $e) {
0112             Zend_Registry::get('logger')->err($e->getMessage() . PHP_EOL . $e->getTraceAsString());
0113         }
0114 
0115         $jTableResult = array();
0116         $jTableResult['Result'] = self::RESULT_OK;
0117 
0118         $this->_helper->json($jTableResult);
0119     }
0120 
0121     public function listAction()
0122     {
0123         $startIndex = (int)$this->getParam('jtStartIndex');
0124         $pageSize = (int)$this->getParam('jtPageSize');
0125         $sorting = $this->getParam('jtSorting');
0126         $filter['member_id'] = $this->getParam('filter_member_id');
0127         $filter['lastname'] = $this->getParam('filter_lastname');
0128         $filter['firstname'] = $this->getParam('filter_firstname');
0129         $filter['username'] = $this->getParam('filter_username');
0130         $filter['mail'] = $this->getParam('filter_mail');
0131 
0132         $select = $this->_model->select()->order($sorting)->limit($pageSize, $startIndex);
0133         //        foreach ($filter as $key => $value) {
0134         //            if (false === empty($value)) {
0135         //                $select->where("{$key} like ?", $value);
0136         //            }
0137         //        }
0138         $metadata = $this->_model->info(Zend_Db_Table_Abstract::METADATA);
0139 
0140         foreach ($filter as $key => $value) {
0141             if (is_array($value)) {
0142                 $list = '';
0143                 foreach ($value as $element) {
0144                     if (isset($element)) {
0145                         $list = $list . ',' . $element;
0146                     }
0147                 }
0148 
0149                 if (empty($list)) {
0150                     continue;
0151                 }
0152 
0153                 $list = substr($list, 1);
0154 
0155                 $select->where("{$key} in ({$list})");
0156 
0157                 continue;
0158             }
0159             if (false === empty($value)) {
0160                 $data_type = $metadata[$key]['DATA_TYPE'];
0161                 if (($data_type == 'varchar') OR ($data_type == 'text')) {
0162                     $select->where("{$key} like ?", '%' . $value . '%');
0163                 } else {
0164                     $select->where("{$key} = ?", $value);
0165                 }
0166             }
0167         }
0168 
0169         $reports = $this->_model->fetchAll($select);
0170 
0171         $reportsAll = $this->_model->fetchAll($select->limit(null, null)->reset('columns')
0172                                                      ->columns(array('countAll' => new Zend_Db_Expr('count(*)'))));
0173 
0174         $jTableResult = array();
0175         $jTableResult['Result'] = self::RESULT_OK;
0176         $jTableResult['Records'] = $reports->toArray();
0177         $jTableResult['TotalRecordCount'] = $reportsAll->current()->countAll;
0178 
0179         $this->_helper->json($jTableResult);
0180     }
0181 
0182     public function memberinfoAction()
0183     {
0184         $jTableResult = array();
0185         try {
0186             $memberId = (int)$this->getParam('member_id');
0187 
0188             $modelMember = new  Default_Model_Member();
0189             $record = $modelMember->find($memberId)->current();
0190             $this->view->member = $record;
0191             $view = $this->view->render('user/member.phtml');
0192 
0193             $jTableResult = array();
0194             $jTableResult['Result'] = self::RESULT_OK;
0195             $jTableResult['Record'] = $record->toArray();
0196             $jTableResult['ViewRecord'] = $view;
0197         } catch (Exception $e) {
0198             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0199             $translate = Zend_Registry::get('Zend_Translate');
0200             $jTableResult['Result'] = self::RESULT_ERROR;
0201             $jTableResult['Message'] = $translate->_('Error while processing data.');
0202         }
0203 
0204         $this->_helper->json($jTableResult);
0205     }
0206 
0207     public function exportAction()
0208     {
0209         $jTableResult = array();
0210         try {
0211             $memberId = (int)$this->getParam('c');
0212 
0213             $modelMember = new  Default_Model_Member();
0214             $record = $modelMember->fetchMemberData($memberId, false);
0215 
0216             $modelOpenCode = new Default_Model_Ocs_Gitlab();
0217             $modelOpenCode->createUserFromArray($record->toArray(), true);
0218             Zend_Registry::get('logger')->debug(__METHOD__ . ' - opencode : ' . implode(PHP_EOL." - ", $modelOpenCode->getMessages()));
0219 
0220             $modelIdent = new Default_Model_Ocs_Ldap();
0221             $modelIdent->addUserFromArray($record->toArray(), true);
0222             Zend_Registry::get('logger')->debug(__METHOD__ . ' - ldap : ' . implode(PHP_EOL." - ", $modelIdent->getMessages()));
0223 
0224             $modelId = new Default_Model_Ocs_OAuth();
0225             $modelId->createUserFromArray($record->toArray(), true);
0226             Zend_Registry::get('logger')->debug(__METHOD__ . ' - oauth : ' . implode(PHP_EOL." - ", $modelId->getMessages()));
0227 
0228             $modelForum = new Default_Model_Ocs_Forum();
0229             $modelForum->createUserFromArray($record->toArray(), true);
0230             Zend_Registry::get('logger')->debug(__METHOD__ . ' - forum : ' . implode(PHP_EOL." - ", $modelForum->getMessages()));
0231 
0232             $jTableResult = array();
0233             $jTableResult['Result'] = self::RESULT_OK;
0234             //$jTableResult['Record'] = $record->toArray();
0235             $jTableResult['Message'] = "OK";
0236         } catch (Exception $e) {
0237             Zend_Registry::get('logger')->err(__METHOD__ . ' - (Line ' . $e->getLine() . ') ' . $e->getMessage());
0238             $jTableResult['Result'] = self::RESULT_ERROR;
0239             $jTableResult['Message'] = $e->getMessage();
0240         }
0241 
0242         $this->_helper->json($jTableResult);
0243     }
0244     
0245     
0246     public function reactivateAction()
0247     {
0248         $memberId = (int)$this->getParam('c');
0249 
0250         $this->_model->setActivated($memberId);
0251 
0252         $identity = Zend_Auth::getInstance()->getIdentity();
0253 
0254         try {
0255             Default_Model_ActivityLog::logActivity($memberId, null, $identity->member_id, Default_Model_ActivityLog::BACKEND_USER_UNDELETE,
0256                 null);
0257         } catch (Exception $e) {
0258             Zend_Registry::get('logger')->err($e->getMessage() . PHP_EOL . $e->getTraceAsString());
0259         }
0260 
0261         $jTableResult = array();
0262         $jTableResult['Result'] = self::RESULT_OK;
0263 
0264         $this->_helper->json($jTableResult);
0265     }
0266 
0267 }