/**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     if (empty($options['data_class'])) {
         return;
     }
     $className = $options['data_class'];
     if (!$this->doctrineHelper->isManageableEntity($className)) {
         return;
     }
     if (!$this->entityConfigProvider->hasConfig($className)) {
         return;
     }
     $uniqueKeys = $this->entityConfigProvider->getConfig($className)->get('unique_key');
     if (empty($uniqueKeys)) {
         return;
     }
     /* @var \Symfony\Component\Validator\Mapping\ClassMetadata $validatorMetadata */
     $validatorMetadata = $this->validator->getMetadataFor($className);
     foreach ($uniqueKeys['keys'] as $uniqueKey) {
         $fields = $uniqueKey['key'];
         $labels = array_map(function ($fieldName) use($className) {
             $label = $this->entityConfigProvider->getConfig($className, $fieldName)->get('label');
             return $this->translator->trans($label);
         }, $fields);
         $constraint = new UniqueEntity(['fields' => $fields, 'errorPath' => '', 'message' => $this->translator->transChoice('oro.entity.validation.unique_field', sizeof($fields), ['%field%' => implode(', ', $labels)])]);
         $validatorMetadata->addConstraint($constraint);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var ConfigContext $context */
     $definition = $context->getResult();
     if (empty($definition)) {
         // an entity configuration does not exist
         return;
     }
     $entityClass = $context->getClassName();
     if (!isset($definition[ConfigUtil::LABEL])) {
         $entityName = $this->entityClassNameProvider->getEntityClassName($entityClass);
         if ($entityName) {
             $definition[ConfigUtil::LABEL] = $entityName;
         }
     }
     if (!isset($definition[ConfigUtil::PLURAL_LABEL])) {
         $entityPluralName = $this->entityClassNameProvider->getEntityClassPluralName($entityClass);
         if ($entityPluralName) {
             $definition[ConfigUtil::PLURAL_LABEL] = $entityPluralName;
         }
     }
     if (!isset($definition[ConfigUtil::DESCRIPTION]) && $this->entityConfigProvider->hasConfig($entityClass)) {
         $definition[ConfigUtil::DESCRIPTION] = new Label($this->entityConfigProvider->getConfig($entityClass)->get('description'));
     }
     $context->setResult($definition);
 }
 /**
  * Handle prePersist.
  *
  * @param LifecycleEventArgs $args
  * @throws \LogicException when getOwner method isn't implemented for entity with ownership type
  */
 public function prePersist(LifecycleEventArgs $args)
 {
     $token = $this->getSecurityContext()->getToken();
     if (!$token) {
         return;
     }
     $user = $token->getUser();
     if (!$user) {
         return;
     }
     $entity = $args->getEntity();
     $className = ClassUtils::getClass($entity);
     if ($this->configProvider->hasConfig($className)) {
         $accessor = PropertyAccess::createPropertyAccessor();
         $config = $this->configProvider->getConfig($className);
         $ownerType = $config->get('owner_type');
         $ownerFieldName = $config->get('owner_field_name');
         // set default owner for organization and user owning entities
         if ($ownerType && in_array($ownerType, [OwnershipType::OWNER_TYPE_ORGANIZATION, OwnershipType::OWNER_TYPE_USER]) && !$accessor->getValue($entity, $ownerFieldName)) {
             $owner = null;
             if (OwnershipType::OWNER_TYPE_USER == $ownerType) {
                 $owner = $user;
             } elseif (OwnershipType::OWNER_TYPE_ORGANIZATION == $ownerType && $token instanceof OrganizationContextTokenInterface) {
                 $owner = $token->getOrganizationContext();
             }
             $accessor->setValue($entity, $ownerFieldName, $owner);
         }
         //set organization
         $this->setDefaultOrganization($token, $config, $entity);
     }
 }
 /**
  * Handle prePersist.
  *
  * @param LifecycleEventArgs $args
  * @throws \LogicException when getOwner method isn't implemented for entity with ownership type
  */
 public function prePersist(LifecycleEventArgs $args)
 {
     $token = $this->getSecurityContext()->getToken();
     if (!$token) {
         return;
     }
     $user = $token->getUser();
     if (!$user) {
         return;
     }
     $entity = $args->getEntity();
     if ($this->configProvider->hasConfig(get_class($entity))) {
         $config = $this->configProvider->getConfig(get_class($entity));
         $ownerType = $config->get('owner_type');
         if ($ownerType && $ownerType !== OwnershipType::OWNER_TYPE_NONE) {
             if (!method_exists($entity, 'getOwner')) {
                 throw new \LogicException(sprintf('Method getOwner must be implemented for %s entity', get_class($entity)));
             }
             if (!$entity->getOwner()) {
                 /**
                  * Automatically set current user as record owner
                  */
                 if (OwnershipType::OWNER_TYPE_USER == $ownerType && method_exists($entity, 'setOwner')) {
                     $entity->setOwner($user);
                 }
             }
         }
     }
 }
 /**
  * Checks if the entity can have comments
  *
  * @param object|null $entity
  *
  * @return bool
  */
 public function isApplicable($entity)
 {
     if (!is_object($entity) || !$this->doctrineHelper->isManageableEntity($entity) || !$this->securityFacade->isGranted('oro_comment_view')) {
         return false;
     }
     $className = ClassUtils::getClass($entity);
     return $this->commentConfigProvider->hasConfig($className) && $this->commentConfigProvider->getConfig($className)->is('enabled') && $this->entityConfigProvider->hasConfig(Comment::ENTITY_NAME, ExtendHelper::buildAssociationName($className));
 }
Exemple #6
0
 /**
  * @param mixed $entity
  * @param bool  $show
  *
  * @return bool
  */
 public function isEntityAuditable($entity, $show)
 {
     if ($show || !is_object($entity)) {
         return $show;
     }
     $className = ClassUtils::getClass($entity);
     return $this->configProvider->hasConfig($className) && $this->configProvider->getConfig($className)->is('auditable');
 }
 /**
  * Checks if the entity can has notes
  *
  * @param object $entity
  * @return bool
  */
 public function isAttachmentAssociationEnabled($entity)
 {
     if (null === $entity || !is_object($entity)) {
         return false;
     }
     $className = ClassUtils::getClass($entity);
     return $this->attachmentConfigProvider->hasConfig($className) && $this->attachmentConfigProvider->getConfig($className)->is('enabled') && $this->entityConfigProvider->hasConfig(AttachmentScope::ATTACHMENT, ExtendHelper::buildAssociationName($className));
 }
 /**
  * Gets translated field name by its name
  *
  * @param string $className
  * @param string $fieldName
  *
  * @return string
  */
 protected function getFieldLabel($className, $fieldName)
 {
     if (!$this->entityConfigProvider->hasConfig($className, $fieldName)) {
         return $fieldName;
     }
     $fieldLabel = $this->entityConfigProvider->getConfig($className, $fieldName)->get('label');
     return $this->translator->trans($fieldLabel);
 }
 /**
  * Checks if the entity can be shared
  *
  * @param object $entity
  * @return bool
  */
 public function isShareEnabled($entity)
 {
     if (null === $entity || !is_object($entity)) {
         return false;
     }
     $className = ClassUtils::getClass($entity);
     return $this->securityFacade->isGranted('SHARE', $entity) && $this->configProvider->hasConfig($className) && $this->configProvider->getConfig($className)->get('share_scopes');
 }
 /**
  * Checks if the entity can has notes
  *
  * @param object $entity
  * @return bool
  */
 public function isNoteAssociationEnabled($entity)
 {
     if (null === $entity || !is_object($entity)) {
         return false;
     }
     $className = ClassUtils::getClass($entity);
     return $this->noteConfigProvider->hasConfig($className) && $this->noteConfigProvider->getConfig($className)->is('enabled') && $this->entityConfigProvider->hasConfig(Note::ENTITY_NAME, ExtendHelper::buildAssociationName($className));
 }
 /**
  * {@inheritdoc}
  */
 public function supports(array $schema)
 {
     if (!$this->groupingConfigProvider->hasConfig($schema['class'])) {
         return false;
     }
     $groups = $this->groupingConfigProvider->getConfig($schema['class'])->get('groups');
     return !empty($groups) && in_array(ActivityScope::GROUP_ACTIVITY, $groups);
 }
Exemple #12
0
 /**
  * Indicates whether the given entity type can be associated with the given activity or not
  *
  * @param string $entityClass
  * @param string $activityEntityClass
  *
  * @return bool
  */
 public function hasActivityAssociation($entityClass, $activityEntityClass)
 {
     if (!$this->activityConfigProvider->hasConfig($entityClass)) {
         return false;
     }
     $activityClassNames = $this->activityConfigProvider->getConfig($entityClass)->get('activities');
     return !empty($activityClassNames) && in_array($activityEntityClass, $activityClassNames);
 }
 /**
  * @param $entity
  * @return string
  */
 public function getOwnerType($entity)
 {
     $ownerClassName = ClassUtils::getRealClass(get_class($entity));
     if (!$this->configProvider->hasConfig($ownerClassName)) {
         return;
     }
     $config = $this->configProvider->getConfig($ownerClassName)->all();
     return $config['owner_type'];
 }
 /**
  * @param object $entity
  * @return string
  */
 public function getOwnerType($entity)
 {
     $ownerClassName = ClassUtils::getRealClass($entity);
     if (!$this->configProvider->hasConfig($ownerClassName)) {
         return null;
     }
     $config = $this->configProvider->getConfig($ownerClassName);
     return $config->get('owner_type');
 }
 /**
  * @param ResultRecordInterface $record
  * @return array
  */
 public function getWorkflowDefinitionPermissions(ResultRecordInterface $record)
 {
     $isActiveWorkflow = false;
     $relatedEntity = $record->getValue('entityClass');
     if ($this->configProvider->hasConfig($relatedEntity)) {
         $config = $this->configProvider->getConfig($relatedEntity);
         $isActiveWorkflow = $record->getValue('name') == $config->get('active_workflow');
     }
     $isSystem = $record->getValue('system');
     return array('activate' => !$isActiveWorkflow, 'clone' => true, 'deactivate' => $isActiveWorkflow, 'delete' => !$isSystem, 'update' => !$isSystem, 'view' => true);
 }
 /**
  * @param string $entityName
  * @return string|null
  */
 protected function getEntityGridName($entityName)
 {
     $gridName = null;
     if ($this->configProvider->hasConfig($entityName)) {
         $config = $this->configProvider->getConfig($entityName);
         $gridName = $config->get('grid_name');
     }
     if (!$gridName) {
         throw new \RuntimeException(sprintf('Grid not found for entity "%s"', $entityName));
     }
     return $gridName;
 }
 /**
  * @param FormInterface $field
  * @param FormView $view
  */
 protected function updateTooltip(FormInterface $field, FormView $view)
 {
     $parentOptions = $field->getParent()->getConfig()->getOptions();
     $parentClassName = isset($parentOptions['data_class']) ? $parentOptions['data_class'] : null;
     if (!isset($view->vars['tooltip']) && $parentClassName && $this->entityConfigProvider->hasConfig($parentClassName, $field->getName())) {
         $tooltip = $this->entityConfigProvider->getConfig($parentClassName, $field->getName())->get('description');
         //@deprecated 1.9.0:1.11.0 tooltips.*.yml will be removed. Use Resources/translations/messages.*.yml instead
         if ($this->translator->hasTrans($tooltip, self::DEFAULT_TRANSLATE_DOMAIN) || $this->translator->hasTrans($tooltip, self::TOOLTIPS_TRANSLATE_DOMAIN)) {
             $view->vars['tooltip'] = $tooltip;
         }
     }
 }
 public function testConfig()
 {
     $this->assertEquals($this->configManager, $this->configProvider->getConfigManager());
     $this->assertEquals(true, $this->configProvider->hasConfig(DemoEntity::ENTITY_NAME));
     $this->assertEquals($this->entityConfig, $this->configProvider->getConfig(DemoEntity::ENTITY_NAME));
     $this->assertEquals('testScope', $this->configProvider->getScope());
     $entityConfigId = new EntityConfigId('testScope', DemoEntity::ENTITY_NAME);
     $fieldConfigId = new FieldConfigId('testScope', DemoEntity::ENTITY_NAME, 'testField', 'string');
     $this->assertEquals($entityConfigId, $this->configProvider->getId(DemoEntity::ENTITY_NAME));
     $this->assertEquals($fieldConfigId, $this->configProvider->getId(DemoEntity::ENTITY_NAME, 'testField', 'string'));
     $entityConfigIdWithOtherScope = new EntityConfigId('otherScope', DemoEntity::ENTITY_NAME);
     $this->assertEquals($this->entityConfig, $this->configProvider->getConfigById($entityConfigIdWithOtherScope));
 }
Exemple #19
0
 /**
  * @param mixed  $entity
  * @param string $entityClass
  * @param bool   $show
  * @return bool
  */
 public function isEntityAuditable($entity, $entityClass, $show)
 {
     if (!is_object($entity) || $show) {
         return $show;
     }
     $classEmpty = empty($entityClass);
     if ($classEmpty && $entity instanceof EntityConfigModel) {
         $className = $entity->getClassName();
     } elseif ($classEmpty) {
         $className = ClassUtils::getClass($entity);
     } else {
         $className = str_replace('_', '\\', $entityClass);
     }
     return $this->configProvider->hasConfig($className) && $this->configProvider->getConfig($className)->is('auditable');
 }
Exemple #20
0
 /**
  * @param string $activityClassName
  * @param string $activityAssociationName
  *
  * @return bool
  */
 protected function isActivityAssociationEnabled($activityClassName, $activityAssociationName)
 {
     if (!$this->extendConfigProvider->hasConfig($activityClassName, $activityAssociationName)) {
         return false;
     }
     return ExtendHelper::isFieldAccessible($this->extendConfigProvider->getConfig($activityClassName, $activityAssociationName));
 }
 /**
  * Check if given class field is deleted
  *
  * @param string $className FQCN
  * @param string $fieldName
  *
  * @return bool
  */
 protected function isDeletedField($className, $fieldName)
 {
     if ($this->extendConfigProvider->hasConfig($className, $fieldName)) {
         return $this->extendConfigProvider->getConfig($className, $fieldName)->is('is_deleted');
     }
     return false;
 }
 /**
  * Loads metadata for the given security type and save them in cache
  *
  * @param $securityType
  */
 protected function loadMetadata($securityType)
 {
     $data = array();
     $securityConfigs = $this->securityConfigProvider->getConfigs();
     foreach ($securityConfigs as $securityConfig) {
         $className = $securityConfig->getId()->getClassName();
         if ($securityConfig->get('type') === $securityType && $this->extendConfigProvider->getConfig($className)->in('state', [ExtendScope::STATE_ACTIVE, ExtendScope::STATE_UPDATE])) {
             $label = '';
             if ($this->entityConfigProvider->hasConfig($className)) {
                 $label = $this->entityConfigProvider->getConfig($className)->get('label');
             }
             $permissions = $securityConfig->get('permissions');
             if (!$permissions || $permissions == 'All') {
                 $permissions = array();
             } else {
                 $permissions = explode(';', $permissions);
             }
             $data[$className] = new EntitySecurityMetadata($securityType, $className, $securityConfig->get('group_name'), $label, $permissions);
         }
     }
     if ($this->cache) {
         $this->cache->save($securityType, $data);
     }
     $this->localCache[$securityType] = $data;
 }
 /**
  * @param object $entity
  * @param int    $pageType
  * @return bool
  */
 protected function isAllowedOnPage($entity, $pageType)
 {
     if (!$this->configProvider->hasConfig($entity)) {
         return false;
     }
     $config = $this->configProvider->getConfig($entity);
     if (!$config->has(ActivityScope::SHOW_ON_PAGE)) {
         return false;
     }
     $configValue = $config->get(ActivityScope::SHOW_ON_PAGE);
     if (!defined($configValue)) {
         throw new \InvalidArgumentException(sprintf('Constant %s is not defined', $configValue));
     }
     $configValue = constant($configValue);
     return $configValue !== ActivityScope::NONE_PAGE && ($configValue & $pageType) === $pageType;
 }
 /**
  * Gets a relation type
  *
  * @param string $className
  * @param string $fieldName
  * @return string
  */
 protected function getRelationType($className, $fieldName)
 {
     if ($this->entityConfigProvider->hasConfig($className, $fieldName)) {
         return $this->entityConfigProvider->getConfig($className, $fieldName)->getId()->getFieldType();
     }
     return '';
 }
Exemple #25
0
 /**
  * @param object $object
  * @param bool   $withMultiValue
  *
  * @return array
  */
 protected function getApplicableRelations($object, $withMultiValue = false)
 {
     $result = [];
     $className = ClassUtils::getClass($object);
     if (!$this->extendConfigProvider->hasConfig($className)) {
         return $result;
     }
     $extendConfig = $this->extendConfigProvider->getConfig($className);
     $relations = $extendConfig->get('relation');
     if (empty($relations)) {
         return $result;
     }
     $targetEntities = $this->getTargetEntities();
     foreach ($relations as $relation) {
         if (empty($relation['owner'])) {
             continue;
         }
         /** @var FieldConfigId $fieldId */
         $fieldId = $relation['field_id'];
         $isApplicableRelationType = $fieldId->getFieldType() === 'manyToOne' || $withMultiValue && $fieldId->getFieldType() === 'manyToMany';
         if (!$isApplicableRelationType) {
             continue;
         }
         $relatedEntityClass = $relation['target_entity'];
         if (!in_array($relatedEntityClass, $targetEntities)) {
             continue;
         }
         if (!isset($result[$relatedEntityClass])) {
             $result[$relatedEntityClass] = [];
         }
         $result[$relatedEntityClass][] = $fieldId->getFieldName();
     }
     return $result;
 }
 /**
  * Get ACL sql conditions and join statements to check shared records
  *
  * @param string $entityName
  * @param string $entityAlias
  * @param mixed $permissions
  *
  * @return array
  */
 public function getAclShareData($entityName, $entityAlias, $permissions = BasicPermissionMap::PERMISSION_VIEW)
 {
     if ($permissions !== BasicPermissionMap::PERMISSION_VIEW) {
         return null;
     }
     $aclClass = $this->getObjectManager()->getRepository('OroSecurityBundle:AclClass')->findOneBy(['classType' => $entityName]);
     if (!$aclClass) {
         return null;
     }
     $shareConfig = null;
     if ($this->configProvider->hasConfig($entityName)) {
         $shareConfig = $this->configProvider->getConfig($entityName)->get('share_scopes');
     }
     if (!$shareConfig) {
         return null;
     }
     $aclSIds = $this->getSecurityIdentityIds((array) $shareConfig);
     if (empty($aclSIds)) {
         return null;
     }
     $observer = new OneShotIsGrantedObserver();
     $this->aclVoter->addOneShotIsGrantedObserver($observer);
     $isGranted = $this->getSecurityContext()->isGranted($permissions, 'entity:' . $entityName);
     if (!$isGranted || !in_array($observer->getAccessLevel(), $this->shareAccessLevels)) {
         return null;
     }
     $shareCondition = ['existsSubselect' => ['select' => 1, 'from' => ['schemaName' => self::ACL_ENTRIES_SCHEMA_NAME, 'alias' => self::ACL_ENTRIES_ALIAS], 'where' => $this->getShareSubselectWhereConditions($entityAlias, $aclSIds, $aclClass)], 'not' => false];
     //Add query components for OutputSqlWalker
     $queryComponents[self::ACL_ENTRIES_ALIAS] = ['metadata' => $this->getObjectManager()->getClassMetadata(self::ACL_ENTRIES_SCHEMA_NAME), 'parent' => null, 'relation' => null, 'map' => null, 'nestingLevel' => null, 'token' => null];
     return [$shareCondition, $queryComponents];
 }
 /**
  * @param string $entityClass
  *
  * @return array
  */
 protected function getEntityVariableGetters($entityClass)
 {
     $entityClass = ClassUtils::getRealClass($entityClass);
     if (!$this->emailConfigProvider->hasConfig($entityClass)) {
         return [];
     }
     $result = [];
     $reflClass = new \ReflectionClass($entityClass);
     $fieldConfigs = $this->emailConfigProvider->getConfigs($entityClass);
     foreach ($fieldConfigs as $fieldConfig) {
         if (!$fieldConfig->is('available_in_template')) {
             continue;
         }
         /** @var FieldConfigId $fieldId */
         $fieldId = $fieldConfig->getId();
         list($varName, $getter) = $this->getFieldAccessInfo($reflClass, $fieldId->getFieldName());
         if (!$varName) {
             continue;
         }
         $resultGetter = $getter;
         $formatters = $this->formatterManager->guessFormatters($fieldId);
         if ($formatters && count($formatters)) {
             $resultGetter = array_merge(['property_path' => $getter], $formatters);
         }
         $result[$varName] = $resultGetter;
     }
     return $result;
 }
 /**
  * @param string $entity
  * @return bool
  */
 protected function isShowWorkflowStep($entity)
 {
     if ($this->configProvider->hasConfig($entity)) {
         return $this->configProvider->getConfig($entity)->is('show_step_in_grid');
     }
     return false;
 }
Exemple #29
0
 /**
  * Check organization. If user try to access entity what was created in organization this user do not have access -
  *  deny access
  *
  * @param int $result
  * @return int
  */
 protected function checkOrganizationContext($result)
 {
     $object = $this->object;
     $token = $this->securityToken;
     if ($token instanceof OrganizationContextTokenInterface && $result === self::ACCESS_GRANTED && $this->extension instanceof EntityAclExtension && is_object($object) && !$object instanceof ObjectIdentity) {
         $className = ClassUtils::getClass($object);
         if ($this->configProvider->hasConfig($className)) {
             $config = $this->configProvider->getConfig($className);
             $accessLevel = $this->extension->getAccessLevel($this->triggeredMask);
             // we need to check organization in case if Access level is not system,
             // or then access level and owner type of test object is User or Business Unit (in this owner types we
             // do not allow to use System access level)
             // (do not allow to manipulate records from another organization)
             if ($accessLevel < AccessLevel::SYSTEM_LEVEL || $accessLevel === AccessLevel::SYSTEM_LEVEL && in_array($config->get('owner_type'), ['USER', 'BUSINESS_UNIT'])) {
                 if ($config->has('organization_field_name')) {
                     $accessor = PropertyAccess::createPropertyAccessor();
                     /** @var Organization $objectOrganization */
                     $objectOrganization = $accessor->getValue($object, $config->get('organization_field_name'));
                     if ($objectOrganization && $objectOrganization->getId() !== $token->getOrganizationContext()->getId()) {
                         $result = self::ACCESS_DENIED;
                     }
                 }
             }
         }
     }
     return $result;
 }
 /**
  * Makes sure metadata for dictionary entities were loaded
  */
 protected function ensureDictionariesInitialized()
 {
     if (null === $this->dictionaries) {
         $this->dictionaries = [];
         $configs = $this->groupingConfigProvider->getConfigs();
         foreach ($configs as $config) {
             $groups = $config->get('groups');
             if (!empty($groups) && in_array(GroupingScope::GROUP_DICTIONARY, $groups)) {
                 $className = $config->getId()->getClassName();
                 $metadata = $this->getManagerForClass($className)->getClassMetadata($className);
                 $fieldNames = null;
                 if ($this->dictionaryConfigProvider->hasConfig($className)) {
                     $fieldNames = $this->dictionaryConfigProvider->getConfig($className)->get('virtual_fields');
                 }
                 if (null === $fieldNames) {
                     $fieldNames = array_filter($metadata->getFieldNames(), function ($fieldName) use(&$metadata) {
                         return !$metadata->isIdentifier($fieldName);
                     });
                 }
                 $fields = [];
                 foreach ($fieldNames as $fieldName) {
                     $fields[$fieldName] = $metadata->getTypeOfField($fieldName);
                 }
                 $this->dictionaries[$className] = $fields;
             }
         }
     }
 }