我很难找到任何方法来检索通过群组继承的角色。
我创建了一个组,mygroup和一个角色mygrouprole,将用户分配到mygroup,并将mygroup分配给mygrouprole。当我点击一个用户时,我可以看到他们继承了mygrouprole。
在我的jsp页面中,我致电com.myco.UserUtil.hasRole(Long userId, String role)
。
当用户直接分配给角色时,这种方法很有效,但不能通过组传递。
public static boolean hasRole(Long userID, String role) {
boolean blnFound = false;
try {
List<Role> lstRoles = RoleServiceUtil.getUserRoles(userID);
m_logger.info("RoleServiceUtil.getUserRoles");
for (Iterator<Role> iterator = lstRoles.iterator(); iterator.hasNext();) {
Role rlRole = iterator.next();
m_logger.info(rlRole.getName());
if (rlRole.getName().equals(role)) {
blnFound = true;
m_logger.debug("User " + Long.toString(userID) + " has role " + role);
break;
}
}
} catch (Exception e) {
...
}
}
到目前为止,我已经尝试了许多替代功能,但似乎没有一个能够找到这个角色。
public static boolean hasRole(Long userID, String role) {
boolean blnFound = false;
try {
List<Role> lstRoles = RoleServiceUtil.getUserRoles(userID);
m_logger.info("RoleServiceUtil.getUserRoles");
for (Iterator<Role> iterator = lstRoles.iterator(); iterator.hasNext();) {
Role rlRole = iterator.next();
m_logger.info(rlRole.getName());
if (rlRole.getName().equals(role)) {
blnFound = true;
m_logger.debug("User " + Long.toString(userID) + " has role " + role);
break;
}
}
lstRoles = RoleLocalServiceUtil.getUserRoles(userID);
m_logger.info("RoleLocalServiceUtil.getUserRoles");
for (Iterator<Role> iterator = lstRoles.iterator(); iterator.hasNext();) {
Role rlRole = iterator.next();
m_logger.info(rlRole.getName());
if (rlRole.getName().equals(role)) {
blnFound = true;
m_logger.debug("User " + Long.toString(userID) + " has role " + role);
break;
}
}
lstRoles = RoleLocalServiceUtil.getUserRelatedRoles(userID,CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId());
m_logger.info("RoleLocalServiceUtil.getUserRelatedRoles");
for (Iterator<Role> iterator = lstRoles.iterator(); iterator.hasNext();) {
Role rlRole = iterator.next();
m_logger.info(rlRole.getName());
if (rlRole.getName().equals(role)) {
blnFound = true;
m_logger.debug("User " + Long.toString(userID) + " has role " + role);
break;
}
}
List<UserGroupRole> lstUGRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(userID);
m_logger.info("UserGroupRoleLocalServiceUtil.getUserGroupRoles");
for (Iterator<UserGroupRole> iterator = lstUGRoles.iterator(); iterator.hasNext();) {
UserGroupRole rlRole = iterator.next();
m_logger.info(rlRole.getRole().getName());
if (rlRole.getRole().getName().equals(role)) {
blnFound = true;
m_logger.debug("User " + Long.toString(userID) + " has role " + role);
break;
}
}
blnFound = UserGroupRoleLocalServiceUtil.hasUserGroupRole(
userID,
// TODO: this only works when there is one company
CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId(),
role
);
m_logger.info("UserGroupRoleLocalServiceUtil.hasUserGroupRole:"+Boolean.toString(blnFound));
blnFound = UserGroupRoleLocalServiceUtil.hasUserGroupRole(
userID,
// TODO: this only works when there is one company
CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId(),
role,
true
);
m_logger.info("UserGroupRoleLocalServiceUtil.hasUserGroupRole:"+Boolean.toString(blnFound));
blnFound = RoleLocalServiceUtil.hasUserRole(
userID,
// TODO: this only works when there is one company
CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId(),
role,
true
);
m_logger.info("RoleLocalServiceUtil.hasUserRole:"+Boolean.toString(blnFound));
blnFound = RoleLocalServiceUtil.hasUserRoles(
userID,
// TODO: this only works when there is one company
CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)).getGroup().getGroupId(),
new String[] {role},
true
);
m_logger.info("RoleLocalServiceUtil.hasUserRoles:"+Boolean.toString(blnFound));
} catch (Exception e) {
...
}
}
UserGroupRoleLocalServiceUtil.getUserGroupRoles
找不到任何角色