Skip to content

Commit 90c5508

Browse files
committed
Add migration to add the new permission to the existing users and roles
This keeps it from being a breaking change.
1 parent 1e2fee5 commit 90c5508

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vdlp\Redirect\Updates;
6+
7+
use Backend\Models\User;
8+
use Backend\Models\UserRole;
9+
use October\Rain\Database\Model;
10+
use October\Rain\Database\Updates\Migration;
11+
12+
class AddNewStatisticsPermissionToCurrentUsersAndRoles extends Migration
13+
{
14+
public function up(): void
15+
{
16+
if (!class_exists(User::class) || !class_exists(UserRole::class)) {
17+
// A check for future releases of October CMS, where the User and Role models might not exist anymore.
18+
return;
19+
}
20+
21+
/** @var User $user */
22+
foreach (User::query()->cursor() as $user) {
23+
$this->updatePermission($user);
24+
}
25+
26+
/** @var UserRole $role */
27+
foreach (UserRole::query()->cursor() as $role) {
28+
$this->updatePermission($role);
29+
}
30+
}
31+
32+
public function down(): void
33+
{
34+
// No need to revert the changes.
35+
}
36+
37+
private function updatePermission(Model $model): void
38+
{
39+
$permissions = $model->getAttribute('permissions');
40+
41+
if (!is_array($permissions) || !array_key_exists('vdlp.redirect.access_redirects', $permissions)) {
42+
return;
43+
}
44+
45+
$permissions['vdlp.redirect.access_redirect_statistics'] = 1;
46+
$model->setAttribute('permissions', $permissions);
47+
$model->save();
48+
}
49+
};

updates/version.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,6 @@ v3.1.10: "Fixed backend filtering for MySQL >= 8.0.3."
8787
v3.1.11:
8888
- "Add option to keep query string when redirecting."
8989
- 20231108_0013_add_keep_querystring_to_redirects_table.php
90+
v3.2.0:
91+
- "Add support for a new statistics permission."
92+
- 20240506_0014_add_new_statistics_permission_to_current_users_and_roles.php

0 commit comments

Comments
 (0)