joas8211 / payload-tenancy

Multi-tenancy plugin for Payload CMS
MIT License
150 stars 11 forks source link

2.0: Invalid field error when trying to create additional tenant manually #27

Closed jackadair closed 1 year ago

jackadair commented 1 year ago

I have succeeded in creating my user and root tenant however, when trying to create further tenants I am receiving a unauthorized error on the parent field:

ERROR (payload): ValidationError: The following field is invalid: parent

    at beforeChange (/home/node/app/node_modules/payload/src/fields/hooks/beforeChange/index.ts:56:11)

    at processTicksAndRejections (node:internal/process/task_queues:95:5)

    at create (/home/node/app/node_modules/payload/src/collections/operations/create.ts:197:31)

    at createHandler (/home/node/app/node_modules/payload/src/collections/requestHandlers/create.ts:26:17)

Dependencies:

{
  "dependencies": {
    "@aws-sdk/client-s3": "^3.427.0",
    "@aws-sdk/lib-storage": "^3.427.0",
    "@payloadcms/bundler-webpack": "^1.0.3",
    "@payloadcms/db-mongodb": "^1.0.3",
    "@payloadcms/plugin-cloud": "^0.0.10",
    "@payloadcms/plugin-cloud-storage": "^1.0.19",
    "@payloadcms/richtext-lexical": "^0.1.5",
    "cross-env": "^7.0.3",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "payload": "^2.0.13",
    "payload-tenancy": "^2.0.0"
  },

It is also a required field so I can't create any additional tenants.

Any help appreciated!

Screencap

https://github.com/joas8211/payload-tenancy/assets/54963231/1e7cca42-6007-4421-adbd-8629f0ecda0e

joas8211 commented 1 year ago

I can't reproduce this. I need some more help to figure out what might be the problem.

The "Unauthorized" validation error triggers when selected parent is some tenant that user has no access to. Access to the tenant depends on what tenant the current user belongs to. The tenant user belongs in and all tenants under that are authorized for that user.

Normally the parent field won't list tenants that user don't have access to, because tenants collection limits read access to only documents that are authorized for the user. So it's weird that this happens at all.

jackadair commented 1 year ago

@joas8211 Thanks for taking a look, I just dropped by database to try again and can reproduce under the following conditions:

  1. Enable plugin like so:
    plugins: [
    tenancy()
    ]
  2. Create initial user manually using the Admin UI
  3. Create root tenant manually using Admin UI
  4. After creating root tenant, I receive the following error in my docker console:
    [20:22:32] WARN (payload): rollbackTransaction called when no transaction exists
    [20:22:32] ERROR (payload): NotFound: The requested resource was not found.
    at findByID (/home/node/app/node_modules/payload/src/collections/operations/findByID.ts:109:15)
     at processTicksAndRejections (node:internal/process/task_queues:95:5)
     at findByIDHandler (/home/node/app/node_modules/payload/src/collections/requestHandlers/findByID.ts:19:17)
  5. At this stage my admin UI will not load, to remedy this, I enable the path based isolation strategy with:
    plugins: [
    tenancy({
        isolationStrategy: "path"   
    )
    ]
  6. I can now see all my collections at the path based url localhost:3000/tenant-slug/admin.
  7. Try to create a child tenant which is where I encounter the previously reported validation error

Maybe there's just some setup that I've missed...I will also try to do the programmatic setup as written in the docs see if that changes anything.

joas8211 commented 1 year ago

I tried to replicate with the latest blank template + Lexical running in Docker using docker-compose.yml provided in the template, and still no luck. You have some other dependencies listed so I assume you have some project you are working on instead of trying this out in isolation. So, is it possible that your code is using transactions but not resolving them? The following warning you provided is indicating something like that.

[20:22:32] WARN (payload): rollbackTransaction called when no transaction exists
jackadair commented 1 year ago

@joas8211 Finally found my issue, it was due to my code but i'll leave this here if anyone else comes across it:

auth: {
    depth: 0,
  },

instead of:

auth: true

Had it left over from Payloads docs about creating multi-tenancy before I found your plugin.

Thanks again for taking a look!