Monday, April 13, 2020

SonarQube Configuration with Liferay DXP

Below are the details which will help to do the setup SonarQube with Liferay DXP.

Required Softwares with Versions :
1. Java 1.8
2. SonarQube 7.8
3. Liferay DXP 
4. Gradle
5. PostgreSQL 12 (You can use any other software if you want)

 I hope all Softwares are available in your system. If not then make it available.

1. PostgreSQL :-

  • Create a new database with the name "sonar".
  • By default, it will create the schema as "public". or else we can create our own schema if we want to use specific schema for sonar.
  • Remember Username and Password which we are using for the 'sonar' database, we can also check in properties of the database if we want to check for Username and Password details.
2. Sonar Qube :-
  • Open sonar.properties from the path 'sonarqube-7.8\conf'
  • Give below properties in that file.
    #----- PostgreSQL 9.3 or greater
    # By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
    sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonar
    sonar.jdbc.username=postgres
    sonar.jdbc.password=postgres

    Note : sonar is database name and postgres is username and password.

  • Save the details.
  • Start the Sonar Server from the below path "sonarqube-7.8\bin".
  • http://localhost:9000/ to check the Sonar Qube started or not.
  • Once the Sonar Server is up Login into Server by using the default user credentials , admin and admin.
  • Go to my Account -> Security -> Generate Token, save this token or remember as we need to use the Gradle project.
  • You can check the sonar related table in public schema of sonar database.
3. Liferay WorkSpace :-
  • Go to Liferay Workspace folder path
  • Open the file gradle.properties and the below properties
    systemProp.sonar.host.url=http://localhost:9000
    systemProp.sonar.login="abc234"

    Note : abc234 is example token generated from the Sonar Qube , you use the token which you will get from your setup.
  • Open the file "build.gradle", you can use parent project build.gradle file or specific module build.gradle file.
  • I have used parent one, add below properties in that file.

    plugins {  id "org.sonarqube" version "2.7"}

    project(":modules:web") {
        sonarqube {
           properties {
                   property "sonar.inclusions", "**/src/main/java**"
            }
         }
    }

    Note: For more details, you can refer "https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-gradle/"
  • Save the Files
4. Execution :

  • Open the Liferay Workspace path in command prompt.
  • Run the command "gradlew sonarQube".
  • It will take some to for execution.
  • Once you see "Build Successfull".
  • You can see the project result in Sonar Server in the path "http://localhost:9000/projects" with the Liferay Workspace name.
  • Below is the example screenshot for your reference.
Note : Please don't go with my error report 😉

Friday, April 10, 2020

Checking Permission for Page in Liferay

The below code will help you to check the guest permission for page in Liferay.

import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.model.Role;
import com.liferay.portal.kernel.model.ResourcePermission;
import com.liferay.portal.kernel.service.ResourcePermissionLocalServiceUtil;
import com.liferay.portal.kernel.service.RoleLocalServiceUtil;
import com.liferay.portal.kernel.util.Validator;

// We are passing the Layout Object, as we know in liferay page information will be available in Layout table
public static boolean checkguestpermission(Layout layout) {
boolean guestPermission = false;
try {
Role guestRole =  RoleLocalServiceUtil.fetchRole(layout.getCompanyId(), "Guest");
List resourcePermissionList = ResourcePermissionLocalServiceUtil.getResourceResourcePermissions(layout.getCompanyId(),
layout.getGroupId(), Layout.class.getName(), String.valueOf(layout.getPlid()));
logger.debug("resourcePermissionList :" + resourcePermissionList.size());
for(ResourcePermission permission : resourcePermissionList){
                  // ActionId 1 for VIEW permission in liferay
if(permission.getActionIds() == 1){
Role role = RoleLocalServiceUtil.getRole(permission.getRoleId());
if(Validator.isNotNull(role)&&permission.getRoleId()==guestRole.getRoleId()){
guestPermission = true;
}
}
}
}catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return guestPermission;
}

Liferay DXP JNDI Data Source Cofiguration

 This Blog will help us to learn about the JNDI Data Source Configuration in Liferay DXP. We have tested this with Liferay 7.3 with Tomcat. ...