通过动态Cognito用户组的AWS Amplify GraphQL过滤器

时间:2019-01-08 08:00:11

标签: graphql aws-appsync aws-amplify

给出以下AWS Amplify GraphQL架构(schema.graphql):

type Organization
  @model
  @auth(rules: [
    { allow: groups, groups: ["Full-Access-Admin"], mutations: [create, update, delete], queries: [list, get] },
    { allow: owner },
    { allow: groups, groupsField: "orgAdminsCognitoGroup", mutations: null, queries: [list, get] }
  ]) {
    id: ID!
    name: String!
    address: String!
    industry: [String]!
    owner: String
    orgAdminsCognitoGroup: String
  }

我可以通过以下方法过滤掉除属于当前身份验证用户的组织之外的所有组织:

        res = await API.graphql(graphqlOperation(listOrganizations, {
            // todo: filter by owner OR by is org admin
            filter: {
                owner: {
                    eq: this.props.currentUser.username
                }
            }
        }));

但是反正还有orgAdminsCognitGroup进行过滤吗?@model是Cognito中属于该组织的动态组?尝试使用其他@auth来帮助保护所有实体的private void Button_Clicked(object sender, EventArgs e) { string pausefunctionString = @" var videos = document.querySelectorAll('video'); [].forEach.call(videos, function(video) { video.pause(); }); "; MyWebView.Eval(pausefunctionString); } 规则并没有取得任何成功。

1 个答案:

答案 0 :(得分:1)

那么,问题是要过滤用户是其所有者或在“ orgAdminsCognitoGroup”中的组?

我认为这是有可能的,尽管我认为最好的方法不是您的想法。相反,我可能建议您设置一个响应映射模板,为您执行一些服务器端筛选。

具体来说,您首先要从当前用户的auth令牌中获取组:

#set($claimPermissions = $ctx.identity.claims.get("cognito:groups"))

然后,您可以遍历结果中的每个组织。如果有所有者是当前用户,请将其添加到响应列表。如果不是,请继续检查orgAdminsCognitoGroup。您可以通过检查$ claimPermissions是否包含该组织的orgAdmin设置的组来做到这一点。如果包含,请将其添加到响应列表。如果没有,请忽略它并继续迭代。

从理论上讲,可以使用用户登录时使用的令牌在客户端进行此操作。响应映射模板的实现方式几乎相同,用户所在的组位于令牌内部。如果您将其打开并拉出组,则可以在其中进行过滤。尽管出于安全考虑,我建议不要这样做。