Flexible Search and Restrictions in SAP Hybris(Commerce)

  1. Running that statement on the database: SQL query is executed.
  • The mandatory <types> parameter for the FROM clause.
  • An optional <conditions> field for the WHERE clause.
  • An optional ORDER BY clause.
SELECT <selects> FROM <types> (WHERE <conditions>)? (ORDER BY <order>)?
Select * from {Customer} - this query returns all columns and rows of a Customer item type.
Select * from {Customer!}
SELECT * FROM {Customer} WHERE {name} IS NOT NULL

Running flexible search queries using API

final Map<String, Object> params = new HashMap<String, Object>();String query =”SELECT * FROM {Customer AS c} WHERE {c:name} LIKE ?name”params.put("name",”Test”);
// Flexible search service injected by Spring
final SearchResult<ProductModel> searchResult = flexibleSearchService.search(query, params);

Restrictions

Restrictions allow to limit search results depending on which type is searched and which user is currently logged in.

Disabling Restrictions

You are trying to get some data using FlexibleSearchQuery but it is responding empty result? You can try to disable restrictions:

import de.hybris.platform.servicelayer.user.UserService;
...
@Autowired
private UserService userService;
...
userService.setCurrentUser(userService.getAdminUser())
import de.hybris.platform.search.restriction.SearchRestrictionService;
...
// Disable search restrictions
searchRestrictionService.disableSearchRestrictions();
// some query goes here

// Enable search restrictions
searchRestrictionService.enableSearchRestrictions();
// some query goes here

Creating Restrictions

  1. Creating Restrictions via the SAP Hybris(Commerce) API
final SearchRestrictionModel search = modelService.create(SearchRestrictionModel.class);
search.setPrincipal(user);
final SearchRestrictionModel searchRestriction = modelService.create(SearchRestrictionModel.class);
searchRestriction.setActive(Boolean.TRUE);
searchRestriction.setGenerate(Boolean.TRUE);
searchRestriction.setCode("not_Jacksons");
searchRestriction.setPrincipal(user);
searchRestriction.setQuery("{" + PrincipalGroupModel.UID + "} NOT IN ( 'Jacksons' )");
searchRestriction.setRestrictedType(typeService.getComposedTypeForClass(PrincipalGroupModel.class));
modelService.save(searchRestriction);
INSERT_UPDATESearchRestriction;code[unique=true];name[lang=en];query;principal(UID);restrictedType(code);active;generate;employee_restriction;Restrict employees visibility;EXISTS ({{ SELECT {pk} FROM {PrincipalGroupRelation} WHERE {source}={item:pk} AND {target} IN ( ?session.branch ) }} ) AND ( {item:active} = 1 OR EXISTS ( {{ select {ug:PK} from {UserGroup as ug} where {ug:PK} IN (?session.user.groups) and {ug:uid} = 'b2badmingroup' }} ));b2bgroup;B2BCustomer;true;true

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store