Lab 2.6: Implementing a Permission Check
Time Estimates for Completion
Allow job execution only under certain conditions.
Description:
5 minutes
Lab exercise:
15 minutes
Description
A permission check may be specified in the job configuration to allow the job execution only under certain conditions. A permission check must implement the interface IPermissionCheck:
checkJobExecutionAllowed(WMElement wmElement) throws WMException — This method is called to check whether job execution is allowed for the specified wmElement.
Available permission check implementation:
DefaultPermissionCheck: restricts the job execution depending on the element's state. States for which job execution is allowed are configured with the XML tag AllowedInState.
Lab Exercise
In this lab we will add a permission check to allow the Simple Model Geometry Check on models, not assemblies.
Prerequisites
You need the SimpleModelGeometryCheckJob.xml and SimpleModelGeometryCheckTemplate.lsp files from Lab 2.5.
Implementing a custom permission check
In order to allow the creation of Model Geometry Checks only for model parts, we implement a special permission check SimpleModelGeometryCheckPermissionCheck. (Click here for a file you can copy and paste.)
package com.acme;
import com.osm.automation.IJobPermissionCheck;
import com.osm.biz.WMElement;
import com.osm.datamgmt.biz.Model;
import com.osm.exception.WMException;

public class SimpleModelGeometryCheckJobPermissionCheck implements IJobPermissionCheck {
public void checkJobExecutionAllowed(WMElement wmElement)
throws WMException {
final boolean ok = (wmElement instanceof Model) && ((Model) wmElement).isModelPart();
if (!ok) {
throw new WMException("Permission denied");
}
}
}
Update the job's xml configuration
The permission check needs to be configured in the XML job configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Job type="modeling.simplegeometrycheck" has_result_data="true">

<DisplayName>Simple Model Geometry Check</DisplayName>

<UI>
<ActionMenu wm_java_class="com.osm.datamgmt.biz.Model" include_derived="true">
<Name>Simple Model Geometry Check</Name>
<JobGroup>Model Checks</JobGroup>
<Action java_class="com.osm.automation.action.MultiSelectCreateJobScheduleAction">
<Description>Checks a model's geometry</Description>
</Action>
</ActionMenu>
</UI>
<Exporter java_class="com.acme.SimpleModelGeometryCheckJobExporter">
<StartScriptTemplate>SimpleModelGeometryCheckTemplate.lsp</StartScriptTemplate>
<ModelLoadRule catalog="model" msg_num="728">Highest Revisions</ModelLoadRule>
</Exporter>
<ResultHandler java_class="com.osm.automation.SaveToFileSystemJobResultHandler">
<DisplayName>Save annotated model to server directory</DisplayName>
<TargetDirectory>C:\taskagent</TargetDirectory>
</ResultHandler>

<PermissionCheck java_class="com.acme.SimpleModelGeometryCheckJobPermissionCheck">
</PermissionCheck>
</Job>
Optional: Allow library parts
By default, we do not allow Simple Model Geometry Check jobs on library parts. We can add the <AllowForLibraryParts> option in the XML configuration to allow it.
  <PermissionCheck java_class="com.acme.SimpleModelGeometryCheckJobPermissionCheck">
<AllowForLibraryParts>true</AllowForLibraryParts>
</PermissionCheck>
To support this, SimpleModelGeometryCheckPermissionCheck must also implement the IXmlConfigurable interface. (Click here for a file you can copy and paste.)
package com.acme;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Element;
import com.osm.automation.IJobPermissionCheck;
import com.osm.biz.WMElement;
import com.osm.config.IXmlConfigurable;
import com.osm.datamgmt.biz.Library;
import com.osm.datamgmt.biz.Model;
import com.osm.exception.WMException;
import com.osm.util.Strings;
import com.osm.xml.DOMUtilities;

public class SimpleModelGeometryCheckJobPermissionCheck implements IJobPermissionCheck, IXmlConfigurable {
private static final Logger log = Logger.getLogger(SimpleModelGeometryCheckJobPermissionCheck.class.getName());
private static final String XML_TAG_ALLOW_FOR_LIBRARY_PARTS = "AllowForLibraryParts";
private boolean allowForLibraryParts = false;
// Interface IXmlConfigurable
public String getRootTagName() {
return XML_TAG_ALLOW_FOR_LIBRARY_PARTS;
}
public void configure(Element xmlConfigElement) {
try {
final Element element = DOMUtilities.getFirstChild(xmlConfigElement, XML_TAG_ALLOW_FOR_LIBRARY_PARTS);
final String booleanString = DOMUtilities.getText(element);
if (!Strings.isEmpty(booleanString)) {
allowForLibraryParts = Boolean.parseBoolean(booleanString);
}
} catch (Exception e) {
log.log(Level.WARNING, "Reading configuration for '" + XML_TAG_ALLOW_FOR_LIBRARY_PARTS + "' failed", e);
}
}
// Interface IJobPermissionCheck
public void checkJobExecutionAllowed(WMElement wmElement)
throws WMException {
boolean ok = wmElement instanceof Model;
ok &= ((Model) wmElement).isModelPart();
ok &= (allowForLibraryParts || !isLibraryPart(wmElement));
if (!ok) {
throw new WMException("Job execution not allowed for this model part.");
}
}
// helper methods
private boolean isLibraryPart(WMElement wmElement) {
return wmElement instanceof Library;
}
}
Was this helpful?