Skip to content

Commit

Permalink
Merge d05055d into 973acdf
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoungA committed Aug 28, 2018
2 parents 973acdf + d05055d commit e091e6d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
Expand Up @@ -130,7 +130,13 @@ protected int deleteTickets(final Stream<String> tickets) {
return tickets.mapToInt(this::deleteTicket).sum();
}

private void deleteLinkedProxyGrantingTickets(final AtomicInteger count, final TicketGrantingTicket tgt) {
/**
* Delete linked pgts.
*
* @param count the total number of deleted tickets
* @param tgt ticket granting ticket to get PGT
*/
protected void deleteLinkedProxyGrantingTickets(final AtomicInteger count, final TicketGrantingTicket tgt) {
val pgts = new LinkedHashSet<>(tgt.getProxyGrantingTickets().keySet());
val hasPgts = !pgts.isEmpty();
count.getAndAdd(deleteTickets(pgts));
Expand All @@ -140,8 +146,13 @@ private void deleteLinkedProxyGrantingTickets(final AtomicInteger count, final T
updateTicket(tgt);
}
}

private void deleteProxyGrantingTicketFromParent(final ProxyGrantingTicket ticket) {

/**
* Delete pgt from parent.
*
* @param ticket pgt to remove
*/
protected void deleteProxyGrantingTicketFromParent(final ProxyGrantingTicket ticket) {
val thePgt = ticket;
thePgt.getTicketGrantingTicket().getProxyGrantingTickets().remove(thePgt.getId());
updateTicket(thePgt.getTicketGrantingTicket());
Expand Down
Expand Up @@ -5,6 +5,7 @@
import org.apereo.cas.ticket.TicketCatalog;
import org.apereo.cas.ticket.TicketDefinition;
import org.apereo.cas.ticket.TicketGrantingTicket;
import org.apereo.cas.ticket.proxy.ProxyGrantingTicket;

import lombok.extern.slf4j.Slf4j;
import lombok.val;
Expand All @@ -20,6 +21,7 @@
import javax.persistence.TypedQuery;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -97,6 +99,34 @@ public Ticket getTicket(final String ticketId) {
}
return null;
}

/**
* Get ticket nevertheless it was expired.
*
* @param ticketId the ticket id
* @param getIfExpired check to get expired ticket
* @return the int
*/
protected Ticket getTicket(final String ticketId, final boolean getIfExpired) {
if (!getIfExpired) {
return getTicket(ticketId);
} else {
try {
val tkt = ticketCatalog.find(ticketId);
val sql = String.format("select t from %s t where t.id = :id", getTicketEntityName(tkt));
val query = entityManager.createQuery(sql, tkt.getImplementationClass());
query.setParameter("id", ticketId);
query.setLockMode(this.lockType);
val result = query.getSingleResult();
if (result != null) {
return result;
}
} catch (final Exception e) {
LOGGER.error("Error getting ticket [{}] from registry.", ticketId, e);
}
return null;
}
}

@Override
public Collection<Ticket> getTickets() {
Expand Down Expand Up @@ -151,6 +181,35 @@ public long serviceTicketCount() {
val query = this.entityManager.createQuery(sql);
return countToLong(query.getSingleResult());
}

@Override
public int deleteTicket(final String ticketId) {
val count = new AtomicInteger(0);

try {
val ticket = getTicket(ticketId, true);

if (ticket instanceof TicketGrantingTicket) {
LOGGER.debug("Removing children of ticket [{}] from the registry.", ticket.getId());
val tgt = (TicketGrantingTicket) ticket;
count.addAndGet(deleteChildren(tgt));

if (ticket instanceof ProxyGrantingTicket) {
deleteProxyGrantingTicketFromParent((ProxyGrantingTicket) ticket);
} else {
deleteLinkedProxyGrantingTickets(count, tgt);
}
}
LOGGER.debug("Removing ticket [{}] from the registry.", ticket);
if (deleteSingleTicket(ticketId)) {
count.incrementAndGet();
}
} catch (final Exception e) {
LOGGER.error("Error deleting ticket [{}] from registry.", ticketId, e);
}

return count.intValue();
}

@Override
public boolean deleteSingleTicket(final String ticketId) {
Expand Down

0 comments on commit e091e6d

Please sign in to comment.