Skip to content

Instantly share code, notes, and snippets.

@rizqidjamaluddin
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rizqidjamaluddin/1647c62e6a2cd86e32ec to your computer and use it in GitHub Desktop.
Save rizqidjamaluddin/1647c62e6a2cd86e32ec to your computer and use it in GitHub Desktop.
New Gatekeeper package setup, now with bonus policies and per-model authorizer classes
<?php
class BlogCommentAuthorizer extends GatekeeperAuthorizer {
public function register () {
// if you own it, you can read and edit it
$this->enforce([new OwnershipPolicy, new ActionPolicy(['read', 'edit'])]);
// members can read, admins can do anything
$this->enforce(new AclPolicy($this->getAcl()));
// if you've been blocked by this blog post, you can't do anything at all
$this->enforce(new BlogPostBanListPolicy);
// membership past 30 days old can create
$this->enforce(new EstablishedMemberBlogPolicy);
}
public function getAcl() {
return [
'administrator' => ['*'],
'member' => ['read']
]
}
}
<?php
class BlogPostBanListPolicy extends BanListPolicy {
public function check (Actor $actor, ProtectedEntity $entity, $action) {
// sql check, repository, whatever
$row = DB::table('blog_blocks')->where('user_id', $actor->id)->where('post_id', $entity->id)->count();
if ($row) {
// override others
return $this->demandNo();
} else {
return $this->abstain();
}
}
}
<?php
class EstablishedMemberBlogPolicy extends CriteriaPolicy {
protected $allow = ['create'];
public function check (Actor $actor, ProtectedEntity $entity, $action) {
if ($actor->isEstablished() && in_array($action, $this->allow)) {
return $this->voteYes();
} else {
return $this->abstain();
}
}
}
$blogCommentAuthorizer->iAm(Auth::user())->mayI('update', $model)->please();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment