public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs)
 {
     $schema = $eventArgs->getSchema();
     //get the entity meta
     $meta = $eventArgs->getEntityManager()->getClassMetadata($this->config->getZfcUserEntityClass());
     //get the table name from the entity
     $revisionsTable = $schema->createTable($this->config->getRevisionTableName());
     $revisionsTable->addColumn('id', $this->config->getRevisionIdFieldType(), array('autoincrement' => true));
     $revisionsTable->addColumn('timestamp', 'datetime');
     $revisionsTable->addColumn('note', 'text', array('notnull' => false));
     $revisionsTable->addColumn('ipaddress', 'text', array('notnull' => false));
     $localColumnNames = array();
     $foreignColumnNames = array();
     foreach ($meta->getIdentifier() as $primaryKey) {
         $columnName = $meta->getColumnName($primaryKey);
         $foreignColumnNames[] = $columnName;
         $columnName = preg_replace('/user[^a-zA-Z0-9]*/', '', $columnName);
         $localColumnName = 'user_' . $columnName;
         $localColumnNames[] = $localColumnName;
         $fieldType = $meta->getTypeOfField($primaryKey);
         $revisionsTable->addColumn($localColumnName, $fieldType, array('notnull' => false));
     }
     //add the tablename and primary key from the entity meta
     $revisionsTable->addForeignKeyConstraint($meta->getTableName(), $localColumnNames, $foreignColumnNames);
     $revisionsTable->setPrimaryKey(array('id'));
 }
Пример #2
0
 public function getServiceConfig()
 {
     return array('factories' => array('auditConfig' => function ($sm) {
         $config = $sm->get('Config');
         $auditconfig = new Configuration();
         $auditconfig->setAuditedEntityClasses($config['zf2-entity-audit']['entities']);
         $auditconfig->setZfcUserEntityClass($config['zf2-entity-audit']['zfcuser.entity_class']);
         return $auditconfig;
     }, 'auditManager' => function ($sm) {
         $config = $sm->get('Config');
         $evm = $sm->get('doctrine.eventmanager.orm_default');
         $auditconfig = $sm->get('auditConfig');
         if ($config['zf2-entity-audit']['zfcuser.integration'] === true) {
             $auth = $sm->get('zfcuser_auth_service');
             if ($auth->hasIdentity()) {
                 $identity = $auth->getIdentity();
                 $auditconfig->setCurrentUser($identity);
             }
             /* need to handle the unauthenticated user action case, do it your own , 99% i will drop support for unauthenticated user auditing  */
         }
         $auditManager = new Manager($auditconfig);
         $evm->addEventSubscriber(new CreateSchemaListener($auditManager));
         $evm->addEventSubscriber(new LogRevisionsListener($auditManager));
         return $auditManager;
     }, 'auditReader' => function ($sm) {
         $auditManager = $sm->get('auditManager');
         $entityManager = $sm->get('doctrine.entitymanager.orm_default');
         return $auditManager->createAuditReader($entityManager);
     }));
 }
Пример #3
0
 public function testCliAuditWithLocalIPAddress()
 {
     unset($_SERVER["REMOTE_ADDR"]);
     $config = new Configuration();
     $config->setCurrentUser($this->ZfcUserMock);
     $ipaddress = $config->getIpAddress();
     $this->assertEquals("127.0.0.1", $ipaddress);
 }
Пример #4
0
 /**
  * @param EntityManager      $em
  * @param AuditConfiguration $config
  * @param MetadataFactory    $factory
  */
 public function __construct(EntityManager $em, Configuration $config, MetadataFactory $factory)
 {
     $this->em = $em;
     $this->config = $config;
     $this->metadataFactory = $factory;
     $this->platform = $this->em->getConnection()->getDatabasePlatform();
     $this->ZfcUserRepository = $this->em->getRepository($config->getZfcUserEntityClass());
 }
Пример #5
0
 public function setUp()
 {
     $this->Bootstrap = new Bootstrap();
     $this->em = $this->Bootstrap->getServiceManager()->get("doctrine.entitymanager.orm_default");
     /// echo sql logger
     //$this->em->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());;
     /// let's create the default user
     $this->ZfcUserMock = $this->createUser();
     $auditConfig = new Configuration();
     $auditConfig->setCurrentUser($this->ZfcUserMock);
     $auditConfig->setAuditedEntityClasses(array('ZF2EntityAuditTest\\Entity\\Article', 'ZF2EntityAuditTest\\Entity\\Writer'));
     $auditConfig->setNote("default note");
     $this->auditManager = new Manager($auditConfig);
     $this->auditManager->registerEvents($this->em->getEventManager());
     /// creating the tables
     $this->schemaTool = $this->getSchemaTool();
     $this->schemaTool->createSchema(array($this->em->getClassMetadata('ZF2EntityAuditTest\\Entity\\Article'), $this->em->getClassMetadata('ZF2EntityAuditTest\\Entity\\Writer')));
 }
 private function getInsertRevisionSQL($class)
 {
     if (!isset($this->insertRevisionSQL[$class->name])) {
         $placeholders = array('?', '?');
         $tableName = $this->config->getTablePrefix() . $class->table['name'] . $this->config->getTableSuffix();
         $sql = "INSERT INTO " . $tableName . " (" . $this->config->getRevisionFieldName() . ", " . $this->config->getRevisionTypeFieldName();
         foreach ($class->fieldNames as $field) {
             $type = Type::getType($class->fieldMappings[$field]['type']);
             $placeholders[] = !empty($class->fieldMappings[$field]['requireSQLConversion']) ? $type->convertToDatabaseValueSQL('?', $this->platform) : '?';
             $sql .= ', ' . $class->getQuotedColumnName($field, $this->platform);
         }
         foreach ($class->associationMappings as $assoc) {
             if (($assoc['type'] & ClassMetadata::TO_ONE) > 0 && $assoc['isOwningSide']) {
                 foreach ($assoc['targetToSourceKeyColumns'] as $sourceCol) {
                     $sql .= ', ' . $sourceCol;
                     $placeholders[] = '?';
                 }
             }
         }
         $sql .= ") VALUES (" . implode(", ", $placeholders) . ")";
         $this->insertRevisionSQL[$class->name] = $sql;
     }
     return $this->insertRevisionSQL[$class->name];
 }
Пример #7
0
 /**
  * @param AuditConfiguration $config
  */
 public function __construct(Configuration $config)
 {
     $this->config = $config;
     $this->metadataFactory = $config->createMetadataFactory();
 }