Skip to content

Instantly share code, notes, and snippets.

@CorneAussems
Created July 7, 2025 13:55
Show Gist options
  • Select an option

  • Save CorneAussems/ef4b032c01ec48a0584ab104134deb3a to your computer and use it in GitHub Desktop.

Select an option

Save CorneAussems/ef4b032c01ec48a0584ab104134deb3a to your computer and use it in GitHub Desktop.
Liferay 7.1 Update Role Groovy
import com.liferay.portal.kernel.model.Role
import com.liferay.portal.kernel.model.RoleConstants
import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal
import com.liferay.portal.kernel.service.RoleLocalServiceUtil
import com.liferay.portal.kernel.service.persistence.RoleUtil
import com.liferay.portal.kernel.service.persistence.UserUtil
import com.liferay.portal.kernel.util.PortalUtil
// Setup parameters
long companyId = PortalUtil.getDefaultCompanyId()
long roleId = 473773104L
String roleName = "dummy-role-fixed-id-${roleId}"
int type = RoleConstants.TYPE_REGULAR
String description = "Dummy role with fixed primary key"
String className = "com.liferay.portal.kernel.model.Role"
try{
role = RoleLocalServiceUtil.deleteRole(roleId)
if (RoleLocalServiceUtil.fetchRole(companyId, roleName)) {
println "❌ Role already exists with this name"
return
}
def existingRole = RoleUtil.fetchByPrimaryKey(roleId)
if (existingRole != null) {
println "❌Role with roleId ${roleId} already exists: ${existingRole.name}"
return
}
long userId = Long.parseLong(userInfo.get("liferay.user.id"))
//long userId = PrincipalThreadLocal.getUserId()
if (!userId || userId == 0L) {
userId = UserUtil.findByCompanyId(companyId)[0].userId
}
Role role = RoleUtil.create(roleId)
println "✅ Updated role ${role}"
role.setUuid(UUID.randomUUID().toString()) // Ensure uniqueness
role.setCompanyId(companyId)
role.setUserId(userId)
role.setUserName()
role.setCreateDate(new Date())
role.setModifiedDate(new Date())
role.setClassNameId(PortalUtil.getClassNameId(className))
role.setClassPK(roleId)
role.setName(roleName)
role.setDescription(description)
role.setType(type)
role = RoleLocalServiceUtil.updateRole(role)
println "✅ Updated role ${role}"
} catch (e) {
println( e )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment