-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from oat-sa/release-2.1.0
Release 2.1.0
- Loading branch information
Showing
14 changed files
with
153 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
return new \oat\taoDacSimple\model\DataBaseAccess([ | ||
\oat\taoDacSimple\model\DataBaseAccess::OPTION_PERSISTENCE => 'default', | ||
]); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
namespace oat\taoDacSimple\model; | ||
|
||
use oat\oatbox\event\EventManagerAwareTrait; | ||
use oat\oatbox\service\ConfigurableService; | ||
use oat\taoDacSimple\model\event\DacAddedEvent; | ||
use oat\taoDacSimple\model\event\DacRemovedEvent; | ||
|
||
|
@@ -31,39 +32,19 @@ | |
* @author Antoine Robin <[email protected]> | ||
* @author Joel Bout <[email protected]> | ||
*/ | ||
class DataBaseAccess | ||
class DataBaseAccess extends ConfigurableService | ||
{ | ||
|
||
use EventManagerAwareTrait; | ||
// --- ASSOCIATIONS --- | ||
|
||
const SERVICE_ID = 'taoDacSimple/DataBaseAccess'; | ||
|
||
// --- ATTRIBUTES --- | ||
const OPTION_PERSISTENCE = 'persistence'; | ||
|
||
|
||
private $persistence = null; | ||
private $persistence; | ||
|
||
const TABLE_PRIVILEGES_NAME = 'data_privileges'; | ||
|
||
// --- OPERATIONS --- | ||
|
||
|
||
public function __construct() | ||
{ | ||
$this->setPersistence(\common_persistence_Manager::getPersistence('default')); | ||
|
||
} | ||
|
||
|
||
/** | ||
* | ||
* @author Lionel Lecaque, [email protected] | ||
* @param \common_persistence_Persistence $persistence | ||
*/ | ||
public function setPersistence(\common_persistence_Persistence $persistence){ | ||
$this->persistence = $persistence; | ||
} | ||
|
||
/** | ||
* We can know which users have a privilege on a resource | ||
* @param array $resourceIds | ||
|
@@ -76,7 +57,7 @@ public function getUsersWithPermissions($resourceIds) | |
$query = "SELECT resource_id, user_id, privilege FROM " . self::TABLE_PRIVILEGES_NAME . " | ||
WHERE resource_id IN ($inQuery)"; | ||
/** @var \PDOStatement $statement */ | ||
$statement = $this->persistence->query($query, $resourceIds); | ||
$statement = $this->getPersistence()->query($query, $resourceIds); | ||
$results = $statement->fetchAll(\PDO::FETCH_ASSOC); | ||
|
||
return $results; | ||
|
@@ -103,7 +84,7 @@ public function getPermissions($userIds, array $resourceIds){ | |
$params[] = $userId; | ||
} | ||
/** @var \PDOStatement $statement */ | ||
$statement = $this->persistence->query($query, $params); | ||
$statement = $this->getPersistence()->query($query, $params); | ||
$results = $statement->fetchAll(\PDO::FETCH_ASSOC); | ||
|
||
foreach ($results as $result) { | ||
|
@@ -127,7 +108,7 @@ public function addPermissions($user, $resourceId, $rights) | |
|
||
foreach ($rights as $privilege) { | ||
// add a line with user URI, resource Id and privilege | ||
$this->persistence->insert( | ||
$this->getPersistence()->insert( | ||
self::TABLE_PRIVILEGES_NAME, | ||
array('user_id' => $user, 'resource_id' => $resourceId, 'privilege' => $privilege) | ||
); | ||
|
@@ -154,7 +135,7 @@ public function getResourcePermissions($resourceId) | |
$query = "SELECT user_id, privilege FROM " . self::TABLE_PRIVILEGES_NAME . " WHERE resource_id = ?"; | ||
|
||
/** @var \PDOStatement $statement */ | ||
$statement = $this->persistence->query($query, array($resourceId)); | ||
$statement = $this->getPersistence()->query($query, array($resourceId)); | ||
$results = $statement->fetchAll(\PDO::FETCH_ASSOC); | ||
|
||
foreach ($results as $result) { | ||
|
@@ -219,7 +200,7 @@ public function removePermissions($user, $resourceId, $rights) | |
} | ||
$params[] = $user; | ||
|
||
$this->persistence->exec($query, $params); | ||
$this->getPersistence()->exec($query, $params); | ||
$this->getEventManager()->trigger(new DacRemovedEvent($user, $resourceId, $rights)); | ||
|
||
return true; | ||
|
@@ -237,11 +218,54 @@ public function removeAllPermissions($resourceIds) | |
//get all entries that match (resourceId) and remove them | ||
$inQuery = implode(',', array_fill(0, count($resourceIds), '?')); | ||
$query = "DELETE FROM " . self::TABLE_PRIVILEGES_NAME . " WHERE resource_id IN ($inQuery)"; | ||
$this->persistence->exec($query, $resourceIds); | ||
$this->getPersistence()->exec($query, $resourceIds); | ||
|
||
$this->getEventManager()->trigger(new DacRemovedEvent('-', $resourceIds, '-')); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @return \common_persistence_SqlPersistence | ||
*/ | ||
private function getPersistence() | ||
{ | ||
if (!$this->persistence){ | ||
|
||
$this->persistence = $this->getServiceManager()->get(\common_persistence_Manager::SERVICE_ID)->getPersistence($this->getOption(self::OPTION_PERSISTENCE)); | ||
} | ||
return $this->persistence; | ||
} | ||
|
||
|
||
public function createTables(){ | ||
|
||
$schemaManager = $this->getPersistence()->getDriver()->getSchemaManager(); | ||
$schema = $schemaManager->createSchema(); | ||
$fromSchema = clone $schema; | ||
$table = $schema->createtable(self::TABLE_PRIVILEGES_NAME); | ||
$table->addColumn('user_id',"string", ["notnull" => null,"length" => 255]); | ||
$table->addColumn('resource_id',"string", ["notnull" => null,"length" => 255]); | ||
$table->addColumn('privilege',"string", ["notnull" => null,"length" => 255]); | ||
$table->setPrimaryKey(["user_id","resource_id","privilege"]); | ||
|
||
$queries = $this->getPersistence()->getPlatform()->getMigrateSchemaSql($fromSchema, $schema); | ||
foreach ($queries as $query){ | ||
$this->getPersistence()->exec($query); | ||
} | ||
} | ||
|
||
|
||
public function removeTables() | ||
{ | ||
$persistence = $this->getPersistence(); | ||
$schema = $persistence->getDriver()->getSchemaManager()->createSchema(); | ||
$fromSchema = clone $schema; | ||
$table = $schema->dropTable(self::TABLE_PRIVILEGES_NAME); | ||
$queries = $persistence->getPlatform()->getMigrateSchemaSql($fromSchema, $schema); | ||
foreach ($queries as $query) { | ||
$persistence->exec($query); | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -21,7 +21,6 @@ | |
namespace oat\taoDacSimple\model; | ||
|
||
use oat\generis\model\data\permission\PermissionInterface; | ||
use oat\taoDacSimple\model\DataBaseAccess; | ||
use oat\oatbox\user\User; | ||
use core_kernel_classes_Class; | ||
use oat\oatbox\service\ConfigurableService; | ||
|
@@ -35,8 +34,7 @@ | |
* @access public | ||
* @author Joel Bout, <[email protected]> | ||
*/ | ||
class PermissionProvider extends ConfigurableService | ||
implements PermissionInterface | ||
class PermissionProvider extends ConfigurableService implements PermissionInterface | ||
{ | ||
|
||
/** | ||
|
@@ -53,7 +51,7 @@ public function getPermissions(User $user, array $resourceIds) { | |
return $permissions; | ||
} | ||
|
||
$dbAccess = new DataBaseAccess(); | ||
$dbAccess = $this->getServiceManager()->get(DataBaseAccess::SERVICE_ID); | ||
$userIds = $user->getRoles(); | ||
$userIds[] = $user->getIdentifier(); | ||
return $dbAccess->getPermissions($userIds, $resourceIds); | ||
|
@@ -65,7 +63,7 @@ public function getPermissions(User $user, array $resourceIds) { | |
*/ | ||
public function onResourceCreated(\core_kernel_classes_Resource $resource) | ||
{ | ||
$dbAccess = new DataBaseAccess(); | ||
$dbAccess = $this->getServiceManager()->get(DataBaseAccess::SERVICE_ID); | ||
// verify resource is created | ||
$permissions = $dbAccess->getResourcePermissions($resource->getUri()); | ||
if (empty($permissions)) { | ||
|
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
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
Oops, something went wrong.