Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller getForm runs before middleware #2710

Closed
kfina-planxy opened this issue Dec 12, 2024 · 1 comment
Closed

Controller getForm runs before middleware #2710

kfina-planxy opened this issue Dec 12, 2024 · 1 comment

Comments

@kfina-planxy
Copy link

kfina-planxy commented Dec 12, 2024

Description

The ModuleController method getForm() runs before any middleware is run.

Why is that a problem?

It makes it impossible to modify the form based on user.

// example 1: Not every field available to every user
if (Auth::user()->hasPermissionTo('admin'))
  $form-add(Input::make()->name('admin_only_field'));
// example 2: Select options based on user
$form->add(Select::make()->name('posts')->options(Options::make([ Auth::user()->posts->map(fn($p) => Option::make([$p->id, $p->title])) ])));

Both of these do not work, because Auth::user() is not available (yet).

Steps to reproduce

add dd(Auth::id()) to the getForm method

Expected result

I get a dd with the value 1 (id of the current user)

Actual result

I get a dd with the value null

Versions

Twill version: 3.3.1
Laravel version: 11.0
PHP version: 8.2
Database engine: MySQL (Xampp)

Note

I would also be content with some kind of hack to achieve this behavior, but I couldn't figure out which vendor files to overwrite for that.

@kfina-planxy
Copy link
Author

Workaround-solution for googlers

Copy file vendor/area17/twill/src/Http/Controllers/Admin/ModuleController.php to outside vendor, I did it into the folder
/vendor-override/twill/ModuleController.php.

Then modify the file in the vendor-override folder in the following way.
Inside the constructor, move the getForm() and getSideFieldsets() methods inside the authentication middleware.

modify composer.json:

{
  ...
  "autoload": {
    "exclude-from-classmap": [ "vendor/area17/twill/src/Http/Controllers/Admin/ModuleController.php" ],
    "files": [ "vendor-override/twill/ModuleController.php" ],
  },
  ...
}

ModuleController.php changes for reference:

OLD METHOD

    public function __construct(Application $app, Request $request)
    {
        parent::__construct();
        $this->app = $app;
        $this->request = $request;

        $this->setUpController();

        $this->modelName = $this->modelName ?? $this->getModelName();
        $this->routePrefix = $this->routePrefix ?? $this->getRoutePrefix();
        $this->namespace = $this->namespace ?? $this->getNamespace();
        $this->repository = $this->repository ?? $this->getRepository();
        $this->viewPrefix = $this->viewPrefix ?? $this->getViewPrefix();
        $this->modelTitle = $this->modelTitle ?? $this->getModelTitle();
        $this->labels = array_merge($this->defaultLabels, $this->labels);
        $this->middleware(function ($request, $next) {
            $this->user = auth('twill_users')->user();

            return $next($request);
        });

        if (!$this instanceof AppSettingsController) {
            $this->getForm($this->repository->getBaseModel())->registerDynamicRepeaters();
            $this->getSideFieldsets($this->repository->getBaseModel())->registerDynamicRepeaters();
        }

        // When no searchColumns are set we default to the title column key.
        if ($this->searchColumns === null) {
            $this->searchColumns = [$this->titleColumnKey];
        }
    }

NEW METHOD

    public function __construct(Application $app, Request $request)
    {
        parent::__construct();
        $this->app = $app;
        $this->request = $request;

        $this->setUpController();

        $this->modelName = $this->modelName ?? $this->getModelName();
        $this->routePrefix = $this->routePrefix ?? $this->getRoutePrefix();
        $this->namespace = $this->namespace ?? $this->getNamespace();
        $this->repository = $this->repository ?? $this->getRepository();
        $this->viewPrefix = $this->viewPrefix ?? $this->getViewPrefix();
        $this->modelTitle = $this->modelTitle ?? $this->getModelTitle();
        $this->labels = array_merge($this->defaultLabels, $this->labels);
        $this->middleware(function ($request, $next) {
            $this->user = auth('twill_users')->user();

            // THIS PART GOT MOVED INSIDE
            if (!$this instanceof AppSettingsController) {
                $this->getForm($this->repository->getBaseModel())->registerDynamicRepeaters();
                $this->getSideFieldsets($this->repository->getBaseModel())->registerDynamicRepeaters();
            }

            return $next($request);
        });

        // When no searchColumns are set we default to the title column key.
        if ($this->searchColumns === null) {
            $this->searchColumns = [$this->titleColumnKey];
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant