当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Tyr.byUid方法代码示例

本文整理汇总了TypeScript中tyranid.Tyr.byUid方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Tyr.byUid方法的具体用法?TypeScript Tyr.byUid怎么用?TypeScript Tyr.byUid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tyranid.Tyr的用法示例。


在下文中一共展示了Tyr.byUid方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: resolveSubjectAndResourceDocuments

async function resolveSubjectAndResourceDocuments(
  resourceData: Tyr.Document | string,
  subjectData: Tyr.Document | string
) {
  const resourceDocument =
    typeof resourceData === 'string'
      ? await Tyr.byUid(resourceData)
      : resourceData;

  if (!resourceDocument) {
    throw new Error(
      `No resourceDocument resolvable given: ${JSON.stringify(resourceData)}`
    );
  }

  const subjectDocument =
    typeof subjectData === 'string'
      ? await Tyr.byUid(subjectData)
      : subjectData;

  if (!subjectDocument) {
    throw new Error(
      `No subjectDocument resolvable given: ${JSON.stringify(subjectData)}`
    );
  }

  return {
    resourceDocument,
    subjectDocument
  };
}
开发者ID:CrossLead,项目名称:tyranid-gracl,代码行数:31,代码来源:PermissionsModel.ts

示例2: determineAccess

  /**
   * Determine access to multiple permissions simultaneously
   */
  public static async determineAccess(
    resourceData: Tyr.Document | string,
    permissionsToCheck: string | string[],
    subjectData: Tyr.Document | string
  ): Promise<Hash<boolean>> {
    const plugin = PermissionsModel.getGraclPlugin();

    if (typeof subjectData === 'string') {
      validate(plugin, subjectData);
      subjectData = (await Tyr.byUid(subjectData)) as Tyr.Document;
    }

    if (!Array.isArray(permissionsToCheck)) {
      permissionsToCheck = [permissionsToCheck];
    }

    const resourceUid: string =
      typeof resourceData === 'string' ? resourceData : resourceData.$uid;

    const result = await PermissionsModel.determineAccesstoAllPermissions(
      subjectData,
      permissionsToCheck,
      [resourceUid]
    );

    return result[resourceUid];
  }
开发者ID:CrossLead,项目名称:tyranid-gracl,代码行数:30,代码来源:PermissionsModel.ts

示例3: updatePermissions

  /**
   * Create/update a permission edge document to
   * set a permission between a given resource and subject
   */
  public static async updatePermissions(
    resourceDocument: Tyr.Document | string,
    permissionChanges: { [key: string]: boolean },
    subjectDocument: Tyr.Document | string,
    attempt = 0
  ) {
    const plugin = PermissionsModel.getGraclPlugin();

    const $set: { [key: string]: boolean } = {};

    _.each(permissionChanges, (access, permissionType) => {
      if (permissionType) {
        validatePermissionExists(plugin, permissionType);
        $set[`access.${permissionType}`] = access;
      }
    });

    if (!resourceDocument) {
      throw new TypeError(`no resource given to updatePermissions`);
    }
    if (!subjectDocument) {
      throw new TypeError(`no subject given to updatePermissions`);
    }

    const resourceComponents = extractIdAndModel(plugin, resourceDocument);
    const subjectComponents = extractIdAndModel(plugin, subjectDocument);

    validateAsResource(plugin, resourceComponents.$model);

    // set the permission
    try {
      await PermissionsModel.db.findOneAndUpdate(
        {
          subjectId: subjectComponents.$uid,
          resourceId: resourceComponents.$uid
        },
        {
          $setOnInsert: {
            subjectId: subjectComponents.$uid,
            resourceId: resourceComponents.$uid,
            subjectType: subjectComponents.$model.def.name,
            resourceType: resourceComponents.$model.def.name
          },
          $set
        },
        { upsert: true }
      );
    } catch (error) {
      // hack for https://jira.mongodb.org/browse/SERVER-14322
      if (attempt < 10 && /E11000 duplicate key error/.test(error.message)) {
        return (await new Promise<Tyr.Document | null>((resolve, reject) => {
          setTimeout(() => {
            PermissionsModel.updatePermissions(
              resourceDocument,
              permissionChanges,
              subjectDocument,
              attempt++
            )
              .then(resolve)
              .catch(reject);
          }, 100);
        })) as Tyr.Document;
      } else if (/E11000 duplicate key error/.test(error.message)) {
        plugin.error(
          `Attempted to update permission 10 times, but recieved "E11000 duplicate key error" on each attempt`
        );
      }
      throw new Error(error);
    }

    if (typeof resourceDocument === 'string') {
      return Tyr.byUid(resourceDocument);
    } else {
      return resourceDocument;
    }
  }
开发者ID:CrossLead,项目名称:tyranid-gracl,代码行数:80,代码来源:PermissionsModel.ts


注:本文中的tyranid.Tyr.byUid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。