Skip to content

Commit

Permalink
fix(sidebar): show authorized links [EE-3610] (#7152)
Browse files Browse the repository at this point in the history
  • Loading branch information
chiptus authored Jul 22, 2022
1 parent 005c48b commit 9a92b97
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 41 deletions.
15 changes: 14 additions & 1 deletion app/kubernetes/__module.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,20 @@ angular.module('portainer.kubernetes', ['portainer.app', registriesModule, custo
},
};

const endpointKubernetesConfiguration = {
name: 'kubernetes.cluster.setup',
url: '/configure',
views: {
'content@': {
templateUrl: './views/configure/configure.html',
controller: 'KubernetesConfigureController',
controllerAs: 'ctrl',
},
},
};

const endpointKubernetesSecurityConstraint = {
name: 'portainer.k8sendpoint.securityConstraint',
name: 'kubernetes.cluster.securityConstraint',
url: '/securityConstraint',
views: {
'content@': {
Expand Down Expand Up @@ -362,6 +374,7 @@ angular.module('portainer.kubernetes', ['portainer.app', registriesModule, custo
$stateRegistryProvider.register(volume);
$stateRegistryProvider.register(registries);
$stateRegistryProvider.register(registriesAccess);
$stateRegistryProvider.register(endpointKubernetesConfiguration);
$stateRegistryProvider.register(endpointKubernetesSecurityConstraint);
},
]);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div ng-if="$ctrl.isAdmin()" class="small" ng-show="$ctrl.serviceType === $ctrl.KubernetesApplicationPublishingTypes.LOAD_BALANCER && !$ctrl.loadbalancerEnabled">
<p style="margin-top: 10px">
<pr-icon icon="'alert-circle'" mode="'primary'" feather="true"></pr-icon> No Load balancer is available in this cluster, click
<a ui-sref="portainer.k8sendpoint.kubernetesConfig({id: $ctrl.state.endpointId})">here</a> to configure load balancer.
<a ui-sref="kubernetes.cluster.setup">here</a> to configure load balancer.
</p>
</div>
<div ng-if="!$ctrl.isAdmin()" class="small" ng-show="$ctrl.serviceType === $ctrl.KubernetesApplicationPublishingTypes.LOAD_BALANCER && !$ctrl.loadbalancerEnabled">
Expand Down
2 changes: 1 addition & 1 deletion app/kubernetes/components/kube-services/kube-services.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<div ng-if="$ctrl.isAdmin()" class="small">
<p style="margin-top: 10px">
<pr-icon icon="'alert-triangle'" mode="'warning'" feather="true"></pr-icon> Ingress is not configured in this namespace, select another namespace or click
<a ui-sref="portainer.k8sendpoint.kubernetesConfig({id: $ctrl.state.endpointId})">here</a> to configure ingress.
<a ui-sref="kubernetes.cluster.setup">here</a> to configure ingress.
</p>
</div>
<div ng-if="!$ctrl.isAdmin()" class="small">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@
<p ng-if="!ctrl.isAdmin"> This feature is currently disabled and must be enabled by an administrator user. </p>
<p ng-if="ctrl.isAdmin">
Server metrics features must be enabled in the
<a ui-sref="portainer.k8sendpoint.kubernetesConfig({id: ctrl.endpoint.Id})" class="ctrl.isAdmin">environment configuration view</a>.
<a ui-sref="kubernetes.cluster.setup" class="ctrl.isAdmin">environment configuration view</a>.
</p>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/kubernetes/views/configure/configureController.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class KubernetesConfigureController {
actionInProgress: false,
displayConfigureClassPanel: {},
viewReady: false,
endpointId: this.$state.params.id,
endpointId: this.$state.params.endpointId,
duplicates: {
ingressClasses: new KubernetesFormValidationReferences(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@
<div class="form-group" ng-if="$ctrl.formValues.IngressClasses.length === 0">
<div class="col-sm-12 small text-muted">
The ingress feature must be enabled in the
<a ui-sref="portainer.k8sendpoint.kubernetesConfig({id: $ctrl.endpoint.Id})">environment configuration view</a> to be able to register ingresses inside this
namespace.
<a ui-sref="kubernetes.cluster.setup">environment configuration view</a> to be able to register ingresses inside this namespace.
</div>
</div>

Expand Down
3 changes: 1 addition & 2 deletions app/kubernetes/views/resource-pools/edit/resourcePool.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@
<div class="form-group" ng-if="ctrl.formValues.IngressClasses.length === 0">
<div class="col-sm-12 small text-muted">
The ingress feature must be enabled in the
<a ui-sref="portainer.k8sendpoint.kubernetesConfig({id: ctrl.endpoint.Id})">environment configuration view</a> to be able to register ingresses inside this
namespace.
<a ui-sref="kubernetes.cluster.setup">environment configuration view</a> to be able to register ingresses inside this namespace.
</div>
</div>

Expand Down
19 changes: 0 additions & 19 deletions app/portainer/__module.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,6 @@ angular
},
};

var k8sendpoint = {
name: 'portainer.k8sendpoint',
url: '/:id',
};

const endpointKubernetesConfiguration = {
name: 'portainer.k8sendpoint.kubernetesConfig',
url: '/configure',
views: {
'content@': {
templateUrl: '../kubernetes/views/configure/configure.html',
controller: 'KubernetesConfigureController',
controllerAs: 'ctrl',
},
},
};

var edgeDeviceCreation = {
name: 'portainer.endpoints.newEdgeDevice',
url: '/newEdgeDevice',
Expand Down Expand Up @@ -484,14 +467,12 @@ angular
$stateRegistryProvider.register(logout);
$stateRegistryProvider.register(endpoints);
$stateRegistryProvider.register(endpoint);
$stateRegistryProvider.register(k8sendpoint);
$stateRegistryProvider.register(endpointAccess);
$stateRegistryProvider.register(endpointKVM);
$stateRegistryProvider.register(edgeDeviceCreation);
$stateRegistryProvider.register(deviceImport);
$stateRegistryProvider.register(addFDOProfile);
$stateRegistryProvider.register(editFDOProfile);
$stateRegistryProvider.register(endpointKubernetesConfiguration);
$stateRegistryProvider.register(groups);
$stateRegistryProvider.register(group);
$stateRegistryProvider.register(groupAccess);
Expand Down
16 changes: 14 additions & 2 deletions app/portainer/hooks/useUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function useUser() {

export function useAuthorizations(
authorizations: string | string[],
forceEnvironmentId?: EnvironmentId,
adminOnlyCE = false
) {
const { user } = useUser();
Expand All @@ -58,7 +59,12 @@ export function useAuthorizations(
return false;
}

return hasAuthorizations(user, authorizations, endpointId, adminOnlyCE);
return hasAuthorizations(
user,
authorizations,
forceEnvironmentId || endpointId,
adminOnlyCE
);
}

export function isEnvironmentAdmin(
Expand Down Expand Up @@ -114,15 +120,21 @@ export function hasAuthorizations(

interface AuthorizedProps {
authorizations: string | string[];
environmentId?: EnvironmentId;
adminOnlyCE?: boolean;
}

export function Authorized({
authorizations,
environmentId,
adminOnlyCE = false,
children,
}: PropsWithChildren<AuthorizedProps>) {
const isAllowed = useAuthorizations(authorizations, adminOnlyCE);
const isAllowed = useAuthorizations(
authorizations,
environmentId,
adminOnlyCE
);

return isAllowed ? <>{children}</> : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ angular
EndpointService.createLocalKubernetesEndpoint(name, tagIds)
.then(function success(result) {
Notifications.success('Environment created', name);
$state.go('portainer.k8sendpoint.kubernetesConfig', { id: result.Id });
$state.go('kubernetes.cluster.setup', { endpoinId: result.Id });
})
.catch(function error(err) {
Notifications.error('Failure', err, 'Unable to create environment');
Expand Down Expand Up @@ -282,7 +282,7 @@ angular
$state.go('portainer.endpoints.endpoint', { id: endpoint.Id });
break;
case PortainerEndpointTypes.AgentOnKubernetesEnvironment:
$state.go('portainer.k8sendpoint.kubernetesConfig', { id: endpoint.Id });
$state.go('kubernetes.cluster.setup', { endpoinId: endpoint.Id });
break;
default:
$state.go('portainer.endpoints', {}, { reload: true });
Expand Down
2 changes: 1 addition & 1 deletion app/portainer/views/endpoints/edit/endpoint.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<span class="small text-muted">
<i class="fa fa-tools blue-icon" aria-hidden="true" style="margin-right: 2px"></i>
You should configure the features available in this Kubernetes environment in the
<a ui-sref="portainer.k8sendpoint.kubernetesConfig({id: endpoint.Id})">Kubernetes configuration</a> view.
<a ui-sref="kubernetes.cluster.setup({endpointId: endpoint.Id})">Kubernetes configuration</a> view.
</span>
</information-panel>
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/portainer/views/init/endpoint/initEndpointController.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class InitEndpointController {
try {
this.state.actionInProgress = true;
const endpoint = await this.EndpointService.createLocalKubernetesEndpoint();
this.$state.go('portainer.k8sendpoint.kubernetesConfig', { id: endpoint.Id });
this.$state.go('kubernetes.cluster.setup', { endpointId: endpoint.Id });
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to connect to the Kubernetes environment');
} finally {
Expand Down Expand Up @@ -130,8 +130,8 @@ class InitEndpointController {
null,
null
);
const routeName = endpoint.Type === PortainerEndpointTypes.AgentOnKubernetesEnvironment ? 'portainer.k8sendpoint.kubernetesConfig' : 'portainer.home';
this.$state.go(routeName, { id: endpoint.Id });
const routeName = endpoint.Type === PortainerEndpointTypes.AgentOnKubernetesEnvironment ? 'kubernetes.cluster.setup' : 'portainer.home';
this.$state.go(routeName, { endpointId: endpoint.Id });
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to connect to the Docker environment');
} finally {
Expand Down
1 change: 1 addition & 0 deletions app/react/sidebar/DockerSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export function DockerSidebar({ environmentId, environment }: Props) {
<Authorized
authorizations="PortainerEndpointUpdateSettings"
adminOnlyCE
environmentId={environmentId}
>
<SidebarItem
to="docker.featuresConfiguration"
Expand Down
21 changes: 16 additions & 5 deletions app/react/sidebar/KubernetesSidebar/KubernetesSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export function KubernetesSidebar({ environmentId }: Props) {
data-cy="k8sSidebar-namespaces"
/>

<Authorized authorizations="HelmInstallChart">
<Authorized
authorizations="HelmInstallChart"
environmentId={environmentId}
>
<SidebarItem
to="kubernetes.templates.helm"
params={{ endpointId: environmentId }}
Expand Down Expand Up @@ -86,18 +89,26 @@ export function KubernetesSidebar({ environmentId }: Props) {
params={{ endpointId: environmentId }}
data-cy="k8sSidebar-cluster"
>
<Authorized authorizations="K8sClusterSetupRW" adminOnlyCE>
<Authorized
authorizations="K8sClusterSetupRW"
adminOnlyCE
environmentId={environmentId}
>
<SidebarItem
to="portainer.k8sendpoint.kubernetesConfig"
to="kubernetes.cluster.setup"
params={{ id: environmentId }}
label="Setup"
data-cy="k8sSidebar-setup"
/>
</Authorized>

<Authorized authorizations="K8sClusterSetupRW" adminOnlyCE>
<Authorized
authorizations="K8sClusterSetupRW"
adminOnlyCE
environmentId={environmentId}
>
<SidebarItem
to="portainer.k8sendpoint.securityConstraint"
to="kubernetes.cluster.securityConstraint"
params={{ id: environmentId }}
label="Security constraints"
data-cy="k8sSidebar-securityConstraints"
Expand Down

0 comments on commit 9a92b97

Please sign in to comment.