/** * Add a view to the given entity. * @param Entity $entity * @return int */ public function add(Entity $entity) { if ($this->user === null) { return 0; } $view = $entity->views()->where('user_id', '=', $this->user->id)->first(); // Add view if model exists if ($view) { $view->increment('views'); return $view->views; } // Otherwise create new view count $entity->views()->save($this->view->create(['user_id' => $this->user->id, 'views' => 1])); return 1; }
/** * Manually set some permissions on an entity. * @param \BookStack\Entity $entity * @param $actions */ protected function setEntityRestrictions(\BookStack\Entity $entity, $actions) { $entity->restricted = true; $entity->permissions()->delete(); $role = $this->user->roles->first(); $viewerRole = $this->viewer->roles->first(); foreach ($actions as $action) { $entity->permissions()->create(['role_id' => $role->id, 'action' => strtolower($action)]); $entity->permissions()->create(['role_id' => $viewerRole->id, 'action' => strtolower($action)]); } $entity->save(); $entity->load('permissions'); $this->restrictionService->buildJointPermissionsForEntity($entity); $entity->load('jointPermissions'); }
/** * Gets the latest activity for an entity, Filtering out similar * items to prevent a message activity list. * @param Entity $entity * @param int $count * @param int $page * @return array */ public function entityActivity($entity, $count = 20, $page = 0) { if ($entity->isA('book')) { $query = $this->activity->where('book_id', '=', $entity->id); } else { $query = $this->activity->where('entity_type', '=', get_class($entity))->where('entity_id', '=', $entity->id); } $activity = $this->permissionService->filterRestrictedEntityRelations($query, 'activities', 'entity_id', 'entity_type')->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get(); return $this->filterSimilar($activity); }
/** * Gets the latest activity for an entitiy, Filtering out similar * items to prevent a message activity list. * @param Entity $entity * @param int $count * @param int $page * @return array */ function entityActivity($entity, $count = 20, $page = 0) { $activity = $entity->hasMany('BookStack\\Activity')->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get(); return $this->filterSimilar($activity); }
/** * Updates entity restrictions from a request * @param $request * @param Entity $entity */ public function updateEntityPermissionsFromRequest($request, Entity $entity) { $entity->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; $entity->permissions()->delete(); if ($request->has('restrictions')) { foreach ($request->get('restrictions') as $roleId => $restrictions) { foreach ($restrictions as $action => $value) { $entity->permissions()->create(['role_id' => $roleId, 'action' => strtolower($action)]); } } } $entity->save(); $this->permissionService->buildJointPermissionsForEntity($entity); }
/** * Check if an entity has restrictions set on itself or its * parent tree. * @param Entity $entity * @param $action * @return bool|mixed */ public function checkIfRestrictionsSet(Entity $entity, $action) { $this->currentAction = $action; if ($entity->isA('page')) { return $entity->restricted || $entity->chapter && $entity->chapter->restricted || $entity->book->restricted; } elseif ($entity->isA('chapter')) { return $entity->restricted || $entity->book->restricted; } elseif ($entity->isA('book')) { return $entity->restricted; } }
/** * Change the page's parent to the given entity. * @param Page $page * @param Entity $parent */ public function changePageParent(Page $page, Entity $parent) { $book = $parent->isA('book') ? $parent : $parent->book; $page->chapter_id = $parent->isA('chapter') ? $parent->id : 0; $page->save(); $page = $this->changeBook($book->id, $page); $page->load('book'); $this->permissionService->buildJointPermissionsForEntity($book); }
/** * Save an array of tags to an entity * @param Entity $entity * @param array $tags * @return array|\Illuminate\Database\Eloquent\Collection */ public function saveTagsToEntity(Entity $entity, $tags = []) { $entity->tags()->delete(); $newTags = []; foreach ($tags as $tag) { if (trim($tag['name']) === '') { continue; } $newTags[] = $this->newInstanceFromInput($tag); } return $entity->tags()->saveMany($newTags); }