This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9312caa
commit 46ec385
Showing
11 changed files
with
539 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
migrations/upgrades/schemas/20190914095509_update_directus_user_sessions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
class UpdateDirectusUserSessions extends AbstractMigration | ||
{ | ||
/** | ||
* Change Method. | ||
* | ||
* Write your reversible migrations using this method. | ||
* | ||
* More information on writing migrations is available here: | ||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class | ||
* | ||
* The following commands can be used in this method and Phinx will | ||
* automatically reverse them when rolling back: | ||
* | ||
* createTable | ||
* renameTable | ||
* addColumn | ||
* renameColumn | ||
* addIndex | ||
* addForeignKey | ||
* | ||
* Remember to call "create()" or "update()" and NOT "save()" when working | ||
* with the Table class. | ||
*/ | ||
public function change() | ||
{ | ||
$table = $this->table('directus_user_sessions'); | ||
if (!$table->hasColumn('token_type')) { | ||
$table->addColumn('token_type', 'string', [ | ||
'null' => true, | ||
'default' => null | ||
]); | ||
} | ||
|
||
if (!$table->hasColumn('token_expired_at')) { | ||
$table->addColumn('token_expired_at', 'datetime', [ | ||
'null' => true, | ||
'default' => null | ||
]); | ||
} | ||
|
||
$table->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
|
||
use Phinx\Seed\AbstractSeed; | ||
|
||
class UserSessionFeedSeeder extends AbstractSeed | ||
{ | ||
/** | ||
* Run Method. | ||
* | ||
* Write your database seeder using this method. | ||
* | ||
* More information on writing seeders is available here: | ||
* http://docs.phinx.org/en/latest/seeding.html | ||
*/ | ||
public function run() | ||
{ | ||
$data = [ | ||
// User Session | ||
// ----------------------------------------------------------------- | ||
[ | ||
'collection' => 'directus_user_sessions', | ||
'field' => 'id', | ||
'type' => \Directus\Database\Schema\DataTypes::TYPE_INTEGER, | ||
'interface' => 'primary-key', | ||
'locked' => 1, | ||
'required' => 1, | ||
'hidden_detail' => 1 | ||
], | ||
[ | ||
'collection' => 'directus_user_sessions', | ||
'field' => 'user', | ||
'type' => \Directus\Database\Schema\DataTypes::TYPE_USER, | ||
'interface' => 'user' | ||
], | ||
[ | ||
'collection' => 'directus_user_sessions', | ||
'field' => 'token_type', | ||
'type' => \Directus\Database\Schema\DataTypes::TYPE_STRING, | ||
'interface' => 'text-input' | ||
], | ||
[ | ||
'collection' => 'directus_user_sessions', | ||
'field' => 'token', | ||
'type' => \Directus\Database\Schema\DataTypes::TYPE_STRING, | ||
'interface' => 'text-input' | ||
], | ||
[ | ||
'collection' => 'directus_user_sessions', | ||
'field' => 'ip_address', | ||
'type' => \Directus\Database\Schema\DataTypes::TYPE_STRING, | ||
'interface' => 'text-input' | ||
], | ||
[ | ||
'collection' => 'directus_user_sessions', | ||
'field' => 'user_agent', | ||
'type' => \Directus\Database\Schema\DataTypes::TYPE_STRING, | ||
'interface' => 'text-input' | ||
], | ||
[ | ||
'collection' => 'directus_user_sessions', | ||
'field' => 'created_on', | ||
'type' => \Directus\Database\Schema\DataTypes::TYPE_DATETIME, | ||
'interface' => 'datetime' | ||
], | ||
[ | ||
'collection' => 'directus_user_sessions', | ||
'field' => 'token_expired_at', | ||
'type' => \Directus\Database\Schema\DataTypes::TYPE_DATETIME, | ||
'interface' => 'datetime' | ||
], | ||
]; | ||
|
||
$files = $this->table('directus_fields'); | ||
$files->insert($data)->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/core/Directus/Database/TableGateway/DirectusUserSessionsTableGateway.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Directus\Database\TableGateway; | ||
|
||
use Directus\Permissions\Acl; | ||
use Directus\Util\DateTimeUtils; | ||
use Zend\Db\Adapter\AdapterInterface; | ||
use Zend\Db\Sql\Insert; | ||
use Zend\Db\Sql\Update; | ||
use Zend\Db\Sql\Select; | ||
|
||
class DirectusUserSessionsTableGateway extends RelationalTableGateway | ||
{ | ||
const TOKEN_COOKIE = 'cookie'; | ||
const TOKEN_JWT = 'jwt'; | ||
|
||
public static $_tableName = 'directus_user_sessions'; | ||
|
||
public $primaryKeyFieldName = 'id'; | ||
|
||
/** | ||
* DirectusUserSessionTableGateway constructor. | ||
* | ||
* @param AdapterInterface $adapter | ||
* @param Acl $acl | ||
*/ | ||
public function __construct(AdapterInterface $adapter, $acl = null) | ||
{ | ||
parent::__construct(self::$_tableName, $adapter, $acl); | ||
} | ||
|
||
public function recordSession($data) | ||
{ | ||
$sessionData = [ | ||
'user' => $data['user'], | ||
'token' => $data['token'], | ||
'token_type' => $data['token_type'], | ||
'token_expired_at' => $data['token_expired_at'], | ||
'created_on' => DateTimeUtils::now()->toString(), | ||
'ip_address' => \Directus\get_request_ip(), | ||
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '' | ||
]; | ||
|
||
$insert = new Insert($this->getTable()); | ||
$insert | ||
->values($sessionData); | ||
|
||
$this->insertWith($insert); | ||
return $this->getLastInsertValue(); | ||
} | ||
|
||
public function updateSession($id,$data) | ||
{ | ||
$update = new Update($this->getTable()); | ||
$update->set($data); | ||
$update->where([ | ||
'id' => $id | ||
]); | ||
$this->updateWith($update); | ||
} | ||
|
||
public function fetchSession($condition) | ||
{ | ||
$select = new Select($this->getTable()); | ||
$select->columns(['*']); | ||
$select->where($condition); | ||
$select->limit(1); | ||
return $this->selectWith($select)->current(); | ||
} | ||
|
||
} |
Oops, something went wrong.