Search in sources :

Example 26 with EntityManager

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

the class ServerTokenStoreImpl method getRefreshTokenByTokenValueFromDatabase.

@Nullable
protected RefreshToken getRefreshTokenByTokenValueFromDatabase(String refreshTokenValue) {
    RefreshToken refreshToken;
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        refreshToken = em.createQuery("select e from sys$RefreshToken e where e.tokenValue = :tokenValue", RefreshToken.class).setParameter("tokenValue", refreshTokenValue).setViewName(View.LOCAL).getFirstResult();
        tx.commit();
        return refreshToken;
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) RefreshToken(com.haulmont.cuba.core.entity.RefreshToken) Transaction(com.haulmont.cuba.core.Transaction) Nullable(javax.annotation.Nullable)

Example 27 with EntityManager

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

the class ServerTokenStoreImpl method getAccessTokenByAuthenticationKeyFromDatabase.

@Nullable
protected AccessToken getAccessTokenByAuthenticationKeyFromDatabase(String authenticationKey) {
    AccessToken accessToken;
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        accessToken = em.createQuery("select e from sys$AccessToken e where e.authenticationKey = :authenticationKey", AccessToken.class).setParameter("authenticationKey", authenticationKey).setViewName(View.LOCAL).getFirstResult();
        tx.commit();
        return accessToken;
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) AccessToken(com.haulmont.cuba.core.entity.AccessToken) Nullable(javax.annotation.Nullable)

Example 28 with EntityManager

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

the class ServerTokenStoreImpl method getAccessTokenByTokenValueFromDatabase.

@Nullable
protected AccessToken getAccessTokenByTokenValueFromDatabase(String accessTokenValue) {
    AccessToken accessToken;
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        accessToken = em.createQuery("select e from sys$AccessToken e where e.tokenValue = :tokenValue", AccessToken.class).setParameter("tokenValue", accessTokenValue).setViewName(View.LOCAL).getFirstResult();
        tx.commit();
        return accessToken;
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) AccessToken(com.haulmont.cuba.core.entity.AccessToken) Nullable(javax.annotation.Nullable)

Example 29 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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 30 with EntityManager

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

the class RememberMeAuthenticationProvider method loadRememberMeToken.

@Nullable
protected RememberMeToken loadRememberMeToken(User user, String rememberMeToken) {
    EntityManager em = persistence.getEntityManager();
    TypedQuery<RememberMeToken> query = em.createQuery("select rt from sec$RememberMeToken rt where rt.token = :token and rt.user.id = :userId", RememberMeToken.class);
    query.setParameter("token", rememberMeToken);
    query.setParameter("userId", user.getId());
    return query.getFirstResult();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) RememberMeToken(com.haulmont.cuba.security.entity.RememberMeToken) Nullable(javax.annotation.Nullable)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 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 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4