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;
}
}
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;
}
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();
}
}
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();
}
}
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();
}
}
Aggregations