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

Add UI permissions based on the user role #6823

Merged
merged 4 commits into from
Aug 27, 2020
Merged

Conversation

hishamco
Copy link
Member

@hishamco hishamco commented Aug 4, 2020

Fixes #6847

@hishamco hishamco marked this pull request as ready for review August 4, 2020 08:02
@if (String.IsNullOrWhiteSpace(returnUrl))
{
<button class="primaryAction btn btn-primary" type="submit" name="submit.Save" value="submit.Save">@T["Save Draft"]</button>
}
else
{
<div class="btn-group">
<button class="btn btn-primary" type="submit" name="submit.Save" value="submit.Save">@T["Save Draft"]</button>
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-reference="parent" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<button class="btn btn-primary @((hasEditOwnContentPermission && contentItem?.Owner == User.Identity.Name) || hasEditContentPermission ? String.Empty : "disabled")" type="submit" name="submit.Save" value="submit.Save">@T["Save Draft"]</button>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure why the Model.ContentItem is null

@deanmarcussen
Copy link
Member

@hishamco I'm afraid this is not the correct solution.

The issue, as I explained to you on your last pr related to this, starts as a problem with the content type filter, which is designed to only list content types for which the user has the Edit permission.

The bug, or issue, is that when the filter is finds no editable content types, it is currently falling back to listing all content types.

That is the issue to resolve, and following that the issue would be how could a contributor list their own content.

One of the answers to that, is that they can't, and the role maybe more intended as a permission for contributors contributing via live writer, or other entry points to the system.

Note: This is how it was in O1 as well

@hishamco
Copy link
Member Author

hishamco commented Aug 4, 2020

The bug, or issue, is that when the filter is finds no editable content types, it is currently falling back to listing all content types.

This is another issue, I started with the issue that raise during the demo of @agriffard last two weeks when he publish a content and got 404, then after checking the roles permissions I realize there is no issue with that, except the UI permissions so that's why I point to that in the original issue

I think we need to change the issue's title to avoid misunderstanding

@hishamco
Copy link
Member Author

hishamco commented Aug 4, 2020

We need to open another PR for the issue that you mentioned

@deanmarcussen
Copy link
Member

Ok maybe they are seperate issues.

Could you read the comments in the code on how to check for content permissions. This is not the correct way

@hishamco
Copy link
Member Author

hishamco commented Aug 4, 2020

Ok maybe they are seperate issues.

Yep, this is another one, I remembered the one that you meant, there is an open PR that needs a revise

Could you read the comments in the code on how to check for content permissions. This is not the correct way

Which comments do you mean? If I'm not wrong AuthorizeAsync() is the way to check the permissions

@sebastienros
Copy link
Member

It's not clear what this PR is trying to fix, it seems unrelated to the issue that is linked. Can you open an issue and describe it?

@hishamco
Copy link
Member Author

hishamco commented Aug 6, 2020

Sure

<div class="form-group">
<div class="btn-group">
@if (Model.CreatableTypes.Any())
{
ContentItem contentItem = await ContentManager.NewAsync(Model.CreatableTypes?.First().Value);
var hasEditContentPermission = await AuthorizationService.AuthorizeAsync(User, OrchardCore.Contents.Permissions.EditContent, contentItem);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check should have already happened in the controller, and it shouldn't be in the CreatableTypes list.

If it isn't happening in the controller it should.

It would also have to happen on every type, not just the first one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the check was there before I added my changes, I will remove it then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list of CreatableTypes should just not contain the ones you can't create. And even that is a minor issue IMO if that complexifies the code too much (or make it too slow).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code you've added needs to be in the controller, or not at all

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deanmarcussen AFAIK when the permission is not applied in the controller we returns Forbid, so in this case how can I check the permission If I move it to the controller?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See where the list is being created

creatableList.Add(new SelectListItem(new LocalizedString(contentTypeDefinition.DisplayName, contentTypeDefinition.DisplayName).Value, contentTypeDefinition.Name));

var authorized = await _authorizationService.AuthorizeAsync(User, Permissions.EditContent, await _contentManager.NewAsync(ctd.Name));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't dig into the code before, it's seems the check is already there, so I remove the one in the view

@if (hasPublished)
{
<a display-for="@contentItem" class="btn btn-success btn-sm" itemprop="">@T["View"]</a>
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2020-08-20 at 09 51 14

???

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange!! let me check again ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah!! the condition get copied twice during merge conflict

@agriffard agriffard merged commit 3c47ed2 into dev Aug 27, 2020
@delete-merged-branch delete-merged-branch bot deleted the hishamco/ui-permissions branch August 27, 2020 12:15
var returnUrl = Context.Request.Query["returnUrl"];
var hasPublishContentPermission = await AuthorizationService.AuthorizeAsync(User, OrchardCore.Contents.Permissions.PublishContent, contentItem);
Copy link
Contributor

@Skrypt Skrypt Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PublishOwnContent. These permissions are not taking in consideration when we want to use PublishOwn or EditOwn they need to be changed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These permissions are not taking in consideration when we want to use PublishOwn or EditOwn

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Skrypt code is good here I think. a PublishContent permission request is morphed to PublishOwn. You shouldn't request PublishOwn directly (see comments in CommonPermissions)

Copy link
Contributor

@Skrypt Skrypt Sep 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work if you have set a PublishOwn_ContentTypeName permission which is dynamic. So the issue is that the PR makes it impossible to have a role that has Publish/Edit/Delete own permission on a single custom content type. I have a PR coming up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -57,3 +57,15 @@
{
<div class="col primary">@await DisplayAsync(Model.Content)</div>
}

<script at="Foot" type="text/javascript">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use a named script with a class selector ? Instead of having tons of instances of this code on the page ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@netwavebe already fixed it @jptissot exactly as you suggested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool, I did not realize this was an old pr :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It got merged to dev a few days ago. See #7070

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @netwavebe :)

@chinasqzl
Copy link
Contributor

ContentsDriver.Edit
Dynamic("Content_PublishButton").Location("Actions:10")
results.Add(Dynamic("Content_SaveDraftButton").Location("Actions:20"));

ContentItem contentItem = Model.ContentItem;
var hasPublishContentPermission = await AuthorizationService.AuthorizeAsync(User, OrchardCore.Contents.Permissions.PublishContent, contentItem);

Model.ContentItem is null

@hishamco
Copy link
Member Author

@Skrypt can you please check this in your PR?

@Skrypt
Copy link
Contributor

Skrypt commented Sep 22, 2020

The ContentItem passed to the ContentTypeAuthorizationHandler can sometimes be null but in this case here it's not supposed to.

In the ContentsDriver it assumes that it renders a ContentItem already. If the ContentItem == null then the ContentTypeAuthorizationHandler will simply return the previous Auth result without processing anything further. I looked last night and ContentTypeAuthorizationHandler is fine. Though here if the ContentItem == null then the ContentDriver should throw an exception and it's a different issue I think.

@chinasqzl
Copy link
Contributor

image
image

@Skrypt

@Skrypt
Copy link
Contributor

Skrypt commented Sep 29, 2020

Yes please PR has moved to #7104 now.
Here the issue comes from the ContentTypeAuthorizationHandler if you are not seeing the proper buttons.
This commit should fix your issue : 7908293#diff-f32a3f59eaeef643f8774e25b903ff08R48
Though, I tested everything in #7104

@hishamco
Copy link
Member Author

Is it regression bug or something else related?

@Skrypt
Copy link
Contributor

Skrypt commented Sep 29, 2020

This PR fixes one part of the issues. I think we fixed both the same issues in 2 different PR's. Mine is going one step further by introducing 2 new permissions for the admin UI. I'm not sure it's a regression. I think the issue was there since a while and adding the permissions on the buttons just revealed the underlying issue.

Skrypt added a commit that referenced this pull request Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disable action buttons based on role permissions
8 participants