private async Task <RolePermissionCacheItem> GetRolePermissionCacheItemAsync(string roleId, string tenantId) { var cacheKey = roleId + "@" + (!string.IsNullOrEmpty(tenantId) ? tenantId : "default"); return(await cacheManager.GetRolePermissionCache().GetCancelableAsync(cacheKey, async(cancellationToken) => { var cacheItem = new RolePermissionCacheItem(roleId); var normalizedRoleId = KeyNormalizer.NormalizeName(roleId); var staticRole = options.Role.StaticRoles.FirstOrDefault(c => string.Equals(c.Id, normalizedRoleId, StringComparison.OrdinalIgnoreCase)); if (staticRole != null) { foreach (var permission in permissionStore.GetAll()) { if (staticRole.IsGrantedByDefault(permission)) { cacheItem.GrantedPermissions.Add(permission.Name); } } } else { var role = await Store.FindByIdAsync(normalizedRoleId, cancellationToken); if (role == null) { throw new StructureException("There is no role with given id: " + normalizedRoleId); } } if (Store is IRolePermissionStore <TRolePermission> rolePermissionStore) { foreach (var rolePermission in await rolePermissionStore.GetPermissionsAsync(normalizedRoleId, cancellationToken)) { if (rolePermission.IsGranted) { cacheItem.GrantedPermissions.AddIfNotContains(rolePermission.PermissionName); } else { cacheItem.GrantedPermissions.Remove(rolePermission.PermissionName); } } } return cacheItem; })); }