Search in sources :

Example 61 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class AuthenticationManagerBean method substituteUser.

@Nonnull
@Override
public UserSession substituteUser(User substitutedUser) {
    UserSession currentSession = userSessionSource.getUserSession();
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user;
        if (currentSession.getUser().equals(substitutedUser)) {
            user = em.find(User.class, substitutedUser.getId());
            if (user == null) {
                throw new NoResultException("User not found");
            }
        } else {
            user = loadSubstitutedUser(substitutedUser, currentSession, em);
        }
        UserSession session = userSessionManager.createSession(currentSession, user);
        withSecurityContext(new SecurityContext(serverSession), () -> publishUserSubstitutedEvent(currentSession, session));
        tx.commit();
        userSessions.remove(currentSession);
        userSessionManager.clearPermissionsOnUser(session);
        userSessions.add(session);
        return session;
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) AppContext.withSecurityContext(com.haulmont.cuba.core.sys.AppContext.withSecurityContext) NoResultException(javax.persistence.NoResultException) Nonnull(javax.annotation.Nonnull)

Example 62 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class AuthenticationManagerBean method authenticate.

@Override
@Nonnull
public AuthenticationDetails authenticate(Credentials credentials) throws LoginException {
    checkNotNullArgument(credentials, "credentials should not be null");
    SecurityContext previousSecurityContext = AppContext.getSecurityContext();
    AppContext.setSecurityContext(new SecurityContext(serverSession));
    try (Transaction tx = persistence.createTransaction()) {
        AuthenticationDetails authenticationDetails = authenticateInternal(credentials);
        tx.commit();
        userSessionManager.clearPermissionsOnUser(authenticationDetails.getSession());
        return authenticationDetails;
    } finally {
        AppContext.setSecurityContext(previousSecurityContext);
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) AppContext.withSecurityContext(com.haulmont.cuba.core.sys.AppContext.withSecurityContext) Nonnull(javax.annotation.Nonnull)

Example 63 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class UserSessionManager method getPermissionValue.

public Integer getPermissionValue(User user, PermissionType permissionType, String target) {
    Integer result;
    List<RoleDefinition> roles = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        user = em.find(User.class, user.getId());
        for (RoleDefinition role : rolesHelper.getRoleDefinitionsForUser(user, false)) {
            if (role != null) {
                roles.add(role);
            }
        }
        RoleDefinition joinedRole = buildJoinedRoleDefinition(roles);
        result = PermissionsUtils.getResultingPermissionValue(joinedRole, permissionType, target, rolesHelper.getPermissionUndefinedAccessPolicy());
        tx.commit();
    } finally {
        tx.end();
    }
    return result;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 64 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class CascadeDeleteTest method testRemoveCascade.

@Test
public void testRemoveCascade() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        CascadeEntity loadedRoot = em.find(CascadeEntity.class, root.getId());
        em.find(CascadeEntity.class, first.getId());
        em.remove(loadedRoot);
        tx.commit();
    }
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        List r = em.createQuery("select e from test$CascadeEntity e where e.id in ?1").setParameter(1, Arrays.asList(root, first, second, third)).getResultList();
        assertEquals(0, r.size());
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) List(java.util.List) CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) Test(org.junit.jupiter.api.Test)

Example 65 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class AttributeAccessServiceBean method computeSecurityState.

@Override
public SecurityState computeSecurityState(Entity entity) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    SecurityState state;
    String storeName = metadataTools.getStoreName(metadata.getClassNN(entity.getClass()));
    Transaction tx = persistence.createTransaction(storeName);
    try {
        Entity e;
        if (entityStates.isNew(entity)) {
            e = entity;
        } else {
            EntityManager em = persistence.getEntityManager(storeName);
            try {
                e = em.merge(entity);
            } catch (OptimisticLockException exception) {
                e = em.find(entity.getClass(), entity.getId());
            }
        }
        support.setupAttributeAccess(e);
        state = BaseEntityInternalAccess.getSecurityState(e);
    // do not commit the transaction
    } finally {
        tx.end();
    }
    return state;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) OptimisticLockException(javax.persistence.OptimisticLockException) SecurityState(com.haulmont.cuba.core.entity.SecurityState)

Aggregations

Transaction (com.haulmont.cuba.core.Transaction)226 EntityManager (com.haulmont.cuba.core.EntityManager)142 Test (org.junit.jupiter.api.Test)59 Query (com.haulmont.cuba.core.Query)30 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)27 User (com.haulmont.cuba.security.entity.User)25 View (com.haulmont.cuba.core.global.View)22 BeforeEach (org.junit.jupiter.api.BeforeEach)18 TypedQuery (com.haulmont.cuba.core.TypedQuery)13 Group (com.haulmont.cuba.security.entity.Group)12 List (java.util.List)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 MetaClass (com.haulmont.chile.core.model.MetaClass)7 Entity (com.haulmont.cuba.core.entity.Entity)7 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)7 ChildCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ChildCachedEntity)5 ParentCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ParentCachedEntity)5 UUID (java.util.UUID)5 Nullable (javax.annotation.Nullable)5 EntityManagerFactory (javax.persistence.EntityManagerFactory)5