Skip to content

Commit

Permalink
Merge pull request #17 from loadsys/f/improve-session-handling
Browse files Browse the repository at this point in the history
F/improve session handling
  • Loading branch information
justinyost authored Jun 27, 2016
2 parents 64fe7f5 + 374af64 commit 43e522d
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 28 deletions.
24 changes: 21 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
tmp/*
[Cc]onfig/core.php
[Cc]onfig/database.php
app/tmp/*
app/[Cc]onfig/core.php
app/[Cc]onfig/database.php
!empty
/vendor/*
/config/app.php
/tmp/*
/logs/*

# OS
.DS_Store
.AppleDouble
.LSOverride
Icon?
._*
.Spotlight-V100
.Trashes
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Ignore the composer lock file-- it should never be included in a distributed package.
/composer.lock
43 changes: 32 additions & 11 deletions src/Model/Behavior/CreatorModifierBehavior.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
<?php
/**
* CreatorModifierBehavior is a tool to set a `creator_id` and `modifier_id`
* on records being saved.
*/
namespace CreatorModifier\Model\Behavior;

use Cake\Event\Event;
use Cake\Log\LogTrait;
use Cake\Network\Request;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use \RuntimeException;
use \UnexpectedValueException;

/**
* CreatorModifierBehavior - Adds the ability to use the Session provided User.primary_key
* \CreatorModifier\Model\Behavior\CreatorModifierBehavior
*
* Adds the ability to use the Session provided User.primary_key
* value to assign to the creator_id and modifier_id on saving an Entity.
*/
class CreatorModifierBehavior extends Behavior {

// Add logging in the event of a failure with the session.
use LogTrait;

/**
* These are merged with user-provided config when the behavior is used.
*
Expand Down Expand Up @@ -70,7 +83,7 @@ public function handleEvent(Event $event, Entity $entity) {

foreach ($events[$eventName] as $field => $when) {
if (!in_array($when, ['always', 'new', 'existing'])) {
throw new \UnexpectedValueException(
throw new UnexpectedValueException(
sprintf('When should be one of "always", "new" or "existing". The passed value "%s" is invalid', $when)
);
}
Expand Down Expand Up @@ -147,15 +160,6 @@ protected function updateField(Entity $entity, $field) {
$entity->set($field, $this->getUserId());
}

/**
* Factory method for the Request object.
*
* @return \Cake\Network\Request New instance of the Request object.
*/
protected function newRequest() {
return new \Cake\Network\Request();
}

/**
* Return the User.id grabbed from the Session information.
*
Expand All @@ -167,8 +171,25 @@ protected function sessionUserId() {

if ($request->session()->started()) {
$userId = $request->session()->read($this->_config['sessionUserIdKey']);
} else {
$this->log('The Session is not started. This typically means a User is not logged in. In this case there is no Session value for the currently active User and therefore we will set the `creator_id` and `modifier_id` to a null value. As a fallback, we are manually starting the session and reading the `$this->_config[sessionUserIdKey]` value, which is probably not correct.', 'debug');
try {
$request->session()->start();
$userId = $request->session()->read($this->_config['sessionUserIdKey']);
} catch (RuntimeException $e) {
}
}

return $userId;
}

/**
* Factory method for the Request object.
*
* @return \Cake\Network\Request New instance of the Request object.
* @codeCoverageIgnore Don't test PHP's ability to use new.
*/
protected function newRequest() {
return new Request();
}
}
Loading

0 comments on commit 43e522d

Please sign in to comment.