/** * 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; }
/** * 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; }
/** * Postprocesses the entity after modification by handling the uploaded * files and setting the flash. * * @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 string $mode * whether to 'edit' or to 'create' the entity * * @return null|\Symfony\Component\HttpFoundation\RedirectResponse * the HTTP response of this modification */ protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode) { $id = $instance->get('id'); $request = $app['request_stack']->getCurrentRequest(); $result = $mode == 'edit' ? $crudData->updateFiles($request, $instance, $entity) : $crudData->createFiles($request, $instance, $entity); if (!$result) { return null; } $app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.' . $mode . '.success', ['%label%' => $crudData->getDefinition()->getLabel(), '%id%' => $id])); return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id])); }
/** * 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; }
/** * Enriches an entity with metadata: * id, version, created_at, updated_at * * @param mixed $id * the id of the entity to enrich * @param Entity $entity * the entity to enrich */ protected function enrichEntityWithMetaData($id, Entity $entity) { $entity->set('id', $id); $createdEntity = $this->get($entity->get('id')); $entity->set('version', $createdEntity->get('version')); $entity->set('created_at', $createdEntity->get('created_at')); $entity->set('updated_at', $createdEntity->get('updated_at')); }
/** * 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; }
public function testGetSet() { $definitionLibrary = $this->crudServiceProvider->getData('library')->getDefinition(); $library = $this->crudServiceProvider->getData('library')->createEmpty(); $library->set('name', 'lib a'); $this->crudServiceProvider->getData('library')->create($library); $definition = $this->crudServiceProvider->getData('book')->getDefinition(); $entity = new Entity($definition); $entity->set('test', 'testdata'); $read = $entity->get('test'); $expected = 'testdata'; $this->assertSame($expected, $read); $entity->set('test', 'testdata2'); $read = $entity->get('test'); $expected = 'testdata2'; $this->assertSame($expected, $read); $read = $entity->get('testNull'); $this->assertNull($read); $entity->set('price', 3.99); $read = $entity->get('price'); $expected = 3.99; $this->assertSame($expected, $read); $entity->set('pages', 111); $read = $entity->get('pages'); $expected = 111; $this->assertSame($expected, $read); $entity->set('library', $library->get('id')); $read = $entity->get('library'); $expected = $library->get('id'); $this->assertSame($expected, $read); $entity = $this->crudServiceProvider->getData('book')->createEmpty(); $entity->set('title', 'title a'); $entity->set('author', 'author a'); $entity->set('pages', 1); $entity->set('library', $library->get('id')); $entity->set('cover', 'cover a'); $this->crudServiceProvider->getData('book')->create($entity); $library->set('libraryBook', [['id' => $entity->get('id')]]); $read = $library->get('libraryBook'); $expected = [['id' => $entity->get('id')]]; $this->assertSame($expected, $read); // Fixed values override $definition->setField('pages', 'value', 666); $entity->set('pages', 111); $read = $entity->get('pages'); $expected = 666; $this->assertSame($expected, $read); $entity = new Entity($definitionLibrary); $entity->set('isOpenOnSundays', true); $read = $entity->get('isOpenOnSundays'); $expected = true; $this->assertSame($expected, $read); $entity->set('opening', ''); $read = $entity->get('opening'); $expected = null; $this->assertSame($expected, $read); $entity->set('opening', '2016-09-12 01:02:03'); $read = $entity->get('opening'); $expected = '2016-09-12 01:02:03'; $this->assertSame($expected, $read); }
/** * 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; }
/** * 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; }