Skip to content

Commit

Permalink
feat(users): Add a visit when connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
fxleblanc committed Feb 15, 2016
1 parent 04bc268 commit 920c45a
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ public function index()
);
}

public function _recordVisit($user)
{
$this->Visits = $this->loadModel('Visits');
$visit = $this->Visits->newEntity();

$visit->user_id = $user['id'];

$res = $this->Visits->save($visit);
}

/**
* Login method
*
Expand All @@ -153,6 +163,8 @@ public function login()
if ($user) {
$this->Auth->setUser($user);

$this->_recordVisit($user);

if ($this->request->Session()->read('actionRef') && $this->request->Session()->read('controllerRef') && !in_array($this->request->Session()->read('actionRef'), ['register/', 'recoverPassword/'])) {
return $this->redirect(['controller' => $this->request->Session()->read('controllerRef'), 'action' => $this->request->Session()->read('actionRef')]);
} else {
Expand Down
30 changes: 30 additions & 0 deletions src/Model/Entity/Visit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

/**
* Visit Entity.
*
* @property int $id
* @property int $user_id
* @property \App\Model\Entity\User $user
* @property \Cake\I18n\Time $created
*/
class Visit extends Entity
{

/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'*' => true,
'id' => false,
];
}
67 changes: 67 additions & 0 deletions src/Model/Table/VisitsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
namespace App\Model\Table;

use App\Model\Entity\Visit;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
* Visits Model
*
* @property \Cake\ORM\Association\BelongsTo $Users
*/
class VisitsTable extends Table
{

/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);

$this->table('visits');
$this->displayField('id');
$this->primaryKey('id');

$this->addBehavior('Timestamp');

$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
}

/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');

return $validator;
}

/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['user_id'], 'Users'));
return $rules;
}
}
45 changes: 45 additions & 0 deletions tests/Fixture/VisitsFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace App\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* VisitsFixture
*
*/
class VisitsFixture extends TestFixture
{

/**
* Fields
*
* @var array
*/
// @codingStandardsIgnoreStart
public $fields = [
'id' => ['type' => 'integer', 'length' => 11, 'unsigned' => false, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null],
'user_id' => ['type' => 'integer', 'length' => 11, 'unsigned' => false, 'null' => false, 'default' => null, 'comment' => '', 'precision' => null, 'autoIncrement' => null],
'created' => ['type' => 'datetime', 'length' => null, 'null' => false, 'default' => null, 'comment' => '', 'precision' => null],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
],
'_options' => [
'engine' => 'InnoDB',
'collation' => 'utf8_general_ci'
],
];
// @codingStandardsIgnoreEnd

/**
* Records
*
* @var array
*/
public $records = [
[
'id' => 1,
'user_id' => 1,
'created' => '2016-02-15 15:01:28'
],
];
}
11 changes: 10 additions & 1 deletion tests/TestCase/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class UsersControllerTest extends IntegrationTestCase
'app.type_missions',
'app.notifications',
'app.applications',
'app.news'
'app.news',
'app.visits'
];

/**
Expand All @@ -71,6 +72,9 @@ public function setUp()

$config = TableRegistry::exists('Users') ? [] : ['className' => 'App\Model\Table\UsersTable'];
$this->Users = TableRegistry::get('Users', $config);

$config = TableRegistry::exists('Visits') ? [] : ['className' => 'App\Model\Table\VisitsTable'];
$this->Visits = TableRegistry::get('Visits', $config);
}

/**
Expand All @@ -82,6 +86,7 @@ public function tearDown()
{
unset($this->UsersTypeMissions);
unset($this->Users);
unset($this->Visits);

parent::tearDown();
}
Expand Down Expand Up @@ -147,6 +152,10 @@ public function testLoginOk()
$this->post('/users/login', $data);

$this->assertRedirect(['controller' => 'Pages', 'action' => 'home']);

$visits = $this->Visits->find('all')->where(['user_id' => 2])->count();

$this->assertEquals(1, $visits);
}

/**
Expand Down

0 comments on commit 920c45a

Please sign in to comment.