public static byte[] getSecurityToken(Entity entity) { SecurityState state = getSecurityState(entity); return state != null ? getSecurityToken(state) : null; }
public static String[] getFilteredAttributes(BaseGenericIdEntity entity) { SecurityState state = getSecurityState(entity); return state != null ? getFilteredAttributes(state) : null; }
private void addInaccessibleAttribute(Entity entity, String property) { SecurityState securityState = getOrCreateSecurityState(entity); String[] attributes = getInaccessibleAttributes(securityState); attributes = attributes == null ? new String[1] : Arrays.copyOf(attributes, attributes.length + 1); attributes[attributes.length - 1] = property; setInaccessibleAttributes(securityState, attributes); }
public static String[] getInaccessibleAttributes(Entity entity) { SecurityState state = getSecurityState(entity); return state != null ? getInaccessibleAttributes(state) : null; }
BaseGenericIdEntity<?> sourceGenericEntity = (BaseGenericIdEntity<?>) source; BaseEntityInternalAccess.setDetached(destGenericEntity, BaseEntityInternalAccess.isDetached(sourceGenericEntity)); BaseEntityInternalAccess.setNew(destGenericEntity, BaseEntityInternalAccess.isNew(sourceGenericEntity)); destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());
SecurityState securityState = getSecurityState(entity); if (getSecurityToken(entity) == null) { return; BaseEntityInternalAccess.setFilteredData(securityState, filteredData); Cipher cipher = getCipher(Cipher.DECRYPT_MODE); try { byte[] decrypted = cipher.doFinal(getSecurityToken(securityState)); String json = new String(decrypted, StandardCharsets.UTF_8); JSONObject jsonObject = new JSONObject(json); BaseEntityInternalAccess.setReadonlyAttributes(securityState, parseJsonArrayAsStrings( jsonObject.getJSONArray(READ_ONLY_ATTRIBUTES_KEY))); BaseEntityInternalAccess.setHiddenAttributes(securityState, parseJsonArrayAsStrings( jsonObject.getJSONArray(HIDDEN_ATTRIBUTES_KEY))); BaseEntityInternalAccess.setRequiredAttributes(securityState, parseJsonArrayAsStrings( jsonObject.getJSONArray(REQUIRED_ATTRIBUTES_KEY)));
protected Entity readEmbeddedEntity(JsonObject jsonObject, MetaProperty metaProperty) { MetaClass metaClass = metaProperty.getRange().asClass(); Entity entity = metadata.create(metaClass); clearFields(entity); readFields(jsonObject, entity); if (globalConfig.getRestRequiresSecurityToken() && entity instanceof EmbeddableEntity) { JsonPrimitive securityTokenJonPrimitive = jsonObject.getAsJsonPrimitive("__securityToken"); if (securityTokenJonPrimitive != null) { byte[] securityToken = Base64.getDecoder().decode(securityTokenJonPrimitive.getAsString()); setSecurityToken(getOrCreateSecurityState(entity), securityToken); } } return entity; }
@Override public String toString() { String state = ""; if (isNew(this)) state += "new,"; if (isManaged(this)) state += "managed,"; if (isDetached(this)) state += "detached,"; if (isRemoved(this)) state += "removed,"; if (state.length() > 0) state = state.substring(0, state.length() - 1); return getClass().getName() + "-" + getId() + " [" + state + "]"; } }
@Override public void afterCompletion(int status) { try { Collection<Entity> instances = container.getAllInstances(); if (log.isTraceEnabled()) log.trace("ContainerResourceSynchronization.afterCompletion: instances = " + instances); for (Object instance : instances) { if (instance instanceof BaseGenericIdEntity) { if (status == TransactionSynchronization.STATUS_COMMITTED) { if (BaseEntityInternalAccess.isNew((BaseGenericIdEntity) instance)) { // new instances become not new and detached only if the transaction was committed BaseEntityInternalAccess.setNew((BaseGenericIdEntity) instance, false); } } else { // commit failed or the transaction was rolled back makeDetached(instance); for (Entity entity : container.getNewDetachedInstances()) { BaseEntityInternalAccess.setNew((BaseGenericIdEntity) entity, true); BaseEntityInternalAccess.setDetached((BaseGenericIdEntity) entity, false); } } } } for (AfterCompleteTransactionListener listener : afterCompleteTxListeners) { listener.afterComplete(status == TransactionSynchronization.STATUS_COMMITTED, instances); } } finally { super.afterCompletion(status); } }
protected void makeDetached(Object instance) { if (instance instanceof BaseGenericIdEntity) { BaseEntityInternalAccess.setNew((BaseGenericIdEntity) instance, false); BaseEntityInternalAccess.setManaged((BaseGenericIdEntity) instance, false); BaseEntityInternalAccess.setDetached((BaseGenericIdEntity) instance, true); } if (instance instanceof FetchGroupTracker) { ((FetchGroupTracker) instance)._persistence_setSession(null); } if (instance instanceof ChangeTracker) { ((ChangeTracker) instance)._persistence_setPropertyChangeListener(null); } }
SecurityState securityState = getOrCreateSecurityState(entity); if (securityState != null) { JSONObject jsonObject = new JSONObject(); Multimap<String, Object> filtered = getFilteredData(securityState); if (filtered != null) { Set<Map.Entry<String, Collection<Object>>> entries = filtered.asMap().entrySet(); filteredAttributes[i++] = entry.getKey(); setFilteredAttributes(securityState, filteredAttributes); throw new RuntimeException("An error occurred while generating security token", e); setSecurityToken(securityState, encrypted);
protected void mergeSystemState(Entity srcEntity, Entity dstEntity) { if (dstEntity instanceof BaseGenericIdEntity) { BaseEntityInternalAccess.copySystemState((BaseGenericIdEntity) srcEntity, (BaseGenericIdEntity) dstEntity); if (srcEntity instanceof FetchGroupTracker && dstEntity instanceof FetchGroupTracker) { FetchGroup srcFetchGroup = ((FetchGroupTracker) srcEntity)._persistence_getFetchGroup(); FetchGroup dstFetchGroup = ((FetchGroupTracker) dstEntity)._persistence_getFetchGroup(); if (srcFetchGroup == null || dstFetchGroup == null) { ((FetchGroupTracker) dstEntity)._persistence_setFetchGroup(null); } else { ((FetchGroupTracker) dstEntity)._persistence_setFetchGroup(FetchGroupUtils.mergeFetchGroups(srcFetchGroup, dstFetchGroup)); } } } else if (dstEntity instanceof AbstractNotPersistentEntity) { BaseEntityInternalAccess.setNew((AbstractNotPersistentEntity) dstEntity, BaseEntityInternalAccess.isNew((AbstractNotPersistentEntity) srcEntity)); } }
/** * Makes a newly constructed object detached. The detached object can be passed to {@code DataManager.commit()} or * to {@code EntityManager.merge()} to save its state to the database. * <p>If an object with such ID does not exist in the database, a new object will be inserted. * <p>If the entity is {@code Versioned}, the version attribute should be equal to the latest version existing in * the database, or null for a new object. * * @param entity entity in the New state * @throws IllegalStateException if the entity is Managed * @see #isDetached(Object) * @see #makePatch(BaseGenericIdEntity) */ public void makeDetached(BaseGenericIdEntity entity) { checkNotNullArgument(entity, "entity is null"); if (BaseEntityInternalAccess.isManaged(entity)) throw new IllegalStateException("entity is managed"); BaseEntityInternalAccess.setNew(entity, false); BaseEntityInternalAccess.setDetached(entity, true); }
/** * Determines whether the instance is <em>New</em>, i.e. just created and not stored in database yet. * * @param entity entity instance * @return - true if the instance is a new persistent entity, or if it is actually in Managed state * but newly-persisted in this transaction <br> * - true if the instance is a new non-persistent entity never returned from DataManager <br> * - false otherwise * @throws IllegalArgumentException if entity instance is null */ public boolean isNew(Object entity) { checkNotNullArgument(entity, "entity is null"); if (entity instanceof BaseGenericIdEntity) { return BaseEntityInternalAccess.isNew((BaseGenericIdEntity) entity); } else if (entity instanceof AbstractNotPersistentEntity) { return BaseEntityInternalAccess.isNew((AbstractNotPersistentEntity) entity); } else { if (log.isTraceEnabled()) { log.trace("EntityStates.isNew is called for unsupported type '{}'. Stacktrace:\n{}", entity.getClass().getSimpleName(), StackTrace.asString()); } } return false; }
public static Entity copyCompositions(Entity source) { Preconditions.checkNotNullArgument(source, "source is null"); Entity dest; try { dest = source.getClass().newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } copyCompositions(source, dest); if (BaseEntityInternalAccess.supportsSecurityState(source)) { BaseEntityInternalAccess.setSecurityState(dest, BaseEntityInternalAccess.getSecurityState(source)); } return dest; }
/** * INTERNAL. */ public void addFiltered(BaseGenericIdEntity<?> entity, String property, Collection ids) { SecurityState securityState = getOrCreateSecurityState(entity); if (getFilteredData(securityState) == null) { setFilteredData(securityState, ArrayListMultimap.create()); } getFilteredData(securityState).putAll(property, ids); }
persistenceSecurity.assertTokenForREST(srcEntity, regularView); persistenceSecurity.restoreSecurityState(srcEntity); srcSecurityState = BaseEntityInternalAccess.getSecurityState(srcEntity); dstSecurityState = BaseEntityInternalAccess.getSecurityState(dstEntity); String propertyName = importViewProperty.getName(); MetaProperty metaProperty = metaClass.getPropertyNN(propertyName); if (BaseEntityInternalAccess.isHiddenOrReadOnly(dstSecurityState, propertyName)) { continue; if (BaseEntityInternalAccess.isRequired(dstSecurityState, propertyName) && srcEntity.getValue(propertyName) == null) { throw new CustomValidationException(format("Attribute [%s] is required for entity %s", propertyName, srcEntity));
byte[] securityToken = BaseEntityInternalAccess.getSecurityToken((BaseGenericIdEntity) entity); instanceEl.addElement("__securityToken").setText(Base64.getEncoder().encodeToString(securityToken)); String[] filteredAttributes = BaseEntityInternalAccess.getFilteredAttributes(baseGenericIdEntity);
public static Multimap<String, Object> getFilteredData(Entity entity) { SecurityState state = getSecurityState(entity); return state != null ? getFilteredData(state) : null; }
private void writeObject(java.io.ObjectOutputStream out) throws IOException { if (isManaged(this)) { setManaged(this, false); setDetached(this, true); } out.defaultWriteObject(); }