Skip to content

Commit

Permalink
Add policy support
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Mar 6, 2017
1 parent 3b851f1 commit 842eb06
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.lock
docs
vendor

.idea
.idea
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class BookController extends Controller
$this
->setTransformer($transformer)
->setModel($bookModel);
//optionally add `shouldAuthorize` to add authorize checks in built in traits
}
}
```
Expand Down
10 changes: 10 additions & 0 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public function setModel($model) {
return $this;
}

/**
* Calls authorization methods on the default traits
*
* @return mixed
*/
public function shouldAuthorize() {
$this->shouldAuthorize = true;
return $this;
}

/**
* Sets resource key for fractal
*
Expand Down
8 changes: 7 additions & 1 deletion src/Transmitters/Destroy.php → src/Traits/Destroy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ trait Destroy
*/
public function destroy($id)
{
$this->model->findOrFail($id)->delete();
$item = $this->model->findOrFail($id);

if ($this->shouldAuthorize) {
$this->authorize('delete', $item);
}

$item->delete();
return $this->respondWithNoContent();
}
}
4 changes: 4 additions & 0 deletions src/Transmitters/Index.php → src/Traits/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ trait Index
*/
public function index()
{
if ($this->shouldAuthorize) {
$this->authorize('index', get_class($this->model));
}

return $this->respondWithPaginatedCollection($this->model);
}
}
8 changes: 7 additions & 1 deletion src/Transmitters/Show.php → src/Traits/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ trait Show
public function show($id)
{
return $this->respondWithItem($this->model, function ($model) use ($id) {
return $model->findOrFail($id);
$item = $model->findOrFail($id);

if ($this->shouldAuthorize) {
$this->authorize('view', $item);
}

return $item;
});
}
}
4 changes: 4 additions & 0 deletions src/Transmitters/Store.php → src/Traits/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ trait Store
*/
public function store()
{
if ($this->shouldAuthorize) {
$this->authorize('create', get_class($this->model));
}

$item = $this->model->create(request()->all());
return $this->respondWithItem($item);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Transmitters/Update.php → src/Traits/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function update($id, Request $request)
{
return $this->respondWithItem($this->model, function ($model) use ($id, $request) {
$item = $model->findOrFail($id);

if ($this->shouldAuthorize) {
$this->authorize('update', $item);
}

$item->fill($request->all());
$item->save();
return $item;
Expand Down

0 comments on commit 842eb06

Please sign in to comment.