use of com.axelor.db.JpaSecurity in project axelor-open-suite by axelor.
the class MailServiceBaseImpl method findEmails.
@Override
public List<InternetAddress> findEmails(String matching, List<String> selected, int maxResult) {
JpaSecurity jpaSecurity = Beans.get(JpaSecurity.class);
// Users
Filter userPermissionFilter = jpaSecurity.getFilter(JpaSecurity.CAN_READ, User.class);
List<String> selectedWithoutNull = new ArrayList<String>(selected);
for (int i = 0; i < selected.size(); i++) {
if (Strings.isNullOrEmpty(selected.get(i)))
selectedWithoutNull.remove(i);
}
final List<String> where = new ArrayList<>();
final Map<String, Object> params = new HashMap<>();
if (userPermissionFilter != null) {
where.add(userPermissionFilter.getQuery());
}
where.add("((self.partner is not null AND self.partner.emailAddress is not null) OR (self.email is not null))");
if (!isBlank(matching)) {
where.add("(LOWER(self.partner.emailAddress.address) like LOWER(:email) OR LOWER(self.partner.fullName) like LOWER(:email) OR LOWER(self.email) like LOWER(:email) OR LOWER(self.name) like LOWER(:email))");
params.put("email", "%" + matching + "%");
}
if (selectedWithoutNull != null && !selectedWithoutNull.isEmpty()) {
where.add("self.partner.emailAddress.address not in (:selected)");
params.put("selected", selectedWithoutNull);
}
final String filter = Joiner.on(" AND ").join(where);
final Query<User> query = Query.of(User.class);
if (!isBlank(filter)) {
if (userPermissionFilter != null) {
query.filter(filter, userPermissionFilter.getParams());
} else {
query.filter(filter);
}
query.bind(params);
}
final List<InternetAddress> addresses = new ArrayList<>();
if (jpaSecurity.isPermitted(JpaSecurity.CAN_READ, User.class)) {
for (User user : query.fetch(maxResult)) {
try {
if (user.getPartner() != null && user.getPartner().getEmailAddress() != null && !Strings.isNullOrEmpty(user.getPartner().getEmailAddress().getAddress())) {
final InternetAddress item = new InternetAddress(user.getPartner().getEmailAddress().getAddress(), user.getFullName());
addresses.add(item);
selectedWithoutNull.add(user.getPartner().getEmailAddress().getAddress());
} else if (!Strings.isNullOrEmpty(user.getEmail())) {
final InternetAddress item = new InternetAddress(user.getEmail(), user.getFullName());
addresses.add(item);
selectedWithoutNull.add(user.getEmail());
}
} catch (UnsupportedEncodingException e) {
TraceBackService.trace(e);
}
}
}
// Partners
Filter partnerPermissionFilter = jpaSecurity.getFilter(JpaSecurity.CAN_READ, Partner.class);
final List<String> where2 = new ArrayList<>();
final Map<String, Object> params2 = new HashMap<>();
if (partnerPermissionFilter != null) {
where2.add(partnerPermissionFilter.getQuery());
}
where2.add("self.emailAddress is not null");
if (!isBlank(matching)) {
where2.add("(LOWER(self.emailAddress.address) like LOWER(:email) OR LOWER(self.fullName) like LOWER(:email))");
params2.put("email", "%" + matching + "%");
}
if (selectedWithoutNull != null && !selectedWithoutNull.isEmpty()) {
where2.add("self.emailAddress.address not in (:selected)");
params2.put("selected", selectedWithoutNull);
}
final String filter2 = Joiner.on(" AND ").join(where2);
final Query<Partner> query2 = Query.of(Partner.class);
if (!isBlank(filter2)) {
if (partnerPermissionFilter != null) {
query2.filter(filter2, partnerPermissionFilter.getParams());
} else {
query2.filter(filter2);
}
query2.bind(params2);
}
if (jpaSecurity.isPermitted(JpaSecurity.CAN_READ, Partner.class)) {
for (Partner partner : query2.fetch(maxResult)) {
try {
if (partner.getEmailAddress() != null && !Strings.isNullOrEmpty(partner.getEmailAddress().getAddress())) {
final InternetAddress item = new InternetAddress(partner.getEmailAddress().getAddress(), partner.getFullName());
addresses.add(item);
}
} catch (UnsupportedEncodingException e) {
TraceBackService.trace(e);
}
}
}
return addresses;
}
use of com.axelor.db.JpaSecurity in project axelor-open-suite by axelor.
the class ProductController method getDisplayedProductIdList.
@SuppressWarnings("unchecked")
private List<Long> getDisplayedProductIdList(ActionRequest request) {
JpaSecurity security = Beans.get(JpaSecurity.class);
List<Long> displayedProductIdList = new ArrayList<>();
Criteria criteria = Criteria.parse(request);
List<?> products = criteria.createQuery(Product.class, security.getFilter(JpaSecurity.CAN_READ, Product.class)).select("id").fetch(-1, -1);
for (Object product : products) {
if (product instanceof Map) {
Long id = (Long) ((Map<String, Object>) product).get("id");
displayedProductIdList.add(id);
}
}
return displayedProductIdList;
}
Aggregations