From d3beb8bc53b6ee285bd00e751afff3e8edefd90b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 4 May 2014 19:06:40 +0200 Subject: [PATCH] Implemented new enums --- src/Access.php | 28 ++++++------ src/Api/Jobs/AddPostJob.php | 4 +- src/Api/Jobs/EditPostSafetyJob.php | 6 +-- src/Api/Jobs/EditUserAccessRankJob.php | 8 ++-- src/Api/Jobs/GetPostContentJob.php | 2 +- src/Api/Jobs/GetPostJob.php | 2 +- src/Api/Jobs/GetPostThumbJob.php | 2 +- src/Auth.php | 4 +- src/Controllers/UserController.php | 8 ++-- src/Enum.php | 12 +++--- src/IValidatable.php | 5 +++ src/Models/AbstractCrudModel.php | 11 ++++- src/Models/Entities/PostEntity.php | 43 +++++++++++++------ src/Models/Entities/UserEntity.php | 32 +++++++++----- src/Models/Enums/AccessRank.php | 33 +++++++++++++- src/Models/Enums/PostSafety.php | 37 ++++++++++++++-- src/Models/Enums/PostType.php | 25 ++++++++++- src/Models/Enums/Privilege.php | 10 ++++- src/Models/PostModel.php | 42 +++++++++--------- .../SearchParsers/CommentSearchParser.php | 7 ++- src/Models/SearchParsers/PostSearchParser.php | 9 +++- src/Models/SearchParsers/TagSearchParser.php | 7 ++- src/Models/UserModel.php | 27 ++++++++---- src/Views/post-edit.phtml | 6 +-- src/Views/post-file-render.phtml | 8 ++-- src/Views/post-small.phtml | 2 +- src/Views/post-upload.phtml | 4 +- src/Views/post-view.phtml | 6 +-- src/Views/top-navigation.phtml | 10 ++--- src/Views/user-edit.phtml | 13 +++--- src/Views/user-settings.phtml | 6 +-- src/Views/user-view.phtml | 2 +- 32 files changed, 290 insertions(+), 131 deletions(-) create mode 100644 src/IValidatable.php diff --git a/src/Access.php b/src/Access.php index b16bd0e6..9df388d4 100644 --- a/src/Access.php +++ b/src/Access.php @@ -40,24 +40,22 @@ class Access $user = Auth::getCurrentUser(); $minAccessRank = AccessRank::Nobody; - $key = TextCaseConverter::convert(Privilege::toString($privilege->primary), + $key = TextCaseConverter::convert($privilege->toString(), TextCaseConverter::CAMEL_CASE, TextCaseConverter::SPINAL_CASE); - if (isset(self::$privileges[$key])) - { - $minAccessRank = self::$privileges[$key]; - } - if ($privilege->secondary != null) - { - $key2 = $key . '.' . strtolower($privilege->secondary); - if (isset(self::$privileges[$key2])) - { - $minAccessRank = self::$privileges[$key2]; - } - } + $privilege->secondary = null; + $key2 = TextCaseConverter::convert($privilege->toString(), + TextCaseConverter::CAMEL_CASE, + TextCaseConverter::SPINAL_CASE); - return intval($user->accessRank) >= $minAccessRank; + + if (isset(self::$privileges[$key])) + $minAccessRank = self::$privileges[$key]; + elseif (isset(self::$privileges[$key2])) + $minAccessRank = self::$privileges[$key2]; + + return $user->getAccessRank()->toInteger() >= $minAccessRank; } public static function assertAuthentication() @@ -98,7 +96,7 @@ class Access return array_filter(PostSafety::getAll(), function($safety) { - return Access::check(new Privilege(Privilege::ListPosts, PostSafety::toString($safety))) + return Access::check(new Privilege(Privilege::ListPosts, $safety->toString())) and Auth::getCurrentUser()->hasEnabledSafety($safety); }); } diff --git a/src/Api/Jobs/AddPostJob.php b/src/Api/Jobs/AddPostJob.php index 05a0cbaa..7a77b3e1 100644 --- a/src/Api/Jobs/AddPostJob.php +++ b/src/Api/Jobs/AddPostJob.php @@ -27,7 +27,7 @@ class AddPostJob extends AbstractJob //- move this to PostEntity::isValid() //- create IValidatable interface //- enforce entity validity upon calling save() in models - if (empty($post->type)) + if (empty($post->getType())) throw new SimpleException('No post type detected; upload faled'); //save to db @@ -43,7 +43,7 @@ class AddPostJob extends AbstractJob : TextHelper::reprUser(Auth::getCurrentUser()), 'post' => TextHelper::reprPost($post), 'tags' => TextHelper::reprTags($post->getTags()), - 'safety' => PostSafety::toString($post->safety), + 'safety' => $post->getSafety()->toString(), 'source' => $post->source]); //finish diff --git a/src/Api/Jobs/EditPostSafetyJob.php b/src/Api/Jobs/EditPostSafetyJob.php index 6ed468ef..a41c9b6a 100644 --- a/src/Api/Jobs/EditPostSafetyJob.php +++ b/src/Api/Jobs/EditPostSafetyJob.php @@ -6,9 +6,9 @@ class EditPostSafetyJob extends AbstractPostEditJob public function execute() { $post = $this->post; - $newSafety = $this->getArgument(self::SAFETY); + $newSafety = new PostSafety($this->getArgument(self::SAFETY)); - $oldSafety = $post->safety; + $oldSafety = $post->getSafety(); $post->setSafety($newSafety); if (!$this->skipSaving) @@ -19,7 +19,7 @@ class EditPostSafetyJob extends AbstractPostEditJob LogHelper::log('{user} changed safety of {post} to {safety}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'post' => TextHelper::reprPost($post), - 'safety' => PostSafety::toString($post->safety)]); + 'safety' => $post->getSafety()->toString()]); } return $post; diff --git a/src/Api/Jobs/EditUserAccessRankJob.php b/src/Api/Jobs/EditUserAccessRankJob.php index b62f070c..357b6805 100644 --- a/src/Api/Jobs/EditUserAccessRankJob.php +++ b/src/Api/Jobs/EditUserAccessRankJob.php @@ -6,13 +6,13 @@ class EditUserAccessRankJob extends AbstractUserEditJob public function execute() { $user = $this->user; - $newAccessRank = UserModel::validateAccessRank($this->getArgument(self::NEW_ACCESS_RANK)); + $newAccessRank = new AccessRank($this->getArgument(self::NEW_ACCESS_RANK)); - $oldAccessRank = $user->accessRank; + $oldAccessRank = $user->getAccessRank(); if ($oldAccessRank == $newAccessRank) return $user; - $user->accessRank = $newAccessRank; + $user->setAccessRank($newAccessRank); if (!$this->skipSaving) UserModel::save($user); @@ -20,7 +20,7 @@ class EditUserAccessRankJob extends AbstractUserEditJob LogHelper::log('{user} changed {subject}\'s access rank to {rank}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'subject' => TextHelper::reprUser($user), - 'rank' => AccessRank::toString($newAccessRank)]); + 'rank' => $newAccessRank->toString()]); return $user; } diff --git a/src/Api/Jobs/GetPostContentJob.php b/src/Api/Jobs/GetPostContentJob.php index 96eff85f..ae3d8c9a 100644 --- a/src/Api/Jobs/GetPostContentJob.php +++ b/src/Api/Jobs/GetPostContentJob.php @@ -38,7 +38,7 @@ class GetPostContentJob extends AbstractJob if ($post->hidden) $privileges []= new Privilege(Privilege::ViewPost, 'hidden'); - $privileges []= new Privilege(Privilege::ViewPost, PostSafety::toString($post->safety)); + $privileges []= new Privilege(Privilege::ViewPost, $post->getSafety()->toString()); return $privileges; } diff --git a/src/Api/Jobs/GetPostJob.php b/src/Api/Jobs/GetPostJob.php index 898bda24..f42194ac 100644 --- a/src/Api/Jobs/GetPostJob.php +++ b/src/Api/Jobs/GetPostJob.php @@ -18,7 +18,7 @@ class GetPostJob extends AbstractPostJob if ($post->hidden) $privileges []= new Privilege(Privilege::ViewPost, 'hidden'); - $privileges []= new Privilege(Privilege::ViewPost, PostSafety::toString($post->safety)); + $privileges []= new Privilege(Privilege::ViewPost, $post->getSafety()->toString()); return $privileges; } diff --git a/src/Api/Jobs/GetPostThumbJob.php b/src/Api/Jobs/GetPostThumbJob.php index 514372e1..c87739c3 100644 --- a/src/Api/Jobs/GetPostThumbJob.php +++ b/src/Api/Jobs/GetPostThumbJob.php @@ -20,7 +20,7 @@ class GetPostThumbJob extends AbstractJob if ($post->hidden) Access::assert(new Privilege(Privilege::ListPosts, 'hidden')); - Access::assert(new Privilege(Privilege::ListPosts, PostSafety::toString($post->safety))); + Access::assert(new Privilege(Privilege::ListPosts, $post->getSafety()->toString())); $post->generateThumb($width, $height); diff --git a/src/Auth.php b/src/Auth.php index e3e91c09..f4ba0840 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -70,7 +70,7 @@ class Auth } else { - $_SESSION['logged-in'] = $user->accessRank != AccessRank::Anonymous; + $_SESSION['logged-in'] = $user->getAccessRank()->toInteger() != AccessRank::Anonymous; $_SESSION['user'] = serialize($user); } } @@ -87,7 +87,7 @@ class Auth $dummy = UserModel::spawn(); $dummy->id = null; $dummy->name = UserModel::getAnonymousName(); - $dummy->accessRank = AccessRank::Anonymous; + $dummy->setAccessRank(new AccessRank(AccessRank::Anonymous)); return $dummy; } } diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index d25b3f90..d6a9a3da 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -81,7 +81,7 @@ class UserController $user->enablePostTagTitles(InputHelper::get('post-tag-titles')); $user->enableHidingDislikedPosts(InputHelper::get('hide-disliked-posts')); - if ($user->accessRank != AccessRank::Anonymous) + if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous) UserModel::save($user); if ($user->id == Auth::getCurrentUser()->id) Auth::setCurrentUser($user); @@ -168,12 +168,12 @@ class UserController Privilege::ChangeUserSettings, Access::getIdentity($user))); - if (!in_array($safety, PostSafety::getAll())) - throw new SimpleExcetpion('Invalid safety'); + $safety = new PostSafety($safety); + $safety->validate(); $user->enableSafety($safety, !$user->hasEnabledSafety($safety)); - if ($user->accessRank != AccessRank::Anonymous) + if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous) UserModel::save($user); Auth::setCurrentUser($user); } diff --git a/src/Enum.php b/src/Enum.php index 6987502c..3188f4d7 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -1,21 +1,23 @@ getConstants(); return array_search($constant, $constants); } - public static function toDisplayString($constant) + public function toDisplayString() { - return TextCaseConverter::convert(static::toString($constant), + return TextCaseConverter::convert($this->toString(), TextCaseConverter::SNAKE_CASE, TextCaseConverter::BLANK_CASE); } - public static function getAll() + public static function getAllConstants() { $cls = new ReflectionClass(get_called_class()); $constants = $cls->getConstants(); diff --git a/src/IValidatable.php b/src/IValidatable.php new file mode 100644 index 00000000..13666bea --- /dev/null +++ b/src/IValidatable.php @@ -0,0 +1,5 @@ +$key = $val; + if (property_exists($entity, $key)) + { + $reflectionProperty = new ReflectionProperty(get_class($entity), $key); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($entity, $val); + } + else + { + $entity->$key = $val; + } } return $entity; } diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 11094c8b..d9c50b99 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -4,22 +4,22 @@ use \Chibi\Database as Database; class PostEntity extends AbstractEntity { - public $type; + protected $type; public $name; public $origName; public $fileHash; public $fileSize; public $mimeType; - public $safety; + protected $safety; public $hidden; public $uploadDate; public $imageWidth; public $imageHeight; public $uploaderId; public $source; - public $commentCount; - public $favCount; - public $score; + public $commentCount = 0; + public $favCount = 0; + public $score = 0; public function getUploader() { @@ -173,9 +173,26 @@ class PostEntity extends AbstractEntity $this->hidden = boolval($hidden); } - public function setSafety($safety) + public function getType() { - $this->safety = PostModel::validateSafety($safety); + return $this->type; + } + + public function setType(PostType $type) + { + $type->validate(); + $this->type = $type; + } + + public function getSafety() + { + return $this->safety; + } + + public function setSafety(PostSafety $safety) + { + $safety->validate(); + $this->safety = $safety; } public function setSource($source) @@ -233,7 +250,7 @@ class PostEntity extends AbstractEntity $srcPath = $this->getFullPath(); $dstPath = $this->getThumbDefaultPath($width, $height); - if ($this->type == PostType::Youtube) + if ($this->getType()->toInteger() == PostType::Youtube) { return ThumbnailHelper::generateFromUrl( 'http://img.youtube.com/vi/' . $this->fileHash . '/mqdefault.jpg', @@ -263,13 +280,13 @@ class PostEntity extends AbstractEntity case 'image/png': case 'image/jpeg': list ($imageWidth, $imageHeight) = getimagesize($srcPath); - $this->type = PostType::Image; + $this->setType(new PostType(PostType::Image)); $this->imageWidth = $imageWidth; $this->imageHeight = $imageHeight; break; case 'application/x-shockwave-flash': list ($imageWidth, $imageHeight) = getimagesize($srcPath); - $this->type = PostType::Flash; + $this->setType(new PostType(PostType::Flash)); $this->imageWidth = $imageWidth; $this->imageHeight = $imageHeight; break; @@ -280,7 +297,7 @@ class PostEntity extends AbstractEntity case 'video/x-flv': case 'video/3gpp': list ($imageWidth, $imageHeight) = getimagesize($srcPath); - $this->type = PostType::Video; + $this->setType(new PostType(PostType::Video)); $this->imageWidth = $imageWidth; $this->imageHeight = $imageHeight; break; @@ -315,7 +332,7 @@ class PostEntity extends AbstractEntity if (preg_match('/youtube.com\/watch.*?=([a-zA-Z0-9_-]+)/', $srcUrl, $matches)) { $youtubeId = $matches[1]; - $this->type = PostType::Youtube; + $this->setType(new PostType(PostType::Youtube)); $this->mimeType = null; $this->fileSize = null; $this->fileHash = $youtubeId; @@ -360,7 +377,7 @@ class PostEntity extends AbstractEntity $x []= TextHelper::reprTag($tag->name); foreach ($this->getRelations() as $relatedPost) $x []= TextHelper::reprPost($relatedPost); - $x []= $this->safety; + $x []= $this->getSafety()->toInteger(); $x []= $this->source; $x []= $this->fileHash; natcasesort($x); diff --git a/src/Models/Entities/UserEntity.php b/src/Models/Entities/UserEntity.php index 083a8a11..871a08a2 100644 --- a/src/Models/Entities/UserEntity.php +++ b/src/Models/Entities/UserEntity.php @@ -12,10 +12,21 @@ class UserEntity extends AbstractEntity public $emailConfirmed; public $joinDate; public $lastLoginDate; - public $accessRank; + protected $accessRank; public $settings; public $banned; + public function getAccessRank() + { + return $this->accessRank; + } + + public function setAccessRank(AccessRank $accessRank) + { + $accessRank->validate(); + $this->accessRank = $accessRank; + } + public function getAvatarUrl($size = 32) { $subject = !empty($this->emailConfirmed) @@ -44,32 +55,31 @@ class UserEntity extends AbstractEntity $this->settings = $settings; } - public function hasEnabledSafety($safety) + public function hasEnabledSafety(PostSafety $safety) { $all = $this->getSetting(UserModel::SETTING_SAFETY); if (!$all) - return $safety == PostSafety::Safe; - return $all & PostSafety::toFlag($safety); + return $safety->toInteger() == PostSafety::Safe; + return $all & $safety->toFlag(); } - public function enableSafety($safety, $enabled) + public function enableSafety(PostSafety $safety, $enabled) { $all = $this->getSetting(UserModel::SETTING_SAFETY); - if (!$all) - $all = PostSafety::toFlag(PostSafety::Safe); $new = $all; if (!$enabled) { - $new &= ~PostSafety::toFlag($safety); - if (!$new) - $new = PostSafety::toFlag(PostSafety::Safe); + $new &= ~$safety->toFlag(); } else { - $new |= PostSafety::toFlag($safety); + $new |= $safety->toFlag(); } + if (!$new) + $new = (new PostSafety(PostSafety::Safe))->toFlag(); + $this->setSetting(UserModel::SETTING_SAFETY, $new); } diff --git a/src/Models/Enums/AccessRank.php b/src/Models/Enums/AccessRank.php index 8b013917..2216a1cf 100644 --- a/src/Models/Enums/AccessRank.php +++ b/src/Models/Enums/AccessRank.php @@ -1,5 +1,5 @@ accessRank = $accessRank; + } + + public function toInteger() + { + return $this->accessRank; + } + + public function toString() + { + return self::_toString($this->accessRank); + } + + public static function getAll() + { + return array_map(function($constantName) + { + return new self($constantName); + }, self::getAllConstants()); + } + + public function validate() + { + if (!in_array($this->accessRank, self::getAllConstants())) + throw new SimpleException('Invalid access rank "%s"', $this->accessRank); + } } diff --git a/src/Models/Enums/PostSafety.php b/src/Models/Enums/PostSafety.php index 845977b5..99a4342e 100644 --- a/src/Models/Enums/PostSafety.php +++ b/src/Models/Enums/PostSafety.php @@ -1,12 +1,43 @@ safety = $safety; + } + + public function toInteger() + { + return $this->safety; + } + + public function toFlag() + { + return pow(2, $this->safety); + } + + public function toString() + { + return self::_toString($this->safety); + } + + public static function getAll() + { + return array_map(function($constantName) + { + return new self($constantName); + }, self::getAllConstants()); + } + + public function validate() + { + if (!in_array($this->safety, self::getAllConstants())) + throw new SimpleException('Invalid safety type "%s"', $this->safety); } } diff --git a/src/Models/Enums/PostType.php b/src/Models/Enums/PostType.php index 3eb305a1..351cbf4c 100644 --- a/src/Models/Enums/PostType.php +++ b/src/Models/Enums/PostType.php @@ -1,8 +1,31 @@ type = $type; + } + + public function toInteger() + { + return $this->type; + } + + public function toString() + { + return self::_toString($this->type); + } + + public function validate() + { + if (!in_array($this->type, self::getAllConstants())) + throw new SimpleException('Invalid post type "%s"', $this->type); + } } diff --git a/src/Models/Enums/Privilege.php b/src/Models/Enums/Privilege.php index 2c58cff1..4b269d34 100644 --- a/src/Models/Enums/Privilege.php +++ b/src/Models/Enums/Privilege.php @@ -51,6 +51,14 @@ class Privilege extends Enum public function __construct($primary, $secondary = null) { $this->primary = $primary; - $this->secondary = $secondary; + $this->secondary = strtolower($secondary); + } + + public function toString() + { + $string = self::_toString($this->primary); + if ($this->secondary) + $string .= '.' . $this->secondary; + return $string; } } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 80b236df..a2ae0760 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -6,23 +6,33 @@ class PostModel extends AbstractCrudModel { protected static $config; - public static function getTableName() - { - return 'post'; - } - public static function init() { self::$config = getConfig(); } + public static function getTableName() + { + return 'post'; + } + + public static function convertRow($row) + { + $entity = parent::convertRow($row); + + if (isset($row['type'])) + $entity->setType(new PostType($row['type'])); + + if (isset($row['safety'])) + $entity->setSafety(new PostSafety($row['safety'])); + + return $entity; + } + public static function spawn() { $post = new PostEntity; - $post->score = 0; - $post->favCount = 0; - $post->commentCount = 0; - $post->safety = PostSafety::Safe; + $post->setSafety(new PostSafety(PostSafety::Safe)); $post->hidden = false; $post->uploadDate = time(); do @@ -40,13 +50,13 @@ class PostModel extends AbstractCrudModel self::forgeId($post); $bindings = [ - 'type' => $post->type, + 'type' => $post->getType()->toInteger(), 'name' => $post->name, 'orig_name' => $post->origName, 'file_hash' => $post->fileHash, 'file_size' => $post->fileSize, 'mime_type' => $post->mimeType, - 'safety' => $post->safety, + 'safety' => $post->getSafety()->toInteger(), 'hidden' => $post->hidden, 'upload_date' => $post->uploadDate, 'image_width' => $post->imageWidth, @@ -266,16 +276,6 @@ class PostModel extends AbstractCrudModel - public static function validateSafety($safety) - { - $safety = intval($safety); - - if (!in_array($safety, PostSafety::getAll())) - throw new SimpleException('Invalid safety type "%s"', $safety); - - return $safety; - } - public static function validateSource($source) { $source = trim($source); diff --git a/src/Models/SearchParsers/CommentSearchParser.php b/src/Models/SearchParsers/CommentSearchParser.php index 492005ba..5147373a 100644 --- a/src/Models/SearchParsers/CommentSearchParser.php +++ b/src/Models/SearchParsers/CommentSearchParser.php @@ -8,7 +8,12 @@ class CommentSearchParser extends AbstractSearchParser $this->statement->addInnerJoin('post', new Sql\EqualsFunctor('post_id', 'post.id')); $crit = new Sql\ConjunctionFunctor(); - $allowedSafety = Access::getAllowedSafety(); + $allowedSafety = array_map( + function($safety) + { + return $safety->toInteger(); + }, + Access::getAllowedSafety()); $crit->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety))); if (!Access::check(new Privilege(Privilege::ListPosts, 'hidden'))) diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index 5fec6ca4..7958f308 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -14,8 +14,13 @@ class PostSearchParser extends AbstractSearchParser $this->tags = []; $crit = new Sql\ConjunctionFunctor(); - $allowedSafety = Access::getAllowedSafety(); - $crit->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety))); + $allowedSafety = array_map( + function($safety) + { + return $safety->toInteger(); + }, + Access::getAllowedSafety()); + $crit->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety))); $this->statement->setCriterion($crit); if (count($tokens) > $config->browsing->maxSearchTokens) diff --git a/src/Models/SearchParsers/TagSearchParser.php b/src/Models/SearchParsers/TagSearchParser.php index 6b348655..169fa9f8 100644 --- a/src/Models/SearchParsers/TagSearchParser.php +++ b/src/Models/SearchParsers/TagSearchParser.php @@ -5,7 +5,12 @@ class TagSearchParser extends AbstractSearchParser { protected function processSetup(&$tokens) { - $allowedSafety = Access::getAllowedSafety(); + $allowedSafety = array_map( + function($safety) + { + return $safety->toInteger(); + }, + Access::getAllowedSafety()); $this->statement ->addInnerJoin('post_tag', new Sql\EqualsFunctor('tag.id', 'post_tag.tag_id')) ->addInnerJoin('post', new Sql\EqualsFunctor('post.id', 'post_tag.post_id')) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index c12cd158..3c5dd3e9 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -14,17 +14,29 @@ class UserModel extends AbstractCrudModel return 'user'; } + public static function convertRow($row) + { + $entity = parent::convertRow($row); + + if (isset($row['access_rank'])) + $entity->setAccessRank(new AccessRank($row['access_rank'])); + + return $entity; + } + public static function spawn() { $user = new UserEntity(); + $user->setAccessRank(new AccessRank(AccessRank::Anonymous)); $user->passSalt = md5(mt_rand() . uniqid()); return $user; } public static function save($user) { - if ($user->accessRank == AccessRank::Anonymous) + if ($user->getAccessRank()->toInteger() == AccessRank::Anonymous) throw new Exception('Trying to save anonymous user into database'); + Database::transaction(function() use ($user) { self::forgeId($user); @@ -38,7 +50,7 @@ class UserModel extends AbstractCrudModel 'email_confirmed' => $user->emailConfirmed, 'join_date' => $user->joinDate, 'last_login_date' => $user->lastLoginDate, - 'access_rank' => $user->accessRank, + 'access_rank' => $user->getAccessRank()->toInteger(), 'settings' => $user->settings, 'banned' => $user->banned ]; @@ -234,15 +246,12 @@ class UserModel extends AbstractCrudModel return $email; } - public static function validateAccessRank($accessRank) + public static function validateAccessRank(AccessRank $accessRank) { - $accessRank = intval($accessRank); + $accessRank->validate(); - if (!in_array($accessRank, AccessRank::getAll())) - throw new SimpleException('Invalid access rank type "%s"', $accessRank); - - if ($accessRank == AccessRank::Nobody) - throw new SimpleException('Cannot set special accesss rank "%s"', $accessRank); + if ($accessRank->toInteger() == AccessRank::Nobody) + throw new SimpleException('Cannot set special access rank "%s"', $accessRank->toString()); return $accessRank; } diff --git a/src/Views/post-edit.phtml b/src/Views/post-edit.phtml index 6f3c1ee9..aa8e09bf 100644 --- a/src/Views/post-edit.phtml +++ b/src/Views/post-edit.phtml @@ -23,11 +23,11 @@ diff --git a/src/Views/post-file-render.phtml b/src/Views/post-file-render.phtml index 17aa4a3c..0bb41679 100644 --- a/src/Views/post-file-render.phtml +++ b/src/Views/post-file-render.phtml @@ -5,7 +5,7 @@ Assets::setPageThumb(\Chibi\Router::linkTo( $post = $this->context->transport->post; ?> -type == PostType::Image): ?> +getType()->toInteger() == PostType::Image): ?> context->imageLink)): ?> @@ -20,7 +20,7 @@ $post = $this->context->transport->post; -type == PostType::Flash): ?> +getType()->toInteger() == PostType::Flash): ?> context->transport->post; -type == PostType::Youtube): ?> +getType()->toInteger() == PostType::Youtube): ?> -type == PostType::Video): ?> +getType()->toInteger() == PostType::Video): ?>