Skip to content

Commit

Permalink
CAS-1071
Browse files Browse the repository at this point in the history
Fixes for JPA.
The changes here ensure the new regex functionality works without requiring
explicit database changes if Hibernate schema generation is enabled,
which is the recommended behavior and what's used in practice by most
deployers.
  • Loading branch information
serac committed Mar 19, 2012
1 parent bf0bada commit c2a78d5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
*/
@Entity
@Inheritance
@DiscriminatorColumn(name = "expression_type")
@DiscriminatorColumn(
name = "expression_type",
length = 15,
discriminatorType = DiscriminatorType.STRING,
columnDefinition = "VARCHAR(15) DEFAULT 'ant'")
@Table(name = "RegisteredServiceImpl")
public abstract class AbstractRegisteredService
implements RegisteredService, Comparable<RegisteredService>, Serializable {
Expand Down Expand Up @@ -201,29 +205,28 @@ public int getEvaluationOrder() {

public Object clone() throws CloneNotSupportedException {
final AbstractRegisteredService clone = newInstance();
copyTo(clone);
clone.copyFrom(this);
return clone;
}

/**
* Copies the attributes of this instance onto the target service.
* Note the {@link #id} field is not copied.
* Copies the properties of the source service into this instance.
*
* @param target Target service to copy properties to.
* @param source Source service from which to copy properties.
*/
public void copyTo(final AbstractRegisteredService target) {
target.setAllowedAttributes(this.allowedAttributes);
target.setAllowedToProxy(this.allowedToProxy);
target.setDescription(this.description);
target.setEnabled(this.enabled);
target.setId(this.id);
target.setName(this.name);
target.setServiceId(this.serviceId);
target.setSsoEnabled(this.ssoEnabled);
target.setTheme(this.theme);
target.setAnonymousAccess(this.anonymousAccess);
target.setIgnoreAttributes(this.ignoreAttributes);
target.setEvaluationOrder(this.evaluationOrder);
public void copyFrom(final RegisteredService source) {
this.setId(source.getId());
this.setAllowedAttributes(new ArrayList<String>(source.getAllowedAttributes()));
this.setAllowedToProxy(source.isAllowedToProxy());
this.setDescription(source.getDescription());
this.setEnabled(source.isEnabled());
this.setName(source.getName());
this.setServiceId(source.getServiceId());
this.setSsoEnabled(source.isSsoEnabled());
this.setTheme(source.getTheme());
this.setAnonymousAccess(source.isAnonymousAccess());
this.setIgnoreAttributes(source.isIgnoreAttributes());
this.setEvaluationOrder(source.getEvaluationOrder());
}

public int compareTo(final RegisteredService other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/
package org.jasig.cas.services;

import org.hibernate.annotations.DiscriminatorFormula;
import org.jasig.cas.authentication.principal.Service;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

/**
Expand All @@ -21,7 +21,7 @@
* @since 3.1
*/
@Entity
@DiscriminatorFormula("expression_type is null")
@DiscriminatorValue("ant")
public class RegisteredServiceImpl extends AbstractRegisteredService {

/** Unique Id for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ protected final void initBinder(final HttpServletRequest request,
protected final ModelAndView onSubmit(final HttpServletRequest request,
final HttpServletResponse response, final Object command,
final BindException errors) throws Exception {
AbstractRegisteredService service = (AbstractRegisteredService) command;
RegisteredService service = (RegisteredService) command;

// only change object class if there isn't an explicit RegisteredService class set
if (this.getCommandClass() == null) {
// CAS-1071
// Treat _new_ patterns starting with ^ character as a regular expression
if (service.getId() < 0 && service.getServiceId().startsWith("^")) {
logger.debug("Detected regular expression starting with ^");
final RegexRegisteredService regexService = new RegexRegisteredService();
service.copyTo(regexService);
regexService.copyFrom(service);
service = regexService;
}
}
Expand All @@ -100,12 +101,15 @@ protected Object formBackingObject(final HttpServletRequest request)
final String id = request.getParameter("id");

if (!StringUtils.hasText(id)) {
logger.debug("Created new service.");
// create a default RegisteredServiceImpl object if an explicit class isn't set
if (this.getCommandClass() == null) {
return new RegisteredServiceImpl();
final Object service;
if (this.getCommandClass() != null) {
service = this.createCommand();
} else {
service = new RegisteredServiceImpl();
}
return this.createCommand();
logger.debug("Created new service of type " + service.getClass().getName());
return service;
}

final RegisteredService service = this.servicesManager.findServiceBy(Long.parseLong(id));
Expand Down
2 changes: 2 additions & 0 deletions cas-server-core/src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="CasPersistence" transaction-type="RESOURCE_LOCAL">
<class>org.jasig.cas.services.AbstractRegisteredService</class>
<class>org.jasig.cas.services.RegexRegisteredService</class>
<class>org.jasig.cas.services.RegisteredServiceImpl</class>
<class>org.jasig.cas.ticket.TicketGrantingTicketImpl</class>
<class>org.jasig.cas.ticket.ServiceTicketImpl</class>
Expand Down
2 changes: 2 additions & 0 deletions cas-server-webapp/src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
version="2.0">

<persistence-unit name="CasPersistence" transaction-type="RESOURCE_LOCAL">
<class>org.jasig.cas.services.AbstractRegisteredService</class>
<class>org.jasig.cas.services.RegexRegisteredService</class>
<class>org.jasig.cas.services.RegisteredServiceImpl</class>
<class>org.jasig.cas.ticket.TicketGrantingTicketImpl</class>
<class>org.jasig.cas.ticket.ServiceTicketImpl</class>
Expand Down

0 comments on commit c2a78d5

Please sign in to comment.