Skip to content

Instantly share code, notes, and snippets.

@craig-davis
Last active April 13, 2017 12:22
Show Gist options
  • Save craig-davis/e3ac9f32616bfdf7d1f5b75a0ad4ea11 to your computer and use it in GitHub Desktop.
Save craig-davis/e3ac9f32616bfdf7d1f5b75a0ad4ea11 to your computer and use it in GitHub Desktop.
<?php
class Post
{
protected $sidebarTypes = [8, 11];
protected $authorEditTimeout = 2592000;
...
public function canEdit(User $user) : bool
{
if ($user->isAdmin()) {
return true;
}
if ($user->isSupervisor() && $this->isSidebarPost()) {
return true;
}
if ($user->isEditor() && $this->isEditedBy($user)) {
return true;
}
if ($this->isAuthoredBy($user) && !$this->isTooOldToEdit()) {
return true;
}
return false;
}
...
public function isAuthoredBy(User $author) : bool
{
return $this->author->getId() === $author->getId();
}
public function isEditedBy(User $editor) : bool
{
return $this->editor->getId() === $editor->getId();
}
public function isSidebarPost() : bool
{
return in_array($this->type, self::$sidebarTypes);
}
public function isTooOldToEdit() : bool
{
return $this->created_at < (time() — self::$authorEditTimeout);
}
}
class User
{
const ROLE_EDITOR = 3;
const ROLE_SUPERVISOR = 4;
const ROLE_ADMIN = 5;
public function isEditor() : bool
{
return $this->role == self::ROLE_EDITOR;
}
public function isSupervisor() : bool
{
return $this->role == self::ROLE_SUPERVISOR;
}
public function isAdmin() : bool
{
return $this->role == self::ROLE_SUPERVISOR;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment