Skip to content

Commit

Permalink
refactor: added more permission type enums
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed May 12, 2024
1 parent 96d9608 commit e0e89aa
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 27 deletions.
3 changes: 2 additions & 1 deletion phpmyfaq/add.php
Expand Up @@ -18,6 +18,7 @@
use phpMyFAQ\Captcha\Captcha;
use phpMyFAQ\Captcha\Helper\CaptchaHelper;
use phpMyFAQ\Configuration;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\CategoryHelper as HelperCategory;
use phpMyFAQ\Question;
Expand Down Expand Up @@ -46,7 +47,7 @@
}

// Check permission to add new faqs
if (-1 !== $user->getUserId() && !$user->perm->hasPermission($user->getUserId(), 'addfaq')) {
if (-1 !== $user->getUserId() && !$user->perm->hasPermission($user->getUserId(), PermissionType::FAQ_ADD)) {
$response = new RedirectResponse($faqSystem->getSystemUri($faqConfig));
$response->send();
}
Expand Down
5 changes: 3 additions & 2 deletions phpmyfaq/admin/dashboard.php
Expand Up @@ -20,6 +20,7 @@
use phpMyFAQ\Component\Alert;
use phpMyFAQ\Configuration;
use phpMyFAQ\Database;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Session;
use phpMyFAQ\System;
Expand Down Expand Up @@ -66,7 +67,7 @@
'hasUserTracking' => $faqConfig->get('main.enableUserTracking'),
'adminDashboardHeaderInactiveFaqs' => Translation::get('ad_record_inactive'),
'adminDashboardInactiveFaqs' => $faq->getInactiveFaqsData(),
'hasPermissionEditConfig' => $user->perm->hasPermission($user->getUserId(), 'editconfig'),
'hasPermissionEditConfig' => $user->perm->hasPermission($user->getUserId(), PermissionType::CONFIGURATION_EDIT),
'showVersion' => $faqConfig->get('main.enableAutoUpdateHint'),
];

Expand All @@ -92,7 +93,7 @@
];
}

if ($user->perm->hasPermission($user->getUserId(), 'editconfig')) {
if ($user->perm->hasPermission($user->getUserId(), PermissionType::CONFIGURATION_EDIT)) {
$api = new Api($faqConfig);

$version = Filter::filterInput(INPUT_POST, 'param', FILTER_SANITIZE_SPECIAL_CHARS);
Expand Down
7 changes: 5 additions & 2 deletions phpmyfaq/admin/faqs.editor.php
Expand Up @@ -310,7 +310,7 @@
//
// Revisions
//
if ($user->perm->hasPermission($currentUserId, 'changebtrevs') && $action === 'editentry') {
if ($user->perm->hasPermission($currentUserId, PermissionType::REVISION_UPDATE) && $action === 'editentry') {
$faqRevision = new Revision($faqConfig);
$revisions = $faqRevision->get($faqData['id'], $faqData['lang'], $faqData['author']);

Expand Down Expand Up @@ -374,7 +374,10 @@
'ad_entry_locale' => Translation::get('ad_entry_locale'),
'languageOptions' => LanguageHelper::renderSelectLanguage($faqData['lang'], false, [], 'lang'),
'hasPermissionForAddAttachments' => $user->perm->hasPermission($currentUserId, PermissionType::ATTACHMENT_ADD),
'hasPermissionForDeleteAttachments' => $user->perm->hasPermission($currentUserId, PermissionType::ATTACHMENT_DELETE),
'hasPermissionForDeleteAttachments' => $user->perm->hasPermission(
$currentUserId,
PermissionType::ATTACHMENT_DELETE
),
'ad_menu_attachments' => Translation::get('ad_menu_attachments'),
'csrfTokenDeleteAttachment' => Token::getInstance()->getTokenString('delete-attachment'),
'attachments' => $attList,
Expand Down
13 changes: 7 additions & 6 deletions phpmyfaq/admin/news.php
Expand Up @@ -20,6 +20,7 @@
use phpMyFAQ\Date;
use phpMyFAQ\Entity\CommentType;
use phpMyFAQ\Entity\NewsMessage;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\LanguageHelper;
use phpMyFAQ\News;
Expand All @@ -43,9 +44,9 @@

$templateVars = [
'action' => $action,
'permissionAddNews' => $user->perm->hasPermission($user->getUserId(), 'addnews'),
'permissionEditNews' => $user->perm->hasPermission($user->getUserId(), 'editnews'),
'permissionDeleteNews' => $user->perm->hasPermission($user->getUserId(), 'delnews'),
'permissionAddNews' => $user->perm->hasPermission($user->getUserId(), PermissionType::NEWS_ADD),
'permissionEditNews' => $user->perm->hasPermission($user->getUserId(), PermissionType::NEWS_EDIT),
'permissionDeleteNews' => $user->perm->hasPermission($user->getUserId(), PermissionType::NEWS_DELETE),
'defaultUrl' => $faqConfig->getDefaultUrl(),
'enableWysiwyg' => $faqConfig->get('main.enableWysiwygEditor'),
'ad_news_add' => Translation::get('ad_news_add'),
Expand Down Expand Up @@ -92,20 +93,20 @@
'csrfToken_activateNews' => Token::getInstance()->getTokenString('activate-news')
];

if ('add-news' == $action && $user->perm->hasPermission($user->getUserId(), 'addnews')) {
if ('add-news' == $action && $user->perm->hasPermission($user->getUserId(), PermissionType::NEWS_ADD)) {
$templateVars = [
...$templateVars,
'userEmail' => $user->getUserData('email'),
'userName' => $user->getUserData('display_name')
];
} elseif ('news' == $action && $user->perm->hasPermission($user->getUserId(), 'editnews')) {
} elseif ('news' == $action && $user->perm->hasPermission($user->getUserId(), PermissionType::NEWS_EDIT)) {
$newsHeaders = $news->getHeader();

$templateVars = [
...$templateVars,
'news' => $newsHeaders,
];
} elseif ('edit-news' == $action && $user->perm->hasPermission($user->getUserId(), 'editnews')) {
} elseif ('edit-news' == $action && $user->perm->hasPermission($user->getUserId(), PermissionType::NEWS_EDIT)) {
$id = Filter::filterInput(INPUT_GET, 'id', FILTER_VALIDATE_INT);
$newsData = $news->get($id, true);

Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/admin/stopwords.php
Expand Up @@ -44,7 +44,7 @@

$templateVars = [
'adminHeaderStopWords' => Translation::get('ad_menu_stopwordsconfig'),
'hasPermission' => $user->perm->hasPermission($user->getUserId(), 'editconfig'),
'hasPermission' => $user->perm->hasPermission($user->getUserId(), PermissionType::CONFIGURATION_EDIT),
'msgDescription' => Translation::get('ad_stopwords_desc'),
'csrfToken' => Token::getInstance()->getTokenInput('stopwords'),
'msgStopWordsLabel' => Translation::get('ad_stopwords_desc'),
Expand Down
3 changes: 2 additions & 1 deletion phpmyfaq/index.php
Expand Up @@ -25,6 +25,7 @@
use phpMyFAQ\Category\Relation;
use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Faq;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\CategoryHelper as HelperCategory;
Expand Down Expand Up @@ -655,7 +656,7 @@
// Show login box or logged-in user information
//
if ($user->isLoggedIn() && $user->getUserId() > 0) {
if ($user->perm->hasPermission($user->getUserId(), 'viewadminlink') || $user->isSuperAdmin()) {
if ($user->perm->hasPermission($user->getUserId(), PermissionType::VIEW_ADMIN_LINK) || $user->isSuperAdmin()) {
$adminSection = sprintf(
'<a class="dropdown-item" href="./admin/index.php">%s</a>',
Translation::get('adminSection')
Expand Down
3 changes: 2 additions & 1 deletion phpmyfaq/news.php
Expand Up @@ -23,6 +23,7 @@
use phpMyFAQ\Configuration;
use phpMyFAQ\Date;
use phpMyFAQ\Entity\CommentType;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Glossary;
use phpMyFAQ\Helper\CommentHelper;
Expand Down Expand Up @@ -82,7 +83,7 @@

// Show a link to edit the news?
$editThisEntry = '';
if ($user->perm->hasPermission($user->getUserId(), 'editnews')) {
if ($user->perm->hasPermission($user->getUserId(), PermissionType::NEWS_EDIT)) {
$editThisEntry = sprintf(
'<a href="./admin/index.php?action=news&amp;do=edit&amp;id=%d">%s</a>',
$newsId,
Expand Down
3 changes: 2 additions & 1 deletion phpmyfaq/pdf.php
Expand Up @@ -20,6 +20,7 @@
*/

use phpMyFAQ\Category;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Export\Pdf;
use phpMyFAQ\Faq;
use phpMyFAQ\Filter;
Expand Down Expand Up @@ -110,7 +111,7 @@

$response->setExpires(new DateTime());

if (true === $getAll && $user->perm->hasPermission($user->getUserId(), 'export')) {
if (true === $getAll && $user->perm->hasPermission($user->getUserId(), PermissionType::EXPORT)) {
$filename = 'FAQs.pdf';
$pdfFile = $pdf->generate(0, true, $lang);
} else {
Expand Down
Expand Up @@ -22,6 +22,7 @@
use phpMyFAQ\Controller\AbstractController;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Translation;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use phpMyFAQ\Filter;
use phpMyFAQ\Session\Token;
Expand Down Expand Up @@ -118,16 +119,22 @@ public function deleteTranslation(Request $request)
}
}

/**
* @throws Exception
*/
#[Route('admin/api/forms/translation-add')]
public function addTranslation(Request $request)
public function addTranslation(Request $request): JsonResponse
{
$this->userHasPermission(PermissionType::FORMS_EDIT);

$data = json_decode($request->getContent());

$formId = Filter::filterVar($data->formId, FILTER_SANITIZE_NUMBER_INT);
$inputId = Filter::filterVar($data->inputId, FILTER_SANITIZE_NUMBER_INT);
$lang = Filter::filterVar($data->lang, FILTER_SANITIZE_SPECIAL_CHARS);
$translation = Filter::filterVar($data->translation, FILTER_SANITIZE_SPECIAL_CHARS);
$forms = new Forms(Configuration::getConfigurationInstance());

if (!Token::getInstance()->verifyToken('add-translation', $data->csrf)) {
return $this->json(['error' => Translation::get('err_NotAuth')], Response::HTTP_UNAUTHORIZED);
}
Expand Down
4 changes: 4 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Enums/PermissionType.php
Expand Up @@ -80,6 +80,8 @@ enum PermissionType: string

case RESTORE = 'restore';

case REVISION_UPDATE = 'changebtrevs';

case STATISTICS_ADMINLOG = 'adminlog';

case STATISTICS_VIEWLOGS = 'viewlog';
Expand All @@ -91,4 +93,6 @@ enum PermissionType: string
case USER_DELETE = 'delete_user';

case FORMS_EDIT = 'forms_edit';

case VIEW_ADMIN_LINK = 'viewadminlink';
}
4 changes: 2 additions & 2 deletions phpmyfaq/src/phpMyFAQ/Permission/BasicPermission.php
Expand Up @@ -111,8 +111,8 @@ public function getRightData(int $rightId): array
/**
* Returns true if the user given by user_id has the right,
* otherwise false. Unlike checkUserRight(), right may be a
* right-ID or a right-name. Another difference is, that also
* group-rights are taken into account.
* right-ID or a right-name. Another difference is that also
* group rights are taken into account.
*
* @param int $userId User ID
* @param mixed $right Right ID or right name
Expand Down
18 changes: 9 additions & 9 deletions phpmyfaq/src/phpMyFAQ/Setup/Installer.php
Expand Up @@ -130,7 +130,7 @@ class Installer extends Setup
],
//18 => "viewadminlink",
[
'name' => 'viewadminlink',
'name' => PermissionType::VIEW_ADMIN_LINK->value,
'description' => 'Right to see the link to the admin section'
],
//20 => "backup",
Expand All @@ -145,42 +145,42 @@ class Installer extends Setup
],
//22 => "delquestion",
[
'name' => 'delquestion',
'name' => PermissionType::QUESTION_DELETE->value,
'description' => 'Right to delete questions',
],
//23 => 'addglossary',
[
'name' => 'addglossary',
'name' => PermissionType::GLOSSARY_ADD->value,
'description' => 'Right to add glossary entries',
],
//24 => 'editglossary',
[
'name' => 'editglossary',
'name' => PermissionType::GLOSSARY_EDIT->value,
'description' => 'Right to edit glossary entries',
],
//25 => 'delglossary'
[
'name' => 'delglossary',
'name' => PermissionType::GLOSSARY_DELETE->value,
'description' => 'Right to delete glossary entries',
],
//26 => 'changebtrevs'
[
'name' => 'changebtrevs',
'name' => PermissionType::REVISION_UPDATE->value,
'description' => 'Right to edit revisions',
],
//27 => "addgroup",
[
'name' => 'addgroup',
'name' => PermissionType::GROUP_ADD->value,
'description' => 'Right to add group accounts',
],
//28 => "editgroup",
[
'name' => 'editgroup',
'name' => PermissionType::GROUP_EDIT->value,
'description' => 'Right to edit group accounts',
],
//29 => "delgroup",
[
'name' => 'delgroup',
'name' => PermissionType::GROUP_DELETE->value,
'description' => 'Right to delete group accounts',
],
//30 => "addtranslation",
Expand Down

0 comments on commit e0e89aa

Please sign in to comment.