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