File indexing completed on 2024-12-22 05:33:26
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 * Created: 06.08.2018 0024 */ 0025 class Backend_CoauthController extends Local_Controller_Action_CliAbstract 0026 { 0027 0028 const filename = "members"; 0029 const filename_errors = "members"; 0030 0031 protected $logfile; 0032 protected $errorlogfile; 0033 /** @var Zend_Config */ 0034 protected $config; 0035 protected $log; 0036 0037 /** 0038 * @inheritDoc 0039 */ 0040 public function __construct( 0041 Zend_Controller_Request_Abstract $request, 0042 Zend_Controller_Response_Abstract $response, 0043 array $invokeArgs = array() 0044 ) { 0045 parent::__construct($request, $response, $invokeArgs); 0046 $this->config = Zend_Registry::get('config')->settings->server->oauth; 0047 $this->log = Zend_Registry::get('logger'); 0048 $this->_helper->viewRenderer->setNoRender(false); 0049 } 0050 0051 0052 /** 0053 * @throws Zend_Db_Statement_Exception 0054 * @throws Zend_Exception 0055 */ 0056 public function runAction() 0057 { 0058 ini_set('memory_limit', '1024M'); 0059 0060 $force = (boolean)$this->getParam('force', false); 0061 $method = $this->getParam('method', 'create'); 0062 0063 $this->log->info("METHOD: {$method}\n--------------\n"); 0064 0065 if ($this->hasParam('member_id')) { 0066 $memberId = $this->getParam('member_id'); 0067 $operator = $this->getParam('op', null); 0068 $members = $this->getMemberList($memberId, $operator); 0069 } else { 0070 $members = $this->getMemberList(); 0071 } 0072 0073 if ('create' == $method) { 0074 $this->exportMembers($members, $force); 0075 0076 return; 0077 } 0078 if ('update' == $method) { 0079 $this->updateMembers($members); 0080 echo "not implemented"; 0081 0082 return; 0083 } 0084 if ('validate' == $method) { 0085 $this->validateMembers($members); 0086 echo "not implemented"; 0087 0088 return; 0089 } 0090 } 0091 0092 /** 0093 * @param null $member_id 0094 * @param string $operator 0095 * 0096 * @return Zend_Db_Statement_Interface 0097 * @throws Zend_Db_Statement_Exception 0098 */ 0099 private function getMemberList($member_id = null, $operator = "=") 0100 { 0101 $filter = ""; 0102 if (empty($operator)) { 0103 $operator = "="; 0104 } 0105 if ($operator == "gt") { 0106 $operator = ">"; 0107 } 0108 if ($operator == "lt") { 0109 $operator = "<"; 0110 } 0111 if (isset($member_id)) { 0112 $filter = " AND `m`.`member_id` {$operator} " . $member_id; 0113 } 0114 0115 $sql = " 0116 SELECT `mei`.`external_id`,`m`.`member_id`, `m`.`username`, `me`.`email_address`, `m`.`password`, `m`.`roleId`, `m`.`firstname`, `m`.`lastname`, `m`.`profile_image_url`, `m`.`created_at`, `m`.`changed_at`, `m`.`source_id`, `m`.`biography`, `me`.`email_address` as `mail`, IF(ISNULL(`me`.`email_checked`),0,1) AS `mail_checked`, `m`.`password_type`, `m`.`is_active`, `m`.`is_deleted` 0117 FROM `member` AS `m` 0118 LEFT JOIN `member_email` AS `me` ON `me`.`email_member_id` = `m`.`member_id` AND `me`.`email_primary` = 1 0119 LEFT JOIN `member_external_id` AS `mei` ON `mei`.`member_id` = `m`.`member_id` 0120 WHERE `m`.`is_active` = 1 0121 AND `m`.`is_deleted` = 0 0122 AND `me`.`email_checked` IS NOT NULL 0123 AND `me`.`email_deleted` = 0 0124 AND LOCATE('_double', `m`.`username`) = 0 0125 AND LOCATE('_double', `me`.`email_address`) = 0 0126 " . $filter . " 0127 ORDER BY `m`.`member_id` ASC 0128 "; 0129 0130 $result = Zend_Db_Table::getDefaultAdapter()->query($sql); 0131 0132 $this->log->info("Load : " . $result->rowCount() . " members..."); 0133 0134 return $result; 0135 } 0136 0137 /** 0138 * @param Zend_Db_Statement_Interface $members 0139 * 0140 * @param bool $force 0141 * 0142 * @return bool 0143 * @throws Zend_Db_Statement_Exception 0144 * @throws Zend_Exception 0145 */ 0146 private function exportMembers($members, $force = false) 0147 { 0148 // only usernames which are valid in github/gitlab 0149 $usernameValidChars = new Local_Validate_UsernameValid(); 0150 $emailValidate = new Zend_Validate_EmailAddress(); 0151 $modelOAuth = new Default_Model_Ocs_OAuth($this->config); 0152 0153 while ($member = $members->fetch()) { 0154 $this->log->info("process " . Zend_Json::encode($member)); 0155 0156 //if (false === $usernameValidChars->isValid($member['username'])) { 0157 // file_put_contents($this->errorlogfile, print_r($member, true) . "user name validation error" . "\n\n", FILE_APPEND); 0158 // continue; 0159 //} 0160 if (false === $emailValidate->isValid($member["email_address"])) { 0161 $this->log->info("messages [\"email address validation error\"] "); 0162 continue; 0163 } 0164 try { 0165 //Export User, if he not exists 0166 $modelOAuth->createUserFromArray($member, $force); 0167 } catch (Exception $e) { 0168 $this->log->info($e->getMessage() . PHP_EOL . $e->getTraceAsString()); 0169 } 0170 $messages = $modelOAuth->getMessages(); 0171 $this->log->info("messages " . Zend_Json::encode($messages)); 0172 } 0173 0174 return true; 0175 } 0176 0177 /** 0178 * @param Zend_Db_Statement_Interface $members 0179 * 0180 * @return bool 0181 * @throws Zend_Db_Statement_Exception 0182 * @throws Zend_Exception 0183 */ 0184 private function updateMembers($members) 0185 { 0186 $modelOAuth = new Default_Model_Ocs_OAuth($this->config); 0187 0188 while ($member = $members->fetch()) { 0189 $this->log->info("process " . Zend_Json::encode($member)); 0190 try { 0191 $modelOAuth->updateUser($member); 0192 } catch (Exception $e) { 0193 $this->log->info($e->getMessage() . PHP_EOL . $e->getTraceAsString()); 0194 } 0195 $messages = $modelOAuth->getMessages(); 0196 $this->log->info("messages " . Zend_Json::encode($messages)); 0197 } 0198 0199 return true; 0200 } 0201 0202 /** 0203 * @param Zend_Db_Statement_Interface $members 0204 * 0205 * @return bool 0206 * @throws Zend_Db_Statement_Exception 0207 * @throws Zend_Exception 0208 */ 0209 private function validateMembers($members) 0210 { 0211 $modelOAuth = new Default_Model_Ocs_OAuth($this->config); 0212 0213 while ($member = $members->fetch()) { 0214 $this->log->info("process " . Zend_Json::encode($member)); 0215 try { 0216 $result = $modelOAuth->validateUser($member); 0217 } catch (Exception $e) { 0218 $this->log->info($e->getMessage() . PHP_EOL . $e->getTraceAsString()); 0219 } 0220 $messages = $modelOAuth->getMessages(); 0221 $this->log->info("messages " . Zend_Json::encode($messages)); 0222 } 0223 0224 return true; 0225 } 0226 0227 0228 }