Skip to content

Commit

Permalink
Merge pull request #5 from Xety/optimizations
Browse files Browse the repository at this point in the history
Updated the code to follow the Cake CS rules + added testscase for PHP7
  • Loading branch information
Xety committed Jan 5, 2016
2 parents b3f278c + f06d089 commit 45f9325
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 343 deletions.
12 changes: 5 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 5.4
- 5.5
- 5.6
- 7.0

env:
matrix:
Expand All @@ -13,7 +14,7 @@ env:

matrix:
allow_failures:
- php: 5.4
- php: 7.0
env: COVERALLS=1 DEFAULT=0

fast_finish: true
Expand All @@ -25,10 +26,10 @@ matrix:
- php: 5.4
env: DB=sqlite db_class='Cake\Database\Driver\Sqlite' db_dsn='sqlite::memory:'

- php: 5.4
- php: 7.0
env: PHPCS=1 DEFAULT=0

- php: 5.4
- php: 7.0
env: COVERALLS=1 DEFAULT=0

before_script:
Expand All @@ -39,9 +40,6 @@ before_script:

- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi"

- sh -c "if [ '$PHPCS' = '1' ]; then pear channel-discover pear.cakephp.org; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then pear install --alldeps cakephp/CakePHP_CodeSniffer; fi"

- sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi"

Expand All @@ -53,7 +51,7 @@ script:
- sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi"
- sh -c "if [ '$DEFAULT' = '1' ]; then phpunit --stderr; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then phpcs -p --extensions=php --standard=CakePHP --ignore=vendor --ignore=docs . ; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi"

notifications:
email: false
69 changes: 35 additions & 34 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
{
"name": "xety/cake3-cookieauth",
"type": "cakephp-plugin",
"description": "A simple Cake3 plugin to authenticate users with Cookies.",
"keywords": ["cakephp", "cake3", "plugin", "cookie", "auth", "authenticate", "login"],
"homepage": "https://github.com/Xety/Cake3-CookieAuth",
"license": "MIT",
"support":{
"source":"https://github.com/Xety/Cake3-CookieAuth",
"issues":"https://github.com/Xety/Cake3-CookieAuth/issues"
},
"authors": [
{
"name": "Xety",
"email": "zoro.fmt@gmail.com",
"homepage": "https://github.com/Xety"
}
],
"require": {
"php": ">=5.4.0",
"cakephp/cakephp": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "4.1.*"
},
"autoload": {
"psr-4": {
"Xety\\Cake3CookieAuth\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Xety\\Cake3CookieAuth\\Test\\": "tests"
}
}
"name": "xety/cake3-cookieauth",
"type": "cakephp-plugin",
"description": "A simple Cake3 plugin to authenticate users with Cookies.",
"keywords": ["cakephp", "cake3", "plugin", "cookie", "auth", "authenticate", "login"],
"homepage": "https://github.com/Xety/Cake3-CookieAuth",
"license": "MIT",
"support":{
"source":"https://github.com/Xety/Cake3-CookieAuth",
"issues":"https://github.com/Xety/Cake3-CookieAuth/issues"
},
"authors": [
{
"name": "Xety",
"email": "zoro.fmt@gmail.com",
"homepage": "https://github.com/Xety"
}
],
"require": {
"php": ">=5.4.16",
"cakephp/cakephp": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"cakephp/cakephp-codesniffer": "master-dev"
},
"autoload": {
"psr-4": {
"Xety\\Cake3CookieAuth\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Xety\\Cake3CookieAuth\\Test\\": "tests"
}
}

}
60 changes: 34 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?><phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Cake3CookieAuth Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>
<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Cake3CookieAuth Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>
<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<!-- Prevent coverage reports from looking in tests, vendors, config folders -->
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
<directory suffix=".ctp">./src/</directory>
</whitelist>
</filter>

</phpunit>
125 changes: 65 additions & 60 deletions src/Auth/CookieAuthenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,77 @@
use Cake\Network\Request;
use Cake\Network\Response;

class CookieAuthenticate extends BaseAuthenticate {
class CookieAuthenticate extends BaseAuthenticate
{

/**
* Constructor.
*
* @param \Cake\Controller\ComponentRegistry $registry The Component registry used on this request.
* @param array $config Array of config to use.
*/
public function __construct(ComponentRegistry $registry, array $config = []) {
$this->_registry = $registry;
$this->config($config);
}
/**
* Constructor.
*
* @param \Cake\Controller\ComponentRegistry $registry The Component registry used on this request.
* @param array $config Array of config to use.
*/
public function __construct(ComponentRegistry $registry, array $config = [])
{
$this->_registry = $registry;
$this->config($config);
}

/**
* Authenticate a user based on the cookies information.
*
* @param \Cake\Network\Request $request The request instance.
* @param \Cake\Network\Response $response The response instance.
*
* @return mixed
*
* @throws \RuntimeException When the CookieComponent is not loaded.
*/
public function authenticate(Request $request, Response $response) {
if (!isset($this->_registry->Cookie) || !$this->_registry->Cookie instanceof CookieComponent) {
throw new \RuntimeException('You need to load the CookieComponent.');
}
/**
* Authenticate a user based on the cookies information.
*
* @param \Cake\Network\Request $request The request instance.
* @param \Cake\Network\Response $response The response instance.
*
* @return mixed
*
* @throws \RuntimeException When the CookieComponent is not loaded.
*/
public function authenticate(Request $request, Response $response)
{
if (!isset($this->_registry->Cookie) || !$this->_registry->Cookie instanceof CookieComponent) {
throw new \RuntimeException('You need to load the CookieComponent.');
}

$cookies = $this->_registry->Cookie->read('CookieAuth');
if (empty($cookies)) {
return false;
}
$cookies = $this->_registry->Cookie->read('CookieAuth');
if (empty($cookies)) {
return false;
}

extract($this->_config['fields']);
if (empty($cookies[$username]) || empty($cookies[$password])) {
return false;
}
extract($this->_config['fields']);
if (empty($cookies[$username]) || empty($cookies[$password])) {
return false;
}

$user = $this->_findUser($cookies[$username], $cookies[$password]);
if ($user) {
return $user;
}
$user = $this->_findUser($cookies[$username], $cookies[$password]);
if ($user) {
return $user;
}

return false;
}
return false;
}

/**
* Returns a list of all events that this authenticate class will listen to.
*
* @return array
*/
public function implementedEvents() {
return [
'Auth.logout' => 'logout'
];
}
/**
* Returns a list of all events that this authenticate class will listen to.
*
* @return array
*/
public function implementedEvents()
{
return [
'Auth.logout' => 'logout'
];
}

/**
* Delete cookies when an user logout.
*
* @param \Cake\Event\Event $event The logout Event.
* @param array $user The user about to be logged out.
*
* @return void
*/
public function logout(Event $event, array $user) {
$this->_registry->Cookie->delete('CookieAuth');
}
/**
* Delete cookies when an user logout.
*
* @param \Cake\Event\Event $event The logout Event.
* @param array $user The user about to be logged out.
*
* @return void
*/
public function logout(Event $event, array $user)
{
$this->_registry->Cookie->delete('CookieAuth');
}
}
67 changes: 34 additions & 33 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,40 @@

use Cake\TestSuite\Fixture\TestFixture;

class UsersFixture extends TestFixture {
class UsersFixture extends TestFixture
{

/**
* Fields
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'username' => ['type' => 'string', 'length' => 20],
'password' => ['type' => 'string', 'length' => 255],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
'username' => ['type' => 'unique', 'columns' => ['username']]
],
'_options' => [
'engine' => 'InnoDB', 'collation' => 'utf8_general_ci'
],
];
/**
* Fields
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'username' => ['type' => 'string', 'length' => 20],
'password' => ['type' => 'string', 'length' => 255],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
'username' => ['type' => 'unique', 'columns' => ['username']]
],
'_options' => [
'engine' => 'InnoDB', 'collation' => 'utf8_general_ci'
],
];

/**
* Records
*
* @var array
*/
public $records = [
[
'username' => 'Mariano',
'password' => 'passmariano'
],
[
'username' => 'Larry',
'password' => 'passlarry'
]
];
/**
* Records
*
* @var array
*/
public $records = [
[
'username' => 'Mariano',
'password' => 'passmariano'
],
[
'username' => 'Larry',
'password' => 'passlarry'
]
];
}
Loading

0 comments on commit 45f9325

Please # to comment.