有权限不在按钮上工作

has-authority not working on a button

所以我使用 JHipster 来生成我的应用程序。

我可以使用 show/hide 菜单的 has-authority 指令看到导航栏。

现在我要做的是使用按钮上的指令仅向 ROLE_ADMIN

的用户显示

这是指令的代码

.directive('hasAuthority', ['Principal', function (Principal) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var setVisible = function () {
                    element.removeClass('hidden');
                },
                setHidden = function () {
                    element.addClass('hidden');
                },
                defineVisibility = function (reset) {

                    if (reset) {
                        setVisible();
                    }

                    Principal.hasAuthority(authority)
                        .then(function (result) {
                            if (result) {
                                setVisible();
                            } else {
                                setHidden();
                            }
                        });
                },
                authority = attrs.hasAuthority.replace(/\s+/g, '');

            if (authority.length > 0) {
                defineVisibility(true);

                scope.$watch(function(scope) {
                    return Principal.isAuthenticated();
                }, function(newValue) {
                    defineVisibility(true);
                });
            }
        }
    };
}]);

这里是它工作的地方

<li ng-class="{active: $state.includes('admin')}"  ng-switch-when="true" has-authority="ROLE_ADMIN" class="dropdown pointer">

这是我希望它工作的地方

<table class="jh-table table table-striped">
    <thead>
        ...
    </thead>
    <tbody>
        <tr
            ng-repeat="offeredService in travelRequest.offeredServices track by offeredService.id">
            <td><a
                ui-sref="offeredServiceType.detail({id:offeredService.offeredServiceType.id})">{{offeredService.offeredServiceType.name}}</a>
            </td>
            <td>{{offeredService.sellingPrice}}
                {{offeredService.currency.symbol}}</td>
            <td>{{offeredService.cost}}
                {{offeredService.currency.symbol}}</td>
            <td>{{offeredService.confirmationDate | date:'medium'}}</td>
            <td><a
                ui-sref="serviceProvider.detail({id:offeredService.serviceProvider.id})">{{offeredService.serviceProvider.name}}</a>
            </td>
            <td class="text-right">
                <div class="btn-group flex-btn-group-container">
                    <button type="submit"
                        ui-sref="offeredService.detail({id:offeredService.id})"
                        class="btn btn-info btn-sm">
                        <span class="glyphicon glyphicon-eye-open"></span> <span
                            class="hidden-xs hidden-sm" translate="entity.action.view"></span>
                    </button>
                    <button type="submit"
                        ui-sref="offeredService.edit({id:offeredService.id})"
                        class="btn btn-primary btn-sm">
                        <span class="glyphicon glyphicon-pencil"></span> <span
                            class="hidden-xs hidden-sm" translate="entity.action.edit"></span>
                    </button>
                    <button has-authority="ROLE_USER" type="submit"
                        ui-sref="offeredService.delete({id:offeredService.id})"
                        class="btn btn-danger btn-sm">
                        <span class="glyphicon glyphicon-remove-circle"></span> <span
                            class="hidden-xs hidden-sm" translate="entity.action.delete"></span>
                    </button>
                </div>
            </td>
        </tr>
    </tbody>
</table>

我觉得你的条件不对:

<button has-authority="ROLE_USER" type="submit"
                        ui-sref="offeredService.delete({id:offeredService.id})"
                        class="btn btn-danger btn-sm">

管理员用户同时拥有 ROLE_USER 和 ROLE_ADMIN 权限。

因此在您的情况下,按钮将始终显示。 不应该像下面这样吗?

<button has-authority="ROLE_ADMIN" type="submit"