Please forgive the beginner question… What is the best way to get a list of all application Users, or users in a specific Access Group?
Thanks
Hi Matthis,
Let me clarify where you are going to use the user list. Do you want to display or use it in a screen controller, or manipulate with it from the server side?
Regards,
Aleksey
Hi Aleksey,
I’m going to use the User list in an EntityListener.
Edit: I also want to use the list to be able to send a mail to all Users.
For both cases it is server side manipulation.
Hi Matthis,
Thank you for the explanation. On the server side, you can simply use EntityManager. You can find sample entity listener below:
package com.company.retrieveuserlist.listener;
import com.haulmont.cuba.core.*;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.security.entity.Group;
import org.springframework.stereotype.Component;
import com.haulmont.cuba.core.listener.BeforeInsertEntityListener;
import com.haulmont.cuba.security.entity.User;
import javax.inject.Inject;
import java.util.List;
import com.company.retrieveuserlist.entity.Test;
@Component("retrieveuserlist_SampleEntityListener")
public class SampleEntityListener implements BeforeInsertEntityListener<Test> {
@Inject
private Persistence persistence;
@Inject
private UserSessionSource sessionSource;
@Override
public void onBeforeInsert(Test entity) {
System.out.println(getUserList().size());
System.out.println(getUserList(sessionSource.getUserSession().getUser().getGroup()).size());
}
/**
*
* @return All users, registered in the system
*/
protected List<User> getUserList() {
EntityManager em = persistence.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from sec$User u", User.class);
return query.getResultList();
}
/**
*
* @param accessGroup
* @return Users, referring to the passed accessGroup
*/
protected List<User> getUserList(Group accessGroup) {
EntityManager em = persistence.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from sec$User u where u.group.id = :group", User.class);
query.setParameter("group", accessGroup);
return query.getResultList();
}
}
Regards,
Aleksey
Aleksey,
Thank you very much for your answer with detailed example!
Best regards