Liferay.com

Wednesday, September 24, 2014

Quick checking of API methods - Groovy Script (at Control Panel > Server Administration > Script)

Here are some snippets,

======================================================
Case 1 : here I am checking whether a particular User Group & Role exists
======================================================

import com.liferay.portal.service.UserGroupLocalServiceUtil
import com.liferay.portal.service.*
import com.liferay.portal.util.*
import com.liferay.portal.kernel.util.PropsUtil
import com.liferay.portal.kernel.util.PropsKeys

companyId = CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getCompanyId()
out.println(companyId );

try{
// Get User Group(s)
def customGroup = UserGroupLocalServiceUtil.getUserGroup(companyId , "My Org Admin")
out.println("User Group exists : Agency Admin :"+customGroup)

// Get role(s)
def customRole = RoleLocalServiceUtil.getRole(companyId, "My Org Admin")
out.println("Role exists : Agency Admin :"+customRole)

def groupIds = [customGroup.getGroupId()] as long[]
GroupLocalServiceUtil.addRoleGroups(customRole.getRoleId(), groupIds)
}catch(Exception){
out.println("My Org Admin -- UG not exists")
}

======================================================
Case 2 : updating one of my custom field
======================================================

import com.liferay.portal.service.*
import com.liferay.portal.util.*
import com.liferay.portal.kernel.json.*
import com.liferay.portal.kernel.util.PropsUtil
import com.liferay.portal.kernel.util.PropsKeys
import com.liferay.portal.model.RoleConstants

companyId = CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getCompanyId()
out.println(companyId );

try{
role = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ADMINISTRATOR)
users = UserLocalServiceUtil.getRoleUsers(role.getRoleId())
out.println(users)
out.println(JSONFactoryUtil.looseSerialize(users))
out.println(">>>>>>>>>>>>>>>>>>>>>. groovy iterate")
users.each() { it ->
println it
field = it.getExpandoBridge().getAttribute("sbm-user-id", false)
out.println("initial value :"+field)

if(!field){
it.getExpandoBridge().setAttribute("sbm-user-id", it.getEmailAddress())
}

out.println("end value :"+it.getExpandoBridge().getAttribute("sbm-user-id", false))
}


}catch(Exception){
out.println("Administrator role not exists")
}

Hope it helps some body :)

Thanks.


Tuesday, September 23, 2014

Grabbing permission keys of a custom role from control panel

We ran into a use case where we need to automate creation of custom roles having several hundreds of permission keys.

It's kind of tire some to grab each & every one either by looking into source code or grabbing using some browser tools.

I wrote a simple java script to grab keys for each portlet. We need to go to each portlet level to grab exact keys.

It will print in console relevant permissions for that particular portlet. I am using 6.2 for you reference

HOW TO TEST

>> After you navigation to your custom role, particular portlet - CTRL+SHIFT+J (in chrome)
>> paste below code & hit enter - it will spill relevant keys on console.

/*
Start
*/

var topPortletTxt = AUI().one('#_128_permissionContentContainer').one('h3').text();
var topGeneralNode = AUI().one('#_128_permissionContentContainer').one('h4');
var topGeneralTxt;
if(topGeneralNode != null){
topGeneralTxt = topGeneralNode.get('firstChild').get('textContent');
}

console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' + topGeneralTxt + ' > ' + topPortletTxt + '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<');

var resourcePermissionsFull = "";

var resourcePermissionStartOpen = '';

var resourcePermissionEnd = "
";
var resourcePermissionActionStart = "";
var resourcePermissionActionClose = "
";
var topLevelResourceId = AUI().one('#_128_portletResource').val();

var commentStart = '';
resourcePermissionsFull = resourcePermissionsFull + commentStart + topGeneralTxt + ' > ' + topPortletTxt + commentEnd + '\n';
resourcePermissionsFull = resourcePermissionsFull + resourcePermissionStartOpen + topLevelResourceId + resourcePermissionStartClose;

var generalResourcePermissionsAll = "";
topGeneralNode.get('nextElementSibling').all(':checked').each(function(node1) {
        console.log(node1.val());
        if (node1.val().indexOf(topLevelResourceId) != -1) {
            generalResourcePermissionsAll = generalResourcePermissionsAll + resourcePermissionActionStart + node1.val().replace(topLevelResourceId, '') + resourcePermissionActionClose;
        }
    }

);

resourcePermissionsFull = resourcePermissionsFull + generalResourcePermissionsAll + resourcePermissionEnd;

console.log("resourcePermissionsFull : Top > \n\n" + resourcePermissionsFull);

// Related portlet permissions
/*
var relatedPortletsTxt = AUI().one('#_128_relatedPortletResources').ancestor().get('previousElementSibling').get('textContent');

var resourcePermissionsFull = resourcePermissionsFull + commentStart + topPortletTxt + ' > ' + relatedPortletsTxt + commentEnd + '\n';

var resourcePermissionsAll2 = "";
AUI().one('#_128_relatedPortletResources').get('nextElementSibling').all(':checked').each(function(node2) {
            console.log(">>" + node2.val());
var resourceId = parseInt(node2.val(), 10);
var resourcePermissionStartTemp = resourcePermissionStartOpen + resourceId + resourcePermissionStartClose;

resourcePermissionsAll2 = resourcePermissionsAll2 + resourcePermissionStartTemp;
resourcePermissionsAll2 = resourcePermissionsAll2 + resourcePermissionActionStart + node2.val().replace(resourceId, '') + resourcePermissionActionClose;
resourcePermissionsAll2 = resourcePermissionsAll2 + resourcePermissionEnd;

}
);
console.log("resourcePermissionsAll2" + resourcePermissionsAll2)

resourcePermissionsFull = resourcePermissionsFull + resourcePermissionsAll2;

console.log("resourcePermissionsFull : After Related portlet permissions > \n\n" + resourcePermissionsFull);
*/

// Resource permissions

var topResourceTxt = AUI().all('.permission-group').get('previousElementSibling').get('firstChild').get('textContent');

AUI().all('.permission-group h5').each(
    function(node) {
        console.log(topResourceTxt + ' > ' + node.html());
        resourcePermissionsFull = resourcePermissionsFull + commentStart + topResourceTxt + ' > ' + node.html() + commentEnd + '\n';

        var nodeIdPackageConv = node.get('id').replace('resource_', '').replace(/_/g, ".");
        console.log(nodeIdPackageConv);
        var resourcePermissionStartTemp = resourcePermissionStartOpen + nodeIdPackageConv + resourcePermissionStartClose;
        var resourcePermissionsAll = "";
        node.get('nextElementSibling').all(':checked').each(function(node2) {

            console.log(">>" + node2.val().replace(nodeIdPackageConv, ''));
            resourcePermissionsAll = resourcePermissionsAll + resourcePermissionActionStart + node2.val().replace(nodeIdPackageConv, '') + resourcePermissionActionClose;
        })
        resourcePermissionsFull = resourcePermissionsFull + resourcePermissionStartTemp + resourcePermissionsAll + resourcePermissionEnd;
    }
);

console.log("resourcePermissionsFull : Final > \n\n" + resourcePermissionsFull);

/*
End
*/

Hope it helps some body :)

Thanks.

Friday, July 18, 2014

Useful portal properties

########### you can hide Openid, Create Account,Forgot Password links by setting below properties in portal-ext.propeties. ###########
##
## Company
##
company.security.send.password=false

company.security.login.form.autocomplete=false

company.security.send.password.reset.link=false

company.security.strangers=false

company.security.strangers.verify=false

##
## OpenID
##

open.id.auth.enabled=false


Set below property to false in portal-ext.properties file if your liferay application server has concurrency issues with deploying large WARs.
auto.deploy.unpack.war=false


########### You can hide Liferay Version by adding below property in portal-ext.properties file ###########
# to hide Liferay Version
http.header.version.verbosity=partial


##  Steps to Change Liferay Context path:
##  Step 1: go to {liferay.home}\conf\Catalina\localhost\ROOT.xml
##  Rename ROOT.xml to required name. Eg. if you want context as portal then you have to rename ROOT.xml as portal.xml
##  Step 2: open portal.xml edit as below.
##   
##  Step 3: Add following property in portal-ext.properties
##  portal.ctx=/portal 

##  Done, restart your server. then access your server using : localhost:8080/portal

P.S. some properties are compiled by gather from online resources

Saturday, July 12, 2014

How to change maximize & minize icon in 6.2

There was a peculiar use case I ran into when i am working on a project.

Portlet icons needs to be changed in following scenarios
1) default maximize icon
2) icon we have with portlet once maximized
3) icon we have with portlet once minimized

Here is the CSS code snippet for the same.
/*to change icono f maximize to icon-resize-full*/
.aui .icon-plus:before {
    content: "\f065";
}
/* to change icon next to Restore when portlet minimized to icon-plus*/
.aui .icon-resize-vertical:before{
 content: "\f067";
}

Hope it helps somebody - Cheers :)

Wednesday, July 2, 2014

VelocityVariables.java in 6.2

Liferay 6.2 prior versions we used to have VelocityVariables.java, this has been changed in 6.2.


This is changed across into different files

VelocityTemplateContextHelper

TemplateContextHelper


Thursday, April 3, 2014

JSON object parameter passing

I came across a scenario where I need to use Liferay's JSON API for display certain no of documents under each folder depending upon configuration/preferences selected/set.

I need to display certain # of records order by modified date descending order

I didn't find a one workable example when I googled for certain time, here comes the savior - liferay documentation helped me in this regard to say frankly

https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/json-web-services

Here is the code snippet I used

        var groupId = '<%= themeDisplay.getLayout().getGroupId() %>';
        var folderNames = ["Folder 1", "Folder 2", "Folder 3"]; //The names of the folders you wish to pull from and display;
        var auth = "<%= AuthTokenUtil.getToken(request) %>"; //url to the file that prints out the p_auth token

        function ajax(url, func, type) {
            result = '';
            jQuery.ajax({
                type: 'GET',
                url: url,
                func: func,
                dataType: type,
                async: false,
                success: function (data) {
                    result = data;
                }
            });
            return result;
        }

        jQuery(document).ready(function () {
            var html = '';
            var quickHitshtml = '';
            var whitePapershtml = '';
            var noOfDocsToDisplay = '<%= noOfMarketCommentaryDocs %>'; // reading value from preferences
            jQuery.each(folders, function (i, item) {
                if (jQuery.inArray(item.name, folderNames) > -1) {
                    var files = ajax("/api/jsonws/dlapp/get-file-entries/repository-id/" + groupId + "/folder-id/" + item.folderId + "/start/0/end/" + noOfDocsToDisplay + "/+obc:com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelModifiedDateComparator?p_auth=" + auth + "&callback=?", 'files', 'jsonp');
                }
            });
        });

please add relavent imports & porltet/theme should be loaded with jQuery

Liferay AUI 2.0 modules