Search in sources :

Example 26 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 27 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<Role> roles = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        user = em.find(User.class, user.getId());
        for (UserRole userRole : user.getUserRoles()) {
            if (userRole.getRole() != null) {
                roles.add(userRole.getRole());
            }
        }
        UserSession session = new UserSession(uuidSource.createUuid(), user, roles, userSessionSource.getLocale(), false);
        compilePermissions(session, roles);
        result = session.getPermissionValue(permissionType, target);
        tx.commit();
    } finally {
        tx.end();
    }
    return result;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) UserSession(com.haulmont.cuba.security.global.UserSession) ArrayList(java.util.ArrayList)

Example 28 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").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.Test)

Example 29 with Transaction

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

the class SchedulingServiceBean method setActive.

@Override
public void setActive(Set<ScheduledTask> tasks, boolean active) {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        for (ScheduledTask task : tasks) {
            ScheduledTask t = em.find(ScheduledTask.class, task.getId());
            t.setActive(active);
        }
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Example 30 with Transaction

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

the class SchedulingServiceBean method setActive.

@Override
public void setActive(ScheduledTask task, boolean active) {
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        ScheduledTask t = em.find(ScheduledTask.class, task.getId());
        t.setActive(active);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Aggregations

Transaction (com.haulmont.cuba.core.Transaction)211 EntityManager (com.haulmont.cuba.core.EntityManager)138 Test (org.junit.Test)44 User (com.haulmont.cuba.security.entity.User)30 Query (com.haulmont.cuba.core.Query)26 View (com.haulmont.cuba.core.global.View)21 Before (org.junit.Before)15 Group (com.haulmont.cuba.security.entity.Group)13 TypedQuery (com.haulmont.cuba.core.TypedQuery)11 MetaClass (com.haulmont.chile.core.model.MetaClass)9 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 Entity (com.haulmont.cuba.core.entity.Entity)7 List (java.util.List)6 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 UUID (java.util.UUID)5 Nullable (javax.annotation.Nullable)5 AppFolder (com.haulmont.cuba.core.entity.AppFolder)4 IdentityEntity (com.haulmont.cuba.testmodel.primary_keys.IdentityEntity)4