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
Use Footprint from shell #21
Comments
There's no session and authentication for shells and as a result no info about authenticated user either. |
Yes, I understand. But if one is importing data from shell, it is usefull that a dataset is created/changed by a "batch"-user. I use a different behavior, now: use ArrayObject;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use UnexpectedValueException;
/**
* CliFootprint behavior
*/
class CliFootprintBehavior extends Behavior
{
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [
'events' => [
'Model.beforeSave' => [
'created_by' => 'new',
'modified_by' => 'always',
]
],
'propertiesMap' => [],
];
/**
* Injects configured field values into entity if those fields are not dirty.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param \Cake\Event\Event $event Event.
* @param \Cake\ORM\Entity $entity Entity.
* @param \ArrayObject $options Options.
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
$eventName = $event->name();
$events = $this->config('events');
$new = $entity->isNew() !== false;
foreach ($events[$eventName] as $field => $when) {
if (!in_array($when, ['always', 'new', 'existing'])) {
throw new UnexpectedValueException(
sprintf('When should be one of "always", "new" or "existing". The passed value "%s" is invalid', $when)
);
}
if ($entity->dirty($field)) {
continue;
}
if ($when === 'always' ||
($when === 'new' && $new) ||
($when === 'existing' && !$new)
) {
$entity->set(
$field,
$this->config('propertiesMap.' . $field)
);
}
}
}
} and I load the default behavior if a table is loaded from web and the shown behavior if a table is loaded by shell. The behavior could be loaded like: $this->_table->addBehavior('CliFootprint', [
'events' => [
'Model.beforeSave' => [
'created_by_user_id' => 'new',
'modified_by_user_id' => 'always',
],
],
'propertiesMap' => [
'created_by_user_id' => 4711, //example id
'modified_by_user_id' => 4711, //example id
],
]); so from shell you have a fixed user id. How do you feel aboud adding a similar thing to the default behavior? If |
How is attaching behavior dynamically with specific user id better than just directly setting required fields with the user id in entity itself before saving? |
If I use the beforesave event I have to add it to every model. Or wirte a behavior as I have done above. I think it is more maintable code to only have one behavior to store the user who created/changed a entity. Evernyone using this plugin with shell should run into the same error. |
It's not a error, it's a technical limitation. I already explained in first response why one won't get the footprint when using shell. To make the behavior work in shell you can set footprint yourself by manually logging user or calling |
Hello,
i'm using severall shell scripts, but there is no
Auth.afterIdentify
event fired. Because of this https://github.com/UseMuffin/Footprint/blob/master/src/Model/Behavior/FootprintBehavior.php#L121 returns allwaysfalse
sothat no entity could be safed.Thanks
The text was updated successfully, but these errors were encountered: