📄 securitywrapper.java
字号:
" does not have a role. Granting access to the current user's role ( " +
userRoleId + ") instead.", module);
}
//Add a Role entity to Entity_Access with the owner's role as the party_id
Debug.logVerbose(
"-->[SecurityWrapper.addRoleInformation] Adding entity access for role ID " +
ownerRoleId + ".", module);
GenericValue entityRoleAccess = new GenericValue(delegator.getModelEntity(
"EntityAccess"));
entityRoleAccess.setDelegator( delegator );
entityRoleAccess.set("entityAccessId",
GenericReplicator.getNextSeqId("EntityAccess", delegator));
entityRoleAccess.set("entity", accessEntityName);
entityRoleAccess.set("entityId", accessEntityAttributeValue);
entityRoleAccess.set("partyId", ownerRoleId);
entityRoleAccess.set("partyEntityType", "Role");
entityRoleAccess.set("entityCreatedBy", userPartyId);
entityRoleAccess.set("createdBy", userPartyId);
entityRoleAccess.set("createdDate", now);
entityRoleAccess.set("modifiedBy", userPartyId);
entityRoleAccess.set("modifiedDate", now);
dataMatrix.addEntity("EntityAccess", false, true);
dataMatrix.getCurrentBuffer().getContentsRow(row).add(entityRoleAccess);
return true;
}
/**
* Add security information to the list of data to store with the specified entitiy
*/
public static boolean updateRoleInformation(DataMatrix dataMatrix, int row,
UserInfo userInfo, String ownerPartyId, String accessEntityName,
String accessEntityAttributeValue, GenericDelegator delegator) {
// Add the owner to the team if it is different from the current user.
Timestamp now = new Timestamp(Calendar.getInstance().getTime().getTime());
String userPartyId = userInfo.getPartyId();
String userRoleId = userInfo.getRoleId();
Debug.logVerbose(
"-->[SecurityWrapper.updateRoleInformation] userPartyId: " +
userPartyId, module);
Debug.logVerbose(
"-->[SecurityWrapper.updateRoleInformation] ownerPartyId: " +
ownerPartyId, module);
// Find the team-type entity access for this entity.
List teamEntityAccessGVL = findEntityAccessGVL(accessEntityName,
accessEntityAttributeValue, "Team", delegator);
Iterator teamEntityAccessGVI = teamEntityAccessGVL.iterator();
if (teamEntityAccessGVI.hasNext()) {
// Team entity access was found. Get the team members.
GenericValue teamEntityAccessGV = (GenericValue) teamEntityAccessGVI.next();
String teamId = teamEntityAccessGV.getString("partyId");
HashMap teamMemberFindMap = new HashMap();
teamMemberFindMap.put("teamId", teamId);
try {
List teamMemberGVL = delegator.findByAnd("TeamMember",
teamMemberFindMap);
Iterator teamMemberGVI = teamMemberGVL.iterator();
boolean ownerFound = false;
while (teamMemberGVI.hasNext()) {
GenericValue teamMemberGV = (GenericValue) teamMemberGVI.next();
String teamMemberPartyId = (teamMemberGV.getString(
"partyId") == null) ? ""
: teamMemberGV.getString(
"partyId");
if (teamMemberPartyId.equals(ownerPartyId)) {
Debug.logVerbose(
"-->[SecurityWrapper.updateRoleInformation] Owner is already on the team.", module);
ownerFound = true;
break;
}
}
if (!ownerFound) {
// Need to add the owner to the team.
Debug.logVerbose(
"-->[SecurityWrapper.updateRoleInformation] Adding owner to the team.", module);
GenericValue ownerTeamMember = new GenericValue(delegator.getModelEntity(
"TeamMember"));
ownerTeamMember.setDelegator ( delegator );
ownerTeamMember.set("teamMemberId",
GenericReplicator.getNextSeqId("TeamMember", delegator));
ownerTeamMember.set("teamId", teamId);
ownerTeamMember.set("partyId", ownerPartyId);
ownerTeamMember.set("teamOwner", "Y");
ownerTeamMember.set("createdBy", userPartyId);
ownerTeamMember.set("createdDate", now);
ownerTeamMember.set("modifiedBy", userPartyId);
ownerTeamMember.set("modifiedDate", now);
dataMatrix.addEntity("TeamMember", false, true);
dataMatrix.getCurrentBuffer().getContentsRow(row).add(ownerTeamMember);
}
} catch (GenericEntityException e2) {
Debug.logError(
"[SecurityWrapper.updateRoleInformation] An error occurred while finding team " +
"members for team " + teamId, module);
}
} else {
Debug.logWarning(
"[SecurityWrapper.updateRoleInformation] Could not find the team for the " +
accessEntityName, module);
}
// Get the owner party's role.
String ownerRoleId = "";
HashMap roleFindMap = new HashMap();
roleFindMap.put("contactId", ownerPartyId);
try {
GenericValue ownerContactGV = delegator.findByPrimaryKey("Contact",
roleFindMap);
ownerRoleId = ownerContactGV.getString("roleId");
Debug.logVerbose(
"-->[SecurityWrapper.updateRoleInformation] Found owner's role ID.", module);
} catch (GenericEntityException e) {
Debug.logError(
"[SecurityWrapper.updateRoleInformation] An error occurred while looking for the " +
"entity owner's contact. (" + ownerPartyId + "):", module);
Debug.logError(e.getLocalizedMessage(), module);
}
if ((ownerRoleId == null) || ownerRoleId.equals("")) {
// The owner does not have a role. Give access to the user that created the entity, and log a warning.
ownerRoleId = userRoleId;
Debug.logWarning(
"[SecurityWrapper.updateRoleInformation] Warning! Owner of " +
accessEntityName + " " + accessEntityAttributeValue +
" does not have a role. Granting access to the current user's role ( " +
userRoleId + ") instead.", module);
}
// Check whether there is an entity access record for the owner's role.
List roleEntityAccessGVL = findEntityAccessGVL(accessEntityName,
accessEntityAttributeValue, "Role", delegator);
String entityCreatedBy = userPartyId;
boolean roleEntityAccessFound = false;
Iterator roleEntityAccessGVI = roleEntityAccessGVL.iterator();
while (roleEntityAccessGVI.hasNext()) {
GenericValue roleEntityAccessGV = (GenericValue) roleEntityAccessGVI.next();
String partyId = (roleEntityAccessGV.getString("partyId") == null)
? "" : roleEntityAccessGV.getString("partyId");
if (partyId.equals(ownerRoleId)) {
roleEntityAccessFound = true;
Debug.logVerbose(
"-->[SecurityWrapper.updateRoleInformation] Found entity access record for owner's role.", module);
} else {
// This entity access record is not for the owner's role. Remove it.
Debug.logVerbose(
"-->[SecurityWrapper.updateRoleInformation] Removing entity access record not " +
"for owner's role (" + roleEntityAccessGV.toString(), module);
entityCreatedBy = roleEntityAccessGV.getString(
"entityCreatedBy");
try {
roleEntityAccessGV.remove();
} catch (GenericEntityException e) {
Debug.logError(
"[SecurityWrapper.entityAccessGVL] Warning! An error occurred while removing an " +
"entity access record:", module);
Debug.logError(e.getLocalizedMessage(), module);
}
}
}
if (!roleEntityAccessFound) {
// No entity access was found for the owner's role. Need to add it.
Debug.logVerbose(
"-->[SecurityWrapper.updateRoleInformation] Adding entity access for role ID " +
ownerRoleId + ".", module);
GenericValue entityRoleAccess = new GenericValue(delegator.getModelEntity(
"EntityAccess"));
entityRoleAccess.setDelegator( delegator );
entityRoleAccess.set("entityAccessId",
GenericReplicator.getNextSeqId("EntityAccess", delegator));
entityRoleAccess.set("entity", accessEntityName);
entityRoleAccess.set("entityId", accessEntityAttributeValue);
entityRoleAccess.set("partyId", ownerRoleId);
entityRoleAccess.set("partyEntityType", "Role");
entityRoleAccess.set("entityCreatedBy", entityCreatedBy);
entityRoleAccess.set("createdBy", userPartyId);
entityRoleAccess.set("createdDate", now);
entityRoleAccess.set("modifiedBy", userPartyId);
entityRoleAccess.set("modifiedDate", now);
dataMatrix.addEntity("EntityAccess", false, true);
dataMatrix.getCurrentBuffer().getContentsRow(row).add(entityRoleAccess);
}
return true;
}
/**
* DOCUMENT ME!
*
* @param accessEntityName
* @param accessEntityAttributeValue
* @param partyEntityType
* @param delegator
*
* @return
*/
protected static List findEntityAccessGVL(String accessEntityName,
String accessEntityAttributeValue, String partyEntityType,
GenericDelegator delegator) {
HashMap entityAccessFindMap = new HashMap();
entityAccessFindMap.put("entity", accessEntityName);
entityAccessFindMap.put("entityId", accessEntityAttributeValue);
entityAccessFindMap.put("partyEntityType", partyEntityType);
try {
return delegator.findByAnd("EntityAccess", entityAccessFindMap);
} catch (GenericEntityException e) {
Debug.logError(
"[SecurityWrapper.entityAccessGVL] Warning! An error occurred while looking for " +
"entity access records for {(accessEntityName, " +
accessEntityName + "), (accessEntityAttributeValue," +
accessEntityAttributeValue + "), (partyEntityType," +
partyEntityType + "):", module);
Debug.logError(e.getLocalizedMessage(), module);
return null;
}
}
/**
* DOCUMENT ME!
*
* @param teamId
* @param contactId
* @param userInfo
* @param delegator
*
* @return
*/
public static boolean addTeamMember(String teamId, String contactId,
UserInfo userInfo, GenericDelegator delegator) {
Debug.logVerbose("[SecurityWrapper.addTeamMember]", module);
try {
// Add the specified user as a non-primary member of the specified team.
String userPartyId = userInfo.getPartyId();
GenericValue teamMember = new GenericValue(delegator.getModelEntity(
"TeamMember"));
teamMember.setDelegator( delegator );
Timestamp now = new Timestamp(Calendar.getInstance().getTime()
.getTime());
teamMember.set("teamMemberId",
GenericReplicator.getNextSeqId("TeamMember", delegator));
teamMember.set("teamId", teamId);
teamMember.set("partyId", contactId);
teamMember.set("teamOwner", "N");
teamMember.set("createdBy", userPartyId);
teamMember.set("createdDate", now);
teamMember.set("modifiedBy", userPartyId);
teamMember.set("modifiedDate", now);
Debug.logVerbose(
"[SecurityWrapper.addTeamMember] About to insert team member " +
teamMember.toString(), module);
delegator.create(teamMember);
} catch (GenericEntityException e) {
Debug.logError(
"[SecurityWrapper.addTeamMember] Error inserting new team member: " +
e.getLocalizedMessage(), module);
return false;
}
return true;
}
/**
* DOCUMENT ME!
*
* @param teamId
* @param contactId
* @param userInfo
* @param delegator
*
* @return
*/
public static boolean removeTeamMember(String teamId, String contactId,
UserInfo userInfo, GenericDelegator delegator) {
Debug.logVerbose("[SecurityWrapper.removeTeamMember]", module);
try {
//Delete a team member
HashMap fields = new HashMap();
fields.put("teamId", teamId);
fields.put("partyId", contactId);
Debug.logVerbose(
"[SecurityWrapper.removeTeamMember] About to remove by and: " +
fields.toString(), module);
delegator.removeByAnd("TeamMember", fields);
} catch (GenericEntityException e) {
Debug.logError(
"[SecurityWrapper.removeTeamMember] Error removing team member: " +
e.getLocalizedMessage(), module);
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -