<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2010-2024 Evolveum and contributors
  ~
 ~ 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.
  -->
<dashboard xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
           oid="7673e10a-6ab3-4b16-b84f-6364a7b1ee8f">
    <name>role-dashboard</name>
    <description>Dashboard for showing roles for AD groups and their members.</description>
    <display>
        <label>Role dashboard</label>
    </display>
    <widget>
        <identifier>roles-ad-group</identifier>
        <display>
            <label>AD Group Roles</label>
            <color>olive</color>
            <icon>
                <cssClass>fe fe-role</cssClass>
            </icon>
        </display>
        <displayOrder>1</displayOrder>
        <data>
            <sourceType>objectCollection</sourceType>
            <collection>
                <collectionRef oid="72b1f98e-f587-4b9f-b92b-72e251dbb255" type="ObjectCollectionType"/>
            </collection>
        </data>

        <presentation>
            <dataField>
                <fieldType>value</fieldType>
                <expression>
                    <proportional>
                        <style>value-only</style>
                    </proportional>
                </expression>
            </dataField>
            <!--            <dataField>-->
            <!--                <fieldType>unit</fieldType>-->
            <!--                <expression>-->
            <!--                    <value>protected</value>-->
            <!--                </expression>-->
            <!--            </dataField>-->

            <view>
                <column>
                    <name>nameColumn</name>
                    <path>name</path>
                    <display>
                        <label>Role name</label>
                    </display>
                </column>
                <column>
                    <name>displayNameColumn</name>
                    <path>displayName</path>
                    <display>
                        <label>Role display name</label>
                    </display>
                    <previousColumn>nameColumn</previousColumn>
                </column>
                <column>
                    <name>membersColumn</name>
                    <previousColumn>displayNameColumn</previousColumn>
                    <display>
                        <label>Members</label>
                    </display>
                    <export>
                        <!--
                        <expression>
                            <script>
                                <code>
                                    import com.evolveum.midpoint.prism.query.*
                                    import com.evolveum.midpoint.xml.ns._public.common.common_3.*

                                    query = prismContext.queryFor(UserType.class).item(AssignmentHolderType.F_ROLE_MEMBERSHIP_REF).ref(object.getOid()).build()
                                    objects = midpoint.searchObjects(UserType.class, query)
                                    return objects.size()
                                </code>
                            </script>
                        </expression>
                        -->
                        <expression>
                            <script>
                                <description>Using MQL</description>
                                <code>
                                    import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType
                                    import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType

                                    import com.evolveum.midpoint.xml.ns._public.common.common_3.*

                                    RoleType role = object as RoleType

                                    // Performs query for searching of all users having this role assigned DIRECTLY or INDIRECTLY
                                    String roleOid = role.getOid()
                                    String queryStr = "roleMembershipRef matches (oid = '${roleOid}' )"

                                    def query = midpoint.queryFor(UserType.class, queryStr)
                                    def result = midpoint.searchObjects(query)
                                    return result?.size()
                                </code>
                            </script>
                        </expression>
                    </export>
                </column>
            </view>
        </presentation>
    </widget>

</dashboard>