Java Code Examples for com.haulmont.cuba.security.entity.User#setGroup()

The following examples show how to use com.haulmont.cuba.security.entity.User#setGroup() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistenceTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    QueryRunner runner = new QueryRunner(cont.persistence().getDataSource());
    runner.update("delete from SYS_SERVER");

    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("testLogin");
        user.setGroup(em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93")));
        em.persist(user);

        tx.commit();
    } finally {
        tx.end();
    }
}
 
Example 2
Source File: LookupPickerFieldDsTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsubscribeComponentListener() {
    LookupPickerField component = uiComponents.create(LookupPickerField.NAME);

    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    component.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());

    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setGroup(groups.get(0));

    component.setDatasource(userDs, "group");

    // unbind
    component.setDatasource(null, null);

    Consumer<HasValue.ValueChangeEvent> listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    component.addValueChangeListener(listener);

    user.setGroup(metadata.create(Group.class));
}
 
Example 3
Source File: LookupPickerFieldDsTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Test
public void testOptionsDsUnsubscribe() {
    LookupPickerField component = uiComponents.create(LookupPickerField.NAME);

    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    component.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());

    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setGroup(groups.get(0));

    component.setDatasource(userDs, "group");

    //unbind
    component.setOptionsDatasource(null);

    Datasource.ItemChangeListener<Group> listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    groupsDs.addItemChangeListener(listener);

    component.setValue(groups.get(1));
}
 
Example 4
Source File: EclipseLinkDetachedTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("testLogin");
        user.setPosition("manager");
        user.setGroup(em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93")));
        em.persist(user);

        UserRole userRole = new UserRole();
        userRoleId = userRole.getId();
        userRole.setUser(user);
        userRole.setRoleName("test-role");
        em.persist(userRole);

        tx.commit();
    } finally {
        tx.end();
    }
}
 
Example 5
Source File: EntityListenerImplicitFlushTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    User user = metadata.create(User.class);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();

        user.setGroup(em.getReference(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93")));
        user.setLogin("u-" + user.getId());
        em.persist(user);

        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.loginLowerCase = :login", User.class);
        query.setParameter("login", user.getLogin());
        query.setViewName(View.LOCAL); // setting a view leads to using FlushModeType.AUTO - see QueryImpl.getQuery()
        List<User> list = query.getResultList();
        assertEquals(1, list.size());
        assertEquals(user, list.get(0));

        tx.commit();
    }
}
 
Example 6
Source File: EntityManagerTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Test
public void testMerge() throws Exception {
    UUID newUserId = UUID.randomUUID();
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = new User();
        user.setId(newUserId);
        user.setName("testMerge");
        user.setLogin("testMerge");
        user.setPassword("testMerge");
        user.setGroup(em.getReference(Group.class, groupId));
        user = em.merge(user);
        User userFromPersistentContext = em.find(User.class, newUserId);
        assertEquals(user, userFromPersistentContext);
        tx.commit();
    } finally {
        tx.end();
        cont.deleteRecord("SEC_USER", newUserId);
    }
}
 
Example 7
Source File: BeforeCommitTransactionListenerTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Test
public void testChangeEntity() throws Exception {
    User u1 = metadata.create(User.class);
    u1.setLogin("TxLstnrTst-1-" + u1.getId());
    u1.setGroup(companyGroup);

    TestBeforeCommitTxListener.test = "testChangeEntity";
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(u1);
        tx.commit();
    } finally {
        TestBeforeCommitTxListener.test = null;
    }

    try (Transaction tx = persistence.createTransaction()) {
        User user = persistence.getEntityManager().find(User.class, u1.getId());
        assertEquals("set by tx listener", user.getName());
    }
}
 
Example 8
Source File: LookupFieldDsTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Test
public void testValueChangeListener() {
    LookupField lookupField = uiComponents.create(LookupField.class);

    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    lookupField.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());

    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setGroup(groups.get(0));

    boolean[] valueWasChanged = {false};
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    lookupField.addValueChangeListener(listener);

    lookupField.setDatasource(userDs, "group");
    assertTrue(valueWasChanged[0]);
}
 
Example 9
Source File: AfterCompleteTransactionListenerTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommit() throws Exception {
    User u = metadata.create(User.class);
    u.setLogin("TxLstnrTst-1-" + u.getId());
    u.setGroup(companyGroup);
    TestAfterCompleteTxListener.test = "testCommit";
    try {
        try (Transaction tx = persistence.createTransaction()) {
            persistence.getEntityManager().persist(u);
            tx.commit();
        }
    } finally {
        TestAfterCompleteTxListener.test = null;
    }

    try (Transaction tx = persistence.createTransaction()) {
        User user = persistence.getEntityManager().find(User.class, u.getId());
        assertEquals("updated by TestAfterCompleteTxListener", user.getName());
    }
}
 
Example 10
Source File: HsqlLikeNullFailTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    DataManager dataManager = AppBeans.get(DataManager.NAME);

    Group group = dataManager.load(new LoadContext<>(Group.class)
            .setId(UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93")));

    User user = new User();
    user.setGroup(group);
    user.setId(UUID.fromString("de0f39d2-e60a-11e1-9b55-3860770d7eaf"));
    user.setName("Test");
    user.setLogin("tEst");
    user.setLoginLowerCase("test");

    dataManager.commit(user);
}
 
Example 11
Source File: BeforeCommitTransactionListenerTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateEntity() throws Exception {
    User u = metadata.create(User.class);
    u.setLogin("u-" + u.getId());
    u.setGroup(companyGroup);

    TestBeforeCommitTxListener.test = "testCreateEntity";
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(u);
        tx.commit();
    } finally {
        TestBeforeCommitTxListener.test = null;
    }

    try (Transaction tx = persistence.createTransaction()) {
        User user = persistence.getEntityManager().find(User.class, TestBeforeCommitTxListener.createdEntityId);
        assertNotNull(user);
    }
}
 
Example 12
Source File: LookupFieldDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsubscribeSubscribeComponentListener() {
    LookupField lookupField = uiComponents.create(LookupField.class);

    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    lookupField.setOptionsDatasource(groupsDs);

    Datasource<User> userDs = getTestUserDatasource();
    Group group = groupsDs.getItems().iterator().next();
    User user = userDs.getItem();
    user.setGroup(group);
    lookupField.setDatasource(userDs, "group");

    // unbind
    lookupField.setDatasource(null, null);

    // setup
    boolean[] valueWasChanged = {false};
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;

    // datasource before listener
    lookupField.setDatasource(userDs, "group");
    lookupField.addValueChangeListener(listener);

    user.setGroup(metadata.create(Group.class));
    assertTrue(valueWasChanged[0]);

    // reset state

    valueWasChanged[0] = false;
    lookupField.setDatasource(null, null);
    lookupField.removeValueChangeListener(listener);
    lookupField.setValue(null);

    // listener before datasource

    lookupField.addValueChangeListener(listener);
    lookupField.setDatasource(userDs, "group");
    assertTrue(valueWasChanged[0]);
}
 
Example 13
Source File: LoadSubstitutionsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        Group group = em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"));

        user = new User();
        user.setLogin("user-" + user.getId());
        user.setGroup(group);

        em.persist(user);

        substitutedUser = new User();
        substitutedUser.setLogin("substitutedUser");
        substitutedUser.setGroup(group);

        em.persist(substitutedUser);

        userSubstitution = new UserSubstitution();
        userSubstitution.setUser(user);
        userSubstitution.setSubstitutedUser(substitutedUser);

        user.setSubstitutions(new ArrayList<>(ImmutableList.of(userSubstitution)));

        em.persist(userSubstitution);

        tx.commit();
    } finally {
        tx.end();
    }

    cont.setupLogging("com.haulmont.cuba.core.sys.FetchGroupManager", Level.TRACE);
}
 
Example 14
Source File: SearchFieldDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsubscribeSubscribeComponentListener() {
    SearchField searchField = uiComponents.create(SearchField.class);

    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    searchField.setOptionsDatasource(groupsDs);

    Datasource<User> userDs = getTestUserDatasource();
    Group group = groupsDs.getItems().iterator().next();
    User user = userDs.getItem();
    user.setGroup(group);
    searchField.setDatasource(userDs, "group");

    // unbind
    searchField.setDatasource(null, null);

    // setup
    boolean[] valueWasChanged = {false};
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;

    // datasource before listener
    searchField.setDatasource(userDs, "group");
    searchField.addValueChangeListener(listener);

    user.setGroup(metadata.create(Group.class));
    assertTrue(valueWasChanged[0]);

    // reset state

    valueWasChanged[0] = false;
    searchField.setDatasource(null, null);
    searchField.removeValueChangeListener(listener);
    searchField.setValue(null);

    // listener before datasource

    searchField.addValueChangeListener(listener);
    searchField.setDatasource(userDs, "group");
    assertTrue(valueWasChanged[0]);
}
 
Example 15
Source File: TestBeforeCommitTxListener.java    From cuba with Apache License 2.0 5 votes vote down vote up
private void createEntity(EntityManager entityManager) {
    Group companyGroup = entityManager.find(Group.class, TestSupport.COMPANY_GROUP_ID);
    User u = metadata.create(User.class);
    createdEntityId = u.getId();
    u.setLogin("TxLstnrTst-" + u.getId());
    u.setLoginLowerCase(u.getLogin().toLowerCase());
    u.setGroup(companyGroup);
    entityManager.persist(u);
}
 
Example 16
Source File: LookupPickerFieldDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsubscribeSubscribeOptions() {
    LookupPickerField component = uiComponents.create(LookupPickerField.NAME);

    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    component.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());

    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setGroup(groups.get(0));

    component.setDatasource(userDs, "group");

    //unbind
    component.setOptionsDatasource(null);

    Datasource.ItemChangeListener<Group> listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    groupsDs.addItemChangeListener(listener);

    component.setValue(groups.get(1));

    //bind
    component.setOptionsDatasource(groupsDs);

    groupsDs.removeItemChangeListener(listener);
    boolean[] valueWasChanged = {false};
    listener = e -> valueWasChanged[0] = true;
    groupsDs.addItemChangeListener(listener);

    component.setValue(groups.get(2));
    assertEquals(true, valueWasChanged[0]);
}
 
Example 17
Source File: UnfetchedAttributeTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Test
public void testSet() throws Exception {
    User user = null;

    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setView(
                new View(User.class, false)
                        .addProperty("login")
                        .addProperty("userRoles", new View(UserRole.class)
                                .addProperty("role", new View(Role.class)
                                        .addProperty("name")))
        );
        q.setParameter(1, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
        List<User> list = q.getResultList();
        if (!list.isEmpty()) {
            user = list.get(0);
            // set value to not present in view
            user.setGroup(group);
        }

        tx.commit();
    } finally {
        tx.end();
    }
    user = reserialize(user);
    assertNotNull(user);
    assertNotNull(user.getUserRoles());
    user.getUserRoles().size();
    assertNotNull(user.getGroup());
}
 
Example 18
Source File: LookupPickerFieldDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueChangeListener() {
    LookupPickerField component = uiComponents.create(LookupPickerField.NAME);

    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    component.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());

    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setGroup(groups.get(0));

    // listener before datasource

    boolean[] valueWasChanged = {false};
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    component.addValueChangeListener(listener);

    component.setDatasource(userDs, "group");
    assertEquals(true, valueWasChanged[0]);

    // reset state
    valueWasChanged[0] = false;
    component.removeValueChangeListener(listener);
    component.setDatasource(null, null);

    // datasource before listener

    component.setDatasource(userDs, "group");
    component.addValueChangeListener(listener);
    user.setGroup(null);
    assertEquals(true, valueWasChanged[0]);
}
 
Example 19
Source File: NonDetachedTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateNew_DM() throws Exception {
    // check versioned entity
    Group companyGroupCopy = metadata.getTools().copy(companyGroup);
    assertTrue(PersistenceHelper.isNew(companyGroupCopy));
    PersistenceHelper.makeDetached(companyGroupCopy);

    User user = persistence.callInTransaction((em) -> em.find(User.class, this.user.getId()));
    User userCopy = metadata.getTools().copy(user);
    assertNull(userCopy.getGroup());

    assertTrue(PersistenceHelper.isNew(userCopy));
    PersistenceHelper.makeDetached(userCopy);

    userCopy.setId(UuidProvider.createUuid());
    userCopy.setVersion(null);
    userCopy.setGroup(companyGroupCopy);
    userCopy.setName("new name");
    try {
        AppBeans.get(DataManager.class).commit(userCopy);

        user = persistence.callInTransaction((em) -> em.find(User.class, userCopy.getId()));
        assertEquals("new name", user.getName());
    } finally {
        cont.deleteRecord(userCopy);
    }

    // check non-versioned entity
    Server server = metadata.create(Server.class);
    server.setName("server-" + server.getId());
    assertTrue(PersistenceHelper.isNew(server));
    PersistenceHelper.makeDetached(server);
    try {
        AppBeans.get(DataManager.class).commit(server);
        Server loaded = persistence.callInTransaction(em -> em.find(Server.class, server.getId()));
        assertNotNull(loaded);
    } finally {
        cont.deleteRecord(server);
    }
}
 
Example 20
Source File: UnfetchedAttributeTest.java    From cuba with Apache License 2.0 4 votes vote down vote up
@AfterEach
public void tearDown() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        // load default 'Company' group
        Query q = em.createQuery("select g from sec$Group g where g.name = ?1");
        q.setView(new View(Group.class, false)
                .addProperty("name"));
        q.setParameter(1, "Company");

        //noinspection unchecked
        List<Group> groups = q.getResultList();
        if (!groups.isEmpty()) {
            Group defaultGroup = groups.get(0);

            // load 'admin' user
            q = em.createQuery("select u from sec$User u where u.id = ?1");
            q.setView(
                    new View(User.class, false)
                            .addProperty("login")
                            .addProperty("userRoles", new View(UserRole.class)
                                    .addProperty("role", new View(Role.class)
                                            .addProperty("name")))
            );
            q.setParameter(1, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));

            List<User> list = q.getResultList();
            if (!list.isEmpty()) {
                User user = list.get(0);
                // set the default value
                user.setGroup(defaultGroup);
            }
        }

        tx.commit();
    } finally {
        tx.end();
    }
}