<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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.
-->

<div ng-controller="AccessCtrl">
    <div class="space-4"></div>
    <div class="pull-right">
        <button class="btn btn-primary btn-xs" ng-click="renewAccess(entity)" ng-if="userService.hasRole('ROLE_ADMIN') || hasPermission('project',entity, permissions.ADMINISTRATION.mask) && !newAccess"><i
                class="fa fa-plus"></i> Grant
        </button>
        <button class="btn btn-primary btn-xs" ng-click="resetNewAcess()" ng-if="newAccess"><i
                class="fa fa-minus"></i> Cancel
        </button>
        <label tooltip-placement="left" tooltip-html-unsafe="{{accessTooltip}}"><i class="fa fa-question-circle" style="font-size: 16px"></i></label>
    </div>
    <div class="clearfix"></div>
    <div class="space-4"></div>

    <div ng-if="newAccess">
        <table class="table table-striped">
            <tr>
                <td style="width: 20%">
                    <label><b>Type&nbsp;</b> </label>
                    <input type="radio" ng-model="newAccess.principal" ng-value=true />  <span tooltip="Username">User</span>
                    <input type="radio" ng-model="newAccess.principal" ng-value=false /> <span tooltip="User Role">Role</span>
                </td>
                <td style="width: 40%" >
                    <label><b>Name&nbsp;</b> </label>
                    <input ng-model="newAccess.sid" ng-if="newAccess.principal==true" placeholder=" User NT Account..." style="width: 80%" />

                    <select chosen ng-model="newAccess.sid" ng-if="newAccess.principal==false" style="width: 80%"
                            ng-options="authority as authority for authority in authorities">
                            <option value=""></option>
                            </select>
                </td>
                <td >
                    <label><b>Permission&nbsp;</b> </label>
                    <select ng-model="newAccess.permission" ng-init="newAccess.permission=permissions.READ.value"
                            ng-options="permission.value as permission.name for (name, permission) in permissionsSorted">
                            <option value=""></option>
                            </select>
                </td>
                <td>
                    <button class="btn btn-default btn-xs" ng-click="grant(type, entity, newAccess)"
                            ng-disabled="!newAccess.sid || !newAccess.permission">Grant
                    </button>
                </td>
            </tr>
        </table>
    </div>

    <div ng-if="!entity.accessLoading && (!entity.accessEntities || entity.accessEntities.length == 0)" no-result></div>
    <loading ng-if="entity.accessLoading" text="Loading Access..."></loading>

    <table ng-show="entity.accessEntities" class="table table-hover table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Type</th>
                <th>Access</th>
                <th ng-if="userService.hasRole('ROLE_ADMIN') || hasPermission('project',entity, permissions.ADMINISTRATION.mask)">Update</th>
                <th ng-if="userService.hasRole('ROLE_ADMIN') || hasPermission('project',entity, permissions.ADMINISTRATION.mask)">Revoke</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="accessEntity in entity.accessEntities">
                <td>
                    {{accessEntity.sid.principal}}
                    {{accessEntity.sid.grantedAuthority}}
                </td>
                <td>
                    <span ng-if="accessEntity.sid.principal" >User</span>
                    <span ng-if="accessEntity.sid.grantedAuthority">Role</span>
                </td>
                <td>
                  <span ng-if="accessEntity.permission.mask == permissions.READ.mask">QUERY</span>
                  <span ng-if="accessEntity.permission.mask == permissions.OPERATION.mask">OPERATION</span>
                  <span ng-if="accessEntity.permission.mask == permissions.MANAGEMENT.mask">MANAGEMENT</span>
                  <span ng-if="accessEntity.permission.mask == permissions.ADMINISTRATION.mask">ADMIN</span>
                </td>
                <td ng-if="userService.hasRole('ROLE_ADMIN') || hasPermission('project',entity, permissions.ADMINISTRATION.mask)">
                    <select ng-model="accessEntity.newPermission"  ng-init="newAccess.permission=permissions.READ.value"
                            ng-options="permission.value as permission.name for (name, permission) in permissionsSorted">
                        <option value="">-- select access --</option>
                    </select>
                    <button class="btn btn-default btn-xs" ng-disabled="!accessEntity.newPermission"
                            ng-click="update(type, entity, accessEntity, accessEntity.newPermission)">Update
                    </button>
                </td>
                <td ng-if="userService.hasRole('ROLE_ADMIN') || hasPermission('project',entity, permissions.ADMINISTRATION.mask)">
                    <button class="btn btn-default btn-xs" ng-click="revoke(type, accessEntity, entity)">Revoke</button>
                </td>
            </tr>
        </tbody>
    </table>
</div>
