Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added separate statistics-only plugin permission; #100 #107

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function registerPermissions(): array
'label' => 'vdlp.redirect::lang.permission.access_redirects.label',
'tab' => 'vdlp.redirect::lang.permission.access_redirects.tab',
],
'vdlp.redirect.access_redirect_statistics' => [
'label' => 'vdlp.redirect::lang.permission.access_redirect_statistics.label',
'tab' => 'vdlp.redirect::lang.permission.access_redirects.tab',
],
];
}

Expand Down Expand Up @@ -150,7 +154,7 @@ public function registerNavigation(): array
'url' => $defaultBackendUrl,
'order' => 201,
'permissions' => [
'vdlp.redirect.access_redirects',
'vdlp.redirect.*',
],
'sideMenu' => $sideMenu,
],
Expand All @@ -163,7 +167,7 @@ public function registerNavigation(): array
'url' => Backend::url('vdlp/redirect/statistics'),
'order' => 10,
'permissions' => [
'vdlp.redirect.access_redirects',
'vdlp.redirect.access_redirect_statistics',
],
];
}
Expand Down Expand Up @@ -220,6 +224,9 @@ public function registerReportWidgets(): array
$reportWidgets[ReportWidgets\CreateRedirect::class] = [
'label' => 'vdlp.redirect::lang.buttons.create_redirect',
'context' => 'dashboard',
'permissions' => [
'vdlp.redirect.access_redirects',
],
];

if (Settings::isStatisticsEnabled()) {
Expand All @@ -231,6 +238,9 @@ public function registerReportWidgets(): array
]
)),
'context' => 'dashboard',
'permissions' => [
'vdlp.redirect.access_redirect_statistics',
],
];
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
final class Statistics extends Controller
{
public $requiredPermissions = ['vdlp.redirect.access_redirects'];
public $requiredPermissions = ['vdlp.redirect.access_redirect_statistics'];
private StatisticsHelper $helper;

public function __construct()
Expand Down
3 changes: 3 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
'label' => 'Redirects',
'tab' => 'Redirects',
],
'access_redirect_statistics' => [
'label' => 'Redirect Statistics',
],
],
'navigation' => [
'menu_label' => 'Redirects',
Expand Down
3 changes: 3 additions & 0 deletions lang/nl/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
'label' => 'Redirects',
'tab' => 'Redirects',
],
'access_redirect_statistics' => [
'label' => 'Redirect statistieken',
],
],
'navigation' => [
'menu_label' => 'Redirects',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Vdlp\Redirect\Updates;

use Backend\Models\User;
use Backend\Models\UserRole;
use October\Rain\Database\Model;
use October\Rain\Database\Updates\Migration;

class AddNewStatisticsPermissionToCurrentUsersAndRoles extends Migration
{
public function up(): void
{
if (!class_exists(User::class) || !class_exists(UserRole::class)) {
// A check for future releases of October CMS, where the User and Role models might not exist anymore.
return;
}

/** @var User $user */
foreach (User::query()->cursor() as $user) {
$this->updatePermission($user);
}

/** @var UserRole $role */
foreach (UserRole::query()->cursor() as $role) {
$this->updatePermission($role);
}
}

public function down(): void
{
// No need to revert the changes.
}

private function updatePermission(Model $model): void
{
$permissions = $model->getAttribute('permissions');

if (!is_array($permissions) || !array_key_exists('vdlp.redirect.access_redirects', $permissions)) {
return;
}

$permissions['vdlp.redirect.access_redirect_statistics'] = 1;
$model->setAttribute('permissions', $permissions);
$model->save();
}
};
3 changes: 3 additions & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,6 @@ v3.1.10: "Fixed backend filtering for MySQL >= 8.0.3."
v3.1.11:
- "Add option to keep query string when redirecting."
- 20231108_0013_add_keep_querystring_to_redirects_table.php
v3.2.0:
- "Add support for a new statistics permission."
- 20240506_0014_add_new_statistics_permission_to_current_users_and_roles.php
Loading