小编Fer*_*c T的帖子

如何检查AngularJS中是否指定了指令的方法参数?

我创建了一个包含按钮的自定义指令.此按钮从'callback'属性指定的父作用域调用方法.

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title>Simple directive</title>

    <script src="js/lib/angular/angular.js"></script>

    <script type="text/javascript">
        var app = angular.module('app', []);

        app.controller('TestController', function($scope) {

            $scope.doSomething = function(param) {
                alert('Something called with: ' + param);
            }
        })

        app.directive('myDirective', function() {
            var ret = {
                restrict: 'E',
                scope: {
                    user: '@',
                    callback: '&'       // bound a function from the scope
                },
                template: '<div>Hello {{user}}<button ng-show="hasCallback()" ng-click="callback({userData: user})">Callback</button>',
                controller: function($scope) {
                    $scope.hasCallback2 = function() {
                        var t = typeof $scope.callback;
                        return t == 'function';
                    }

                    $scope.hasCallback = …
Run Code Online (Sandbox Code Playgroud)

parameters scope directive callback angularjs

39
推荐指数
2
解决办法
1万
查看次数

在 NestJS 防护中使用 CASL

在这部分文档中,并未清楚地解释所有防护用法的用例:
NestJS 文档 - 基于声明的授权

CaslAbilityFactory 为这些用例实现:

  • 管理员可以管理(创建/读取/更新/删除)所有实体
  • 用户对所有内容拥有只读访问权限
  • 用户可以更新他们的文章(article.authorId === userId)
  • 无法删除已发布的文章 (article.isPublished === true)

并仅解释了最琐碎的用例:

用户对所有内容拥有只读访问权限

它通过以下控制器方法进行了演示:

@Get()
@UseGuards(PoliciesGuard)
@checkPolicies((ability: AppAbility) => ability.can(Action.Read, Article))
findAll() {
    return this.articlesService.findAll();
}
Run Code Online (Sandbox Code Playgroud)

但我应该如何注释一个方法来检查第三个或第四个用例:

已发布的文章无法删除:
(article.isPublished === true)

@Delete()
@UseGuards(PoliciesGuard)
@checkPolicies(?????????????????????????????)
delete(@Body() article: Article) {
    return this.articlesService.delete(article.id);
}
Run Code Online (Sandbox Code Playgroud)

有可能吗?对于此要求,@checkPolicies 中声明的 PoliciesGuard 或处理程序应该能够访问方法参数。

如何从守卫访问控制器方法参数?

当然,如果您直接从控制器方法调用ability.can(...),则有一个解决方案:

@Delete()
@UseGuards(SomeGuards but NOT PoliciesGuard)
delete(@Body() article: Article) {
    const ability = this.caslAbilityFactory.createForUser(<<user from request>>);
    if (!ability.can(Action.Delete, article)) {
       throw new UnauthorizedException();
    }
    return this.articlesService.delete(article.id);
}
Run Code Online (Sandbox Code Playgroud)

但这个解决方案不符合原来的声明模式。

authorization nestjs casl

9
推荐指数
1
解决办法
1984
查看次数