+ $permissionPrefix = $entity->getType() . '-' . $action;
+ $roleHasPermission = $role->hasPermission($permissionPrefix . '-all');
+ $roleHasPermissionOwn = $role->hasPermission($permissionPrefix . '-own');
+
+ if ($entity->isA('book')) {
+
+ if (!$entity->restricted) {
+ return $this->createEntityPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn);
+ } else {
+ $hasAccess = $entity->hasRestriction($role->id, $action);
+ return $this->createEntityPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
+ }
+
+ } elseif ($entity->isA('chapter')) {
+
+ if (!$entity->restricted) {
+ $hasAccessToBook = $entity->book->hasRestriction($role->id, $action);
+ return $this->createEntityPermissionDataArray($entity, $role, $action,
+ ($roleHasPermission && $hasAccessToBook), ($roleHasPermissionOwn && $hasAccessToBook));
+ } else {
+ $hasAccess = $entity->hasRestriction($role->id, $action);
+ return $this->createEntityPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
+ }
+
+ } elseif ($entity->isA('page')) {
+
+ if (!$entity->restricted) {
+ $hasAccessToBook = $entity->book->hasRestriction($role->id, $action);
+ $hasAccessToChapter = $entity->chapter ? ($entity->chapter->hasRestriction($role->id, $action)) : true;
+ return $this->createEntityPermissionDataArray($entity, $role, $action,
+ ($roleHasPermission && $hasAccessToBook && $hasAccessToChapter),
+ ($roleHasPermissionOwn && $hasAccessToBook && $hasAccessToChapter));
+ } else {
+ $hasAccess = $entity->hasRestriction($role->id, $action);
+ return $this->createEntityPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
+ }
+
+ }
+ }
+
+ protected function createEntityPermissionDataArray(Entity $entity, Role $role, $action, $permissionAll, $permissionOwn)
+ {
+ $entityClass = get_class($entity);
+ return [
+ 'role_id' => $role->id,
+ 'entity_id' => $entity->id,
+ 'entity_type' => $entityClass,
+ 'action' => $action,
+ 'has_permission' => $permissionAll,
+ 'has_permission_own' => $permissionOwn,
+ 'created_by' => $entity->created_by
+ ];