From 5080b4996e32faed8d62f2625e2d310b432202e6 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Fri, 6 May 2016 20:33:08 +0100 Subject: [PATCH] Started base work on attribute system --- app/Attribute.php | 19 ++++++++++ app/Entity.php | 25 ++++++++++++ app/Http/Controllers/AttributeController.php | 32 ++++++++++++++++ app/Http/routes.php | 7 +++- app/Repos/AttributeRepo.php | 32 ++++++++++++++++ app/Services/PermissionService.php | 19 +++++++--- ...6_05_06_185215_create_attributes_table.php | 38 +++++++++++++++++++ 7 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 app/Attribute.php create mode 100644 app/Http/Controllers/AttributeController.php create mode 100644 app/Repos/AttributeRepo.php create mode 100644 database/migrations/2016_05_06_185215_create_attributes_table.php diff --git a/app/Attribute.php b/app/Attribute.php new file mode 100644 index 000000000..62dc62be7 --- /dev/null +++ b/app/Attribute.php @@ -0,0 +1,19 @@ +morphTo('entity'); + } +} \ No newline at end of file diff --git a/app/Entity.php b/app/Entity.php index a0b25eba7..abf3e834e 100644 --- a/app/Entity.php +++ b/app/Entity.php @@ -54,6 +54,15 @@ abstract class Entity extends Ownable return $this->morphMany(View::class, 'viewable'); } + /** + * Get the Attribute models that have been user assigned to this entity. + * @return \Illuminate\Database\Eloquent\Relations\MorphMany + */ + public function attributes() + { + return $this->morphMany(Attribute::class, 'entity'); + } + /** * Get this entities restrictions. */ @@ -114,6 +123,22 @@ abstract class Entity extends Ownable return strtolower(static::getClassName()); } + /** + * Get an instance of an entity of the given type. + * @param $type + * @return Entity + */ + public static function getEntityInstance($type) + { + $types = ['Page', 'Book', 'Chapter']; + $className = str_replace([' ', '-', '_'], '', ucwords($type)); + if (!in_array($className, $types)) { + return null; + } + + return app('BookStack\\' . $className); + } + /** * Gets a limited-length version of the entities name. * @param int $length diff --git a/app/Http/Controllers/AttributeController.php b/app/Http/Controllers/AttributeController.php new file mode 100644 index 000000000..09523af47 --- /dev/null +++ b/app/Http/Controllers/AttributeController.php @@ -0,0 +1,32 @@ +attributeRepo = $attributeRepo; + } + + + /** + * Get all the Attributes for a particular entity + * @param $entityType + * @param $entityId + */ + public function getForEntity($entityType, $entityId) + { + + } +} diff --git a/app/Http/routes.php b/app/Http/routes.php index 9565b7576..8b7ec3bc2 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -80,11 +80,16 @@ Route::group(['middleware' => 'auth'], function () { Route::delete('/{imageId}', 'ImageController@destroy'); }); - // Ajax routes + // AJAX routes Route::put('/ajax/page/{id}/save-draft', 'PageController@saveDraft'); Route::get('/ajax/page/{id}', 'PageController@getPageAjax'); Route::delete('/ajax/page/{id}', 'PageController@ajaxDestroy'); + // Attribute routes (AJAX) + Route::group(['prefix' => 'ajax/attributes'], function() { + Route::get('/get/{entityType}/{entityId}', 'AttributeController@getForEntity'); + }); + // Links Route::get('/link/{id}', 'PageController@redirectFromLink'); diff --git a/app/Repos/AttributeRepo.php b/app/Repos/AttributeRepo.php new file mode 100644 index 000000000..d5cf5b0fd --- /dev/null +++ b/app/Repos/AttributeRepo.php @@ -0,0 +1,32 @@ +attribute = $attr; + $this->entity = $ent; + $this->permissionService = $ps; + } + + +} \ No newline at end of file diff --git a/app/Services/PermissionService.php b/app/Services/PermissionService.php index 2d5ee97a5..17c1b1285 100644 --- a/app/Services/PermissionService.php +++ b/app/Services/PermissionService.php @@ -400,9 +400,7 @@ class PermissionService } }); - if ($this->isAdmin) return $query; - $this->currentAction = $action; - return $this->entityRestrictionQuery($query); + return $this->enforceEntityRestrictions($query, $action); } /** @@ -413,9 +411,7 @@ class PermissionService */ public function enforceChapterRestrictions($query, $action = 'view') { - if ($this->isAdmin) return $query; - $this->currentAction = $action; - return $this->entityRestrictionQuery($query); + return $this->enforceEntityRestrictions($query, $action); } /** @@ -425,6 +421,17 @@ class PermissionService * @return mixed */ public function enforceBookRestrictions($query, $action = 'view') + { + $this->enforceEntityRestrictions($query, $action); + } + + /** + * Add restrictions for a generic entity + * @param $query + * @param string $action + * @return mixed + */ + public function enforceEntityRestrictions($query, $action = 'view') { if ($this->isAdmin) return $query; $this->currentAction = $action; diff --git a/database/migrations/2016_05_06_185215_create_attributes_table.php b/database/migrations/2016_05_06_185215_create_attributes_table.php new file mode 100644 index 000000000..df3e55421 --- /dev/null +++ b/database/migrations/2016_05_06_185215_create_attributes_table.php @@ -0,0 +1,38 @@ +increments('id'); + $table->integer('entity_id'); + $table->string('entity_type', 100); + $table->string('name'); + $table->string('value'); + $table->timestamps(); + + $table->index('name'); + $table->index('value'); + $table->index(['entity_id', 'entity_type']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('attributes'); + } +}