Implemented new enums

This commit is contained in:
Marcin Kurczewski 2014-05-04 19:06:40 +02:00
parent 977989ffed
commit d3beb8bc53
32 changed files with 290 additions and 131 deletions

View file

@ -40,24 +40,22 @@ class Access
$user = Auth::getCurrentUser(); $user = Auth::getCurrentUser();
$minAccessRank = AccessRank::Nobody; $minAccessRank = AccessRank::Nobody;
$key = TextCaseConverter::convert(Privilege::toString($privilege->primary), $key = TextCaseConverter::convert($privilege->toString(),
TextCaseConverter::CAMEL_CASE, TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE); TextCaseConverter::SPINAL_CASE);
if (isset(self::$privileges[$key])) $privilege->secondary = null;
{ $key2 = TextCaseConverter::convert($privilege->toString(),
$minAccessRank = self::$privileges[$key]; TextCaseConverter::CAMEL_CASE,
} TextCaseConverter::SPINAL_CASE);
if ($privilege->secondary != null)
{
$key2 = $key . '.' . strtolower($privilege->secondary);
if (isset(self::$privileges[$key2]))
{
$minAccessRank = self::$privileges[$key2];
}
}
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() public static function assertAuthentication()
@ -98,7 +96,7 @@ class Access
return array_filter(PostSafety::getAll(), function($safety) 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); and Auth::getCurrentUser()->hasEnabledSafety($safety);
}); });
} }

View file

@ -27,7 +27,7 @@ class AddPostJob extends AbstractJob
//- move this to PostEntity::isValid() //- move this to PostEntity::isValid()
//- create IValidatable interface //- create IValidatable interface
//- enforce entity validity upon calling save() in models //- 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'); throw new SimpleException('No post type detected; upload faled');
//save to db //save to db
@ -43,7 +43,7 @@ class AddPostJob extends AbstractJob
: TextHelper::reprUser(Auth::getCurrentUser()), : TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($post), 'post' => TextHelper::reprPost($post),
'tags' => TextHelper::reprTags($post->getTags()), 'tags' => TextHelper::reprTags($post->getTags()),
'safety' => PostSafety::toString($post->safety), 'safety' => $post->getSafety()->toString(),
'source' => $post->source]); 'source' => $post->source]);
//finish //finish

View file

@ -6,9 +6,9 @@ class EditPostSafetyJob extends AbstractPostEditJob
public function execute() public function execute()
{ {
$post = $this->post; $post = $this->post;
$newSafety = $this->getArgument(self::SAFETY); $newSafety = new PostSafety($this->getArgument(self::SAFETY));
$oldSafety = $post->safety; $oldSafety = $post->getSafety();
$post->setSafety($newSafety); $post->setSafety($newSafety);
if (!$this->skipSaving) if (!$this->skipSaving)
@ -19,7 +19,7 @@ class EditPostSafetyJob extends AbstractPostEditJob
LogHelper::log('{user} changed safety of {post} to {safety}', [ LogHelper::log('{user} changed safety of {post} to {safety}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($post), 'post' => TextHelper::reprPost($post),
'safety' => PostSafety::toString($post->safety)]); 'safety' => $post->getSafety()->toString()]);
} }
return $post; return $post;

View file

@ -6,13 +6,13 @@ class EditUserAccessRankJob extends AbstractUserEditJob
public function execute() public function execute()
{ {
$user = $this->user; $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) if ($oldAccessRank == $newAccessRank)
return $user; return $user;
$user->accessRank = $newAccessRank; $user->setAccessRank($newAccessRank);
if (!$this->skipSaving) if (!$this->skipSaving)
UserModel::save($user); UserModel::save($user);
@ -20,7 +20,7 @@ class EditUserAccessRankJob extends AbstractUserEditJob
LogHelper::log('{user} changed {subject}\'s access rank to {rank}', [ LogHelper::log('{user} changed {subject}\'s access rank to {rank}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'subject' => TextHelper::reprUser($user), 'subject' => TextHelper::reprUser($user),
'rank' => AccessRank::toString($newAccessRank)]); 'rank' => $newAccessRank->toString()]);
return $user; return $user;
} }

View file

@ -38,7 +38,7 @@ class GetPostContentJob extends AbstractJob
if ($post->hidden) if ($post->hidden)
$privileges []= new Privilege(Privilege::ViewPost, '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; return $privileges;
} }

View file

@ -18,7 +18,7 @@ class GetPostJob extends AbstractPostJob
if ($post->hidden) if ($post->hidden)
$privileges []= new Privilege(Privilege::ViewPost, '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; return $privileges;
} }

View file

@ -20,7 +20,7 @@ class GetPostThumbJob extends AbstractJob
if ($post->hidden) if ($post->hidden)
Access::assert(new Privilege(Privilege::ListPosts, '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); $post->generateThumb($width, $height);

View file

@ -70,7 +70,7 @@ class Auth
} }
else else
{ {
$_SESSION['logged-in'] = $user->accessRank != AccessRank::Anonymous; $_SESSION['logged-in'] = $user->getAccessRank()->toInteger() != AccessRank::Anonymous;
$_SESSION['user'] = serialize($user); $_SESSION['user'] = serialize($user);
} }
} }
@ -87,7 +87,7 @@ class Auth
$dummy = UserModel::spawn(); $dummy = UserModel::spawn();
$dummy->id = null; $dummy->id = null;
$dummy->name = UserModel::getAnonymousName(); $dummy->name = UserModel::getAnonymousName();
$dummy->accessRank = AccessRank::Anonymous; $dummy->setAccessRank(new AccessRank(AccessRank::Anonymous));
return $dummy; return $dummy;
} }
} }

View file

@ -81,7 +81,7 @@ class UserController
$user->enablePostTagTitles(InputHelper::get('post-tag-titles')); $user->enablePostTagTitles(InputHelper::get('post-tag-titles'));
$user->enableHidingDislikedPosts(InputHelper::get('hide-disliked-posts')); $user->enableHidingDislikedPosts(InputHelper::get('hide-disliked-posts'));
if ($user->accessRank != AccessRank::Anonymous) if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous)
UserModel::save($user); UserModel::save($user);
if ($user->id == Auth::getCurrentUser()->id) if ($user->id == Auth::getCurrentUser()->id)
Auth::setCurrentUser($user); Auth::setCurrentUser($user);
@ -168,12 +168,12 @@ class UserController
Privilege::ChangeUserSettings, Privilege::ChangeUserSettings,
Access::getIdentity($user))); Access::getIdentity($user)));
if (!in_array($safety, PostSafety::getAll())) $safety = new PostSafety($safety);
throw new SimpleExcetpion('Invalid safety'); $safety->validate();
$user->enableSafety($safety, !$user->hasEnabledSafety($safety)); $user->enableSafety($safety, !$user->hasEnabledSafety($safety));
if ($user->accessRank != AccessRank::Anonymous) if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous)
UserModel::save($user); UserModel::save($user);
Auth::setCurrentUser($user); Auth::setCurrentUser($user);
} }

View file

@ -1,21 +1,23 @@
<?php <?php
class Enum abstract class Enum
{ {
public static function toString($constant) public abstract function toString();
public function _toString($constant)
{ {
$cls = new ReflectionClass(get_called_class()); $cls = new ReflectionClass(get_called_class());
$constants = $cls->getConstants(); $constants = $cls->getConstants();
return array_search($constant, $constants); 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::SNAKE_CASE,
TextCaseConverter::BLANK_CASE); TextCaseConverter::BLANK_CASE);
} }
public static function getAll() public static function getAllConstants()
{ {
$cls = new ReflectionClass(get_called_class()); $cls = new ReflectionClass(get_called_class());
$constants = $cls->getConstants(); $constants = $cls->getConstants();

5
src/IValidatable.php Normal file
View file

@ -0,0 +1,5 @@
<?php
interface IValidatable
{
function validate();
}

View file

@ -88,8 +88,17 @@ abstract class AbstractCrudModel implements IModel
TextCaseConverter::LOWER_CAMEL_CASE); TextCaseConverter::LOWER_CAMEL_CASE);
} }
if (property_exists($entity, $key))
{
$reflectionProperty = new ReflectionProperty(get_class($entity), $key);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($entity, $val);
}
else
{
$entity->$key = $val; $entity->$key = $val;
} }
}
return $entity; return $entity;
} }

View file

@ -4,22 +4,22 @@ use \Chibi\Database as Database;
class PostEntity extends AbstractEntity class PostEntity extends AbstractEntity
{ {
public $type; protected $type;
public $name; public $name;
public $origName; public $origName;
public $fileHash; public $fileHash;
public $fileSize; public $fileSize;
public $mimeType; public $mimeType;
public $safety; protected $safety;
public $hidden; public $hidden;
public $uploadDate; public $uploadDate;
public $imageWidth; public $imageWidth;
public $imageHeight; public $imageHeight;
public $uploaderId; public $uploaderId;
public $source; public $source;
public $commentCount; public $commentCount = 0;
public $favCount; public $favCount = 0;
public $score; public $score = 0;
public function getUploader() public function getUploader()
{ {
@ -173,9 +173,26 @@ class PostEntity extends AbstractEntity
$this->hidden = boolval($hidden); $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) public function setSource($source)
@ -233,7 +250,7 @@ class PostEntity extends AbstractEntity
$srcPath = $this->getFullPath(); $srcPath = $this->getFullPath();
$dstPath = $this->getThumbDefaultPath($width, $height); $dstPath = $this->getThumbDefaultPath($width, $height);
if ($this->type == PostType::Youtube) if ($this->getType()->toInteger() == PostType::Youtube)
{ {
return ThumbnailHelper::generateFromUrl( return ThumbnailHelper::generateFromUrl(
'http://img.youtube.com/vi/' . $this->fileHash . '/mqdefault.jpg', 'http://img.youtube.com/vi/' . $this->fileHash . '/mqdefault.jpg',
@ -263,13 +280,13 @@ class PostEntity extends AbstractEntity
case 'image/png': case 'image/png':
case 'image/jpeg': case 'image/jpeg':
list ($imageWidth, $imageHeight) = getimagesize($srcPath); list ($imageWidth, $imageHeight) = getimagesize($srcPath);
$this->type = PostType::Image; $this->setType(new PostType(PostType::Image));
$this->imageWidth = $imageWidth; $this->imageWidth = $imageWidth;
$this->imageHeight = $imageHeight; $this->imageHeight = $imageHeight;
break; break;
case 'application/x-shockwave-flash': case 'application/x-shockwave-flash':
list ($imageWidth, $imageHeight) = getimagesize($srcPath); list ($imageWidth, $imageHeight) = getimagesize($srcPath);
$this->type = PostType::Flash; $this->setType(new PostType(PostType::Flash));
$this->imageWidth = $imageWidth; $this->imageWidth = $imageWidth;
$this->imageHeight = $imageHeight; $this->imageHeight = $imageHeight;
break; break;
@ -280,7 +297,7 @@ class PostEntity extends AbstractEntity
case 'video/x-flv': case 'video/x-flv':
case 'video/3gpp': case 'video/3gpp':
list ($imageWidth, $imageHeight) = getimagesize($srcPath); list ($imageWidth, $imageHeight) = getimagesize($srcPath);
$this->type = PostType::Video; $this->setType(new PostType(PostType::Video));
$this->imageWidth = $imageWidth; $this->imageWidth = $imageWidth;
$this->imageHeight = $imageHeight; $this->imageHeight = $imageHeight;
break; break;
@ -315,7 +332,7 @@ class PostEntity extends AbstractEntity
if (preg_match('/youtube.com\/watch.*?=([a-zA-Z0-9_-]+)/', $srcUrl, $matches)) if (preg_match('/youtube.com\/watch.*?=([a-zA-Z0-9_-]+)/', $srcUrl, $matches))
{ {
$youtubeId = $matches[1]; $youtubeId = $matches[1];
$this->type = PostType::Youtube; $this->setType(new PostType(PostType::Youtube));
$this->mimeType = null; $this->mimeType = null;
$this->fileSize = null; $this->fileSize = null;
$this->fileHash = $youtubeId; $this->fileHash = $youtubeId;
@ -360,7 +377,7 @@ class PostEntity extends AbstractEntity
$x []= TextHelper::reprTag($tag->name); $x []= TextHelper::reprTag($tag->name);
foreach ($this->getRelations() as $relatedPost) foreach ($this->getRelations() as $relatedPost)
$x []= TextHelper::reprPost($relatedPost); $x []= TextHelper::reprPost($relatedPost);
$x []= $this->safety; $x []= $this->getSafety()->toInteger();
$x []= $this->source; $x []= $this->source;
$x []= $this->fileHash; $x []= $this->fileHash;
natcasesort($x); natcasesort($x);

View file

@ -12,10 +12,21 @@ class UserEntity extends AbstractEntity
public $emailConfirmed; public $emailConfirmed;
public $joinDate; public $joinDate;
public $lastLoginDate; public $lastLoginDate;
public $accessRank; protected $accessRank;
public $settings; public $settings;
public $banned; public $banned;
public function getAccessRank()
{
return $this->accessRank;
}
public function setAccessRank(AccessRank $accessRank)
{
$accessRank->validate();
$this->accessRank = $accessRank;
}
public function getAvatarUrl($size = 32) public function getAvatarUrl($size = 32)
{ {
$subject = !empty($this->emailConfirmed) $subject = !empty($this->emailConfirmed)
@ -44,32 +55,31 @@ class UserEntity extends AbstractEntity
$this->settings = $settings; $this->settings = $settings;
} }
public function hasEnabledSafety($safety) public function hasEnabledSafety(PostSafety $safety)
{ {
$all = $this->getSetting(UserModel::SETTING_SAFETY); $all = $this->getSetting(UserModel::SETTING_SAFETY);
if (!$all) if (!$all)
return $safety == PostSafety::Safe; return $safety->toInteger() == PostSafety::Safe;
return $all & PostSafety::toFlag($safety); return $all & $safety->toFlag();
} }
public function enableSafety($safety, $enabled) public function enableSafety(PostSafety $safety, $enabled)
{ {
$all = $this->getSetting(UserModel::SETTING_SAFETY); $all = $this->getSetting(UserModel::SETTING_SAFETY);
if (!$all)
$all = PostSafety::toFlag(PostSafety::Safe);
$new = $all; $new = $all;
if (!$enabled) if (!$enabled)
{ {
$new &= ~PostSafety::toFlag($safety); $new &= ~$safety->toFlag();
if (!$new)
$new = PostSafety::toFlag(PostSafety::Safe);
} }
else else
{ {
$new |= PostSafety::toFlag($safety); $new |= $safety->toFlag();
} }
if (!$new)
$new = (new PostSafety(PostSafety::Safe))->toFlag();
$this->setSetting(UserModel::SETTING_SAFETY, $new); $this->setSetting(UserModel::SETTING_SAFETY, $new);
} }

View file

@ -1,5 +1,5 @@
<?php <?php
class AccessRank extends Enum class AccessRank extends Enum implements IValidatable
{ {
const Anonymous = 0; const Anonymous = 0;
const Registered = 1; const Registered = 1;
@ -7,4 +7,35 @@ class AccessRank extends Enum
const Moderator = 3; const Moderator = 3;
const Admin = 4; const Admin = 4;
const Nobody = 5; const Nobody = 5;
protected $accessRank;
public function __construct($accessRank)
{
$this->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);
}
} }

View file

@ -1,12 +1,43 @@
<?php <?php
class PostSafety extends Enum class PostSafety extends Enum implements IValidatable
{ {
const Safe = 1; const Safe = 1;
const Sketchy = 2; const Sketchy = 2;
const Unsafe = 3; const Unsafe = 3;
public static function toFlag($safety) protected $safety;
public function __construct($safety)
{ {
return pow(2, $safety); $this->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);
} }
} }

View file

@ -1,8 +1,31 @@
<?php <?php
class PostType extends Enum class PostType extends Enum implements IValidatable
{ {
const Image = 1; const Image = 1;
const Flash = 2; const Flash = 2;
const Youtube = 3; const Youtube = 3;
const Video = 4; const Video = 4;
protected $type;
public function __construct($type)
{
$this->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);
}
} }

View file

@ -51,6 +51,14 @@ class Privilege extends Enum
public function __construct($primary, $secondary = null) public function __construct($primary, $secondary = null)
{ {
$this->primary = $primary; $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;
} }
} }

View file

@ -6,23 +6,33 @@ class PostModel extends AbstractCrudModel
{ {
protected static $config; protected static $config;
public static function getTableName()
{
return 'post';
}
public static function init() public static function init()
{ {
self::$config = getConfig(); 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() public static function spawn()
{ {
$post = new PostEntity; $post = new PostEntity;
$post->score = 0; $post->setSafety(new PostSafety(PostSafety::Safe));
$post->favCount = 0;
$post->commentCount = 0;
$post->safety = PostSafety::Safe;
$post->hidden = false; $post->hidden = false;
$post->uploadDate = time(); $post->uploadDate = time();
do do
@ -40,13 +50,13 @@ class PostModel extends AbstractCrudModel
self::forgeId($post); self::forgeId($post);
$bindings = [ $bindings = [
'type' => $post->type, 'type' => $post->getType()->toInteger(),
'name' => $post->name, 'name' => $post->name,
'orig_name' => $post->origName, 'orig_name' => $post->origName,
'file_hash' => $post->fileHash, 'file_hash' => $post->fileHash,
'file_size' => $post->fileSize, 'file_size' => $post->fileSize,
'mime_type' => $post->mimeType, 'mime_type' => $post->mimeType,
'safety' => $post->safety, 'safety' => $post->getSafety()->toInteger(),
'hidden' => $post->hidden, 'hidden' => $post->hidden,
'upload_date' => $post->uploadDate, 'upload_date' => $post->uploadDate,
'image_width' => $post->imageWidth, '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) public static function validateSource($source)
{ {
$source = trim($source); $source = trim($source);

View file

@ -8,7 +8,12 @@ class CommentSearchParser extends AbstractSearchParser
$this->statement->addInnerJoin('post', new Sql\EqualsFunctor('post_id', 'post.id')); $this->statement->addInnerJoin('post', new Sql\EqualsFunctor('post_id', 'post.id'));
$crit = new Sql\ConjunctionFunctor(); $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))); $crit->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety)));
if (!Access::check(new Privilege(Privilege::ListPosts, 'hidden'))) if (!Access::check(new Privilege(Privilege::ListPosts, 'hidden')))

View file

@ -14,8 +14,13 @@ class PostSearchParser extends AbstractSearchParser
$this->tags = []; $this->tags = [];
$crit = new Sql\ConjunctionFunctor(); $crit = new Sql\ConjunctionFunctor();
$allowedSafety = Access::getAllowedSafety(); $allowedSafety = array_map(
$crit->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety))); function($safety)
{
return $safety->toInteger();
},
Access::getAllowedSafety());
$crit->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety)));
$this->statement->setCriterion($crit); $this->statement->setCriterion($crit);
if (count($tokens) > $config->browsing->maxSearchTokens) if (count($tokens) > $config->browsing->maxSearchTokens)

View file

@ -5,7 +5,12 @@ class TagSearchParser extends AbstractSearchParser
{ {
protected function processSetup(&$tokens) protected function processSetup(&$tokens)
{ {
$allowedSafety = Access::getAllowedSafety(); $allowedSafety = array_map(
function($safety)
{
return $safety->toInteger();
},
Access::getAllowedSafety());
$this->statement $this->statement
->addInnerJoin('post_tag', new Sql\EqualsFunctor('tag.id', 'post_tag.tag_id')) ->addInnerJoin('post_tag', new Sql\EqualsFunctor('tag.id', 'post_tag.tag_id'))
->addInnerJoin('post', new Sql\EqualsFunctor('post.id', 'post_tag.post_id')) ->addInnerJoin('post', new Sql\EqualsFunctor('post.id', 'post_tag.post_id'))

View file

@ -14,17 +14,29 @@ class UserModel extends AbstractCrudModel
return 'user'; 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() public static function spawn()
{ {
$user = new UserEntity(); $user = new UserEntity();
$user->setAccessRank(new AccessRank(AccessRank::Anonymous));
$user->passSalt = md5(mt_rand() . uniqid()); $user->passSalt = md5(mt_rand() . uniqid());
return $user; return $user;
} }
public static function save($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'); throw new Exception('Trying to save anonymous user into database');
Database::transaction(function() use ($user) Database::transaction(function() use ($user)
{ {
self::forgeId($user); self::forgeId($user);
@ -38,7 +50,7 @@ class UserModel extends AbstractCrudModel
'email_confirmed' => $user->emailConfirmed, 'email_confirmed' => $user->emailConfirmed,
'join_date' => $user->joinDate, 'join_date' => $user->joinDate,
'last_login_date' => $user->lastLoginDate, 'last_login_date' => $user->lastLoginDate,
'access_rank' => $user->accessRank, 'access_rank' => $user->getAccessRank()->toInteger(),
'settings' => $user->settings, 'settings' => $user->settings,
'banned' => $user->banned 'banned' => $user->banned
]; ];
@ -234,15 +246,12 @@ class UserModel extends AbstractCrudModel
return $email; return $email;
} }
public static function validateAccessRank($accessRank) public static function validateAccessRank(AccessRank $accessRank)
{ {
$accessRank = intval($accessRank); $accessRank->validate();
if (!in_array($accessRank, AccessRank::getAll())) if ($accessRank->toInteger() == AccessRank::Nobody)
throw new SimpleException('Invalid access rank type "%s"', $accessRank); throw new SimpleException('Cannot set special access rank "%s"', $accessRank->toString());
if ($accessRank == AccessRank::Nobody)
throw new SimpleException('Cannot set special accesss rank "%s"', $accessRank);
return $accessRank; return $accessRank;
} }

View file

@ -23,11 +23,11 @@
<label> <label>
<input type="radio" <input type="radio"
name="safety" name="safety"
<?php if ($this->context->transport->post->safety == $safety): ?> <?php if ($this->context->transport->post->getSafety() == $safety): ?>
checked="checked" checked="checked"
<?php endif ?> <?php endif ?>
value="<?= $safety ?>"/> value="<?= $safety->toInteger() ?>"/>
&nbsp;<?= ucfirst(PostSafety::toDisplayString($safety)) ?> &nbsp;<?= ucfirst($safety->toDisplayString()) ?>
</label> </label>
<?php endforeach ?> <?php endforeach ?>
</div> </div>

View file

@ -5,7 +5,7 @@ Assets::setPageThumb(\Chibi\Router::linkTo(
$post = $this->context->transport->post; $post = $this->context->transport->post;
?> ?>
<?php if ($post->type == PostType::Image): ?> <?php if ($post->getType()->toInteger() == PostType::Image): ?>
<?php if (!empty($this->context->imageLink)): ?> <?php if (!empty($this->context->imageLink)): ?>
<a href="<?= $this->context->imageLink ?>"> <a href="<?= $this->context->imageLink ?>">
@ -20,7 +20,7 @@ $post = $this->context->transport->post;
</a> </a>
<?php endif ?> <?php endif ?>
<?php elseif ($post->type == PostType::Flash): ?> <?php elseif ($post->getType()->toInteger() == PostType::Flash): ?>
<object <object
type="<?= $post->mimeType ?>" type="<?= $post->mimeType ?>"
@ -37,7 +37,7 @@ $post = $this->context->transport->post;
</object> </object>
<?php elseif ($post->type == PostType::Youtube): ?> <?php elseif ($post->getType()->toInteger() == PostType::Youtube): ?>
<iframe <iframe
style="width: 800px; height: 600px; border: 0;" style="width: 800px; height: 600px; border: 0;"
@ -46,7 +46,7 @@ $post = $this->context->transport->post;
</iframe> </iframe>
<?php elseif ($post->type == PostType::Video): ?> <?php elseif ($post->getType()->toInteger() == PostType::Video): ?>
<video style="max-width: 100%" controls> <video style="max-width: 100%" controls>
<source <source

View file

@ -6,7 +6,7 @@ $classNames =
'post', 'post',
'post-type-' . TextCaseConverter::convert( 'post-type-' . TextCaseConverter::convert(
TextHelper::resolveMimeType($this->context->post->mimeType) TextHelper::resolveMimeType($this->context->post->mimeType)
?: PostType::toString($this->context->post->type), ?: $this->context->post->getType()->toString(),
TextCaseConverter::CAMEL_CASE, TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE), TextCaseConverter::SPINAL_CASE),
]; ];

View file

@ -86,9 +86,9 @@ Assets::addScript('../lib/tagit/jquery.tagit.js');
<?php if (!$checked): ?> <?php if (!$checked): ?>
checked="checked" checked="checked"
<?php endif ?> <?php endif ?>
value="<?= $safety ?>" /> value="<?= $safety->toInteger() ?>" />
<?= ucfirst(PostSafety::toDisplayString($safety)) ?> <?= ucfirst($safety->toDisplayString()) ?>
<?php $checked = true ?> <?php $checked = true ?>
</label> </label>

View file

@ -116,7 +116,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="key-value safety"> <div class="key-value safety">
<span class="key">Safety:</span> <span class="key">Safety:</span>
<span class="value safety-<?= $val = PostSafety::toDisplayString($this->context->transport->post->safety) ?>" <span class="value safety-<?= $val = $this->context->transport->post->getSafety()->toDisplayString() ?>"
title="<?= $val ?>"> title="<?= $val ?>">
<?= $val ?> <?= $val ?>
</span> </span>
@ -184,7 +184,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
</div> </div>
<div class="unit hl-options"> <div class="unit hl-options">
<?php if ($this->context->transport->post->type != PostType::Youtube): ?> <?php if ($this->context->transport->post->getType()->toInteger() != PostType::Youtube): ?>
<div class="hl-option"> <div class="hl-option">
<a title="Download" href="<?= \Chibi\Router::linkTo( <a title="Download" href="<?= \Chibi\Router::linkTo(
['PostController', 'fileView'], ['PostController', 'fileView'],
@ -370,7 +370,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
</div> </div>
<?php endif ?> <?php endif ?>
<div class="post-wrapper post-type-<?= PostType::toString($this->context->transport->post->type) ?>"> <div class="post-wrapper post-type-<?= $this->context->transport->post->getType()->toString() ?>">
<?= \Chibi\View::render('post-file-render', $this->context) ?> <?= \Chibi\View::render('post-file-render', $this->context) ?>
</div> </div>

View file

@ -112,24 +112,24 @@
<?php foreach (PostSafety::getAll() as $safety): ?> <?php foreach (PostSafety::getAll() as $safety): ?>
<?php if (Access::check(new Privilege( <?php if (Access::check(new Privilege(
Privilege::ListPosts, Privilege::ListPosts,
PostSafety::toString($safety)))): ?> $safety->toString()))): ?>
<li class="safety-<?= TextCaseConverter::convert( <li class="safety-<?= TextCaseConverter::convert(
PostSafety::toString($safety), $safety->toString(),
TextCaseConverter::CAMEL_CASE, TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE) ?>"> TextCaseConverter::SPINAL_CASE) ?>">
<a class="<?= Auth::getCurrentUser()->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>" <a class="<?= Auth::getCurrentUser()->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>"
href="<?= \Chibi\Router::linkTo( href="<?= \Chibi\Router::linkTo(
['UserController', 'toggleSafetyAction'], ['UserController', 'toggleSafetyAction'],
['safety' => $safety]) ?>" ['safety' => $safety->toInteger()]) ?>"
title="<?= sprintf('Searching %s posts: %s', title="<?= sprintf('Searching %s posts: %s',
PostSafety::toDisplayString($safety), $safety->toDisplayString(),
Auth::getCurrentUser()->hasEnabledSafety($safety) Auth::getCurrentUser()->hasEnabledSafety($safety)
? 'enabled' ? 'enabled'
: 'disabled') ?>"> : 'disabled') ?>">
<span><?= ucfirst(PostSafety::toDisplayString($safety)) ?></span> <span><?= ucfirst($safety->toDisplayString()) ?></span>
</a> </a>
</li> </li>

View file

@ -91,14 +91,15 @@
<div class="input-wrapper"> <div class="input-wrapper">
<select name="access-rank" id="access-rank"> <select name="access-rank" id="access-rank">
<?php foreach (AccessRank::getAll() as $rank): ?> <?php foreach (AccessRank::getAll() as $rank): ?>
<?php if ($rank == AccessRank::Nobody) continue ?> <?php if ($rank->toInteger() == AccessRank::Nobody) continue ?>
<?php if ($rank == InputHelper::get('access-rank') or (!InputHelper::get('access-rank') <?php if ($rank->toInteger() == InputHelper::get('access-rank')
and $rank == $this->context->transport->user->accessRank)): ?> or (!InputHelper::get('access-rank') and
<option value="<?= $rank ?>" selected="selected"> $rank == $this->context->transport->user->getAccessRank())): ?>
<option value="<?= $rank->toInteger() ?>" selected="selected">
<?php else: ?> <?php else: ?>
<option value="<?= $rank ?>"> <option value="<?= $rank->toInteger() ?>">
<?php endif ?> <?php endif ?>
<?= AccessRank::toDisplayString($rank) ?> <?= $rank->toDisplayString() ?>
</option> </option>
<?php endforeach ?> <?php endforeach ?>
</select> </select>

View file

@ -11,19 +11,19 @@
<?php foreach (PostSafety::getAll() as $safety): ?> <?php foreach (PostSafety::getAll() as $safety): ?>
<?php if (Access::check(new Privilege( <?php if (Access::check(new Privilege(
Privilege::ListPosts, Privilege::ListPosts,
PostSafety::toString($safety)))): ?> $safety->toString()))): ?>
<label> <label>
<?php <?php
$attrs = []; $attrs = [];
$attrs['type'] = 'checkbox'; $attrs['type'] = 'checkbox';
$attrs['name'] = 'safety[]'; $attrs['name'] = 'safety[]';
$attrs['value'] = $safety; $attrs['value'] = $safety->toInteger();
if ($this->context->transport->user->hasEnabledSafety($safety)) if ($this->context->transport->user->hasEnabledSafety($safety))
$attrs['checked'] = 'checked'; $attrs['checked'] = 'checked';
echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs); echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs);
?> ?>
<?= ucfirst(PostSafety::toDisplayString($safety)) ?> <?= ucfirst($safety->toDisplayString()) ?>
</label> </label>
<?php endif ?> <?php endif ?>
<?php endforeach ?> <?php endforeach ?>

View file

@ -45,7 +45,7 @@ Assets::addStylesheet('user-view.css');
<span class="key">Access rank:</span> <span class="key">Access rank:</span>
<span <span
class="value" class="value"
title="<?= $val = AccessRank::toDisplayString($this->context->transport->user->accessRank) ?>"> title="<?= $val = $this->context->transport->user->getAccessRank()->toDisplayString() ?>">
<?= $val ?> <?= $val ?>