File indexing completed on 2024-12-22 05:36:24

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 Local_Validate_UsernameUnique extends Zend_Validate_Abstract
0024 {
0025     const EXISTS = 'already_exists';
0026 
0027     protected $_messageTemplates = array(
0028         self::EXISTS => 'An activated username already exists.'
0029     );
0030 
0031     /**
0032      * @param mixed $value
0033      * @param null  $context
0034      *
0035      * @return bool
0036      * @throws Zend_Exception
0037      */
0038     public function isValid($value, $context = null)
0039     {
0040         $value = (string)$value;
0041         $this->_setValue($value);
0042 
0043         return $this->isUsernameUnique($value, $context);
0044     }
0045 
0046     /**
0047      * @param $username
0048      * @param $context
0049      *
0050      * @return bool
0051      * @throws Zend_Db_Statement_Exception
0052      */
0053     private function isUsernameUnique($username, $context)
0054     {
0055         $sql = "
0056         SELECT `username`, `mail`, COUNT(*) AS `amount`
0057         FROM `member` AS `m`
0058         WHERE `m`.`is_active` = 1 AND `m`.`is_deleted` = 0 AND lower(`username`) = lower(:username)
0059         GROUP BY lower(`username`) -- , mail
0060         HAVING COUNT(*) > 1
0061         ";
0062 
0063         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('username' => $username))->fetch();
0064 
0065         if (is_array($result) AND count($result) > 0) {
0066             $this->_error(self::EXISTS, $username);
0067 
0068             return false;
0069         }
0070 
0071         return true;
0072     }
0073 
0074 }