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

Use Footprint from shell #21

Closed
stmeyer opened this issue Mar 21, 2016 · 5 comments
Closed

Use Footprint from shell #21

stmeyer opened this issue Mar 21, 2016 · 5 comments

Comments

@stmeyer
Copy link

stmeyer commented Mar 21, 2016

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 allways false sothat no entity could be safed.

Thanks

@ADmad
Copy link
Member

ADmad commented May 13, 2016

There's no session and authentication for shells and as a result no info about authenticated user either.

@ADmad ADmad closed this as completed May 13, 2016
@stmeyer
Copy link
Author

stmeyer commented May 17, 2016

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 propertiesMap is configurated as integer that the default behavior uses the integer instead of a user entity?

@ADmad
Copy link
Member

ADmad commented May 17, 2016

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?

@stmeyer
Copy link
Author

stmeyer commented May 17, 2016

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.

@ADmad
Copy link
Member

ADmad commented May 17, 2016

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 FootprintAwareTrait::footprint() with handmade Auth.afterIdentify event instance which has user info.

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

2 participants