@model Disco.Web.Models.User.ShowModel
@using Disco.Services.Authorization.Roles;
@{
    Authorization.Require(Claims.User.ShowAuthorization);

    var isDiscoAdmin = Authorization.Has(Claims.DiscoAdminAccount);

    Html.BundleDeferred("~/Style/Fancytree");
    Html.BundleDeferred("~/ClientScripts/Modules/jQuery-Fancytree");
}
<div id="UserDetailTab-Authorization" class="UserPart">
    <div id="UserDetailTab-AuthorizationContainer" class="clearfix">

        @if (Model.ClaimNavigator != null)
        {
            <div id="UserDetailTab-Authorization_ClaimsTree_Container">
                <h3>Effective Permissions</h3>
                <div id="UserDetailTab-Authorization_ClaimsTree">
                </div>
                <script>
                    (function(){
                        var claimNodes = @(new HtmlString(Newtonsoft.Json.JsonConvert.SerializeObject(Model.ClaimNavigatorFancyTreeNodes)));

                        $(function(){
                            var tree = $('#UserDetailTab-Authorization_ClaimsTree').fancytree({
                                source: claimNodes,
                                checkbox: false,
                                selectMode: 3,
                                keyboard: false
                            });
                        });
                    })();
                </script>
                <span class="smallMessage">Calculated from authorization roles</span>
            </div>
        }
        else
        {
            <div id="UserDetailTab-Authorization_NoAccess">
                <h3>User Not Authorized</h3>
                <p>The user (<span class="code">@(Model.User.UserId)</span>) is not authorized to access any authenticated components.</p>
                @if (isDiscoAdmin)
                {
                    @Html.ActionLinkButton("Configure Authorization Roles", MVC.Config.AuthorizationRole.Index(null), null, "small")
                }
            </div>
        }
        <div id="UserDetailTab-Authorization_Membership">
            @if (Model.ClaimNavigator != null)
            {
                <h3>Associated Roles</h3>
                <ul id="UserDetailTab-Authorization_Membership_Roles">
                    @foreach (var roleToken in Model.AuthorizationToken.RoleTokens.Cast<RoleToken>())
                    {
                        <li>@if (roleToken.Role.Id < 0)
                            {
                            <text>@roleToken.Role.Name <span class="code">[SYSTEM ROLE]</span></text>
                            }
                            else
                            {
                                if (isDiscoAdmin)
                                {
                            @Html.ActionLink(roleToken.Role.Name, MVC.Config.AuthorizationRole.Index(roleToken.Role.Id))
                                }
                                else
                                {
                            @roleToken.Role.Name
                                }
                            }
                        </li>
                    }
                </ul>
            }
            <div id="UserDetailTab-Authorization_Membership_Groups_Container">
                <h4>Associated Active Directory Security Groups</h4>
                @if (Model.AuthorizationToken != null)
                {
                    <ul id="UserDetailTab-Authorization_Membership_Groups">
                        @foreach (var group in Model.AuthorizationToken.GroupMembership)
                        {
                            <li>@group</li>
                        }
                    </ul>
                    <span class="smallMessage">[Note: Includes inherited groups]</span>
                }
                else
                {
                    <h5>&lt;None&gt;</h5>
                    <span class="smallMessage">[Note: Not found in Active Directory]</span>    
                }
            </div>
        </div>


    </div>
    <script>
        $('#UserDetailTabItems').append('<li><a href="#UserDetailTab-Authorization">Authorization</a></li>');
    </script>
</div>
