<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2017 Evolveum
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->
<objectTemplate oid="e63e1118-cbe5-11e5-b08e-3c970e44b9e2"
   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
   xmlns='http://midpoint.evolveum.com/xml/ns/public/common/common-3'
   xmlns:c='http://midpoint.evolveum.com/xml/ns/public/common/common-3'
   xmlns:t='http://prism.evolveum.com/xml/ns/public/types-3'
   xmlns:org="http://midpoint.evolveum.com/xml/ns/public/common/org-3"
   xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
    <name>User Template</name>

<!-- TODO: figure out how to set default value customer-user -->
    <item>
        <ref>subtype</ref>
        <valueEnumerationRef oid="5783c174-d141-11e4-bb29-001e8c717e5b"/>
    </item>

    <item>
        <ref>organization</ref>
        <limitations>
            <layer>presentation</layer>
            <access>
                <read>true</read>
                <add>false</add>
                <modify>false</modify>
            </access>
        </limitations>
    </item>

    <item>
        <ref>organizationalUnit</ref>
        <limitations>
            <layer>presentation</layer>
            <access>
                <read>true</read>
                <add>false</add>
                <modify>false</modify>
            </access>
        </limitations>
    </item>
    <mapping>
        <name>Fullname</name>
        <authoritative>true</authoritative>
        <strength>strong</strength>
        <source>
            <path>givenName</path>
        </source>
        <source>
            <path>familyName</path>
        </source>
    <expression>
        <script>
            <code>basic.concatName(givenName, familyName)</code>
        </script>
    </expression>
    <target>
        <path>fullName</path>
    </target>
    </mapping>
    <mapping>
        <name>Org mapping - organization member</name>
        <description>
            Look for appropriate Org objects by using the user's organizationalUnit property
            as the name of the org object. When no such object is found we want to create it on
            demand. We want to populate new Org object with a name and displayName derived from
            the user.
        </description>
        <authoritative>true</authoritative>
        <strength>strong</strength>
        <source>
            <path>organization</path>
        </source>
        <source>
            <path>organizationalUnit</path>
        </source>
        <expression>
            <assignmentTargetSearch>
                <targetType>c:OrgType</targetType>
                <filter>
                    <q:text>
                        name = ```
                        log.info("XXXXXX USER TEMPLATE assignmentTargetSearch, organizationalUnit {}", organizationalUnit);
                        return organizationalUnit
                        ```
                    </q:text>
                </filter>
                <createOnDemand>true</createOnDemand>
                <populateObject>
                    <populateItem>
                        <expression>
                            <script>
                                <code>
                                log.info("XXXXXX USER TEMPLATE populate item organizationalUnit {}", organizationalUnit);
                                return organizationalUnit
                                </code>
                            </script>
                        </expression>
                        <target>
                            <path>name</path>
                        </target>
                    </populateItem>
                    <populateItem>
                        <expression>
                            <script>
                                <code>
                                log.info("XXXXXX USER TEMPLATE populate item organization {}", organization);
                                return organization
                                </code>
                            </script>
                        </expression>
                        <target>
                            <path>displayName</path>
                        </target>
                    </populateItem>
                    <populateItem>
                        <expression>
                            <value>customer</value>
                        </expression>
                        <target>
                            <path>subtype</path>
                        </target>
                    </populateItem>
                </populateObject>
            </assignmentTargetSearch>
        </expression>
        <target>
            <path>assignment</path>
        </target>
    </mapping>

    <mapping>
        <name>Org mapping - organization manager</name>
        <description>
            Look for appropriate Org objects by using the user's organizationalUnit property
            as the name of the org object. When no such object is found we want to create it on
            demand. We want to populate new Org object with a name and displayName derived from
            the user.
        </description>
        <authoritative>true</authoritative>
        <strength>strong</strength>
        <source>
            <path>organization</path>
        </source>
        <source>
            <path>organizationalUnit</path>
        </source>
        <source>
            <path>subtype</path>
        </source>
        <expression>
            <assignmentTargetSearch>
                <targetType>c:OrgType</targetType>
                <filter>
                    <q:text>
                        name = ```
                            log.info("XXXXXX USER TEMPLATE assignmentTargetSearch, organizationalUnit {}", organizationalUnit);
                            return organizationalUnit
                            ```
                    </q:text>
                </filter>
                <assignmentProperties>
                    <relation>org:manager</relation>
                </assignmentProperties>
            </assignmentTargetSearch>
        </expression>
        <target>
            <path>assignment</path>
        </target>
    <condition>
        <script>
            <code>subtype == 'customer-admin'</code>
        </script>
    </condition>
    </mapping>

<!-- FIXME FIXME assign once more with manager relation; do not create. Ordering???????????????? -->
    <mapping>
        <name>Basic Customer Admin role assignment</name>
        <strength>strong</strength>
        <source>
            <path>$user/subtype</path>
        </source>
        <expression>
            <assignmentTargetSearch>
                <targetType>c:RoleType</targetType>
        <oid>3410a44a-cc00-11e5-bc03-3c970e44b9e2</oid>
            </assignmentTargetSearch>
        </expression>
          <target>
            <path>assignment</path>
        </target>
    <condition>
        <script>
            <code>subtype == 'customer-admin'</code>
        </script>
    </condition>
    </mapping>

    <mapping>
        <name>Delegated Administration role assignment</name>
        <strength>strong</strength>
        <source>
            <path>$user/subtype</path>
        </source>
        <expression>
            <assignmentTargetSearch>
                <targetType>c:RoleType</targetType>
        <oid>c546fe38-def1-11e5-85bb-3c970e44b9e2</oid>
            </assignmentTargetSearch>
        </expression>
          <target>
            <path>assignment</path>
        </target>
    <condition>
        <script>
            <code>subtype == 'customer-admin'</code>
        </script>
    </condition>
    </mapping>

    <mapping>
        <name>Basic Customer User role assignment</name>
        <strength>strong</strength>
        <source>
            <path>$user/subtype</path>
        </source>
        <expression>
            <assignmentTargetSearch>
                <targetType>c:RoleType</targetType>
        <oid>0d76dd40-cc05-11e5-8cfb-3c970e44b9e2</oid>
            </assignmentTargetSearch>
        </expression>
          <target>
            <path>assignment</path>
        </target>
    <condition>
        <script>
            <code>subtype == 'customer-admin' || subtype == 'customer-user'</code>
        </script>
    </condition>
    </mapping>

    <mapping>
        <name>Customer End User role assignment</name>
        <strength>strong</strength>
        <source>
            <path>$user/subtype</path>
        </source>
        <expression>
            <assignmentTargetSearch>
                <targetType>c:RoleType</targetType>
                <filter>
                    <q:text>name = "Customer End User Role"</q:text>
                </filter>
            </assignmentTargetSearch>
        </expression>
          <target>
            <path>assignment</path>
        </target>
    <condition>
        <script>
            <code>subtype == 'customer-admin' || subtype == 'customer-user'</code>
        </script>
    </condition>
    </mapping>
</objectTemplate>
