diff --git a/application/Espo/Core/Utils/Api/Auth.php b/application/Espo/Core/Utils/Api/Auth.php index 5cac0386ca..af14eb3dd5 100644 --- a/application/Espo/Core/Utils/Api/Auth.php +++ b/application/Espo/Core/Utils/Api/Auth.php @@ -41,6 +41,8 @@ class Auth extends \Slim\Middleware $authKey = $req->headers('PHP_AUTH_USER'); $authSec = $req->headers('PHP_AUTH_PW'); + $GLOBALS['log']->add('DEBUG', 'AUTH: Try to authenticate'); + if ($authKey && $authSec) { $isAuthenticated = false; @@ -53,11 +55,16 @@ class Auth extends \Slim\Middleware 'password' => md5($password) ), )); - if ($user instanceof \Espo\Entities\User) { - $this->entityManager->setUser($user); + + + if ($user instanceof \Espo\Entities\User) { + $this->entityManager->setUser($user); $this->container->setUser($user); - $isAuthenticated = true; + $isAuthenticated = true; } + + $GLOBALS['log']->add('DEBUG', 'AUTH: Result of authenticate =['.$isAuthenticated.']'); + if ($isAuthenticated) { $this->next->call(); } else { diff --git a/application/Espo/Core/Utils/Database/Converters/Orm.php b/application/Espo/Core/Utils/Database/Converters/Orm.php index 558d9de9f9..b116c71b24 100644 --- a/application/Espo/Core/Utils/Database/Converters/Orm.php +++ b/application/Espo/Core/Utils/Database/Converters/Orm.php @@ -111,16 +111,6 @@ class Orm $ormMeta = Util::merge($ormMeta, $convertedLinks); - //todo: move to separate file - //hardcode for emails - if (!isset($ormMeta['EmailAddress'])) { - $ormMeta['EmailAddress'] = $this->getOrmDefs('EmailAddress'); - } - - if (!isset($ormMeta['EntityEmailAddress'])) { - $ormMeta['EntityEmailAddress'] = $this->getOrmDefs('EntityEmailAddress'); - } //END: hardcode for emails - return $ormMeta; } @@ -156,6 +146,11 @@ class Orm $typeDefs = $this->getRelationManager()->process('typePersonName', $entityName, array('name' => $fieldName)); $fieldParams = Util::merge($fieldParams, $typeDefs[$entityName]['fields'][$fieldName]); break; + + case 'email': + $typeDefs = $this->getRelationManager()->process('entityEmailAddress', $entityName, array('name' => $fieldName)); + $entityParams = Util::merge($entityParams, $typeDefs[$entityName]); + break; } } } @@ -448,99 +443,4 @@ class Orm return $values; } - - //todo: move to separete file - protected function getOrmDefs($tableName) - { - switch ($tableName) { - case 'EmailAddress': - return array( - 'fields' => array( - 'id' => - array ( - 'type' => 'id', - 'dbType' => $this->idParams['dbType'], - 'len' => $this->idParams['len'], - ), - 'emailAddress' => - array ( - 'type' => 'varchar', - 'len' => 150, - ), - 'emailAddressLower' => - array ( - 'type' => 'varchar', - 'len' => 150, - ), - 'invalid' => - array ( - 'type' => 'bool', - 'default' => 0, - ), - 'optOut' => - array ( - 'type' => 'bool', - 'default' => 0, - ), - 'deleted' => - array ( - 'type' => 'bool', - 'default' => 0, - ), - ), - 'relations' => array( - ), - ); - break; - - case 'EntityEmailAddress': - return array( - 'fields' => array( - 'id' => - array ( - 'type' => 'id', - 'dbType' => 'int', - 'len' => $this->defaultLength['int'], - 'autoincrement' => true, - 'unique' => true, - ), - 'entityId' => - array ( - 'type' => $this->idParams['dbType'], - 'len' => $this->idParams['len'], - 'index' => 'entity', - ), - 'emailAddressId' => - array ( - 'type' => $this->idParams['dbType'], - 'len' => $this->idParams['len'], - ), - 'entityType' => - array ( - 'type' => 'varchar', - 'len' => $this->defaultLength['varchar'], - 'index' => 'entity', - ), - 'primary' => - array ( - 'type' => 'bool', - 'default' => 0, - ), - 'replyTo' => - array ( - 'type' => 'bool', - 'default' => 0, - ), - 'deleted' => - array ( - 'type' => 'bool', - 'default' => 0, - ), - ), - 'relations' => array( - ), - ); - break; - } - } } \ No newline at end of file diff --git a/application/Espo/Core/Utils/Database/Converters/RelationManager.php b/application/Espo/Core/Utils/Database/Converters/RelationManager.php index 45c28f1cd8..0f887c8d1c 100644 --- a/application/Espo/Core/Utils/Database/Converters/RelationManager.php +++ b/application/Espo/Core/Utils/Database/Converters/RelationManager.php @@ -63,21 +63,22 @@ class RelationManager $params['targetEntity'] = $foreignParams['entityName']; $foreignParams['targetEntity'] = $params['entityName']; - //entityDefs item with Relation Name - if (isset($link['params']['relationName'])) { //todo rename to 'helperName' or other one - $helperName = ucfirst($link['params']['relationName']); - $className = '\Espo\Custom\Core\Utils\Database\Helpers\\'.$helperName; - if (!class_exists($className)) { - $className = '\Espo\Core\Utils\Database\Helpers\\'.$helperName; - } + //relationDefs defined in separate file + //todo rename to 'helperName' or other one + $helperName = isset($link['params']['relationName']) ? ucfirst($link['params']['relationName']) : ucfirst($method); - if (class_exists($className)) { - $helperClass = new $className(); - return $helperClass->getRelation($params, $foreignParams); - } - } - //END: entityDefs item with Relation Name + $className = '\Espo\Custom\Core\Utils\Database\Helpers\\'.$helperName; + if (!class_exists($className)) { + $className = '\Espo\Core\Utils\Database\Helpers\\'.$helperName; + } + + if (class_exists($className) && method_exists($className, 'getRelation')) { + $helperClass = new $className(); + return $helperClass->getRelation($params, $foreignParams); + } + //END: relationDefs defined in separate file + if (method_exists($this, $method)) { return $this->$method($params, $foreignParams); diff --git a/application/Espo/Core/Utils/Database/Converters/Schema.php b/application/Espo/Core/Utils/Database/Converters/Schema.php index dc0c95cee1..18b3bd050b 100644 --- a/application/Espo/Core/Utils/Database/Converters/Schema.php +++ b/application/Espo/Core/Utils/Database/Converters/Schema.php @@ -77,14 +77,14 @@ class Schema $primaryColumns[] = Util::toUnderScore($fieldName); break; - case 'array': + /*case 'array': case 'json_array': $fieldParams['default'] = ''; //for db type TEXT can't be defined a default value break; case 'bool': $fieldParams['default'] = intval($fieldParams['default']); - break; + break;*/ case 'int': if (isset($fieldParams['autoincrement']) && $fieldParams['autoincrement']) { @@ -95,7 +95,7 @@ class Schema $fieldType = isset($fieldParams['dbType']) ? $fieldParams['dbType'] : $fieldParams['type']; if (!in_array($fieldType, $this->typeList)) { - $GLOBALS['log']->add('DEBUG', 'Converters/Schema::process(): Field type ['.$fieldType.'] does not exist '.$entityName.':'.$fieldName); + $GLOBALS['log']->add('DEBUG', 'Converters\Schema::process(): Field type ['.$fieldType.'] does not exist '.$entityName.':'.$fieldName); continue; } @@ -138,20 +138,7 @@ class Schema //check for duplication tables if (!isset($tables[$tableName])) { //no needs to create the table if it already exists - - if (isset($entityDefs[$entityName]['links'][$relationName]['relationName'])) { //todo: rename relationName to helperName - - if (isset($relationParams['conditions'])) { - $relationParams['midKeys'] = array_merge($relationParams['midKeys'], array_keys($relationParams['conditions'])); - } - $tables[$tableName] = $this->prepareManyMany($entityName, $relationParams, $tables, false); - - //add foreign and Indexes for entityTeam table - //$table->addForeignKeyConstraint($tables['Team'], array($usMidKey), array($relationKey), array("onUpdate" => "CASCADE")); - } else { - $tables[$tableName] = $this->prepareManyMany($entityName, $relationParams, $tables); - } - + $tables[$tableName] = $this->prepareManyMany($entityName, $relationParams, $tables); } break; @@ -181,33 +168,62 @@ class Schema * * @return \Doctrine\DBAL\Schema\Table */ - protected function prepareManyMany($entityName, $relationParams, $tables, $isForeignKey = true) + protected function prepareManyMany($entityName, $relationParams, $tables) { $tableName = $relationParams['relationName']; + $isForeignKey = true; + if (!isset($relationParams['key']) || !isset($relationParams['foreignKey'])) { + $isForeignKey = false; + } + + $table = $this->getSchema()->createTable( Util::toUnderScore($tableName) ); - $table->addColumn('id', 'int', array('length'=>$this->defaultLength['int'], 'autoincrement' => true)); + $table->addColumn('id', 'int', array('length'=>$this->defaultLength['int'], 'autoincrement' => true,)); //'unique' => true, if ($isForeignKey) { $relationEntities = array($entityName, $relationParams['entity']); $relationKeys = array($relationParams['key'], $relationParams['foreignKey']); } + //add midKeys to a schema foreach($relationParams['midKeys'] as $index => $midKey) { - $usMidKey = Util::toUnderScore($midKey); - if (preg_match('/_id$/i', $usMidKey)) { - $table->addColumn($usMidKey, $this->idParams['dbType'], array('length'=>$this->idParams['len'])); - } else { - $table->addColumn($usMidKey, 'varchar', array('length'=>$this->defaultLength['varchar'])); - } + $usMidKey = Util::toUnderScore($midKey); + $table->addColumn($usMidKey, $this->idParams['dbType'], array('length'=>$this->idParams['len'])); if ($isForeignKey) { $relationKey = Util::toUnderScore($relationKeys[$index]); $table->addForeignKeyConstraint($tables[$relationEntities[$index]], array($usMidKey), array($relationKey), array("onUpdate" => "CASCADE")); + } else { + $table->addIndex(array($usMidKey)); + } + } //END: add midKeys to a schema + + + //add additionalColumns + if (isset($relationParams['conditions'])) { + foreach($relationParams['conditions'] as $fieldName => $condition) { + $table->addColumn(Util::toUnderScore($fieldName), 'varchar', array('length'=>$this->defaultLength['varchar'])); } } + if (isset($relationParams['additionalColumns'])) { + foreach($relationParams['additionalColumns'] as $fieldName => $fieldParams) { + + if (!isset($fieldParams['type'])) { + $fieldParams = array_merge($fieldParams, array( + 'type' => 'varchar', + 'length' => $this->defaultLength['varchar'], + )); + } + + $table->addColumn(Util::toUnderScore($fieldName), $fieldParams['type'], $this->getDbFieldParams($fieldParams)); + } + } + //END: add additionalColumns + + $table->addColumn('deleted', 'bool', array('default' => 0)); $table->setPrimaryKey(array("id")); @@ -226,6 +242,17 @@ class Schema } } + switch ($fieldParams['type']) { + case 'array': + case 'json_array': + $dbFieldParams['default'] = ''; //for db type TEXT can't be defined a default value + break; + + case 'bool': + $dbFieldParams['default'] = intval($dbFieldParams['default']); + break; + } + return $dbFieldParams; } } \ No newline at end of file diff --git a/application/Espo/Core/Utils/Database/Helpers/EntityEmailAddress.php b/application/Espo/Core/Utils/Database/Helpers/EntityEmailAddress.php new file mode 100644 index 0000000000..b3074c356d --- /dev/null +++ b/application/Espo/Core/Utils/Database/Helpers/EntityEmailAddress.php @@ -0,0 +1,36 @@ + array( + 'relations' => array( + $params['link']['name'] => array( + 'type' => 'manyMany', + 'entity' => 'EmailAddress', + 'relationName' => 'entityEmailAddress', + 'midKeys' => array( + 'entity_id', + 'email_address_id', + ), + 'conditions' => array( + 'entityType' => $params['entityName'], + ), + 'additionalColumns' => array( + 'primary' => array( + 'type' => 'bool', + 'default' => false, + ), + ), + ), + ), + ), + ); + } + +} \ No newline at end of file diff --git a/application/Espo/Resources/metadata/fields/email.json b/application/Espo/Resources/metadata/fields/email.json index 2b4b87d9a0..1bccdcce9f 100644 --- a/application/Espo/Resources/metadata/fields/email.json +++ b/application/Espo/Resources/metadata/fields/email.json @@ -13,8 +13,5 @@ "search":{ "basic":true, "advanced":true - }, - "database":{ - "type":"varchar" } } diff --git a/testAuth.php b/testAuth.php index 179563b5f8..90ff1e09dc 100644 --- a/testAuth.php +++ b/testAuth.php @@ -52,14 +52,15 @@ class Auth extends \Slim\Middleware $user = $this->entityManager->getRepository('User')->findOne(array( 'whereClause' => array( 'userName' => $username, + 'password' => md5($password) ), )); + if ($user instanceof \Espo\Entities\User) { - if ($password == $user->get('password')) { - $this->container->setUser($user); - $isAuthenticated = true; - } + $this->entityManager->setUser($user); + $this->container->setUser($user); + $isAuthenticated = true; } //$isAuthenticated = true; diff --git a/tests/Espo/Core/Utils/Database/RelationsTest.php b/tests/Espo/Core/Utils/Database/RelationsTest.php index eae7a15295..4ef46ffcf1 100644 --- a/tests/Espo/Core/Utils/Database/RelationsTest.php +++ b/tests/Espo/Core/Utils/Database/RelationsTest.php @@ -470,6 +470,64 @@ class RelationsTest extends \PHPUnit_Framework_TestCase } + function testEntityEmailAddress() + { + $input = array( + 'params' => array ( + 'entityName' => 'User', + 'link' => + array ( + 'name' => 'email', + ), + 'targetEntity' => 'User', + ), + + 'foreignParams' => array ( + 'entityName' => 'User', + 'link' => + array ( + ), + 'targetEntity' => 'User', + ), + ); + + $result = array ( + 'User' => + array ( + 'relations' => + array ( + 'email' => + array ( + 'type' => 'manyMany', + 'entity' => 'EmailAddress', + 'relationName' => 'entityEmailAddress', + 'midKeys' => + array ( + 0 => 'entity_id', + 1 => 'email_address_id', + ), + 'conditions' => + array ( + 'entityType' => 'User', + ), + 'additionalColumns' => + array ( + 'primary' => + array ( + 'type' => 'bool', + 'default' => false, + ), + ), + ), + ), + ), + ); + + //todo: move to RelationManager file + $this->assertEquals( $result, $this->invokeMethod('entityEmailAddress', array($input['params'], $input['foreignParams'])) ); + } + +