|
| 1 | +import React, { useEffect, useRef, useState } from 'react' |
| 2 | +import { |
| 3 | + CButton, |
| 4 | + CCallout, |
| 5 | + CCol, |
| 6 | + CForm, |
| 7 | + CRow, |
| 8 | + CAccordion, |
| 9 | + CAccordionHeader, |
| 10 | + CAccordionBody, |
| 11 | + CAccordionItem, |
| 12 | +} from '@coreui/react' |
| 13 | +import { Field, Form, FormSpy } from 'react-final-form' |
| 14 | +import { RFFCFormRadioList, RFFSelectSearch } from 'src/components/forms' |
| 15 | +import { useGenericGetRequestQuery, useLazyGenericPostRequestQuery } from 'src/store/api/app' |
| 16 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
| 17 | +import { TenantSelectorMultiple, ModalService, CippOffcanvas } from 'src/components/utilities' |
| 18 | +import PropTypes from 'prop-types' |
| 19 | +import { OnChange } from 'react-final-form-listeners' |
| 20 | +import { useListTenantsQuery } from 'src/store/api/tenants' |
| 21 | +import { OffcanvasListSection } from 'src/components/utilities/CippListOffcanvas' |
| 22 | +import CippButtonCard from 'src/components/contentcards/CippButtonCard' |
| 23 | +import GDAPRoles from 'src/data/GDAPRoles' |
| 24 | + |
| 25 | +const SettingsSAMRoles = () => { |
| 26 | + const [genericPostRequest, postResults] = useLazyGenericPostRequestQuery() |
| 27 | + const [selectedTenant, setSelectedTenant] = useState([]) |
| 28 | + const tenantSelectorRef = useRef() |
| 29 | + const { |
| 30 | + data: tenants = [], |
| 31 | + isFetching: tenantsFetching, |
| 32 | + isSuccess: tenantSuccess, |
| 33 | + } = useListTenantsQuery({ |
| 34 | + showAllTenantSelector: true, |
| 35 | + }) |
| 36 | + |
| 37 | + const { |
| 38 | + data: cippSAMRoles = [], |
| 39 | + isFetching: roleListFetching, |
| 40 | + isSuccess: roleListSuccess, |
| 41 | + refetch: refetchRoleList, |
| 42 | + } = useGenericGetRequestQuery({ |
| 43 | + path: 'api/ExecSAMRoles', |
| 44 | + }) |
| 45 | + |
| 46 | + const handleTenantChange = (e) => { |
| 47 | + setSelectedTenant(e) |
| 48 | + } |
| 49 | + |
| 50 | + const handleSubmit = async (values) => { |
| 51 | + //filter on only objects that are 'true' |
| 52 | + genericPostRequest({ |
| 53 | + path: '/api/ExecSAMRoles?Action=Update', |
| 54 | + values: { |
| 55 | + Roles: values.Roles, |
| 56 | + Tenants: selectedTenant.map((tenant) => tenant.value), |
| 57 | + }, |
| 58 | + }).then(() => { |
| 59 | + refetchRoleList() |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + useEffect(() => { |
| 64 | + if (roleListSuccess && cippSAMRoles.Tenants.length > 0) { |
| 65 | + var selectedTenants = [] |
| 66 | + tenants.map((tenant) => { |
| 67 | + if (cippSAMRoles.Tenants.includes(tenant.customerId)) { |
| 68 | + selectedTenants.push({ label: tenant.displayName, value: tenant.customerId }) |
| 69 | + } |
| 70 | + }) |
| 71 | + tenantSelectorRef.current.setValue(selectedTenants) |
| 72 | + } |
| 73 | + }, [cippSAMRoles, roleListSuccess, tenantSuccess, tenantSelectorRef, tenants]) |
| 74 | + |
| 75 | + return ( |
| 76 | + <CippButtonCard title="CIPP-SAM Roles" titleType="big" isFetching={roleListFetching}> |
| 77 | + <> |
| 78 | + <p className="me-1"> |
| 79 | + Add your CIPP-SAM application Service Principal directly to Admin Roles in the tenant. |
| 80 | + This is an advanced use case where you need access to additional Graph endpoints or |
| 81 | + Exchange Cmdlets otherwise unavailable via Delegated permissions. |
| 82 | + </p> |
| 83 | + <p className="small"> |
| 84 | + <FontAwesomeIcon icon="triangle-exclamation" className="me-2" /> This functionality is in |
| 85 | + beta and should be treated as such. Roles are added during the Update Permissions process |
| 86 | + or a CPV refresh. |
| 87 | + </p> |
| 88 | + |
| 89 | + {roleListSuccess && ( |
| 90 | + <Form |
| 91 | + onSubmit={handleSubmit} |
| 92 | + initialValues={cippSAMRoles} |
| 93 | + render={({ handleSubmit, submitting, values }) => { |
| 94 | + return ( |
| 95 | + <CForm onSubmit={handleSubmit}> |
| 96 | + <CRow className="mb-3"> |
| 97 | + <CCol xl={8} md={12} className="mb-3"> |
| 98 | + <div className="mb-3"> |
| 99 | + <RFFSelectSearch |
| 100 | + name="Roles" |
| 101 | + label="Admin Roles" |
| 102 | + values={GDAPRoles.map((role) => ({ |
| 103 | + name: role.Name, |
| 104 | + value: role.ObjectId, |
| 105 | + }))} |
| 106 | + multi={true} |
| 107 | + refreshFunction={() => refetchRoleList()} |
| 108 | + placeholder="Select admin roles" |
| 109 | + /> |
| 110 | + </div> |
| 111 | + <div className="mb-3"> |
| 112 | + <h5>Selected Tenants</h5> |
| 113 | + <TenantSelectorMultiple |
| 114 | + ref={tenantSelectorRef} |
| 115 | + values={selectedTenant} |
| 116 | + AllTenants={true} |
| 117 | + valueIsDomain={true} |
| 118 | + onChange={(e) => handleTenantChange(e)} |
| 119 | + /> |
| 120 | + </div> |
| 121 | + </CCol> |
| 122 | + </CRow> |
| 123 | + <CRow className="me-3"> |
| 124 | + {postResults.isSuccess && ( |
| 125 | + <CCallout color="success">{postResults.data.Results}</CCallout> |
| 126 | + )} |
| 127 | + <CRow className="mb-3"> |
| 128 | + <CCol xl={4} md={12}> |
| 129 | + <CButton className="me-2" type="submit" disabled={submitting}> |
| 130 | + <FontAwesomeIcon |
| 131 | + icon={postResults.isFetching ? 'circle-notch' : 'save'} |
| 132 | + spin={postResults.isFetching} |
| 133 | + className="me-2" |
| 134 | + /> |
| 135 | + Save |
| 136 | + </CButton> |
| 137 | + </CCol> |
| 138 | + </CRow> |
| 139 | + </CRow> |
| 140 | + </CForm> |
| 141 | + ) |
| 142 | + }} |
| 143 | + /> |
| 144 | + )} |
| 145 | + </> |
| 146 | + </CippButtonCard> |
| 147 | + ) |
| 148 | +} |
| 149 | + |
| 150 | +export default SettingsSAMRoles |
0 commit comments