File size: 5,146 Bytes
ae01f49 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | /////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import { makeStyles } from '@mui/styles';
import _ from 'lodash';
import React from 'react';
import { InputCheckbox, InputText } from './FormComponents';
import clsx from 'clsx';
import PropTypes from 'prop-types';
const useStyles = makeStyles(()=>({
/* Display the privs table only when focussed */
root: {
'&:not(:focus-within) .priv-table': {
display: 'none',
}
},
table: {
borderSpacing: 0,
width: '100%',
fontSize: '0.8em',
},
tableCell: {
textAlign: 'left',
}
}));
export default function Privilege({value, onChange, controlProps}) {
// All available privileges in the PostgreSQL database server for
// generating the label for the specific Control
const LABELS = {
'C': 'CREATE',
'T': 'TEMPORARY',
'c': 'CONNECT',
'a': 'INSERT',
'r': 'SELECT',
'w': 'UPDATE',
'd': 'DELETE',
'D': 'TRUNCATE',
'x': 'REFERENCES',
't': 'TRIGGER',
'U': 'USAGE',
'X': 'EXECUTE',
};
let all = false;
let allWithGrant = false;
const classes = useStyles();
let textValue = '';
for(const v of value||[]) {
if(v.privilege) {
textValue += v.privilege_type;
if(v.with_grant) {
textValue += '*';
}
}
}
const checkboxId = _.uniqueId();
/* Calculate the real display value by merging all supported privs with incoming value */
const realVal = (controlProps?.supportedPrivs || []).map((priv)=>{
let inValue = _.find(value, (v)=>v.privilege_type===priv) || {
privilege: false, with_grant: false,
};
return {
privilege_type: priv,
privilege: Boolean(inValue.privilege),
with_grant: Boolean(inValue.with_grant),
};
});
const onCheckAll = (e, forWithGrant)=>{
let newValue = [];
/* Push all the privs or ignore if unchecked and return empty */
realVal.forEach((v)=>{
if(forWithGrant) {
newValue.push({
...v,
privilege: true,
with_grant: e.target.checked,
});
} else if(e.target.checked) {
newValue.push({
...v,
privilege: e.target.checked,
});
}
});
onChange(newValue);
};
const onCheck = (e, forWithGrant)=>{
let exists = false;
let newValue = [];
/* Calculate the newValue by pushing all selected and ignore if unchecked */
(value||[]).forEach((v)=>{
if(v.privilege_type === e.target.name) {
exists = true;
if(forWithGrant) {
newValue.push({
...v,
with_grant: e.target.checked,
});
}
} else {
newValue.push(v);
}
});
if(!exists && e.target.checked) {
newValue.push({
privilege_type: e.target.name,
privilege: e.target.checked,
with_grant: false,
});
}
onChange(newValue);
};
/* If all supported privs and incoming value length matches, clearly all are selected */
all = (realVal.length === (value || []).length);
allWithGrant = (realVal.length === (value || []).length) && (value || []).every((d)=>d.with_grant);
return (
<div className={classes.root}>
<InputText value={textValue} readOnly/>
<table className={clsx(classes.table, 'priv-table')} tabIndex="0">
{(realVal.length > 1) && <thead>
<tr>
<td className={classes.tableCell}>
<InputCheckbox name="all" controlProps={{label: 'ALL'}} id={checkboxId} size="small"
onChange={(e)=>onCheckAll(e, false)} value={all}/>
</td>
<td className={classes.tableCell}>
<InputCheckbox name="all" controlProps={{label: 'WITH GRANT OPTION'}} id={checkboxId} size="small"
disabled={!all} onChange={(e)=>onCheckAll(e, true)} value={allWithGrant}/>
</td>
</tr>
</thead>}
<tbody>
{
realVal.map((d)=>{
return (
<tr key={d.privilege_type}>
<td className={classes.tableCell}>
<InputCheckbox name={d.privilege_type} controlProps={{label: LABELS[d.privilege_type]}}
id={checkboxId} value={Boolean(d.privilege)} size="small"
onChange={(e)=>onCheck(e, false)}/>
</td>
<td className={classes.tableCell}>
<InputCheckbox name={d.privilege_type} controlProps={{label: 'WITH GRANT OPTION'}}
id={checkboxId} value={Boolean(d.with_grant)} size="small" disabled={!d.privilege}
onChange={(e)=>onCheck(e, true)}/>
</td>
</tr>
);
})
}
</tbody>
</table>
</div>
);
}
Privilege.propTypes = {
value: PropTypes.array,
onChange: PropTypes.func,
controlProps: PropTypes.object,
};
|