Tag list respects safety settings

This commit is contained in:
Marcin Kurczewski 2013-10-30 16:22:46 +01:00
parent e0c4c28e70
commit 36caef3831
3 changed files with 31 additions and 12 deletions

View file

@ -63,6 +63,16 @@ class PrivilegesHelper
if (!$user->email_confirmed) if (!$user->email_confirmed)
throw new SimpleException('Need e-mail address confirmation to continue'); throw new SimpleException('Need e-mail address confirmation to continue');
} }
public static function getAllowedSafety()
{
$context = \Chibi\Registry::getContext();
return array_filter(PostSafety::getAll(), function($safety) use ($context)
{
return PrivilegesHelper::confirm(Privilege::ListPosts, PostSafety::toString($safety)) and
$context->user->hasEnabledSafety($safety);
});
}
} }
PrivilegesHelper::init(); PrivilegesHelper::init();

View file

@ -31,11 +31,7 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder
protected static function filterUserSafety($dbQuery) protected static function filterUserSafety($dbQuery)
{ {
$context = \Chibi\Registry::getContext(); $context = \Chibi\Registry::getContext();
$allowedSafety = array_filter(PostSafety::getAll(), function($safety) use ($context) $allowedSafety = PrivilegesHelper::getAllowedSafety();
{
return PrivilegesHelper::confirm(Privilege::ListPosts, PostSafety::toString($safety)) and
$context->user->hasEnabledSafety($safety);
});
$dbQuery->addSql('safety')->in('(' . R::genSlots($allowedSafety) . ')'); $dbQuery->addSql('safety')->in('(' . R::genSlots($allowedSafety) . ')');
foreach ($allowedSafety as $s) foreach ($allowedSafety as $s)
$dbQuery->put($s); $dbQuery->put($s);

View file

@ -3,21 +3,34 @@ class model_Tag_QueryBuilder implements AbstractQueryBuilder
{ {
public static function build($dbQuery, $query) public static function build($dbQuery, $query)
{ {
$allowedSafety = PrivilegesHelper::getAllowedSafety();
$limitQuery = false; $limitQuery = false;
$dbQuery->addSql(', COUNT(post_tag.post_id)')->as('count'); $dbQuery
$dbQuery->from('tag'); ->addSql(', COUNT(post_tag.post_id)')
$dbQuery->innerJoin('post_tag'); ->as('count')
$dbQuery->on('tag.id = post_tag.tag_id'); ->from('tag')
->innerJoin('post_tag')
->on('tag.id = post_tag.tag_id')
->innerJoin('post')
->on('post.id = post_tag.post_id')
->where('safety IN (' . R::genSlots($allowedSafety) . ')');
foreach ($allowedSafety as $s)
$dbQuery->put($s);
if ($query !== null) if ($query !== null)
{ {
$limitQuery = true; $limitQuery = true;
if (strlen($query) >= 3) if (strlen($query) >= 3)
$query = '%' . $query; $query = '%' . $query;
$query .= '%'; $query .= '%';
$dbQuery->where('LOWER(tag.name) LIKE LOWER(?)')->put($query); $dbQuery
->and('LOWER(tag.name)')
->like('LOWER(?)')
->put($query);
} }
$dbQuery->groupBy('tag.id'); $dbQuery
$dbQuery->orderBy('LOWER(tag.name)')->asc(); ->groupBy('tag.id')
->orderBy('LOWER(tag.name)')
->asc();
if ($limitQuery) if ($limitQuery)
$dbQuery->limit(15); $dbQuery->limit(15);
} }