javasecurityexceptionliferay-7expando

Liferay 7 : PermissionChecker not initialized while getting Organization custom field


I've created custom field for Organization in Liferay 7 portal. Now I wanna get a value of the custom field and getting PrincipalException - PermissionChecker not initialized:

    Long userId = (Long) req.getSession().getAttribute(WebKeys.USER_ID);
    List<Organization> mylist = OrganizationLocalServiceUtil.getUserOrganizations(userId);
    if (!mylist.isEmpty()) {

        Organization organization = mylist.get(0);

        String orgUrl = group.getFriendlyURL();

>>>     ExpandoBridge expandoBridge = organization.getExpandoBridge();
        System.out.println(expandoBridge.getAttribute("custom_field"));
    }

exception :

Caused by: com.liferay.portal.kernel.security.auth.PrincipalException: PermissionChecker not initialized
at com.liferay.portal.kernel.service.BaseServiceImpl.getPermissionChecker(BaseServiceImpl.java:81)
at com.liferay.portlet.expando.service.impl.ExpandoValueServiceImpl.getData(ExpandoValueServiceImpl.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:163)

Solution

  • Received answer from colleague. Now it's working. set permissions for custom field to true for guest and add this code

    try {
                User user = (User) req.getAttribute(WebKeys.USER);
                PrincipalThreadLocal.setName(userId);
                PermissionChecker permissionChecker;
    
                permissionChecker = PermissionCheckerFactoryUtil.create(user);
                PermissionThreadLocal.setPermissionChecker(permissionChecker);
    
                Organization organization = mylist.get(0);
    
                ExpandoBridge expandoBridge = organization.getExpandoBridge();
                System.out.println(expandoBridge.getAttribute("custom_field"));
    } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
    }