Skip to content

Commit 4f238b0

Browse files
committed
added permission checking for querying content types
1 parent b5ecb62 commit 4f238b0

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

src/Darryldecode/Backend/Components/ContentBuilder/Commands/QueryContentCommand.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function handle(Dispatcher $dispatcher, ContentType $contentType, Content
6464
// fire before query
6565
$dispatcher->fire('content.beforeQuery', array($this->args));
6666

67+
/** @todo add permission check here: {contentType}.manage */
68+
6769
// query
6870
$results = $this->query($contentType, $content, $config);
6971

@@ -104,12 +106,24 @@ private function query($contentType, $content, $config)
104106

105107
if( !is_null($this->id) && ($this->id != '') )
106108
{
107-
return $q->find($this->id);
109+
$result = $q->find($this->id);
108110
}
109111
else
110112
{
111-
return $q->ofSlug($this->slug)->ofTitle($this->title)->first();
113+
$result = $q->ofSlug($this->slug)->ofTitle($this->title)->first();
114+
}
115+
116+
if( ! $this->disablePermissionChecking )
117+
{
118+
$requiredPermission = $result->type->type.'.manage';
119+
120+
if( ! $this->user->hasAnyPermission([$requiredPermission]) )
121+
{
122+
return new CommandResult(false, "Not enough permission.", null, 403);
123+
}
112124
}
125+
126+
return $result;
113127
}
114128

115129
/**
@@ -119,7 +133,11 @@ private function query($contentType, $content, $config)
119133
*/
120134
protected function createContentModel($content, $config)
121135
{
122-
$contentModelUsed = $config->get('backend.backend.content_model');
136+
if( ! $contentModelUsed = $config->get('backend.backend.content_model') )
137+
{
138+
return $content;
139+
};
140+
123141
$contentModelUsed = new $contentModelUsed();
124142

125143
if( $contentModelUsed instanceof Content )

src/Darryldecode/Backend/Components/ContentBuilder/Commands/QueryContentTypeCommand.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public function handle(ContentType $contentType, Dispatcher $dispatcher)
4242
// begin before query
4343
$dispatcher->fire('contentType.beforeQuery', array($this->args));
4444

45+
// check if has permission
46+
if( ! $this->disablePermissionChecking )
47+
{
48+
if( ! $this->user->hasAnyPermission(['contentBuilder.manage']) )
49+
{
50+
return new CommandResult(false, "Not enough permission.", null, 403);
51+
}
52+
}
53+
4554
// begin query
4655
$results = $contentType->with(array('terms.taxonomy','taxonomies','taxonomies.terms','formGroups'))->ofType($this->type)->get();
4756

src/Darryldecode/Backend/Components/ContentBuilder/Commands/QueryContentsCommand.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public function handle(Dispatcher $dispatcher, ContentType $contentType, Content
131131
// fire before query
132132
$dispatcher->fire('contents.beforeQuery', array($this->args));
133133

134+
/** @todo add permission check here: {contentType}.manage */
135+
134136
// query
135137
$results = $this->query($contentType, $content, $config);
136138

@@ -175,8 +177,9 @@ protected function query($contentType, $content, $config)
175177
$q->where('author_id', $this->authorId);
176178
}
177179

178-
// check if type is provided so we can include it in our query conditions
179-
if( !is_null($this->type) && ($this->type != '') )
180+
// check if type is provided, we need to provide content type
181+
// we will not allow to query all of contents
182+
if( ! is_null($this->type) && ($this->type != '') )
180183
{
181184
if( is_numeric($this->type) )
182185
{
@@ -193,8 +196,23 @@ protected function query($contentType, $content, $config)
193196
{
194197
$q->where('type',$cType->type);
195198
});
199+
200+
// let's check first if the user querying has the permission to access this kind of content
201+
if( ! $this->disablePermissionChecking )
202+
{
203+
$requiredPermission = $cType->type.'.manage';
204+
205+
if( ! $this->user->hasAnyPermission([$requiredPermission]) )
206+
{
207+
return new CommandResult(false, "Not enough permission.", null, 403);
208+
}
209+
}
196210
}
197211
}
212+
else
213+
{
214+
return new CommandResult(false, "Content Type should be provided.", null, 400);
215+
}
198216

199217
// check if terms are provided so we can include it in query conditions
200218
if( !is_null($this->terms) && ($this->terms != '') )
@@ -306,7 +324,11 @@ private function extractTerms($terms)
306324
*/
307325
protected function createContentModel($content, $config)
308326
{
309-
$contentModelUsed = $config->get('backend.backend.content_model');
327+
if( ! $contentModelUsed = $config->get('backend.backend.content_model') )
328+
{
329+
return $content;
330+
};
331+
310332
$contentModelUsed = new $contentModelUsed();
311333

312334
if( $contentModelUsed instanceof Content )

0 commit comments

Comments
 (0)