<commit_before><?php

require_once APPLICATION_PATH . '/library/password_compat/password.php';

/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2016 BillRun Technologies Ltd. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

/**
 * Users model class
 *
 * @package  Models
 * @subpackage Table
 * @since    0.5
 */
class UsersModel extends TableModel {
	
	const ROLES = ['admin', 'read', 'write'];

	public function __construct(array $params = array()) {
		$params['collection'] = Billrun_Factory::db()->users;
		parent::__construct($params);
		$this->search_key = "username";
	}
	
	public function getUserById($userId){
		$mongoId = new MongoId($userId);
		Billrun_Factory::log("Finish get user by id", Zend_Log::INFO);
		return current(iterator_to_array($this->collection->query(['_id' => $mongoId])->cursor()))->getRawData();
	}
	
	public function deleteUserById($userId){
		try{
			$mongoId = new MongoId($userId);
			$deleteQuery = $this->collection->remove(['_id' => $mongoId]);
			Billrun_Factory::log("Finish remove user", Zend_Log::INFO);
		}catch(\MongoException $e){
			$this->reportError($e->getMessage(), Zend_Log::NOTICE);
		}
		return "{$deleteQuery['nModified']} rows been removed";
	}
	
	public function insertUser($username, $roles, $password){
		try{ 
			$password = password_hash($password, PASSWORD_DEFAULT);
			$insertQuery = $this->collection->insert(['username' => $username, 'password' => $password, 'roles' => $roles]);
			Billrun_Factory::log("Finish insert new user", Zend_Log::INFO);
		}catch(\MongoException $e){
			$this->reportError($e->getMessage(), Zend_Log::NOTICE);
		}
			return "{$insertQuery['nModified']} rows been inserted";
	}
	
	public function updateUser($userId, $username, $roles, $password){
		$mongoId = new MongoId($userId);
		$setArray = array('username' => (string) $username,'roles' => $roles );
		
		foreach($roles as $role){
			if(!in_array($role, self::ROLES)){
				Billrun_Factory::log()->log("Illegal roles entered", Zend_Log::CRIT);
				return "Illegal roles";
			}
		}
		
		if($password){
			$password = password_hash($password, PASSWORD_DEFAULT);
			$setArray['password'] = $password;
		}
		
		try{
			Billrun_Factory::log("Start Update {$setArray}", Zend_Log::INFO);
			$updateQuery = $this->collection->update(array('_id' => $mongoId), array('$set' => $setArray));
		}catch(\MongoException $e){
			$this->reportError($e->getMessage(), Zend_Log::NOTICE);
		}<commit_msg>adding validation in model for existing username<commit_after><?php

require_once APPLICATION_PATH . '/library/password_compat/password.php';

/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2016 BillRun Technologies Ltd. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

/**
 * Users model class
 *
 * @package  Models
 * @subpackage Table
 * @since    0.5
 */
class UsersModel extends TableModel {
	
	const ROLES = ['admin', 'read', 'write'];

	public function __construct(array $params = array()) {
		$params['collection'] = Billrun_Factory::db()->users;
		parent::__construct($params);
		$this->search_key = "username";
	}
	
	public function getUserById($userId){
		$mongoId = new MongoId($userId);
		Billrun_Factory::log("Finish get user by id", Zend_Log::INFO);
		return current(iterator_to_array($this->collection->query(['_id' => $mongoId])->cursor()))->getRawData();
	}
	
	public function deleteUserById($userId){
		try{
			$mongoId = new MongoId($userId);
			$deleteQuery = $this->collection->remove(['_id' => $mongoId]);
			Billrun_Factory::log("Finish remove user", Zend_Log::INFO);
		}catch(\MongoException $e){
			return $this->reportError($e->getMessage(), Zend_Log::NOTICE);
		}
		return "{$deleteQuery['nModified']} rows been removed";
	}
	
	public function insertUser($username, $roles, $password){
		try{ 
			$password = password_hash($password, PASSWORD_DEFAULT);
			$checkForExistUsername = current(iterator_to_array($this->collection->query(['username' => $username])->cursor()));
			
			if($checkForExistUsername){
				Billrun_Factory::log("Username already exist {$username}", Zend_Log::INFO);
				throw $ex = new Billrun_Exceptions_Api(0, array(), 'Username already exist');
			}

			$insertQuery = $this->collection->insert(['username' => $username, 'password' => $password, 'roles' => $roles]);
			Billrun_Factory::log("Finish insert new user", Zend_Log::INFO);
		}catch(\MongoException $e){
			Billrun_Factory::log()->log($e->getMessage(), Zend_Log::CRIT);
		}
			return "{$insertQuery['nModified']} rows been inserted";
	}
	
	public function updateUser($userId, $username, $roles, $password){
		$mongoId = new MongoId($userId);
		$setArray = array('username' => (string) $username,'roles' => $roles );
		
		foreach($roles as $role){
			if(!in_array($role, self::ROLES)){
				Billrun_Factory::log()->log("Illegal roles entered", Zend_Log::CRIT);
				return "Illegal roles";
			}
		}
		
		if($password){
			$password = password_hash($password, PASSWORD_DEFAULT);
			$setArray['password'] = $password;
		}
		
		try{
			Billrun_Factory::log("Start Update {$setArray}", Zend_Log::INFO);
			$updateQuery = $this->collection->update(array('_id' => $mongoId), array('$set' => $setArray));
		}catch(\MongoException $e){
			Billrun_Factory::log()->log($e->getMessage(), Zend_Log::CRIT);
		}
{'fraction_non_alphanumeric': 0.17629629629629628, 'fraction_numerical': 0.0042592592592592595, 'mean_word_length': 5.014476614699332, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1333922', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1699, 'n_words': 360}