# |
May 19th 2021, 10:59 |
me1367 |
I wouldn't do that in the bootstrap.php because it indeed, can get messy real quick. Instead, I'd create a utility class for that and call that. Something like this: ```namespace MyApp\Utility; use Cake\Routing\Router; class CurrencyConverter { private $user = null; __construct() { // Get the authenticated user $this->user = Router::getRequest()->getSession()->read('Auth.User'); } convert(int $amount) { |
# |
May 19th 2021, 10:55 |
ndm |
Instinctively I'd say no, as it sounds like you're introducing lots of global state and hard dependencies. |
# |
May 19th 2021, 10:50 |
slackebot |
site to run (it handles currency conversion based on each logged in or not logged in user) - so I thought bootstrap can be a good place for that, but I'm not sure if it's ok in terms of best practices |
# |
May 19th 2021, 10:50 |
khalil |
Hello support! A quick question related to best practices, is it ok to add a global function that needs to be accessed anywhere in the app (models, views and controllers) in config/bootstrap.php ? and is it ok if this function needs to access the authenticated user? I know I can create helpers and components for, but it gets easily messy since this needs to be accessed literally everywhere, and this function is required for the |
# |
May 19th 2021, 10:45 |
neon1024 |
I put a debug backtrace into the table initialize, and there isn’t a single controller there |
# |
May 19th 2021, 10:41 |
neon1024 |
Thanks for the lead there @ndm I think you’re right. As the Auth is kicking things off from the Middleware, no controllers are called! |
# |
May 19th 2021, 10:41 |
kevin.pfeifer |
i see |
# |
May 19th 2021, 10:41 |
steinkel |
yes |
# |
May 19th 2021, 10:41 |
kevin.pfeifer |
and you load/require that events.php in the bootstrap.php or somewhere else? |
# |
May 19th 2021, 10:40 |
steinkel |
^ I usually define a specific config/events.php and add docblock per event with links to the dispatch line in the code, the reason of this event, any data in/out expected, exceptions raised etc |
# |
May 19th 2021, 10:37 |
kevin.pfeifer |
Talking about Event Listeners: Wouldn’t it suffice to put all the Event Listeners in the `config/bootstrap.php` (or at least put some logic in there to load all of them in there)? In other words: Is there a distinct “recommended place” where to put Event Listeners in general? Or is the answer “it depends” ,:) |
# |
May 19th 2021, 10:24 |
me1367 |
I'll have a look at it, thanks |
# |
May 19th 2021, 10:24 |
ndm |
I think it was this plugin: https://github.com/liqueurdetoile/cakephp-orm-json |
# |
May 19th 2021, 10:23 |
ndm |
Yeah, no, it's going to be a bit work :) |
# |
May 19th 2021, 10:20 |
me1367 |
Yea, just wondered if the ORM was able to do this all for me or not. Would make it a lot easier since now I have a bunch of data in different tables to keep said tables smol (column wise). |
# |
May 19th 2021, 10:19 |
ndm |
I remember seeing a plugin that supports generating all sorts of JSON Path syntax, but IIRC everything that the path syntax can do, can be done with the equivalent function calls too. |
# |
May 19th 2021, 10:17 |
me1367 |
r.i.p |
# |
May 19th 2021, 10:17 |
neon1024 |
Good point, my `auth` finder does use some of these |
# |
May 19th 2021, 10:17 |
ndm |
@me1367 JSON functions can be generated like any other methods via the functions builder... aside from that, there's no specific JSON support. |
# |
May 19th 2021, 10:13 |
ndm |
@neon1024 Maybe whatever models are involved, are being constructed earlier, maybe in your policies, and/or for resolving identities... Put a debug or a breakpoint in their `initialize()` method to figure whether initialization happens before the listener is registered. |
# |
May 19th 2021, 10:11 |
me1367 |
Quick question... Does CakePHP's ORM have support for this feature in MariaDB? https://mariadb.com/database-topics/semi-structured-data/ |
# |
May 19th 2021, 10:11 |
neon1024 |
As we deployed the 4.0.10 upgrade to prod already and it’s fine |
# |
May 19th 2021, 10:10 |
neon1024 |
It worked fine until I added the Authorization plugin |
# |
May 19th 2021, 10:10 |
neon1024 |
It’s the first thing in the method |
# |
May 19th 2021, 10:10 |
neon1024 |
I am using `\App\Controller\Admin\AppController::initialize` |
# |
May 19th 2021, 10:10 |
ndm |
Might depend on when you register that listener, the event will only be triggered once for each table alias when its constructed, so if you register it too late, it won't be invoked. |
# |
May 19th 2021, 10:07 |
neon1024 |
I added a `pr()` to the `setCurrentOrganisation` method to see how often it was called. Unless Crud doesn’t instantiate the table classes any more :thinking_face: |
# |
May 19th 2021, 10:06 |
neon1024 |
The next thing.. always another.. is that `EventManager::instance()->on('Model.initialize', [$this, 'setCurrentOrganisation']);` doesn’t seem to be called for any Table classes any more except `Requests` and `Panels` from DebugKit. |
# |
May 19th 2021, 10:05 |
neon1024 |
Yes, super, thanks very much @kevin.pfeifer |
# |
May 19th 2021, 09:40 |
kevin.pfeifer |
it should then redirect without the need to extend the middleware and use your own try catch block |
# |
May 19th 2021, 09:30 |
neon1024 |
:thumbsup: |
# |
May 19th 2021, 09:22 |
kevin.pfeifer |
as stated in https://book.cakephp.org/authorization/2/en/middleware.html#handling-unauthorized-requests |
# |
May 19th 2021, 09:22 |
slackebot |
],``` |
# |
May 19th 2021, 09:22 |
kevin.pfeifer |
I just checked your screenshot again and it seems you misplaced the `exceptions` array. It should be inside the `unatuhorizedHandler` array like that ``` 'unauthorizedHandler' => [ 'className' => 'Authorization.Redirect', 'url' => '/users/login', 'queryParam' => 'redirectUrl', 'exceptions' => [ MissingIdentityException::class, OtherException::class, ], |
# |
May 19th 2021, 09:13 |
neon1024 |
Dirty, but it works |
# |
May 19th 2021, 08:59 |
neon1024 |
Well I have a meeting. Thanks for the help though, really appreciated it. This is the start of Day 2 trying to get this Authorization plugin working ,:) |
# |
May 19th 2021, 08:58 |
neon1024 |
I tried catching the instantiation of the service also, but as it’s middleware, you can’t |
# |
May 19th 2021, 08:57 |
neon1024 |
Otherwise I guess we can’t use the Authorization plugin as it basically doesn’t work |
# |
May 19th 2021, 08:57 |
neon1024 |
That’s why I thought I’d just extend the class and I can catch that exception myself |
# |
May 19th 2021, 08:56 |
neon1024 |
Ah a short class name is no help `use Authorization\Exception\ForbiddenException;` |
# |
May 19th 2021, 08:56 |
neon1024 |
Looks the same |