|
| 1 | +import React, { useState } from 'react' |
| 2 | +import { CButton, CCallout, CCol, CForm, CRow, CSpinner, CTooltip } from '@coreui/react' |
| 3 | +import { useSelector } from 'react-redux' |
| 4 | +import { Field, Form } from 'react-final-form' |
| 5 | +import { RFFCFormSwitch } from 'src/components/forms' |
| 6 | +import { |
| 7 | + useGenericGetRequestQuery, |
| 8 | + useLazyGenericGetRequestQuery, |
| 9 | + useLazyGenericPostRequestQuery, |
| 10 | +} from 'src/store/api/app' |
| 11 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
| 12 | +import { faCircleNotch, faEdit, faEye } from '@fortawesome/free-solid-svg-icons' |
| 13 | +import { CippPage, CippPageList } from 'src/components/layout' |
| 14 | +import 'react-datepicker/dist/react-datepicker.css' |
| 15 | +import { ModalService, TenantSelector } from 'src/components/utilities' |
| 16 | +import arrayMutators from 'final-form-arrays' |
| 17 | +import { useListConditionalAccessPoliciesQuery } from 'src/store/api/tenants' |
| 18 | +import CippButtonCard from 'src/components/contentcards/CippButtonCard' |
| 19 | +import { CellTip, cellGenericFormatter } from 'src/components/tables/CellGenericFormat' |
| 20 | +import { cellBadgeFormatter, cellDateFormatter } from 'src/components/tables' |
| 21 | + |
| 22 | +const CreateBackup = () => { |
| 23 | + const [ExecuteGetRequest, getResults] = useLazyGenericGetRequestQuery() |
| 24 | + const currentDate = new Date() |
| 25 | + const [startDate, setStartDate] = useState(currentDate) |
| 26 | + const [endDate, setEndDate] = useState(currentDate) |
| 27 | + |
| 28 | + const tenantDomain = useSelector((state) => state.app.currentTenant.defaultDomainName) |
| 29 | + const [refreshState, setRefreshState] = useState(false) |
| 30 | + const [genericPostRequest, postResults] = useLazyGenericPostRequestQuery() |
| 31 | + |
| 32 | + const onSubmit = (values) => { |
| 33 | + const startDate = new Date() |
| 34 | + startDate.setHours(0, 0, 0, 0) |
| 35 | + //decrease by 45 seconds to ensure the task runs after the current time |
| 36 | + const unixTime = Math.floor(startDate.getTime() / 1000) - 45 |
| 37 | + const shippedValues = { |
| 38 | + TenantFilter: tenantDomain, |
| 39 | + Name: `CIPP Backup ${tenantDomain}`, |
| 40 | + Command: { value: `New-CIPPBackup` }, |
| 41 | + Parameters: { ...values }, |
| 42 | + ScheduledTime: unixTime, |
| 43 | + Recurrence: '1d', |
| 44 | + } |
| 45 | + genericPostRequest({ path: '/api/AddScheduledItem?hidden=true', values: shippedValues }).then( |
| 46 | + (res) => { |
| 47 | + setRefreshState(res.requestId) |
| 48 | + }, |
| 49 | + ) |
| 50 | + } |
| 51 | + const Offcanvas = (row, rowIndex, formatExtraData) => { |
| 52 | + const handleDeleteSchedule = (apiurl, message) => { |
| 53 | + ModalService.confirm({ |
| 54 | + title: 'Confirm', |
| 55 | + body: <div>{message}</div>, |
| 56 | + onConfirm: () => |
| 57 | + ExecuteGetRequest({ path: apiurl }).then((res) => { |
| 58 | + setRefreshState(res.requestId) |
| 59 | + }), |
| 60 | + confirmLabel: 'Continue', |
| 61 | + cancelLabel: 'Cancel', |
| 62 | + }) |
| 63 | + } |
| 64 | + let jsonResults |
| 65 | + try { |
| 66 | + jsonResults = JSON.parse(row.Results) |
| 67 | + } catch (error) { |
| 68 | + jsonResults = row.Results |
| 69 | + } |
| 70 | + |
| 71 | + return ( |
| 72 | + <> |
| 73 | + <CTooltip content="Delete task"> |
| 74 | + <CButton |
| 75 | + onClick={() => |
| 76 | + handleDeleteSchedule( |
| 77 | + `/api/RemoveScheduledItem?&ID=${row.RowKey}`, |
| 78 | + 'Do you want to delete this job?', |
| 79 | + ) |
| 80 | + } |
| 81 | + size="sm" |
| 82 | + variant="ghost" |
| 83 | + color="danger" |
| 84 | + > |
| 85 | + <FontAwesomeIcon icon={'trash'} href="" /> |
| 86 | + </CButton> |
| 87 | + </CTooltip> |
| 88 | + </> |
| 89 | + ) |
| 90 | + } |
| 91 | + const columns = [ |
| 92 | + { |
| 93 | + name: 'Tenant', |
| 94 | + selector: (row) => row['Tenant'], |
| 95 | + sortable: true, |
| 96 | + cell: (row) => CellTip(row['Tenant']), |
| 97 | + exportSelector: 'Tenant', |
| 98 | + }, |
| 99 | + { |
| 100 | + name: 'Task State', |
| 101 | + selector: (row) => row['TaskState'], |
| 102 | + sortable: true, |
| 103 | + cell: cellBadgeFormatter(), |
| 104 | + exportSelector: 'TaskState', |
| 105 | + }, |
| 106 | + { |
| 107 | + name: 'Last executed time', |
| 108 | + selector: (row) => row['ExecutedTime'], |
| 109 | + sortable: true, |
| 110 | + cell: cellDateFormatter({ format: 'relative' }), |
| 111 | + exportSelector: 'ExecutedTime', |
| 112 | + }, |
| 113 | + { |
| 114 | + name: 'Actions', |
| 115 | + cell: Offcanvas, |
| 116 | + maxWidth: '100px', |
| 117 | + }, |
| 118 | + ] |
| 119 | + |
| 120 | + const { |
| 121 | + data: users = [], |
| 122 | + isFetching: usersIsFetching, |
| 123 | + error: usersError, |
| 124 | + } = useGenericGetRequestQuery({ |
| 125 | + path: '/api/ListGraphRequest', |
| 126 | + params: { |
| 127 | + TenantFilter: tenantDomain, |
| 128 | + Endpoint: 'users', |
| 129 | + $select: 'id,displayName,userPrincipalName,accountEnabled', |
| 130 | + $count: true, |
| 131 | + $top: 999, |
| 132 | + $orderby: 'displayName', |
| 133 | + }, |
| 134 | + }) |
| 135 | + |
| 136 | + const { |
| 137 | + data: caPolicies = [], |
| 138 | + isFetching: caIsFetching, |
| 139 | + error: caError, |
| 140 | + } = useListConditionalAccessPoliciesQuery({ domain: tenantDomain }) |
| 141 | + |
| 142 | + return ( |
| 143 | + <CippPage title={`Add Backup Schedule`} tenantSelector={false}> |
| 144 | + <> |
| 145 | + <CRow> |
| 146 | + <CCol md={4}> |
| 147 | + <CippButtonCard |
| 148 | + CardButton={ |
| 149 | + <CButton type="submit" form="addTask"> |
| 150 | + Create Backup Schedule |
| 151 | + {postResults.isFetching && ( |
| 152 | + <FontAwesomeIcon icon={faCircleNotch} spin className="ms-2" size="1x" /> |
| 153 | + )} |
| 154 | + </CButton> |
| 155 | + } |
| 156 | + title="Add backup Schedule" |
| 157 | + icon={faEdit} |
| 158 | + > |
| 159 | + <Form |
| 160 | + onSubmit={onSubmit} |
| 161 | + mutators={{ |
| 162 | + ...arrayMutators, |
| 163 | + }} |
| 164 | + render={({ handleSubmit, submitting, values }) => { |
| 165 | + return ( |
| 166 | + <CForm id="addTask" onSubmit={handleSubmit}> |
| 167 | + <p> |
| 168 | + Backups are stored in CIPPs storage and can be restored using the CIPP |
| 169 | + Restore Backup Wizard. Backups run daily or on demand by clicking the backup |
| 170 | + now button. |
| 171 | + </p> |
| 172 | + <CRow className="mb-3"> |
| 173 | + <CCol> |
| 174 | + <label>Tenant</label> |
| 175 | + <Field name="tenantFilter">{(props) => <TenantSelector />}</Field> |
| 176 | + </CCol> |
| 177 | + </CRow> |
| 178 | + <CRow> |
| 179 | + <hr /> |
| 180 | + </CRow> |
| 181 | + <CRow className="mb-3"> |
| 182 | + <CCol> |
| 183 | + <h3 className="underline mb-4">Identity</h3> |
| 184 | + <RFFCFormSwitch name="users" label="User List" /> |
| 185 | + <RFFCFormSwitch name="groups" label="Groups" /> |
| 186 | + <h3 className="underline mb-4">Conditional Access</h3> |
| 187 | + <RFFCFormSwitch name="ca" label="Conditional Access" /> |
| 188 | + <RFFCFormSwitch name="namedlocations" label="Named Locations" /> |
| 189 | + <RFFCFormSwitch name="authstrengths" label="Authentication Strengths" /> |
| 190 | + <h3 className="underline mb-4">Intune</h3> |
| 191 | + <RFFCFormSwitch |
| 192 | + name="intuneconfig" |
| 193 | + label="Intune Configuration Policies" |
| 194 | + /> |
| 195 | + <RFFCFormSwitch |
| 196 | + name="intunecompliance" |
| 197 | + label="Intune Compliance Policies" |
| 198 | + /> |
| 199 | + <RFFCFormSwitch |
| 200 | + name="intuneprotection" |
| 201 | + label="Intune Protection Policies" |
| 202 | + /> |
| 203 | + <h3 className="underline mb-4">CIPP</h3> |
| 204 | + <RFFCFormSwitch name="CippAlerts" label="Alerts Configuration" /> |
| 205 | + <RFFCFormSwitch name="CippStandards" label="Standards Configuration" /> |
| 206 | + </CCol> |
| 207 | + </CRow> |
| 208 | + {postResults.isSuccess && ( |
| 209 | + <CCallout color="success"> |
| 210 | + <li>{postResults.data.Results}</li> |
| 211 | + </CCallout> |
| 212 | + )} |
| 213 | + {getResults.isFetching && ( |
| 214 | + <CCallout color="info"> |
| 215 | + <CSpinner>Loading</CSpinner> |
| 216 | + </CCallout> |
| 217 | + )} |
| 218 | + {getResults.isSuccess && ( |
| 219 | + <CCallout color="info">{getResults.data?.Results}</CCallout> |
| 220 | + )} |
| 221 | + {getResults.isError && ( |
| 222 | + <CCallout color="danger"> |
| 223 | + Could not connect to API: {getResults.error.message} |
| 224 | + </CCallout> |
| 225 | + )} |
| 226 | + </CForm> |
| 227 | + ) |
| 228 | + }} |
| 229 | + /> |
| 230 | + </CippButtonCard> |
| 231 | + </CCol> |
| 232 | + |
| 233 | + <CCol md={8}> |
| 234 | + <CippPageList |
| 235 | + key={refreshState} |
| 236 | + capabilities={{ |
| 237 | + allTenants: true, |
| 238 | + helpContext: 'https://google.com', |
| 239 | + }} |
| 240 | + title="Backup Tasks" |
| 241 | + tenantSelector={false} |
| 242 | + datatable={{ |
| 243 | + tableProps: { |
| 244 | + selectableRows: true, |
| 245 | + actionsList: [ |
| 246 | + { |
| 247 | + label: 'Delete task', |
| 248 | + modal: true, |
| 249 | + modalUrl: `/api/RemoveScheduledItem?&ID=!RowKey`, |
| 250 | + modalMessage: 'Do you want to delete this job?', |
| 251 | + }, |
| 252 | + ], |
| 253 | + }, |
| 254 | + keyField: 'id', |
| 255 | + columns, |
| 256 | + reportName: `Scheduled-Jobs`, |
| 257 | + path: `/api/ListScheduledItems?RefreshGuid=${refreshState}&showHidden=true&Type=New-CIPPBackup`, |
| 258 | + }} |
| 259 | + /> |
| 260 | + </CCol> |
| 261 | + </CRow> |
| 262 | + </> |
| 263 | + </CippPage> |
| 264 | + ) |
| 265 | +} |
| 266 | + |
| 267 | +export default CreateBackup |
0 commit comments