diff --git a/app/EntityPermission.php b/app/EntityPermission.php new file mode 100644 index 000000000..6b4ddd212 --- /dev/null +++ b/app/EntityPermission.php @@ -0,0 +1,28 @@ +belongsTo(Role::class); + } + + /** + * Get the entity this points to. + * @return \Illuminate\Database\Eloquent\Relations\MorphOne + */ + public function entity() + { + return $this->morphOne(Entity::class, 'entity'); + } +} diff --git a/app/Services/RestrictionService.php b/app/Services/RestrictionService.php index 50cbe4a51..8d57b9edc 100644 --- a/app/Services/RestrictionService.php +++ b/app/Services/RestrictionService.php @@ -1,6 +1,13 @@ currentUser = auth()->user(); $this->userRoles = $this->currentUser ? $this->currentUser->roles->pluck('id') : []; $this->isAdmin = $this->currentUser ? $this->currentUser->hasRole('admin') : false; + + $this->entityPermission = $entityPermission; + $this->role = $role; + $this->permission = $permission; + $this->book = $book; + $this->chapter = $chapter; + $this->page = $page; + } + + + /** + * Re-generate all entity permission from scratch. + */ + public function buildEntityPermissions() + { + $this->entityPermission->truncate(); + + // Get all roles (Should be the most limited dimension) + $roles = $this->role->load('permissions')->all(); + + // Chunk through all books + $this->book->chunk(500, function ($books) use ($roles) { + $this->createManyEntityPermissions($books, $roles); + }); + + // Chunk through all chapters + $this->chapter->chunk(500, function ($books) use ($roles) { + $this->createManyEntityPermissions($books, $roles); + }); + + // Chunk through all pages + $this->page->chunk(500, function ($books) use ($roles) { + $this->createManyEntityPermissions($books, $roles); + }); + } + + /** + * Create & Save entity permissions for many entities and permissions. + * @param Collection $entities + * @param Collection $roles + */ + protected function createManyEntityPermissions($entities, $roles) + { + $entityPermissions = []; + foreach ($entities as $entity) { + foreach ($roles as $role) { + $entityPermissions[] = $this->createEntityPermission($entity, $role); + } + } + $this->entityPermission->insert($entityPermissions); + } + + + protected function createEntityPermissionData(Entity $entity, Role $role) + { + // TODO - Check the permission values and return an EntityPermission } /** diff --git a/database/migrations/2016_04_20_192649_create_entity_permissions_table.php b/database/migrations/2016_04_20_192649_create_entity_permissions_table.php new file mode 100644 index 000000000..359f25df9 --- /dev/null +++ b/database/migrations/2016_04_20_192649_create_entity_permissions_table.php @@ -0,0 +1,34 @@ +increments('id'); + $table->integer('role_id'); + $table->string('entity_type'); + $table->integer('entity_id'); + $table->string('action'); + $table->boolean('has_permission')->default(false); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('entity_permissions'); + } +}