Skip to content

Commit

Permalink
Collection upload/deprecate - fix permission checks
Browse files Browse the repository at this point in the history
Issue: AAH-2439
Issue: AAH-2853
  • Loading branch information
himdel committed Nov 18, 2023
1 parent 0d292bf commit 0583d32
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGES/2439.bug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Collection upload/deprecate - fix permission checks
1 change: 1 addition & 0 deletions CHANGES/2853.bug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Collection upload/deprecate - fix permission checks
57 changes: 29 additions & 28 deletions src/components/headers/collection-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ export class CollectionHeader extends React.Component<IProps, IState> {
return <Navigate to={redirect} />;
}

const canSign = canSignNamespace(this.context, this.state.namespace);
const { hasPermission } = this.context;
const hasObjectPermission = (permission, namespace) =>
namespace?.related_fields?.my_permissions?.includes?.(permission);
Expand All @@ -247,27 +246,29 @@ export class CollectionHeader extends React.Component<IProps, IState> {
IS_COMMUNITY &&
hasObjectPermission('galaxy.change_namespace', this.state.namespace);

const canDeleteCollection =
hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection;
const canSign = canSignNamespace(this.context, this.state.namespace);
const canUpload = hasPermission('galaxy.upload_to_namespace');
const canDeprecate = canUpload;

const dropdownItems = [
DeleteCollectionUtils.deleteMenuOption({
canDeleteCollection:
hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection,
canDeleteCollection,
noDependencies,
onClick: () => this.openDeleteModalWithConfirm(null, true),
deleteAll: true,
display_repositories: display_repositories,
}),
DeleteCollectionUtils.deleteMenuOption({
canDeleteCollection:
hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection,
canDeleteCollection,
noDependencies,
onClick: () => this.openDeleteModalWithConfirm(null, false),
deleteAll: false,
display_repositories: display_repositories,
}),
(hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection) && (
canDeleteCollection && (
<DropdownItem
data-cy='delete-collection-version'
key='delete-collection-version'
Expand All @@ -276,17 +277,15 @@ export class CollectionHeader extends React.Component<IProps, IState> {
{t`Delete version ${version} from system`}
</DropdownItem>
),
(hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection) &&
display_repositories && (
<DropdownItem
data-cy='remove-collection-version'
key='remove-collection-version'
onClick={() => this.openDeleteModalWithConfirm(version, false)}
>
{t`Delete version ${version} from repository`}
</DropdownItem>
),
canDeleteCollection && display_repositories && (
<DropdownItem
data-cy='remove-collection-version'
key='remove-collection-version'
onClick={() => this.openDeleteModalWithConfirm(version, false)}
>
{t`Delete version ${version} from repository`}
</DropdownItem>
),
canSign && !can_upload_signatures && (
<DropdownItem
key='sign-all'
Expand Down Expand Up @@ -314,21 +313,23 @@ export class CollectionHeader extends React.Component<IProps, IState> {
{t`Sign version ${version}`}
</DropdownItem>
),
hasPermission('galaxy.upload_to_namespace') && (
canDeprecate && (
<DropdownItem
onClick={() => this.deprecate(collection)}
key='deprecate'
>
{collection.is_deprecated ? t`Undeprecate` : t`Deprecate`}
</DropdownItem>
),
<DropdownItem
key='upload-collection-version'
onClick={() => this.checkUploadPrivilleges(collection)}
data-cy='upload-collection-version-dropdown'
>
{t`Upload new version`}
</DropdownItem>,
canUpload && (
<DropdownItem
key='upload-collection-version'
onClick={() => this.checkUploadPrivilleges(collection)}
data-cy='upload-collection-version-dropdown'
>
{t`Upload new version`}
</DropdownItem>
),
display_repositories && (
<DropdownItem
key='copy-collection-version-to-repository-dropdown'
Expand Down
37 changes: 19 additions & 18 deletions src/containers/namespace-detail/namespace-detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1017,13 +1017,16 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
const canDeleteCommunityCollection =
IS_COMMUNITY &&
hasObjectPermission('galaxy.change_namespace', this.state.namespace);
const canDeleteCollection = hasPermission('ansible.delete_collection') || canDeleteCommunityCollection;
const canUpload = hasPermission('galaxy.upload_to_namespace');
const canDeprecate = canUpload;

if (!showControls) {
return;
}

return {
uploadButton: (
uploadButton: canUpload && (
<Button
onClick={() =>
this.handleCollectionAction(
Expand All @@ -1040,9 +1043,7 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
<StatefulDropdown
items={[
DeleteCollectionUtils.deleteMenuOption({
canDeleteCollection:
hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection,
canDeleteCollection,
noDependencies: null,
onClick: () =>
DeleteCollectionUtils.tryOpenDeleteModalWithConfirm({
Expand All @@ -1055,9 +1056,7 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
display_repositories: display_repositories,
}),
DeleteCollectionUtils.deleteMenuOption({
canDeleteCollection:
hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection,
canDeleteCollection,
noDependencies: null,
onClick: () =>
DeleteCollectionUtils.tryOpenDeleteModalWithConfirm({
Expand All @@ -1069,17 +1068,19 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
deleteAll: false,
display_repositories: display_repositories,
}),
<DropdownItem
onClick={() =>
this.handleCollectionAction(
collection.collection_version.pulp_href,
'deprecate',
)
}
key='deprecate'
>
{collection.is_deprecated ? t`Undeprecate` : t`Deprecate`}
</DropdownItem>,
canDeprecate && (
<DropdownItem
onClick={() =>
this.handleCollectionAction(
collection.collection_version.pulp_href,
'deprecate',
)
}
key='deprecate'
>
{collection.is_deprecated ? t`Undeprecate` : t`Deprecate`}
</DropdownItem>
),
].filter(Boolean)}
ariaLabel='collection-kebab'
/>
Expand Down
17 changes: 8 additions & 9 deletions src/containers/search/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,13 @@ class Search extends React.Component<RouteProps, IState> {
'galaxy.change_namespace',
collection.collection_version.namespace,
);
const canDeleteCollection = hasPermission('ansible.delete_collection') || canDeleteCommunityCollection;
const canUpload = hasPermission('galaxy.upload_to_namespace');
const canDeprecate = canUpload;

const menuItems = [
DeleteCollectionUtils.deleteMenuOption({
canDeleteCollection:
hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection,
canDeleteCollection,
noDependencies: null,
onClick: () =>
DeleteCollectionUtils.tryOpenDeleteModalWithConfirm({
Expand All @@ -414,9 +415,7 @@ class Search extends React.Component<RouteProps, IState> {
display_repositories: display_repositories,
}),
DeleteCollectionUtils.deleteMenuOption({
canDeleteCollection:
hasPermission('ansible.delete_collection') ||
canDeleteCommunityCollection,
canDeleteCollection,
noDependencies: null,
onClick: () =>
DeleteCollectionUtils.tryOpenDeleteModalWithConfirm({
Expand All @@ -428,15 +427,15 @@ class Search extends React.Component<RouteProps, IState> {
deleteAll: false,
display_repositories: display_repositories,
}),
hasPermission('galaxy.upload_to_namespace') && (
canDeprecate && (
<DropdownItem
onClick={() => this.handleControlClick(collection)}
key='deprecate'
>
{collection.is_deprecated ? t`Undeprecate` : t`Deprecate`}
</DropdownItem>
),
!list && hasPermission('galaxy.upload_to_namespace') && (
!list && canUpload && (
<DropdownItem
onClick={() => this.checkUploadPrivilleges(collection)}
key='upload new version'
Expand All @@ -450,7 +449,7 @@ class Search extends React.Component<RouteProps, IState> {

if (list) {
return {
uploadButton: hasPermission('galaxy.upload_to_namespace') ? (
uploadButton: canUpload ? (
<Button
onClick={() => this.checkUploadPrivilleges(collection)}
variant='secondary'
Expand Down

0 comments on commit 0583d32

Please sign in to comment.