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_CgitlabController 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->opencode; 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 = (int)$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, $force); 0086 0087 return; 0088 } 0089 if ('block' == $method) { 0090 $this->blockMember($members); 0091 0092 return; 0093 } 0094 if ('unblock' == $method) { 0095 $this->unblockMember($members); 0096 0097 return; 0098 } 0099 } 0100 0101 /** 0102 * @param null $member_id 0103 * @param string $operator 0104 * 0105 * @return Zend_Db_Statement_Interface 0106 * @throws Zend_Db_Statement_Exception 0107 */ 0108 private function getMemberList($member_id = null, $operator = "=") 0109 { 0110 $filter = ""; 0111 if (empty($operator)) { 0112 $operator = "="; 0113 } 0114 if ($operator == "gt") { 0115 $operator = ">"; 0116 } 0117 if ($operator == "lt") { 0118 $operator = "<"; 0119 } 0120 if (isset($member_id)) { 0121 $filter = " AND `m`.`member_id` {$operator} " . $member_id; 0122 } 0123 0124 $sql = " 0125 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`, `m`.`is_active`, `m`.`is_deleted` 0126 FROM `member` AS `m` 0127 LEFT JOIN `member_email` AS `me` ON `me`.`email_member_id` = `m`.`member_id` AND `me`.`email_primary` = 1 0128 LEFT JOIN `member_external_id` AS `mei` ON `mei`.`member_id` = `m`.`member_id` 0129 WHERE `m`.`is_active` = 1 0130 AND `m`.`is_deleted` = 0 0131 AND `me`.`email_checked` IS NOT NULL 0132 AND `me`.`email_deleted` = 0 0133 AND LOCATE('_double', `m`.`username`) = 0 0134 AND LOCATE('_double', `me`.`email_address`) = 0 0135 " . $filter . " 0136 ORDER BY `m`.`member_id` DESC 0137 "; 0138 0139 $result = Zend_Db_Table::getDefaultAdapter()->query($sql); 0140 0141 $this->log->info("Load : " . $result->rowCount() . " members..."); 0142 0143 return $result; 0144 } 0145 0146 /** 0147 * @param Zend_Db_Statement_Interface $members 0148 * 0149 * @param bool $force 0150 * 0151 * @return bool 0152 * @throws Zend_Db_Statement_Exception 0153 * @throws Zend_Exception 0154 */ 0155 private function exportMembers($members, $force = false) 0156 { 0157 // only usernames which are valid in github/gitlab 0158 $usernameValidChars = new Local_Validate_UsernameValid(); 0159 $emailValidate = new Zend_Validate_EmailAddress(); 0160 $modelOpenCode = new Default_Model_Ocs_Gitlab($this->config); 0161 0162 while ($member = $members->fetch()) { 0163 $this->log->info("process " . Zend_Json::encode($member)); 0164 0165 //if (false === $usernameValidChars->isValid($member['username'])) { 0166 // file_put_contents($this->errorlogfile, print_r($member, true) . "user name validation error" . "\n\n", FILE_APPEND); 0167 // continue; 0168 //} 0169 if (false === $emailValidate->isValid($member["email_address"])) { 0170 $this->log->info("messages [\"email address validation error\"] "); 0171 0172 continue; 0173 } 0174 try { 0175 //Export User, if he not exists 0176 $modelOpenCode->createUserFromArray($member, $force); 0177 } catch (Exception $e) { 0178 $this->log->info($e->getMessage() . PHP_EOL . $e->getTraceAsString()); 0179 } 0180 $messages = $modelOpenCode->getMessages(); 0181 $this->log->info("messages " . Zend_Json::encode($messages)); 0182 } 0183 0184 return true; 0185 } 0186 0187 /** 0188 * @param Zend_Db_Statement_Interface $members 0189 * 0190 * @param bool $force 0191 * 0192 * @return bool 0193 * @throws Zend_Db_Statement_Exception 0194 */ 0195 private function validateMembers($members, $force) 0196 { 0197 $modelSubSystem = new Default_Model_Ocs_Gitlab($this->config); 0198 0199 while ($member = $members->fetch()) { 0200 $modelSubSystem->resetMessages(); 0201 $this->log->info("process " . Zend_Json::encode($member)); 0202 try { 0203 $userSubsystem = $modelSubSystem->getUser($member['external_id'], $member['username']); 0204 if (false === $userSubsystem) { 0205 $this->log->info('Fail : user not exist (' . $member['member_id'] . ', ' . $member['username'] . ') ' . Zend_Json::encode($modelSubSystem->getMessages())); 0206 if ($force) { 0207 $modelSubSystem->createUserFromArray($member, true); 0208 $this->log->info("Message : " . Zend_Json::encode($modelSubSystem->getMessages())); 0209 } 0210 0211 continue; 0212 } 0213 0214 $result = $modelSubSystem->validateUserData($member, $userSubsystem); 0215 if (false === empty($result)) { 0216 $this->log->info('Fail : ' . implode(" ", $result)); 0217 if ($force) { 0218 $modelSubSystem->createUserFromArray($member, true); 0219 } 0220 } else { 0221 $this->log->info('Success'); 0222 } 0223 } catch (Exception $e) { 0224 $this->log->info($e->getMessage() . PHP_EOL . $e->getTraceAsString()); 0225 } 0226 $messages = $modelSubSystem->getMessages(); 0227 if (false === empty($messages)) { 0228 $this->log->info("Message : " . Zend_Json::encode($messages)); 0229 } 0230 } 0231 0232 return true; 0233 } 0234 0235 private function blockMember($members) 0236 { 0237 $modelSubSystem = new Default_Model_Ocs_Gitlab($this->config); 0238 0239 while ($member = $members->fetch()) { 0240 $result = $modelSubSystem->blockUser($member); 0241 if (false == $result) { 0242 $this->log->info('Fail'); 0243 } 0244 $result = $modelSubSystem->blockUserProjects($member); 0245 if (false == $result) { 0246 $this->log->info('Fail'); 0247 } 0248 $messages = $modelSubSystem->getMessages(); 0249 if (false === empty($messages)) { 0250 $this->log->info("Message : " . Zend_Json::encode($messages)); 0251 } 0252 } 0253 } 0254 0255 private function unblockMember($members) 0256 { 0257 $modelSubSystem = new Default_Model_Ocs_Gitlab($this->config); 0258 0259 while ($member = $members->fetch()) { 0260 $result = $modelSubSystem->unblockUser($member); 0261 if (false == $result) { 0262 $this->log->info('Fail'); 0263 } 0264 $result = $modelSubSystem->unblockUserProjects($member); 0265 if (false == $result) { 0266 $this->log->info('Fail'); 0267 } 0268 $messages = $modelSubSystem->getMessages(); 0269 if (false === empty($messages)) { 0270 $this->log->info("Message : " . Zend_Json::encode($messages)); 0271 } 0272 } 0273 } 0274 0275 }