< 072022-09-08⋎09 >

Topic for #farmOS is: farmOS (farmos.org) is a free and open source, web-based farm management system. User guide: farmos.org/guide/ - Forum: farmOS.discourse.groupSet by mstenta on 2021-06-15 at 12:52
02:57*uploaded an image: (368KiB) < libera.ems.host/_matrix/media/r0/downloa >
03:13paul121[m]Nice!!
03:13paul121[m]Next up, the cost of food being parsed from the receipt image? ;-)
03:15paul121[m]Still haven't tried out asset link myself :-( but I'm very eager to
15:53mstenta[m]@room farmOS dev call in ~6 minutes: meet.jit.si/farmos-dev
18:53symbioquine[m]I seem to be running into an issue where normal "Manager" users don't have permission to do file uploads via the API... is that expected?
18:54symbioquine[m]My code is similar to [flow #2](www.drupal.org/node/3024331).
18:54symbioquine[m]It works as the administrator user though.
18:55symbioquine[m]Error is... (full message at libera.ems.host/_matrix/media/r0/downloa)
19:05mstenta[m]Huh no that is not expected
19:05symbioquine[m]Looks like other folks might have run into something similar: drupal.stackexchange.com/questions/30111
19:07symbioquine[m]Unfortunately, it would be pretty limiting to use [flow #1](www.drupal.org/node/3024331) because it wouldn't work as part of entity creation (only updates)
19:16paul121[m]oh dang. do we not grant the `create asset` permission via managed roles? Instead we only grant the granular `create {type} asset` ?
19:16symbioquine[m]paul121[m]: I think that's correct
19:16symbioquine[m]Actually `create asset` doesn't even show up in the list of permissions for some reason...
19:19mstenta[m]Wait what would `create asset` perm be for?
19:20*uploaded an image: (4KiB) < libera.ems.host/_matrix/media/r0/downloa >
19:20paul121[m]It would be more like `create any asset`, but yeah that doesn't seem to exist
19:20mstenta[m](Sorry on my phone waiting to pick up kids)
19:20symbioquine[m]np 😁
19:20mstenta[m]But why?
19:20mstenta[m]Isnt it a file you want to create?
19:21paul121[m]> POST /jsonapi/node/article/field_image
19:21paul121[m]So they should be able to get the bundle *I think*... the POST includes entity type and bundle
19:22symbioquine[m]mstenta[m]: Yeah, it seems odd
19:22paul121[m]mstenta[m]: it's a file you want to create, but iirc each file field has the storage location configured to it. This could be different per bundle. So it kinda makes sense there is a permission check here
19:23paul121[m]otherwise people could create files in the private/public directory structure where you don't want them to? maybe?
19:23mstenta[m]Yea but wouldnt that check be for "updating own asset"?
19:23mstenta[m](In addition to allowing file creation I imagine)
19:24mstenta[m](Sorry maybe you're talking about creating an asset not updating one)
19:24symbioquine[m]Either one
19:26symbioquine[m]The nice thing about flow #2 is that you post to `/api/asset/animal/image` without needing to specify the animal asset that the file will get related to
19:26symbioquine[m]Then make a second request with the resulting `file--file` id to add the relationship to an existing `asset--animal` record, or include it in a new `asset-animal` record.
19:27symbioquine[m]The first request doesn't know how the resulting file will be used other than that it will be used in the specified relationship field.
19:31paul121[m]hmm. well I think this is the relevant source:
19:31paul121[m]git.drupalcode.org/project/drupal/-/blob
19:31paul121[m]git.drupalcode.org/project/drupal/-/blob
19:31paul121[m]it seems odd that the error says "permissions are required: .... OR ..." ... this line seems to do a `$access_result->andIf()` check, which isn't OR?
19:31paul121[m]git.drupalcode.org/project/drupal/-/blob
19:33paul121[m]I believe the `fieldAccess('edit'..)` call is what gets the `administer assets` permission. Not 100% sure tho... (full message at libera.ems.host/_matrix/media/r0/downloa)
19:34symbioquine[m]It seems like that code is trying to do a bundle-specific check: git.drupalcode.org/project/drupal/-/blob
19:34symbioquine[m]I guess I should add some logging in there and see what some of those values are...
19:38paul121[m]ah yes. but this is a base field! not a bundle field
19:38paul121[m]> // Base fields do not have target bundles.
19:38paul121[m]maybe that is part of the issue? hmm
19:52paul121[m] * I believe the `fieldAccess('edit'..)` call is what gets the `administer assets` permission. Not 100% sure tho... (full message at libera.ems.host/_matrix/media/r0/downloa)
19:52paul121[m] * ~I believe the `fieldAccess('edit'..)` call is what gets the `administer assets` permission. Not 100% sure tho~... (full message at libera.ems.host/_matrix/media/r0/downloa)
19:52symbioquine[m]`$entity` and `$bundle` are both `NULL`.
19:52symbioquine[m]`$field_definition` is `Drupal\Core\Field\BaseFieldDefinition Object ( [type:protected] => image [propertyDefinitions:protected] => [schema:protected] => [indexes:protected] => Array ( ) [itemDefinition:protected] => Drupal\Core\Field\TypedData\FieldItemDataDefinition Object ( [fieldDefinition:protected] => Drupal\... (full message at libera.ems.host/_matrix/media/r0/downloa)
19:54paul121[m]This looks suspicious in the `drupal/entity` module. It has the `or` for both `administer {type}` and `create {type}` permissions:
19:54paul121[m]git.drupalcode.org/project/entity/-/blob
19:54paul121[m]this might be a wild chase of entity access control handlers... which can be implemented in different classes, react to hooks, and alter hooks. so debugger will be necessary :-)
19:55paul121[m]give the the `$bundle` is `NULL`... this might be the issue?
19:56symbioquine[m]Yeah, I think `$bundle` being `NULL` just validates what you said about the image being a base field.
19:56paul121[m]If you turn on field_ui and make a bundle file field I bet it will work!
19:57symbioquine[m]Hmmm, I'm skeptical...
19:57symbioquine[m]Wouldn't I also have to add an extra permission in that case too?
19:57symbioquine[m]oh
19:57symbioquine[m]I see
19:58symbioquine[m]You're hoping to cause it to line up with the existing `create animal asset` permissions that we already grant?
19:58symbioquine[m](as part of the `farm_manager` role that is)
19:59paul121[m]yep!
20:04*sandrajude[m] has joined
20:16symbioquine[m]I agree that would probably make it work, but I'm not sure it's that important of a hypothesis to test since we're not actually contemplating making the image/file fields into bundle fields right?
20:17symbioquine[m]I guess I'm more interested in what a reasonable path forward for the fields we actually have is...
20:17symbioquine[m]It seems like that access check is probably pretty reasonable as a default for Drupal core.
20:18symbioquine[m]farmOS is doing something special beyond that by breaking down the create/update/delete permissions by asset/log bundle right?
20:19paul121[m]Yeah I agree. The thing is Drupal core doesn't really have the "bundle permissions" itself... that's via the `entity` module :-/
20:19paul121[m]yeah right
20:19symbioquine[m]So naively, that makes me think we need a access control handler which says "oh right, you have permission to create animal assets, so you should be allowed to create file/image entities related to them"
20:22paul121[m]Yeah
20:23paul121[m]only a matter of how... :D
20:28paul121[m]`$entity_access_control_handler->createAccess();` accepts a `$context` array, but nothing is provided here. Maybe that could be used.
20:28paul121[m]git.drupalcode.org/project/drupal/-/blob
20:30sandrajude[m]t.me/Etxcapital1
20:30sandrajude[m]I'll 20 people on how to earn $10,000 or more in just 3day's from the crypto market.but you will pay me 10% commission when you receive your profit. if interested send me a direct message, for more information... Contact via telegram
20:30sandrajude[m]t.me/Etxcapital1
20:32symbioquine[m]^ Reported as spam
20:32*sandrajude[m] has left
20:32mstenta[m]banned!
20:32symbioquine[m]Yeah, I think both are helpful
20:36symbioquine[m]Fortunately, `$field_access_result` is getting set to `Drupal\Core\Access\AccessResultAllowed`
20:37symbioquine[m]So we only need to figure out how to get that `$entity_access_result` to pass
20:37symbioquine[m]Right now it's `Drupal\Core\Access\AccessResultNeutral Object ( [reason:protected] => The following permissions are required: 'administer assets' OR 'create asset'. [cacheContexts:protected] => Array ( [0] => user.permissions ) [cacheTags:protected] => Array ( ) [cacheMaxAge:protected] => -1 )`
20:46paul121[m]hmm. I wonder if it would be okay to return TRUE if the user has any `create {bundle} asset` ?? 🤔
20:46symbioquine[m]Couldn't we change [this](github.com/farmOS/farmOS/blob/b495cdb840) to use a decorated access handler?
20:47paul121[m]if they don't, then it would be FALSE. And the `create asset` permission really shouldn't be used anywhere, this is quite the edge case where that permission string is being generated
20:47paul121[m]symbioquine[m]: yes exactly!
20:49paul121[m]$permissions = array_map(function ($bundle) {
20:49paul121[m] return "create $bundle asset";
20:49paul121[m]}, $asset_bundles);
20:49paul121[m]$result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
20:49paul121[m] * maybe this?... (full message at libera.ems.host/_matrix/media/r0/downloa)
21:02symbioquine[m]Having some network trouble right now... Once it comes back I'll see if I can get something like that working.
21:18symbioquine[m]Alternatively, I think it might be possible to implement [`hook_entity_create_access`](github.com/drupal/drupal/blob/ecbe3ad37c) and use the [`'entity_type_id'` field](github.com/drupal/drupal/blob/ecbe3ad37c) of the context...
21:21paul121[m]ah nice, implementing the hook might be better anyways!
21:58symbioquine[m]<paul121[m]> "ah nice, implementing the hook..." <- I realized the entity bundle is the part that's missing there...
21:58symbioquine[m]It could be hacked up something like this;
21:58*sent a php code block: libera.ems.host/_matrix/media/r0/downloa
21:59symbioquine[m]Which seems to work, but there might be a better strategy than getting the bundle from the URL...
22:00symbioquine[m] * ```php... (full message at libera.ems.host/_matrix/media/r0/downloa)
22:14paul121[m]nice. yes, are you familiar with Route Parameters?
22:15paul121[m]you should be able to get the route parameters object from Drupal::request()
22:15paul121[m]I think these jsonapi file routes are created here: git.drupalcode.org/project/drupal/-/blob
22:16paul121[m]and the "resource type" route param: git.drupalcode.org/project/drupal/-/blob
22:18paul121[m]not sure the best way to identify the "file upload routes" though... maybeee there is some other metadata about these routes you could use?
22:18paul121[m]if not just your regex!
22:22symbioquine[m]<paul121[m]> "nice. yes, are you familiar with..." <- Sounds similar to getting parameters from the route match interface. e.g. github.com/farmOS/farmOS/blob/04bf771d91
22:22paul121[m]yes I think so!
23:02symbioquine[m]This one's a bit stricter/cleaner;... (full message at libera.ems.host/_matrix/media/r0/downloa)
23:03symbioquine[m]* if (empty($entity_type_id) || empty($entity_bundle) || empty($route_match)) {
23:04symbioquine[m] * This one's a bit stricter/cleaner;... (full message at libera.ems.host/_matrix/media/r0/downloa)
23:06symbioquine[m] * This one's a bit stricter/cleaner;... (full message at libera.ems.host/_matrix/media/r0/downloa)
23:08symbioquine[m] * This one's a bit stricter/cleaner;... (full message at libera.ems.host/_matrix/media/r0/downloa)
23:26paul121[m]That looks great. Assuming it works too?

< 072022-09-08⋏09 >