usermanagement-core/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayRoleManager.java

323 lines
10 KiB
Java

package org.gcube.vomanagement.usermanagement.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.gcube.vomanagement.usermanagement.RoleManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.model.GCubeRole;
import org.gcube.vomanagement.usermanagement.util.ManagementUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.UserGroupRole;
import com.liferay.portal.service.ClassNameLocalServiceUtil;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
public class LiferayRoleManager implements RoleManager {
/**
* logger
*/
private static final Logger _log = LoggerFactory.getLogger(LiferayRoleManager.class);
private static final String ADMINISTRATOR = "Administrator";
private static final int ROLE_TYPE = 2; // role type. 1=regular, 2=site, 3=organization
//simple role mapping
protected static GCubeRole mapLRRole(Role r) throws PortalException, SystemException {
if (r != null) {
return new GCubeRole( r.getRoleId(), r.getName(), r.getDescription());
}
else
return null;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isAdmin(long userId) throws UserRetrievalFault {
try {
UserLocalServiceUtil.getUser(userId);
} catch (PortalException e) {
throw new UserRetrievalFault("User not existing", e);
} catch (SystemException e) {
e.printStackTrace();
}
try {
long roleId = RoleLocalServiceUtil.getRole(ManagementUtils.getCompany().getCompanyId(), ADMINISTRATOR).getRoleId();
return UserLocalServiceUtil.hasRoleUser(roleId, userId);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public GCubeRole getRole(String roleName, long groupId) throws RoleRetrievalFault, GroupRetrievalFault {
try {
GroupLocalServiceUtil.getGroup(groupId);
List<Role> roles = RoleLocalServiceUtil.getGroupRelatedRoles(groupId);
for (Role role : roles) {
_log.debug(role.toString());
if (role.getName().compareTo(roleName) == 0)
return (mapLRRole(role));
}
} catch (PortalException e) {
_log.warn(roleName + " Role not existing");
} catch (SystemException e) {
throw new RoleRetrievalFault(e.getMessage(), e);
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public GCubeRole getRole(long roleId) throws RoleRetrievalFault, RoleRetrievalFault {
try {
return mapLRRole(RoleLocalServiceUtil.getRole(roleId));
} catch (PortalException e) {
_log.warn(roleId + " Role id not existing");
} catch (SystemException e) {
_log.error("Liferay SystemException");
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public long getRoleId(String roleName, long groupId) throws RoleRetrievalFault, GroupRetrievalFault {
return getRole(roleName, groupId).getRoleId();
}
/**
* {@inheritDoc}
*/
@Override
public long getRoleIdByName(String roleName) throws RoleRetrievalFault {
try {
Role toReturn = RoleLocalServiceUtil.getRole(ManagementUtils.getCompany().getCompanyId(), roleName);
return toReturn.getRoleId();
} catch (PortalException e) {
throw new RoleRetrievalFault("Role not existing: " + roleName);
} catch (SystemException e) {
_log.error("Liferay SystemException");
}
return -1;
}
/**
* {@inheritDoc}
*/
@Override
public boolean assignRoleToUser(long userId, long groupId, long roleId) throws UserManagementSystemException, UserRetrievalFault, GroupRetrievalFault, RoleRetrievalFault {
try {
_log.debug("Trying to assign role to " + UserLocalServiceUtil.getUser(userId).getFullName() +" in group " + groupId);
long[] roleIds = {roleId};
return (UserGroupRoleLocalServiceUtil.addUserGroupRoles(userId, groupId, roleIds).size() > 0);
} catch (PortalException e) {
throw new UserRetrievalFault("User, not existing or roleId could not be found", e);
} catch (SystemException e) {
e.printStackTrace();
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean assignRolesToUser(long userId, long groupId, long[] roleIds) throws UserManagementSystemException, UserRetrievalFault, GroupRetrievalFault, RoleRetrievalFault {
try {
_log.debug("Trying to assign role to " + UserLocalServiceUtil.getUser(userId).getFullName() +" in group " + groupId);
return (UserGroupRoleLocalServiceUtil.addUserGroupRoles(userId, groupId, roleIds).size() > 0);
} catch (PortalException e) {
throw new UserRetrievalFault("User, not existing, or group/roleId could not be found", e);
} catch (SystemException e) {
e.printStackTrace();
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean createRole(String roleName, String roleDescription) throws UserManagementSystemException {
_log.debug("Check createRole " + roleName);
long existingroleId = -1;
try {
existingroleId = getRoleIdByName(roleName);
} catch (Exception e) {
_log.debug("Site Role having name " + roleName + " does not exist, proceed with creation OK");
}
if (existingroleId == -1) {
try {
_log.debug("Trying createRole " + roleName);
Date now = new Date();
Long defaultCompanyId = PortalUtil.getDefaultCompanyId();
Long defaultUserId = UserLocalServiceUtil.getDefaultUserId(defaultCompanyId);
Long roleClassNameId = ClassNameLocalServiceUtil.getClassNameId(Role.class);
Long roleId = CounterLocalServiceUtil.increment();
Role role = RoleLocalServiceUtil.createRole(roleId);
role.setName(roleName);
role.setDescription(roleDescription);
role.setType(ROLE_TYPE);
role.setUserId(defaultUserId);
role.setCompanyId(defaultCompanyId);
role.setClassNameId(roleClassNameId);
role.setClassPK(roleId);
role.setCreateDate(now);
role.setModifiedDate(now);
RoleLocalServiceUtil.addRole(role);
_log.debug("CreateRole " + roleName + " SUCCESS");
return true;
} catch (SystemException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
}
} else {
_log.error("Site Role having name " + roleName + " exist already, skipping creation");
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean deleteRole(long roleId) throws UserManagementSystemException, RoleRetrievalFault {
try {
RoleLocalServiceUtil.deleteRole(roleId);
return true;
} catch (PortalException e) {
throw new RoleRetrievalFault("The roleId does not exists", e);
} catch (SystemException e) {
e.printStackTrace();
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public GCubeRole updateRole(long roleId, String roleName, String roleDescription) throws RoleRetrievalFault {
Role toEdit;
try {
toEdit = RoleLocalServiceUtil.getRole(roleId);
toEdit.setName(roleName);
toEdit.setDescription(roleDescription);
Role toReturn = RoleLocalServiceUtil.updateRole(toEdit);
return mapLRRole(toReturn);
} catch (PortalException e) {
throw new RoleRetrievalFault("The roleId does not exists", e);
} catch (SystemException e) {
e.printStackTrace();
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public boolean removeRoleFromUser(long userId, long groupId, long roleId) throws UserManagementSystemException, UserRetrievalFault, GroupRetrievalFault, RoleRetrievalFault {
try {
_log.debug("Trying to remove role to " + UserLocalServiceUtil.getUser(userId).getFullName() +" in group " + groupId);
long[] roleIds = {roleId};
UserGroupRoleLocalServiceUtil.deleteUserGroupRoles(userId, groupId, roleIds);
return true;
} catch (PortalException e) {
throw new UserRetrievalFault("User, not existing, or group/roleId could not be found", e);
} catch (SystemException e) {
e.printStackTrace();
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean removeAllRolesFromUser(long userId, long... groupIds) throws UserManagementSystemException, UserRetrievalFault, GroupRetrievalFault {
try {
_log.debug("Trying to remove all roles to " + UserLocalServiceUtil.getUser(userId).getFullName() +" in groups " + groupIds);
UserGroupRoleLocalServiceUtil.deleteUserGroupRoles(userId, groupIds);
return true;
} catch (PortalException e) {
throw new UserRetrievalFault("User, not existing, or group/roleId could not be found", e);
} catch (SystemException e) {
e.printStackTrace();
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public List<GCubeRole> listAllRoles() {
List<GCubeRole> toReturn = new ArrayList<GCubeRole>();
try {
List<Role> roles = RoleLocalServiceUtil.getRoles(ManagementUtils.getCompany().getCompanyId());
for (Role role : roles) {
toReturn.add(mapLRRole(role));
}
} catch (SystemException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
}
return toReturn;
}
/**
* {@inheritDoc}
*/
@Override
public List<GCubeRole> listAllGroupRoles() {
List<GCubeRole> toReturn = new ArrayList<GCubeRole>();
try {
List<Role> roles = RoleLocalServiceUtil.getRoles(ManagementUtils.getCompany().getCompanyId());
for (Role role : roles) {
if (role.getType()==ROLE_TYPE)
toReturn.add(mapLRRole(role));
}
} catch (SystemException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
}
return toReturn;
}
/**
* {@inheritDoc}
*/
@Override
public List<GCubeRole> listRolesByUserAndGroup(long userId, long groupId) throws GroupRetrievalFault, UserRetrievalFault {
List<GCubeRole> toReturn = new ArrayList<GCubeRole>();
try {
List<UserGroupRole> roles = UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroup(groupId);
for (UserGroupRole ugr : roles) {
if (ugr.getUserId()==userId)
toReturn.add(mapLRRole(ugr.getRole()));
}
} catch (SystemException e) {
e.printStackTrace();
} catch (PortalException e) {
throw new UserRetrievalFault("User, not existing, or groupId could not be found", e);
}
return toReturn;
}
}