<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform 
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:query="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.XdsQueryProcessor" 
	xmlns:cmd="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.XdsCommandProcessor"
	exclude-result-prefixes="query cmd" 
>

<!--==================-->
<!-- GLOBAL VARIABLES -->
	<xsl:variable name="STYLESHEET_NAME" select="'UseTemplate'"/>
	<xsl:variable name="TEMPLATE_DN" select="'ORG\Template'"/>  <!-- template object to use for user creations -->



<!--================================================================================
  UseTemplate

        Contact: Tim Bailen
                 tmbailen@hotmail.com 
                 XNS: =TimBailen
                  
        Purpose: Add attributes specified by a template when creating new accounts.
                 The ability to exclude certain attributes from being copied is
                 also included.
        
                 Why not just use the template-dn functionality of <add> commands?
                 DirXML sometimes adds the template name as an additional (non-
                 naming) CN of the created object. This is often undesired, one 
                 reason being that many matching rules do not properly take into
                 account multi-valued CNs.
        
 Rule placement: publisher create rule
            
  Chaining
   Requirements: put at the end of the chain for efficiency's sake (previous create
                 rules might veto the add)
   
   Dependencies: none
   
	DISCLAIMER: NO WARRANTIES ARE EITHER EXPRESSED OR IMPLIED, INCLUDING
	WARRANTIES OF MECHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
	USE THIS CODE AT YOUR OWN RISK.
	            
	YOU ARE HEREBY AUTHORIZED TO USE AND MODIFY THIS CODE AS YOU DESIRE, AS LONG
	THE ORIGINAL ATTRIBUTION TO TIM BAILEN REMAINS.
================================================================================-->


<!--=======================-->
<!-- STYLESHEET PARAMETERS -->
	<!-- (parameters passed to the stylesheet by DirXML) -->
	<xsl:param name="fromNDS"/>					<!-- boolean value that is true if the rule is being processed by the 
	                                                 Subscriber channel and false if the rule is being processed by the 
	                                                 Publisher channel. (used on Input and Output transform rules) -->
	<xsl:param name="srcQueryProcessor"/>		<!-- used to send queries to the channel's source directory -->
	<xsl:param name="destQueryProcessor"/>		<!-- used to send queries to the channel's destination directory -->
	<xsl:param name="srcCommandProcessor"/>		<!-- used to send arbitrary commands to the channel's source directory -->
	<xsl:param name="destCommandProcessor"/>	<!-- used to send arbitrary commands to the channel's destination directory -->


	<!-- The following lines make the output pretty when testing outside of DirXML -->
	<xsl:strip-space elements="*"/>
	<xsl:preserve-space elements="value component password check-password"/>
	<xsl:output indent="yes" method="xml"/>


<!--===========-->
<!-- TEMPLATES -->
	<xsl:template match="/">
		<xsl:message>Stylesheet: <xsl:value-of select="$STYLESHEET_NAME"/></xsl:message>
	
		<xsl:apply-templates select="node()|@*"/>
	</xsl:template>
	
	
	<!-- Identity transform -->
	<xsl:template match="node()|@*">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>
	

	<!--================================================================
	  Add template specified attributes to the <add> document.
	  (The template to use is specified by the TEMPLATE_DN variable at
	  the top of the stylesheet.)
	  ================================================================-->
	<xsl:template match="add[@class-name='User']">
		<xsl:copy>
			<xsl:apply-templates select="@* | association"/>
			
			<!-- add the attributes of the template object -->
			<xsl:variable name="xdsQueryTemplate">
				<query dest-dn="{$TEMPLATE_DN}" scope="entry"/>
			</xsl:variable>
			
			<xsl:variable name="template" select="query:query($destQueryProcessor, $xdsQueryTemplate)"/>
			
			
			<xsl:for-each select="$template//instance/attr">
				<xsl:apply-templates select="." mode="templateAttrs"/>
			</xsl:for-each>
			
			
			<!-- copy the contents of the add document on through -->
			<xsl:apply-templates select="node()[name() != 'association']"/>
		</xsl:copy>
	</xsl:template>
	
		<!-- copy template's attributes as add-attr's to the result document 
		     unless a more specific (e.g. exclusion) template matches -->
		<xsl:template match="attr" mode="templateAttrs">
			<add-attr attr-name="{@attr-name}">
				<xsl:copy-of select="value"/>
			</add-attr>
		</xsl:template>
		
		<!-- emulate DirXML / ConsoleOne behavior by setting the "Login Grace Remaining"
		     to be equal to the template's "Login Grace Limit" -->
		<xsl:template match="attr[@attr-name='Login Grace Limit']" mode="templateAttrs">
			<add-attr attr-name="{@attr-name}">
				<xsl:copy-of select="value"/>
			</add-attr>
	
			<add-attr attr-name="Login Grace Remaining">
				<xsl:copy-of select="value"/>
			</add-attr>
		</xsl:template>
		
		<!-- don't copy these attributes -->
		<xsl:template match="attr[@attr-name='Object Class']" mode="templateAttrs"/>         <!-- definitely don't want to copy these... -->
		<xsl:template match="attr[@attr-name='Members Of Template']" mode="templateAttrs"/>
		<xsl:template match="attr[@attr-name='Revision']" mode="templateAttrs"/>
		<xsl:template match="attr[@attr-name='GUID']" mode="templateAttrs"/>
		<xsl:template match="attr[@attr-name='creatorsName']" mode="templateAttrs"/>
		<xsl:template match="attr[@attr-name='modifiersName']" mode="templateAttrs"/>
		<xsl:template match="attr[@attr-name='CN']" mode="templateAttrs"/>                   <!-- not copying this is the point of this stylesheet -->
		<xsl:template match="attr[@attr-name='Description']" mode="templateAttrs"/>          <!-- not copying this allows us to add a description
		                                                                                          to the template that will not be copied to template
		                                                                                          created accounts -->
</xsl:transform>