Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
ブログ記事と固定ページの非認証APIで草稿を非公開に変更
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Apr 16, 2023
1 parent ac10b4a commit eca9141
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
10 changes: 6 additions & 4 deletions plugins/baser-core/src/Controller/Api/PagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public function index(PagesServiceInterface $service)
{
$this->request->allowMethod('get');
$queryParams = $this->getRequest()->getQueryParams();
if (isset($queryParams['status']) || isset($queryParams['contain'])) {
if (isset($queryParams['status']) || isset($queryParams['contain']) || isset($queryParams['draft'])) {
throw new ForbiddenException();
}

$queryParams = array_merge([
'status' => 'publish',
'contain' => null
'contain' => null,
'draft' => false
], $queryParams);

$this->set([
Expand All @@ -69,12 +70,13 @@ public function view(PagesServiceInterface $service, $id)
{
$this->request->allowMethod('get');
$queryParams = $this->getRequest()->getQueryParams();
if (isset($queryParams['status']) || isset($queryParams['contain'])) {
if (isset($queryParams['status']) || isset($queryParams['contain']) || isset($queryParams['draft'])) {
throw new ForbiddenException();
}

$queryParams = array_merge([
'status' => 'publish'
'status' => 'publish',
'draft' => false
], $queryParams);

$page = $message = null;
Expand Down
19 changes: 14 additions & 5 deletions plugins/baser-core/src/Service/PagesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,21 @@ public function get($id, array $options = []): EntityInterface
{
$options = array_merge([
'status' => '',
'contain' => ['Contents' => ['Sites']]
'contain' => ['Contents' => ['Sites']],
'draft' => false
], $options);
$conditions = [];
if ($options['status'] === 'publish') {
$conditions = $this->Pages->Contents->getConditionAllowPublish();
}
return $this->Pages->get($id, [
$entity = $this->Pages->get($id, [
'contain' => $options['contain'],
'conditions' => $conditions
'conditions' => $conditions,
]);
if($options['draft'] === false) {
unset($entity->draft);
}
return $entity;
}

/**
Expand Down Expand Up @@ -178,11 +183,15 @@ public function getIndex(array $queryParams = []): Query
{
$options = array_merge([
'status' => '',
'contain' => ['Contents']
'contain' => ['Contents'],
'draft' => null
], $queryParams);

$fields = $this->Pages->getSchema()->columns();
if (is_null($options['contain'])) {
if (is_null($options['contain']) || $options['draft'] === false) {
if($options['draft'] === false) {
unset($fields[array_search('draft', $fields)]);
}
$query = $this->Pages->find()->contain('Contents')->select($fields);
} else {
$query = $this->Pages->find()->contain($options['contain']);
Expand Down
10 changes: 6 additions & 4 deletions plugins/bc-blog/src/Controller/Api/BlogPostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public function index(BlogPostsServiceInterface $service)
$this->request->allowMethod('get');

$queryParams = $this->getRequest()->getQueryParams();
if (isset($queryParams['status']) || isset($queryParams['contain'])) {
if (isset($queryParams['status']) || isset($queryParams['contain']) || isset($queryParams['draft'])) {
throw new ForbiddenException();
}

$queryParams = array_merge([
'contain' => null,
'status' => 'publish'
'status' => 'publish',
'draft' => false
], $queryParams);
$this->set([
'blogPosts' => $this->paginate($service->getIndex($queryParams))
Expand All @@ -70,12 +71,13 @@ public function view(BlogPostsServiceInterface $service, $id)
{
$this->request->allowMethod('get');
$queryParams = $this->getRequest()->getQueryParams();
if (isset($queryParams['status']) || isset($queryParams['contain'])) {
if (isset($queryParams['status']) || isset($queryParams['contain']) || isset($queryParams['draft'])) {
throw new ForbiddenException();
}

$queryParams = array_merge([
'status' => 'publish'
'status' => 'publish',
'draft' => false
], $queryParams);

$blogPost = $message = null;
Expand Down
19 changes: 15 additions & 4 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,22 @@ public function get(int $id, array $options = [])
'BlogContents' => ['Contents' => ['Sites']],
'BlogCategories',
'BlogTags'
]
],
'draft' => null
], $options);
$conditions = [];
if ($options['status'] === 'publish') {
$conditions = $this->BlogPosts->getConditionAllowPublish();
$conditions = array_merge($conditions, $this->BlogPosts->BlogContents->Contents->getConditionAllowPublish());
}
return $this->BlogPosts->get($id, [
$entity = $this->BlogPosts->get($id, [
'conditions' => $conditions,
'contain' => $options['contain']]);
if($options['draft'] === false) {
unset($entity->content_draft);
unset($entity->detail_draft);
}
return $entity;
}

/**
Expand Down Expand Up @@ -129,7 +135,8 @@ public function getIndex(array $queryParams = []): Query
'BlogContents',
'BlogComments',
'BlogTags',
]
],
'draft' => null
], $queryParams);

if (!empty($options['num'])) $options['limit'] = $options['num'];
Expand Down Expand Up @@ -242,8 +249,12 @@ protected function createIndexConditions(Query $query, array $params)
// ステータス
if (($params['status'] === 'publish' || (string)$params['status'] === '1') && !$params['preview']) {
$conditions = $this->BlogPosts->getConditionAllowPublish();
if(empty($params['contain'])) {
if(empty($params['contain']) || $params['draft'] === false) {
$fields = $this->BlogPosts->getSchema()->columns();
if($params['draft'] === false) {
unset($fields[array_search('content_draft', $fields)]);
unset($fields[array_search('detail_draft', $fields)]);
}
$query = $this->BlogPosts->find()->contain(['BlogContents' => ['Contents']])->select($fields);
} elseif(!isset($params['contain']['BlogContents']['Contents'])) {
$query = $this->BlogPosts->find()->contain(array_merge_recursive($query->getContain(), ['BlogContents' => ['Contents']]));
Expand Down

0 comments on commit eca9141

Please sign in to comment.