joas8211 / payload-tenancy

Multi-tenancy plugin for Payload CMS
MIT License
122 stars 6 forks source link

Tenant field is not queried in resouce collections #15

Closed Bradleykingz closed 1 year ago

Bradleykingz commented 1 year ago

When querying resource collections, the tenant field is not returned, making it impossible for the frontend to filter documents according to the tenant.

For example, here is the "pages" resource.

image

Even querying via GraphQL leads to the same result - tenant is always null.

And, since the tenant field doesn't exist, the frontend can't filter by tenant id, and users can see each other's documents.

Adding hidden: false to the CollectionConfig seems to help, but only when querying for a single page:

image

But when querying all the pages, it remains null:

image

However, it works just fine for the user collection:

image

Any clues what the issue might be?

Bradleykingz commented 1 year ago

Changing hidden: false in resourceTenant fixes the issue.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createResourceTenantField = void 0;
/** @returns Tenant field for generic resources. */
var createResourceTenantField = function (_a) {
    var tenantCollection = _a.options.tenantCollection;
    return ({
        type: "relationship",
        name: "tenant",
        relationTo: tenantCollection,
        hidden: false, // changed this
        hooks: {
            beforeChange: [
                function (_a) {
                    var _b;
                    var req = _a.req;
                    // Assign tenant to the document when it's created (or updated).
                    var _c = req, tenant = _c.tenant, user = _c.user;
                    return (tenant === null || tenant === void 0 ? void 0 : tenant.id) || ((_b = user === null || user === void 0 ? void 0 : user.tenant) === null || _b === void 0 ? void 0 : _b.id);
                },
            ],
        },
    });
};
exports.createResourceTenantField = createResourceTenantField;

The compiled code looks different from what's in the repo. Maybe https://github.com/joas8211/payload-tenancy/commit/fc7c4f7b451dfc391efb97a7a8be4be3c14a7874 is missing from npm?

Bradleykingz commented 1 year ago

Yup. Just confirmed it. npm is behind.

@joas8211 Do you mind creating a new release? :slightly_smiling_face:

Bradleykingz commented 1 year ago

I fixed this by updating Payload, confusingly :sweat_smile: