edu.internet2.middleware.grouper.app.duo.GrouperDuoTargetDao Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grouper Show documentation
Show all versions of grouper Show documentation
Internet2 Groups Management Toolkit
package edu.internet2.middleware.grouper.app.duo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import edu.internet2.middleware.grouper.app.provisioning.GrouperProvisioningLists;
import edu.internet2.middleware.grouper.app.provisioning.ProvisioningEntity;
import edu.internet2.middleware.grouper.app.provisioning.ProvisioningGroup;
import edu.internet2.middleware.grouper.app.provisioning.ProvisioningMembership;
import edu.internet2.middleware.grouper.app.provisioning.ProvisioningObjectChange;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.GrouperProvisionerDaoCapabilities;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.GrouperProvisionerTargetDaoBase;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoDeleteEntityRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoDeleteEntityResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoDeleteGroupRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoDeleteGroupResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoDeleteMembershipRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoDeleteMembershipResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoInsertEntityRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoInsertEntityResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoInsertGroupRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoInsertGroupResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoInsertMembershipRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoInsertMembershipResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveAllDataRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveAllDataResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveAllEntitiesRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveAllEntitiesResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveAllGroupsRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveAllGroupsResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveEntitiesRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveEntitiesResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveEntityRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveEntityResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveGroupRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveGroupResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveGroupsRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveGroupsResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveMembershipsByEntityRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveMembershipsByEntityResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveMembershipsByGroupRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoRetrieveMembershipsByGroupResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoTimingInfo;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoUpdateEntityRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoUpdateEntityResponse;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoUpdateGroupRequest;
import edu.internet2.middleware.grouper.app.provisioning.targetDao.TargetDaoUpdateGroupResponse;
import edu.internet2.middleware.grouper.util.GrouperHttpClient;
import edu.internet2.middleware.grouper.util.GrouperHttpClientLog;
import edu.internet2.middleware.grouper.util.GrouperUtil;
import edu.internet2.middleware.grouperClient.util.ExpirableCache;
public class GrouperDuoTargetDao extends GrouperProvisionerTargetDaoBase {
@Override
public boolean loggingStart() {
return GrouperHttpClient.logStart(new GrouperHttpClientLog());
}
@Override
public String loggingStop() {
return GrouperHttpClient.logEnd();
}
@Override
public TargetDaoRetrieveAllGroupsResponse retrieveAllGroups(
TargetDaoRetrieveAllGroupsRequest targetDaoRetrieveAllGroupsRequest) {
long startNanos = System.nanoTime();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this
.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
List results = new ArrayList();
List grouperDuoGroups = GrouperDuoApiCommands
.retrieveDuoGroups(duoConfiguration.getDuoExternalSystemConfigId());
for (GrouperDuoGroup grouperDuoGroup : grouperDuoGroups) {
ProvisioningGroup targetGroup = grouperDuoGroup.toProvisioningGroup();
results.add(targetGroup);
}
return new TargetDaoRetrieveAllGroupsResponse(results);
} finally {
this.addTargetDaoTimingInfo(
new TargetDaoTimingInfo("retrieveAllGroups", startNanos));
}
}
@Override
public TargetDaoRetrieveAllEntitiesResponse retrieveAllEntities(
TargetDaoRetrieveAllEntitiesRequest targetDaoRetrieveAllEntitiesRequest) {
long startNanos = System.nanoTime();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
List results = new ArrayList();
boolean loadEntitiesToGrouperTable = this.getGrouperProvisioner().retrieveGrouperProvisioningBehavior().isLoadEntitiesToGrouperTable();
List grouperDuoUsers = GrouperDuoApiCommands.retrieveDuoUsers(duoConfiguration.getDuoExternalSystemConfigId(), loadEntitiesToGrouperTable);
TargetDaoRetrieveAllEntitiesResponse targetDaoRetrieveAllEntitiesResponse = new TargetDaoRetrieveAllEntitiesResponse(results);
Map targetEntityToTargetNativeEntity = targetDaoRetrieveAllEntitiesResponse
.getTargetEntityToTargetNativeEntity();
for (GrouperDuoUser grouperDuoUser : grouperDuoUsers) {
ProvisioningEntity targetEntity = grouperDuoUser.toProvisioningEntity();
results.add(targetEntity);
if (targetDaoRetrieveAllEntitiesRequest.isIncludeNativeEntity()) {
targetEntityToTargetNativeEntity.put(targetEntity, grouperDuoUser);
}
}
return targetDaoRetrieveAllEntitiesResponse;
} finally {
this.addTargetDaoTimingInfo(
new TargetDaoTimingInfo("retrieveAllEntities", startNanos));
}
}
@Override
public TargetDaoRetrieveEntityResponse retrieveEntity(
TargetDaoRetrieveEntityRequest targetDaoRetrieveEntityRequest) {
long startNanos = System.nanoTime();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this
.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
// we can retrieve by id or username, prefer id
GrouperDuoUser grouperDuoUser = null;
if (StringUtils.equals("id", targetDaoRetrieveEntityRequest.getSearchAttribute())) {
grouperDuoUser = GrouperDuoApiCommands.retrieveDuoUser(duoConfiguration.getDuoExternalSystemConfigId(),
GrouperUtil.stringValue(targetDaoRetrieveEntityRequest.getSearchAttributeValue()));
} else if (StringUtils.equals("loginId", targetDaoRetrieveEntityRequest.getSearchAttribute())) {
grouperDuoUser = GrouperDuoApiCommands.retrieveDuoUserByName(
duoConfiguration.getDuoExternalSystemConfigId(), GrouperUtil.stringValue(targetDaoRetrieveEntityRequest.getSearchAttributeValue()));
} else {
throw new RuntimeException("Not expecting search attribute '" + targetDaoRetrieveEntityRequest.getSearchAttribute() + "'");
}
ProvisioningEntity targetEntity = grouperDuoUser == null ? null
: grouperDuoUser.toProvisioningEntity();
TargetDaoRetrieveEntityResponse targetDaoRetrieveEntityResponse = new TargetDaoRetrieveEntityResponse(targetEntity);
if (targetDaoRetrieveEntityRequest.isIncludeNativeEntity()) {
targetDaoRetrieveEntityResponse.setTargetNativeEntity(grouperDuoUser);
}
return targetDaoRetrieveEntityResponse;
} finally {
this.addTargetDaoTimingInfo(new TargetDaoTimingInfo("retrieveEntity", startNanos));
}
}
private static ExpirableCache> cacheGroupNameToGroup = new ExpirableCache>(5);
@Override
public TargetDaoRetrieveGroupResponse retrieveGroup(TargetDaoRetrieveGroupRequest targetDaoRetrieveGroupRequest) {
long startNanos = System.nanoTime();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
ProvisioningGroup grouperTargetGroup = targetDaoRetrieveGroupRequest.getTargetGroup();
GrouperDuoGroup grouperDuoGroup = null;
if (StringUtils.equals("id", targetDaoRetrieveGroupRequest.getSearchAttribute())) {
grouperDuoGroup = GrouperDuoApiCommands.retrieveDuoGroup(duoConfiguration.getDuoExternalSystemConfigId(), GrouperUtil.stringValue(targetDaoRetrieveGroupRequest.getSearchAttributeValue()));
} else if (StringUtils.equals("name", targetDaoRetrieveGroupRequest.getSearchAttribute())) {
String name = GrouperUtil.stringValue(targetDaoRetrieveGroupRequest.getSearchAttributeValue());
if (StringUtils.isNotBlank(name)) {
Map groupNameToGroup = cacheGroupNameToGroup.get(Boolean.TRUE);
grouperDuoGroup = groupNameToGroup == null ? null : groupNameToGroup.get(name);
if (grouperDuoGroup == null) {
List allDuoGroups = GrouperDuoApiCommands.retrieveDuoGroups(duoConfiguration.getDuoExternalSystemConfigId());
groupNameToGroup = new HashMap();
for (GrouperDuoGroup currentDuoGroup: GrouperUtil.nonNull(allDuoGroups)) {
groupNameToGroup.put(currentDuoGroup.getName(), currentDuoGroup);
}
cacheGroupNameToGroup.put(Boolean.TRUE, groupNameToGroup);
grouperDuoGroup = groupNameToGroup.get(name);
}
}
} else {
throw new RuntimeException("Not expecting search attribute '" + targetDaoRetrieveGroupRequest.getSearchAttribute() + "'");
}
ProvisioningGroup targetGroup = grouperDuoGroup == null ? null : grouperDuoGroup.toProvisioningGroup();
return new TargetDaoRetrieveGroupResponse(targetGroup);
} finally {
this.addTargetDaoTimingInfo(new TargetDaoTimingInfo("retrieveGroup", startNanos));
}
}
@Override
public TargetDaoInsertGroupResponse insertGroup(TargetDaoInsertGroupRequest targetDaoInsertGroupRequest) {
long startNanos = System.nanoTime();
ProvisioningGroup targetGroup = targetDaoInsertGroupRequest.getTargetGroup();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
GrouperDuoGroup grouperDuoGroup = GrouperDuoGroup.fromProvisioningGroup(targetGroup, null);
GrouperDuoGroup createdDuoGroup = GrouperDuoApiCommands.createDuoGroup(duoConfiguration.getDuoExternalSystemConfigId(), grouperDuoGroup);
targetGroup.setId(createdDuoGroup.getGroup_id());
targetGroup.setProvisioned(true);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetGroup.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(true);
}
return new TargetDaoInsertGroupResponse();
} catch (Exception e) {
targetGroup.setProvisioned(false);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetGroup.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(false);
}
throw e;
} finally {
this.addTargetDaoTimingInfo(new TargetDaoTimingInfo("insertGroup", startNanos));
}
}
@Override
public TargetDaoInsertMembershipResponse insertMembership(TargetDaoInsertMembershipRequest targetDaoInsertMembershipRequest) {
long startNanos = System.nanoTime();
ProvisioningMembership targetMembership = targetDaoInsertMembershipRequest.getTargetMembership();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
GrouperDuoApiCommands.associateUserToGroup(duoConfiguration.getDuoExternalSystemConfigId(), targetMembership.getProvisioningEntityId(), targetMembership.getProvisioningGroupId());
targetMembership.setProvisioned(true);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetMembership.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(true);
}
return new TargetDaoInsertMembershipResponse();
} catch (Exception e) {
targetMembership.setProvisioned(false);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetMembership.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(false);
}
throw e;
} finally {
this.addTargetDaoTimingInfo(new TargetDaoTimingInfo("insertMembership", startNanos));
}
}
public TargetDaoDeleteMembershipResponse deleteMembership(TargetDaoDeleteMembershipRequest targetDaoDeleteMembershipRequest) {
long startNanos = System.nanoTime();
ProvisioningMembership targetMembership = targetDaoDeleteMembershipRequest.getTargetMembership();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
GrouperDuoApiCommands.disassociateUserFromGroup(duoConfiguration.getDuoExternalSystemConfigId(), targetMembership.getProvisioningEntityId(), targetMembership.getProvisioningGroupId());
targetMembership.setProvisioned(true);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetMembership.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(true);
}
return new TargetDaoDeleteMembershipResponse();
} catch (Exception e) {
targetMembership.setProvisioned(false);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetMembership.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(false);
}
throw new RuntimeException("Failed to delete Duo group member (groupId '" + targetMembership.getProvisioningGroupId() + "', member '" + targetMembership.getProvisioningEntityId() + "'", e);
} finally {
this.addTargetDaoTimingInfo(new TargetDaoTimingInfo("deleteMembership", startNanos));
}
}
@Override
public TargetDaoUpdateGroupResponse updateGroup(
TargetDaoUpdateGroupRequest targetDaoUpdateGroupRequest) {
long startNanos = System.nanoTime();
ProvisioningGroup targetGroup = targetDaoUpdateGroupRequest.getTargetGroup();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
// lets make sure we are doing the right thing
Set fieldNamesToUpdate = new HashSet();
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetGroup.getInternal_objectChanges())) {
String fieldName = provisioningObjectChange.getAttributeName();
fieldNamesToUpdate.add(fieldName);
}
GrouperDuoGroup grouperDuoGroup = GrouperDuoGroup.fromProvisioningGroup(targetGroup, null);
GrouperDuoApiCommands.updateDuoGroup(duoConfiguration.getDuoExternalSystemConfigId(), grouperDuoGroup, fieldNamesToUpdate);
targetGroup.setProvisioned(true);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetGroup.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(true);
}
return new TargetDaoUpdateGroupResponse();
} catch (Exception e) {
targetGroup.setProvisioned(false);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetGroup.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(false);
}
throw e;
} finally {
this.addTargetDaoTimingInfo(new TargetDaoTimingInfo("updateGroup", startNanos));
}
}
@Override
public TargetDaoDeleteGroupResponse deleteGroup(TargetDaoDeleteGroupRequest targetDaoDeleteGroupRequest) {
long startNanos = System.nanoTime();
ProvisioningGroup targetGroup = targetDaoDeleteGroupRequest.getTargetGroup();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
GrouperDuoGroup grouperDuoGroup = GrouperDuoGroup.fromProvisioningGroup(targetGroup, null);
GrouperDuoApiCommands.deleteDuoGroup(duoConfiguration.getDuoExternalSystemConfigId(), grouperDuoGroup.getGroup_id());
targetGroup.setProvisioned(true);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetGroup.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(true);
}
Map groupNameToGroup = cacheGroupNameToGroup.get(Boolean.TRUE);
if (groupNameToGroup != null) {
groupNameToGroup.remove(grouperDuoGroup.getName());
}
return new TargetDaoDeleteGroupResponse();
} catch (Exception e) {
targetGroup.setProvisioned(false);
for (ProvisioningObjectChange provisioningObjectChange : GrouperUtil.nonNull(targetGroup.getInternal_objectChanges())) {
provisioningObjectChange.setProvisioned(false);
}
throw e;
} finally {
this.addTargetDaoTimingInfo(new TargetDaoTimingInfo("deleteGroup", startNanos));
}
}
@Override
public TargetDaoRetrieveMembershipsByEntityResponse retrieveMembershipsByEntity(TargetDaoRetrieveMembershipsByEntityRequest targetDaoRetrieveMembershipsByEntityRequest) {
long startNanos = System.nanoTime();
ProvisioningEntity targetEntity = targetDaoRetrieveMembershipsByEntityRequest.getTargetEntity();
try {
GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
String targetEntityId = resolveTargetEntityId(targetEntity);
if (StringUtils.isBlank(targetEntityId)) {
return new TargetDaoRetrieveMembershipsByEntityResponse(new ArrayList());
}
List duoGroups = GrouperDuoApiCommands.retrieveDuoGroupsByUser(duoConfiguration.getDuoExternalSystemConfigId(), targetEntityId);
List provisioningMemberships = new ArrayList();
for (GrouperDuoGroup duoGroup : duoGroups) {
ProvisioningMembership targetMembership = new ProvisioningMembership(false);
targetMembership.setProvisioningGroupId(duoGroup.getGroup_id());
targetMembership.setProvisioningEntityId(targetEntity.getId());
provisioningMemberships.add(targetMembership);
}
return new TargetDaoRetrieveMembershipsByEntityResponse(provisioningMemberships);
} finally {
this.addTargetDaoTimingInfo(new TargetDaoTimingInfo("retrieveMembershipsByEntity", startNanos));
}
}
// @Override
// public TargetDaoRetrieveMembershipsByGroupResponse retrieveMembershipsByGroup(TargetDaoRetrieveMembershipsByGroupRequest targetDaoRetrieveMembershipsByGroupRequest) {
// long startNanos = System.nanoTime();
// ProvisioningGroup targetGroup = targetDaoRetrieveMembershipsByGroupRequest.getTargetGroup();
//
// try {
// GrouperDuoConfiguration duoConfiguration = (GrouperDuoConfiguration) this.getGrouperProvisioner().retrieveGrouperProvisioningConfiguration();
//
// Set userIds = GrouperDuoApiCommands.retrieveDuoGroupMembers(duoConfiguration.getDuoExternalSystemConfigId(), targetGroup.getId());
//
// List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy