Skip to content

Commit

Permalink
allow project access page to update the settings twice
Browse files Browse the repository at this point in the history
  • Loading branch information
debsmita1 committed Jun 20, 2022
1 parent 0ce6463 commit 3ccb960
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as _ from 'lodash';
import Helmet from 'react-helmet';
import { useTranslation, Trans } from 'react-i18next';
import { Link } from 'react-router-dom';
import { getActiveNamespace } from '@console/internal/actions/ui';
import {
history,
LoadingBox,
Expand All @@ -24,7 +23,7 @@ import {
getRolesWithMultipleSubjects,
} from './project-access-form-submit-utils';
import { getUserRoleBindings, Roles } from './project-access-form-utils';
import { Verb, UserRoleBinding, roleBinding } from './project-access-form-utils-types';
import { Verb, UserRoleBinding } from './project-access-form-utils-types';
import { validationSchema } from './project-access-form-validation-utils';
import ProjectAccessForm from './ProjectAccessForm';

Expand Down Expand Up @@ -57,7 +56,6 @@ const ProjectAccess: React.FC<ProjectAccessProps> = ({

const initialValues = {
projectAccess: roleBindings.loaded && userRoleBindings,
namespace,
};

const handleSubmit = (values, actions) => {
Expand All @@ -81,16 +79,15 @@ const ProjectAccess: React.FC<ProjectAccessProps> = ({
}
updateRoles.push(...updateRolesWithMultipleSubjects);
const roleBindingRequests = [];
roleBinding.metadata.namespace = namespace;

if (updateRoles.length > 0) {
roleBindingRequests.push(...sendRoleBindingRequest(Verb.Patch, updateRoles, roleBinding));
roleBindingRequests.push(...sendRoleBindingRequest(Verb.Patch, updateRoles, namespace));
}
if (newRoles.length > 0) {
roleBindingRequests.push(...sendRoleBindingRequest(Verb.Create, newRoles, roleBinding));
roleBindingRequests.push(...sendRoleBindingRequest(Verb.Create, newRoles, namespace));
}
if (removeRoles.length > 0) {
roleBindingRequests.push(...sendRoleBindingRequest(Verb.Remove, removeRoles, roleBinding));
roleBindingRequests.push(...sendRoleBindingRequest(Verb.Remove, removeRoles, namespace));
}

return Promise.all(roleBindingRequests)
Expand Down Expand Up @@ -118,11 +115,9 @@ const ProjectAccess: React.FC<ProjectAccessProps> = ({
{
"Project access allows you to add or remove a user's access to the project. More advanced management of role-based access control appear in "
}
<Link to={`/k8s/ns/${getActiveNamespace()}/${RoleModel.plural}`}>Roles</Link> and{' '}
<Link to={`/k8s/ns/${getActiveNamespace()}/${RoleBindingModel.plural}`}>
Role Bindings
</Link>
. For more information, see the{' '}
<Link to={`/k8s/ns/${namespace}/${RoleModel.plural}`}>Roles</Link> and{' '}
<Link to={`/k8s/ns/${namespace}/${RoleBindingModel.plural}`}>Role Bindings</Link>. For
more information, see the{' '}
<ExternalLink href={rbacLink}>role-based access control documentation</ExternalLink>.
</Trans>
</PageHeading>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,15 @@ export const getRemovedRoles = (
export const sendRoleBindingRequest = (
verb: string,
roles: UserRoleBinding[],
roleBinding: RoleBinding,
namespace: string,
) => {
const rb = _.clone(roleBinding);
const finalArray = [];
const finalArray: Promise<K8sResourceKind>[] = [];
_.forEach(roles, (user) => {
const roleBindingName =
verb === Verb.Create
? generateRoleBindingName(user.subject.name, user.role)
: user.roleBindingName;
rb.subjects =
const subjects =
verb === Verb.Create || verb === Verb.Remove
? [
{
Expand All @@ -107,9 +106,21 @@ export const sendRoleBindingRequest = (
: user.subjects.length > 1
? user.subjects
: [user.subject];
rb.roleRef.name = user.role;
rb.metadata.name = roleBindingName;
finalArray.push(sendK8sRequest(verb, rb));
const roleBinding: RoleBinding = {
apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'RoleBinding',
metadata: {
name: roleBindingName,
namespace,
},
roleRef: {
apiGroup: 'rbac.authorization.k8s.io',
kind: 'ClusterRole',
name: user.role,
},
subjects,
};
finalArray.push(sendK8sRequest(verb, roleBinding));
});
return finalArray;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,3 @@ export type RoleBinding = K8sResourceCommon & {
roleRef: SubjectType;
subjects?: SubjectType[];
};

export const roleBinding: RoleBinding = {
apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'RoleBinding',
metadata: {
name: '',
namespace: '',
},
roleRef: {
apiGroup: 'rbac.authorization.k8s.io',
kind: 'ClusterRole',
name: '',
},
subjects: [],
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as yup from 'yup';
import i18n from '@console/internal/i18n';

export const validationSchema = yup.object().shape({
namespace: yup.string().required(i18n.t('devconsole~Required')),
projectAccess: yup.array().of(
yup.object().shape({
subject: yup.object().shape({ name: yup.string().required(i18n.t('devconsole~Required')) }),
Expand Down

0 comments on commit 3ccb960

Please sign in to comment.