diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9dccbbb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +test/ +vendor/ \ No newline at end of file diff --git a/Entity.php b/Entity.php deleted file mode 100644 index 2ce4007..0000000 --- a/Entity.php +++ /dev/null @@ -1,130 +0,0 @@ - - */ - -namespace RapidAuthorization; - -use \PDO; -use \ArrayObject; - -class Entity -{ - /** - * @var ClientPreferences - */ - protected $preferences; - - /** - * @var ArrayObject - */ - protected $preferencesList; - - /** - * @var this - */ - private static $instance; - - - /** - * @var PDO - */ - protected $db; - - public $id = 0; - public $name = ''; - public $business_name = ''; - public $description = null; - - protected function __construct(ClientPreferences $preferences, PDO $pdo) - { - $this->preferences = $preferences; - $this->preferencesList = $this->preferences->getPreferencesList(); - $this->db = $pdo; - } - - /** - * @return this - */ - public static function instance(ClientPreferences $preferences, PDO $pdo) - { - return self::$instance = new self($preferences, $pdo); - } - - public static function getInstance() { - return self::$instance; - } - - public function create($businessName, $name = null, $description = null) - { - $this->name = $name; - $this->business_name = $businessName; - $this->description = $description; - return $this->save(); - } - - /** - *
Set '' to $description to set NULL on database
- */ - public function update($id, $businessName, $name = null, $description = null) - { - if($this->populateById($id)) { - $this->id = $id; - $this->business_name = $businessName; - - if($name !== null) { - $this->name = $name; - } - - if($description !== null) { - $this->description = $description; - } - - return $this->save(); - } - - return 0; - } - - /** - *Populate object with values from record on database
- */ - private function populateById($roleId) - { - $task = $this->findById($roleId); - - if($task) { - $this->id = (int) $task['id']; - $this->name = $task['name']; - $this->business_name = $task['business_name']; - $this->description = $task['description']; - return true; - } - - return false; - } - - protected function saveFromSQL($sql) { - $stmt = $this->db->prepare($sql); - $stmt->bindParam(':id', $this->id, PDO::PARAM_INT); - $stmt->bindParam(':businessName', $this->business_name, PDO::PARAM_STR); - - $name = ($this->name ? $this->name : null); - $stmt->bindParam(':name', $name); - - $description = ($this->description ? $this->description : null); - $stmt->bindParam(':description', $description); - - $stmt->execute(); - - if(!$this->id) { - $this->id = (int) $this->db->lastInsertId(); - } - - return $this->id = (int) $this->id; - } - -} diff --git a/composer.json b/composer.json index 2f9046d..92ef46b 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "maykonn/rapid-authorization", "type": "library", "description": "RapidAuthorization is a PHP framework for Access Control, based in Role-based Access Control (RBAC)", - "keywords": ["access control", "role-based access control", "role based access control", "rbac", "framework"], + "keywords": ["PHP", "access control", "role-based access control", "role based access control", "rbac", "framework"], "homepage": "https://github.com/Maykonn/RapidAuthorization", "license": "MIT", "authors": [ @@ -15,8 +15,8 @@ "php": ">=5.3.0" }, "autoload": { - "psr-0": { - "RapidAuthorization": "" + "psr-4": { + "RapidAuthorization\\": "src/" } } } diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..321c23c --- /dev/null +++ b/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "d6af800c0f7a2c2e418164f041b126a4", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3.0" + }, + "platform-dev": [] +} diff --git a/src/AbstractEntity.php b/src/AbstractEntity.php new file mode 100644 index 0000000..539c12e --- /dev/null +++ b/src/AbstractEntity.php @@ -0,0 +1,126 @@ + + */ + +namespace RapidAuthorization; + +use \PDO; +use \ArrayObject; + +abstract class AbstractEntity implements IEntity +{ + /** + * @var ClientPreferences + */ + protected $preferences; + + /** + * @var ArrayObject + */ + protected $preferencesList; + + /** + * @var $this + */ + protected static $instance; + + + /** + * @var PDO + */ + protected $db; + + public $id = 0; + public $name = ''; + public $business_name = ''; + public $description = null; + + protected function __construct(ClientPreferences $preferences, PDO $pdo) + { + $this->preferences = $preferences; + $this->preferencesList = $this->preferences->getPreferencesList(); + $this->db = $pdo; + } + + public static function getInstance() + { + return self::$instance; + } + + public function create($businessName, $name = null, $description = null) + { + $this->name = $name; + $this->business_name = $businessName; + $this->description = $description; + + return $this->save(); + } + + /** + *Set '' to $description to set NULL on database
+ */ + public function update($id, $businessName, $name = null, $description = null) + { + if ($this->populateById($id)) { + $this->id = $id; + $this->business_name = $businessName; + + if ($name !== null) { + $this->name = $name; + } + + if ($description !== null) { + $this->description = $description; + } + + return $this->save(); + } + + return 0; + } + + /** + *Populate object with values from record on database
+ */ + private function populateById($roleId) + { + $task = $this->findById($roleId); + + if ($task) { + $this->id = (int) $task['id']; + $this->name = $task['name']; + $this->business_name = $task['business_name']; + $this->description = $task['description']; + + return true; + } + + return false; + } + + protected function saveFromSQL($sql) + { + $stmt = $this->db->prepare($sql); + $stmt->bindParam(':id', $this->id, PDO::PARAM_INT); + $stmt->bindParam(':businessName', $this->business_name, PDO::PARAM_STR); + + $name = ($this->name ? $this->name : null); + $stmt->bindParam(':name', $name); + + $description = ($this->description ? $this->description : null); + $stmt->bindParam(':description', $description); + + $stmt->execute(); + + if ( ! $this->id) { + $this->id = (int) $this->db->lastInsertId(); + } + + return $this->id = (int) $this->id; + } + +} diff --git a/Autoload.php b/src/Autoload.php similarity index 100% rename from Autoload.php rename to src/Autoload.php diff --git a/AvailablePreferences.php b/src/AvailablePreferences.php similarity index 100% rename from AvailablePreferences.php rename to src/AvailablePreferences.php diff --git a/ClientPreferences.php b/src/ClientPreferences.php similarity index 100% rename from ClientPreferences.php rename to src/ClientPreferences.php diff --git a/Database/MySQL.php b/src/Database/MySQL.php similarity index 100% rename from Database/MySQL.php rename to src/Database/MySQL.php diff --git a/Database/MySQLSchemaHandler.php b/src/Database/MySQLSchemaHandler.php similarity index 100% rename from Database/MySQLSchemaHandler.php rename to src/Database/MySQLSchemaHandler.php diff --git a/Database/schema.sql b/src/Database/schema.sql similarity index 100% rename from Database/schema.sql rename to src/Database/schema.sql diff --git a/src/IEntity.php b/src/IEntity.php new file mode 100644 index 0000000..a3267eb --- /dev/null +++ b/src/IEntity.php @@ -0,0 +1,13 @@ + + */ + +namespace RapidAuthorization; + +interface IEntity +{ + public static function instance(ClientPreferences $preferences, \PDO $pdo); +} \ No newline at end of file diff --git a/Operation.php b/src/Operation.php similarity index 98% rename from Operation.php rename to src/Operation.php index f6cb68e..a678e44 100644 --- a/Operation.php +++ b/src/Operation.php @@ -11,20 +11,22 @@ use \Exception; use RapidAuthorization\Database\MySQL; -class Operation extends Entity +class Operation extends AbstractEntity { - - public $id = 0; - public $name = ''; - public $business_name = ''; - public $description = null; - /** * Verify if needs to check the authorization, see Operation::populateById() * @var Enum '1', '0' */ public $needs_authorization = '1'; + /** + * @return $this + */ + public static function instance(ClientPreferences $preferences, PDO $pdo) + { + return self::$instance = new self($preferences, $pdo); + } + /** *An Operation can be, e.g. Create Product or Edit Customer
*/ diff --git a/RapidAuthorization.php b/src/RapidAuthorization.php similarity index 98% rename from RapidAuthorization.php rename to src/RapidAuthorization.php index 526b780..d7816ca 100644 --- a/RapidAuthorization.php +++ b/src/RapidAuthorization.php @@ -8,10 +8,10 @@ namespace RapidAuthorization; // Necessárias antes do Autoload -require_once 'ClientPreferences.php'; +/*require_once 'ClientPreferences.php'; require_once 'AvailablePreferences.php'; require_once 'Autoload.php'; - +*/ use RapidAuthorization\Database\MySQL; use RapidAuthorization\Database\MySQLSchemaHandler; diff --git a/Role.php b/src/Role.php similarity index 97% rename from Role.php rename to src/Role.php index f5f77c3..dcc584d 100644 --- a/Role.php +++ b/src/Role.php @@ -11,8 +11,16 @@ use \Exception; use RapidAuthorization\Database\MySQL; -class Role extends Entity +class Role extends AbstractEntity { + /** + * @return $this + */ + public static function instance(ClientPreferences $preferences, PDO $pdo) + { + return self::$instance = new self($preferences, $pdo); + } + public function delete($id) { if($this->findById($id)) { diff --git a/Task.php b/src/Task.php similarity index 97% rename from Task.php rename to src/Task.php index 9e83afb..2d2c5c5 100644 --- a/Task.php +++ b/src/Task.php @@ -11,8 +11,16 @@ use \Exception; use RapidAuthorization\Database\MySQL; -class Task extends Entity +class Task extends AbstractEntity { + /** + * @return $this + */ + public static function instance(ClientPreferences $preferences, PDO $pdo) + { + return self::$instance = new self($preferences, $pdo); + } + public function delete($id) { if($this->findById($id)) { diff --git a/User.php b/src/User.php similarity index 97% rename from User.php rename to src/User.php index 7abbe62..dd5892a 100644 --- a/User.php +++ b/src/User.php @@ -12,10 +12,15 @@ use \Exception; use RapidAuthorization\Database\MySQL; -class User extends Entity +class User extends AbstractEntity { - - public $id; + /** + * @return $this + */ + public static function instance(ClientPreferences $preferences, PDO $pdo) + { + return self::$instance = new self($preferences, $pdo); + } public function getRoles($userId) {