With this objects, the data is passed arround and validated.
 /**
  * Loads the roles of an user via a many-to-many relationship
  *
  * @param Entity $user
  * the id of the user
  *
  * @return string[]
  * the roles of the user
  */
 protected function loadUserRolesViaManyToMany($user)
 {
     $roles = ['ROLE_USER'];
     if (is_string($this->userRoleIdentifier)) {
         foreach ($user->get($this->userRoleIdentifier) as $role) {
             $roles[] = $role['name'];
         }
     }
     return $roles;
 }
Exemple #2
0
 /**
  * Performs the regular unique validation.
  *
  * @param $value
  * the value to validate
  * @param $data
  * the data instance to validate with
  * @param $entity
  * the entity of the field
  * @param $field
  * the field to validate
  *
  * @return boolean
  * true if everything is valid
  */
 protected function isValidUnique($value, AbstractData $data, Entity $entity, $field)
 {
     $params = [$field => $value];
     $paramsOperators = [$field => '='];
     if ($entity->get('id') !== null) {
         $params['id'] = $entity->get('id');
         $paramsOperators['id'] = '!=';
     }
     $amount = intval($data->countBy($data->getDefinition()->getTable(), $params, $paramsOperators, true));
     return $amount == 0;
 }
Exemple #3
0
 /**
  * Constructor.
  *
  * @param string $usernameField
  * the username
  *
  * @param string $passwordField
  * the password (hash)
  *
  * @param string $saltField
  * the password hash salt
  *
  * @param Entity $userEntity
  * the actual user data
  *
  * @param array $roles
  * the roles
  */
 public function __construct($usernameField, $passwordField, $saltField, Entity $userEntity, array $roles)
 {
     $this->usernameField = $usernameField;
     $this->passwordField = $passwordField;
     $this->saltField = $saltField;
     // We have to copy it over as symfony/security wants something serializable.
     $this->userData = [];
     foreach ($userEntity->getDefinition()->getFieldNames() as $field) {
         $this->userData[$field] = $userEntity->get($field);
     }
     $this->roles = $roles;
 }
Exemple #4
0
 /**
  * Creates a new, empty entity instance having all fields prefilled with
  * null or the defined value in case of fixed fields.
  *
  * @return Entity
  * the newly created entity
  */
 public function createEmpty()
 {
     $entity = new Entity($this->definition);
     $fields = $this->definition->getEditableFieldNames();
     foreach ($fields as $field) {
         $value = null;
         if ($this->definition->getType($field) == 'fixed') {
             $value = $this->definition->getField($field, 'value');
         }
         $entity->set($field, $value);
     }
     $entity->set('id', null);
     return $entity;
 }
 /**
  * Validates and saves the new or updated entity and returns the appropriate HTTP
  * response.
  *
  * @param Application $app
  * the current application
  * @param AbstractData $crudData
  * the data instance of the entity
  * @param Entity $instance
  * the entity
  * @param string $entity
  * the name of the entity
  * @param boolean $edit
  * whether to edit (true) or to create (false) the entity
  *
  * @return Response
  * the HTTP response of this modification
  */
 protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit)
 {
     $fieldErrors = [];
     $mode = $edit ? 'edit' : 'create';
     $request = $app['request_stack']->getCurrentRequest();
     if ($request->getMethod() == 'POST') {
         $instance->populateViaRequest($request);
         $validator = new EntityValidator($instance);
         $validation = $validator->validate($crudData, intval($request->get('version')));
         $fieldErrors = $validation['errors'];
         if (!$validation['valid']) {
             $optimisticLocking = isset($fieldErrors['version']);
             $this->setValidationFailedFlashes($app, $optimisticLocking, $mode);
         } else {
             $modified = $edit ? $crudData->update($instance) : $crudData->create($instance);
             $response = $modified ? $this->modifyFilesAndSetFlashBag($app, $crudData, $instance, $entity, $mode) : false;
             if ($response) {
                 return $response;
             }
             $app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.' . $mode . '.failed'));
         }
     }
     return $app['twig']->render($app['crud']->getTemplate($app, 'template', 'form', $entity), ['crudEntity' => $entity, 'crudData' => $crudData, 'entity' => $instance, 'mode' => $mode, 'fieldErrors' => $fieldErrors, 'layout' => $app['crud']->getTemplate($app, 'layout', $mode, $entity)]);
 }
Exemple #6
0
 /**
  * Determines whether the entity needs a new hash generated.
  *
  * @param AbstractData $data
  * the CRUDlex data instance of the user entity
  * @param Entity $entity
  * the entity
  * @param string $passwordField
  * the field holding the password hash in the entity
  * @param string $password
  * the current password hash
  * @param boolean $newSalt
  * whether a new password hash salt was generated
  *
  * @return boolean
  * true if the entity needs a new hash
  */
 public function doGenerateHash(AbstractData $data, Entity $entity, $passwordField, $password, $newSalt)
 {
     $doGenerateHash = true;
     $id = $entity->get('id');
     if ($id !== null) {
         $oldEntity = $data->get($entity->get('id'));
         $doGenerateHash = $oldEntity->get($passwordField) !== $password || $newSalt;
     }
     return $doGenerateHash;
 }
Exemple #7
0
 public function testGetRaw()
 {
     $definition = $this->crudServiceProvider->getData('book')->getDefinition();
     $entity = new Entity($definition);
     $entity->set('test', 'testdata');
     $read = $entity->getRaw('test');
     $expected = 'testdata';
     $this->assertSame($expected, $read);
     $read = $entity->getRaw('test2');
     $this->assertNull($read);
 }
Exemple #8
0
 /**
  * Constructor.
  *
  * @param Entity $entity
  * the entity to validate
  */
 public function __construct(Entity $entity)
 {
     $this->entity = $entity;
     $this->definition = $entity->getDefinition();
 }
 /**
  * Constructs a file system path for the given parameters for storing the
  * file of the file field.
  *
  * @param string $entityName
  * the entity name
  * @param Entity $entity
  * the entity
  * @param string $field
  * the file field in the entity
  *
  * @return string
  * the constructed path for storing the file of the file field
  */
 protected function getPath($entityName, Entity $entity, $field)
 {
     return $this->basePath . $entity->getDefinition()->getField($field, 'path') . '/' . $entityName . '/' . $entity->get('id') . '/' . $field;
 }
Exemple #10
0
 /**
  * First, deletes all to the given entity related many-to-many entries from the DB
  * and then writes them again.
  *
  * @param Entity $entity
  * the entity to save the many-to-many entries of
  */
 protected function saveMany(Entity $entity)
 {
     $manyFields = $this->getManyFields();
     $id = $entity->get('id');
     foreach ($manyFields as $manyField) {
         $thisField = $this->definition->getSubTypeField($manyField, 'many', 'thisField');
         $thatField = $this->definition->getSubTypeField($manyField, 'many', 'thatField');
         $this->database->delete($manyField, [$thisField => $id]);
         $manyValues = $entity->get($manyField) ?: [];
         foreach ($manyValues as $thatId) {
             $this->database->insert($manyField, [$thisField => $id, $thatField => $thatId['id']]);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function renderFile(Entity $entity, $entityName, $field)
 {
     $fileName = $entity->get($field);
     $mimeTypes = new MimeTypes();
     $mimeType = $mimeTypes->getMimeTypeByExtension($fileName);
     $key = $this->getKey($entity, $entityName, $field) . '/' . $fileName;
     $result = $this->client->getObject(['Bucket' => $this->bucket, 'Key' => $key]);
     $result['Body']->rewind();
     $response = new StreamedResponse(function () use($result) {
         while ($data = $result['Body']->read(1024)) {
             echo $data;
             flush();
         }
     }, 200, ['Cache-Control' => 'public, max-age=86400', 'Content-length' => $result['ContentLength'], 'Content-Type' => $mimeType, 'Content-Disposition' => 'attachment; filename="' . $fileName . '"']);
     $response->send();
     return $response;
 }
Exemple #12
0
 public function testList()
 {
     $library = $this->dataLibrary->createEmpty();
     $library->set('name', 'nameA');
     $this->dataLibrary->create($library);
     $book = $this->dataBook->createEmpty();
     $book->set('title', 'title');
     $book->set('author', 'author');
     $book->set('pages', 111);
     $book->set('library', $library->get('id'));
     $this->dataBook->create($book);
     $library->set('libraryBook', [['id' => $book->get('id')]]);
     $this->dataLibrary->update($library);
     $entity = new Entity($this->dataBook->getDefinition());
     $entity->set('name', 'nameB');
     $this->dataLibrary->create($entity);
     $list = $this->dataLibrary->listEntries(['name' => 'nameA'], ['name' => '=']);
     $read = $list[0]->get('libraryBook');
     $expected = [['id' => $book->get('id'), 'name' => $book->get('title')]];
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['libraryBook' => [['id' => $book->get('id')]]], ['libraryBook' => '=']);
     $read = count($list);
     $expected = 1;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries();
     $read = count($list);
     $expected = 2;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['name' => 'nameB'], ['name' => '=']);
     $read = count($list);
     $expected = 1;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['name' => 'nameB', 'id' => 2], ['name' => '=', 'id' => '=']);
     $read = count($list);
     $expected = 1;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['type' => null], ['type' => '=']);
     $read = count($list);
     $expected = 2;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['name' => '%eB%'], ['name' => 'LIKE']);
     $read = count($list);
     $expected = 1;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], null, null, 'name');
     $expected = 'nameB';
     $this->assertSame($expected, $list[0]->get('name'));
     $expected = 'nameA';
     $this->assertSame($expected, $list[1]->get('name'));
     // Sorting by many fields should fall back to the initial sort field
     $list = $this->dataLibrary->listEntries([], [], null, null, 'libraryBook');
     $expected = 'nameB';
     $this->assertSame($expected, $list[0]->get('name'));
     $expected = 'nameA';
     $this->assertSame($expected, $list[1]->get('name'));
     for ($i = 0; $i < 15; ++$i) {
         $entity->set('name', 'name' . $i);
         $this->dataLibrary->create($entity);
     }
     $list = $this->dataLibrary->listEntries([], [], null, null);
     $read = count($list);
     $expected = 17;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], null, 5);
     $read = count($list);
     $expected = 5;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], 0, 5);
     $read = count($list);
     $expected = 5;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], 15, 5);
     $read = count($list);
     $expected = 2;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], 5, null);
     $read = count($list);
     $expected = 12;
     $this->assertSame($expected, $read);
 }