Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Dynamic permission can't save in role #13024

Closed
hyzx86 opened this issue Jan 2, 2023 · 10 comments · Fixed by #13040
Closed

Dynamic permission can't save in role #13024

hyzx86 opened this issue Jan 2, 2023 · 10 comments · Fixed by #13040
Labels
Milestone

Comments

@hyzx86
Copy link
Contributor

hyzx86 commented Jan 2, 2023

bug in pull: #12510

  1. Edit a role
  2. Select some dynamic permissions , like 'ExecuteApi_RecentBlogPosts'
  3. Click save .
  4. Reopen the role , ExecuteApi_RecentBlogPosts is not checked

cuz the code only can load the static permissions , the program will break on line 235

var permissionNames = _permissionProviders.SelectMany(x => x.GetDefaultStereotypes())
.SelectMany(y => y.Permissions ?? Enumerable.Empty<Permission>())
.Select(x => x.Name)
.ToList();
// Save
var rolePermissions = new List<RoleClaim>();
foreach (var key in Request.Form.Keys)
{
var permissionName = key;
if (key.StartsWith("Checkbox.", StringComparison.Ordinal))
{
permissionName = key.Substring("Checkbox.".Length);
}
if (!permissionNames.Contains(permissionName, StringComparer.OrdinalIgnoreCase))
{
// The request contains an invalid permission, let's ignore it
continue;
}
if (!rolesDocument.PermissionGroups[role.RoleName].Contains(permissionName, StringComparer.OrdinalIgnoreCase))
{
// Save the permission in the permissions history so it never gets auto assigned to this role.
rolesDocument.PermissionGroups[role.RoleName].Add(permissionName);
updateRolesDocument = true;
}
if (String.Equals(Request.Form[key], "true", StringComparison.OrdinalIgnoreCase))
{
rolePermissions.Add(new RoleClaim { ClaimType = Permission.ClaimType, ClaimValue = permissionName });
}
}

We should load all permissions this way:

var installedPermissions = await GetInstalledPermissionsAsync();
var allPermissions = installedPermissions.SelectMany(x => x.Value);

@hyzx86 hyzx86 added the bug 🐛 label Jan 2, 2023
@hyzx86 hyzx86 changed the title Dynamic permission can't save Dynamic permission can't save on role Jan 2, 2023
@hyzx86 hyzx86 changed the title Dynamic permission can't save on role Dynamic permission can't save in role Jan 2, 2023
@hyzx86
Copy link
Contributor Author

hyzx86 commented Jan 2, 2023

chrome_SF0p462j0X.mp4

@gvkries
Copy link
Contributor

gvkries commented Jan 2, 2023

Hehe, I just wanted to open the same bug. Actually there is a second even bigger issue with this PR. The way the RoleUpdater is now implemented causes it to always overwrite any admin settings, whenever the site starts. E.g. the 'Anonymous' role is now always getting the 'ViewContent' permission on startup, which breaks the whole content type permission system. Every content type is then allowed implicitly for everyone.

@hyzx86
Copy link
Contributor Author

hyzx86 commented Jan 2, 2023

It seems that I should turn off PR first 🤣,
I haven't looked at the code carefully, I just let it work
Embarrassed, I found that the role permissions could not be changed until I released the program yesterday

@jtkech
Copy link
Member

jtkech commented Jan 3, 2023

@hyzx86

I think you were in the right path, yes using GetInstalledPermissionsAsync() in the controller edit post seems to fix the issue because it uses GetPermissionsAsync() that also gets dynamic permissions.

For info I could also make it working by just using

        var permissionNames = new List<string>();
        foreach (var permissionProvider in _permissionProviders)
        {
            var permissions = await permissionProvider.GetPermissionsAsync();
            permissionNames.AddRange(permissions.Select(permission => permission.Name));
        }

Maybe you could re-open your PR ;)

As a reminder, things to be checked.

  • Check if okay to use _rolesDocumentManager directly in RoleUpdater and AdminController to update the permissions history, knowing that the RoleStore (called by the aspnet RoleManager) will update the same document.

  • Check if necessary to call the RoleUpdater logic on each feature enabling, before was on feature installed (called once on the first enabling).

  • Check if necessary to call the RoleUpdater logic on each tenant activation. Hmm when a feature state change the shell will be rebuilt and then activated, so maybe at least only on tenant activation but not on any feature change, will see.

@hyzx86
Copy link
Contributor Author

hyzx86 commented Jan 3, 2023

@jtkech reopened,
But I haven't looked at the problem mentioned by @gvkries carefully

@jtkech
Copy link
Member

jtkech commented Jan 3, 2023

Okay no problem will look at the PR tomorrow

In the meantime can you try my above suggestion, if you have time

@hyzx86
Copy link
Contributor Author

hyzx86 commented Jan 3, 2023

I am planning to build a module to obtain the corresponding site settings by specifying a type name
CustomSettings

The first time I tested with 'ViewContent'. Then I changed it to: DeleteCcontent to test again.
I don't think the latter should be given to any user without a role

But it didn't turn out, as I expected, that it actually had access

I have a big screenshot here, which shows my problems

image

@MikeAlhayek
Copy link
Member

@hyzx86 are you working on a PR for this?

@hyzx86
Copy link
Contributor Author

hyzx86 commented Jan 3, 2023

Not really. I just fixed the problem of edit role
#13025

The permission check is not work. I don't know what the problem is

@gvkries
Copy link
Contributor

gvkries commented Jan 3, 2023

I've created a separate issue #13035 for the RoleUpdater bug. I think your PR fixes the bug described here, the other one is related but a little bit harder to solve correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants