當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。