Showing
7 changed files
with
165 additions
and
7 deletions
app/Attribute.php
0 → 100644
| 1 | +<?php namespace BookStack; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * Class Attribute | ||
| 5 | + * @package BookStack | ||
| 6 | + */ | ||
| 7 | +class Attribute extends Model | ||
| 8 | +{ | ||
| 9 | + protected $fillable = ['name', 'value']; | ||
| 10 | + | ||
| 11 | + /** | ||
| 12 | + * Get the entity that this attribute belongs to | ||
| 13 | + * @return \Illuminate\Database\Eloquent\Relations\MorphTo | ||
| 14 | + */ | ||
| 15 | + public function entity() | ||
| 16 | + { | ||
| 17 | + return $this->morphTo('entity'); | ||
| 18 | + } | ||
| 19 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -55,6 +55,15 @@ abstract class Entity extends Ownable | ... | @@ -55,6 +55,15 @@ abstract class Entity extends Ownable |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | /** | 57 | /** |
| 58 | + * Get the Attribute models that have been user assigned to this entity. | ||
| 59 | + * @return \Illuminate\Database\Eloquent\Relations\MorphMany | ||
| 60 | + */ | ||
| 61 | + public function attributes() | ||
| 62 | + { | ||
| 63 | + return $this->morphMany(Attribute::class, 'entity'); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + /** | ||
| 58 | * Get this entities restrictions. | 67 | * Get this entities restrictions. |
| 59 | */ | 68 | */ |
| 60 | public function permissions() | 69 | public function permissions() |
| ... | @@ -115,6 +124,22 @@ abstract class Entity extends Ownable | ... | @@ -115,6 +124,22 @@ abstract class Entity extends Ownable |
| 115 | } | 124 | } |
| 116 | 125 | ||
| 117 | /** | 126 | /** |
| 127 | + * Get an instance of an entity of the given type. | ||
| 128 | + * @param $type | ||
| 129 | + * @return Entity | ||
| 130 | + */ | ||
| 131 | + public static function getEntityInstance($type) | ||
| 132 | + { | ||
| 133 | + $types = ['Page', 'Book', 'Chapter']; | ||
| 134 | + $className = str_replace([' ', '-', '_'], '', ucwords($type)); | ||
| 135 | + if (!in_array($className, $types)) { | ||
| 136 | + return null; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + return app('BookStack\\' . $className); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + /** | ||
| 118 | * Gets a limited-length version of the entities name. | 143 | * Gets a limited-length version of the entities name. |
| 119 | * @param int $length | 144 | * @param int $length |
| 120 | * @return string | 145 | * @return string | ... | ... |
app/Http/Controllers/AttributeController.php
0 → 100644
| 1 | +<?php namespace BookStack\Http\Controllers; | ||
| 2 | + | ||
| 3 | +use BookStack\Repos\AttributeRepo; | ||
| 4 | +use Illuminate\Http\Request; | ||
| 5 | + | ||
| 6 | +use BookStack\Http\Requests; | ||
| 7 | + | ||
| 8 | +class AttributeController extends Controller | ||
| 9 | +{ | ||
| 10 | + | ||
| 11 | + protected $attributeRepo; | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * AttributeController constructor. | ||
| 15 | + * @param $attributeRepo | ||
| 16 | + */ | ||
| 17 | + public function __construct(AttributeRepo $attributeRepo) | ||
| 18 | + { | ||
| 19 | + $this->attributeRepo = $attributeRepo; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * Get all the Attributes for a particular entity | ||
| 25 | + * @param $entityType | ||
| 26 | + * @param $entityId | ||
| 27 | + */ | ||
| 28 | + public function getForEntity($entityType, $entityId) | ||
| 29 | + { | ||
| 30 | + | ||
| 31 | + } | ||
| 32 | +} |
| ... | @@ -80,11 +80,16 @@ Route::group(['middleware' => 'auth'], function () { | ... | @@ -80,11 +80,16 @@ Route::group(['middleware' => 'auth'], function () { |
| 80 | Route::delete('/{imageId}', 'ImageController@destroy'); | 80 | Route::delete('/{imageId}', 'ImageController@destroy'); |
| 81 | }); | 81 | }); |
| 82 | 82 | ||
| 83 | - // Ajax routes | 83 | + // AJAX routes |
| 84 | Route::put('/ajax/page/{id}/save-draft', 'PageController@saveDraft'); | 84 | Route::put('/ajax/page/{id}/save-draft', 'PageController@saveDraft'); |
| 85 | Route::get('/ajax/page/{id}', 'PageController@getPageAjax'); | 85 | Route::get('/ajax/page/{id}', 'PageController@getPageAjax'); |
| 86 | Route::delete('/ajax/page/{id}', 'PageController@ajaxDestroy'); | 86 | Route::delete('/ajax/page/{id}', 'PageController@ajaxDestroy'); |
| 87 | 87 | ||
| 88 | + // Attribute routes (AJAX) | ||
| 89 | + Route::group(['prefix' => 'ajax/attributes'], function() { | ||
| 90 | + Route::get('/get/{entityType}/{entityId}', 'AttributeController@getForEntity'); | ||
| 91 | + }); | ||
| 92 | + | ||
| 88 | // Links | 93 | // Links |
| 89 | Route::get('/link/{id}', 'PageController@redirectFromLink'); | 94 | Route::get('/link/{id}', 'PageController@redirectFromLink'); |
| 90 | 95 | ... | ... |
app/Repos/AttributeRepo.php
0 → 100644
| 1 | +<?php namespace BookStack\Repos; | ||
| 2 | + | ||
| 3 | +use BookStack\Attribute; | ||
| 4 | +use BookStack\Entity; | ||
| 5 | +use BookStack\Services\PermissionService; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * Class AttributeRepo | ||
| 9 | + * @package BookStack\Repos | ||
| 10 | + */ | ||
| 11 | +class AttributeRepo | ||
| 12 | +{ | ||
| 13 | + | ||
| 14 | + protected $attribute; | ||
| 15 | + protected $entity; | ||
| 16 | + protected $permissionService; | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * AttributeRepo constructor. | ||
| 20 | + * @param Attribute $attr | ||
| 21 | + * @param Entity $ent | ||
| 22 | + * @param PermissionService $ps | ||
| 23 | + */ | ||
| 24 | + public function __construct(Attribute $attr, Entity $ent, PermissionService $ps) | ||
| 25 | + { | ||
| 26 | + $this->attribute = $attr; | ||
| 27 | + $this->entity = $ent; | ||
| 28 | + $this->permissionService = $ps; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + | ||
| 32 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -400,9 +400,7 @@ class PermissionService | ... | @@ -400,9 +400,7 @@ class PermissionService |
| 400 | } | 400 | } |
| 401 | }); | 401 | }); |
| 402 | 402 | ||
| 403 | - if ($this->isAdmin) return $query; | 403 | + return $this->enforceEntityRestrictions($query, $action); |
| 404 | - $this->currentAction = $action; | ||
| 405 | - return $this->entityRestrictionQuery($query); | ||
| 406 | } | 404 | } |
| 407 | 405 | ||
| 408 | /** | 406 | /** |
| ... | @@ -413,9 +411,7 @@ class PermissionService | ... | @@ -413,9 +411,7 @@ class PermissionService |
| 413 | */ | 411 | */ |
| 414 | public function enforceChapterRestrictions($query, $action = 'view') | 412 | public function enforceChapterRestrictions($query, $action = 'view') |
| 415 | { | 413 | { |
| 416 | - if ($this->isAdmin) return $query; | 414 | + return $this->enforceEntityRestrictions($query, $action); |
| 417 | - $this->currentAction = $action; | ||
| 418 | - return $this->entityRestrictionQuery($query); | ||
| 419 | } | 415 | } |
| 420 | 416 | ||
| 421 | /** | 417 | /** |
| ... | @@ -426,6 +422,17 @@ class PermissionService | ... | @@ -426,6 +422,17 @@ class PermissionService |
| 426 | */ | 422 | */ |
| 427 | public function enforceBookRestrictions($query, $action = 'view') | 423 | public function enforceBookRestrictions($query, $action = 'view') |
| 428 | { | 424 | { |
| 425 | + $this->enforceEntityRestrictions($query, $action); | ||
| 426 | + } | ||
| 427 | + | ||
| 428 | + /** | ||
| 429 | + * Add restrictions for a generic entity | ||
| 430 | + * @param $query | ||
| 431 | + * @param string $action | ||
| 432 | + * @return mixed | ||
| 433 | + */ | ||
| 434 | + public function enforceEntityRestrictions($query, $action = 'view') | ||
| 435 | + { | ||
| 429 | if ($this->isAdmin) return $query; | 436 | if ($this->isAdmin) return $query; |
| 430 | $this->currentAction = $action; | 437 | $this->currentAction = $action; |
| 431 | return $this->entityRestrictionQuery($query); | 438 | return $this->entityRestrictionQuery($query); | ... | ... |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +use Illuminate\Database\Schema\Blueprint; | ||
| 4 | +use Illuminate\Database\Migrations\Migration; | ||
| 5 | + | ||
| 6 | +class CreateAttributesTable extends Migration | ||
| 7 | +{ | ||
| 8 | + /** | ||
| 9 | + * Run the migrations. | ||
| 10 | + * | ||
| 11 | + * @return void | ||
| 12 | + */ | ||
| 13 | + public function up() | ||
| 14 | + { | ||
| 15 | + Schema::create('attributes', function (Blueprint $table) { | ||
| 16 | + $table->increments('id'); | ||
| 17 | + $table->integer('entity_id'); | ||
| 18 | + $table->string('entity_type', 100); | ||
| 19 | + $table->string('name'); | ||
| 20 | + $table->string('value'); | ||
| 21 | + $table->timestamps(); | ||
| 22 | + | ||
| 23 | + $table->index('name'); | ||
| 24 | + $table->index('value'); | ||
| 25 | + $table->index(['entity_id', 'entity_type']); | ||
| 26 | + }); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * Reverse the migrations. | ||
| 31 | + * | ||
| 32 | + * @return void | ||
| 33 | + */ | ||
| 34 | + public function down() | ||
| 35 | + { | ||
| 36 | + Schema::drop('attributes'); | ||
| 37 | + } | ||
| 38 | +} |
-
Please register or sign in to post a comment