-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#7 Permissions #9
Merged
+280
−13
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
05b51be
Permissions
70b19e4
Parse incoming HTML strings for permission titles with Interweave.
7351d07
Store the permissions of each role
dawehner 7663ba1
Move the admin role to the right
dawehner ef5701e
Cleanup Permissions. Peace-Wave loading initial work.
9cbe922
Updated Peace-Wave loading component.
406d2ab
Fixes -
e6126cb
Fixes initial state setup. Adjusts naming of Table exports.
2513f1e
Use human readable provider_label for permissions grouping label
tedbow 107e1fa
Fix filesystem case issue.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,160 @@ | ||
import React from 'react'; | ||
import { css } from 'emotion'; | ||
import React, { Component, Fragment } from 'react'; | ||
import makeCancelable from 'makecancelable'; | ||
import { Markup } from 'interweave'; | ||
|
||
const styles = { | ||
title: css` | ||
text-decoration: underline; | ||
`, | ||
}; | ||
import Loading from '../../Helpers/Loading'; | ||
import { Table, TBody, THead } from '../../UI/table'; | ||
|
||
const Permissions = () => ( | ||
<div> | ||
<h1 className={styles.title}>Permissions</h1> | ||
<p>This will be the permissions page.</p> | ||
</div> | ||
); | ||
const Permissions = class Permissions extends Component { | ||
state = { | ||
loaded: false, | ||
rawPermissions: [], | ||
renderablePermissions: [], | ||
}; | ||
componentDidMount() { | ||
this.cancelFetch = makeCancelable( | ||
Promise.all([ | ||
fetch( | ||
`${ | ||
process.env.REACT_APP_DRUPAL_BASE_URL | ||
}/admin-api/permissions?_format=json`, | ||
).then(res => res.json()), | ||
fetch( | ||
`${ | ||
process.env.REACT_APP_DRUPAL_BASE_URL | ||
}/jsonapi/user_role/user_role`, | ||
{ headers: { Accept: 'application/vnd.api+json' } }, | ||
).then(res => res.json()), | ||
]) | ||
.then(([permissions, { data: roles }]) => | ||
this.setState({ | ||
rawPermissions: permissions, | ||
renderablePermissions: permissions, | ||
changedRoles: [], | ||
// Move admin roles to the right. | ||
roles: roles.sort((a, b) => { | ||
if (a.attributes.is_admin && b.attributes.is_admin) { | ||
return a.attributes.id - b.attributes.id; | ||
} else if (a.attributes.is_admin) { | ||
return 1; | ||
} else if (b.attributes.is_admin) { | ||
return -1; | ||
} | ||
return a.attributes.id - b.attributes.id; | ||
}), | ||
loaded: true, | ||
}), | ||
) | ||
.catch(err => this.setState({ loaded: false, err })), | ||
); | ||
} | ||
componentWillUnmount() { | ||
this.cancelFetch(); | ||
} | ||
onPermissionCheck = (roleName, permission) => { | ||
this.setState(prevState => ({ | ||
changedRoles: [...new Set(prevState.changedRoles).add(roleName).values()], | ||
roles: this.togglePermission(permission, roleName, prevState.roles), | ||
})); | ||
}; | ||
togglePermission = (permission, roleName, roles) => { | ||
const roleIndex = roles.map(role => role.attributes.id).indexOf(roleName); | ||
const role = roles[roleIndex]; | ||
const index = role.attributes.permissions.indexOf(permission); | ||
if (index !== -1) { | ||
role.attributes.permissions.splice(index, 1); | ||
} else { | ||
role.attributes.permissions.push(permission); | ||
} | ||
roles[roleIndex] = role; | ||
return roles; | ||
}; | ||
groupPermissions = permissions => | ||
Object.entries( | ||
permissions.reduce((acc, cur) => { | ||
acc[cur.provider] = acc[cur.provider] || { | ||
providerLabel: cur.provider_label, | ||
permissions: [], | ||
}; | ||
acc[cur.provider].permissions.push(cur); | ||
return acc; | ||
}, {}), | ||
); | ||
createTableRows = (groupedPermissions, roles) => | ||
[].concat( | ||
...groupedPermissions.map( | ||
([providerMachineName, { providerLabel, permissions }]) => [ | ||
{ | ||
key: `permissionGroup-${providerMachineName}`, | ||
colspan: roles.length + 1, | ||
tds: [[`td-${providerMachineName}`, <b>{providerLabel}</b>]], | ||
}, | ||
...permissions.map(permission => ({ | ||
key: `permissionGroup-${providerMachineName}-${permission.title}`, | ||
tds: [ | ||
[ | ||
`td-${providerMachineName}-${permission.title}`, | ||
<Markup content={permission.title} />, | ||
], | ||
...roles.map(({ attributes }, index) => [ | ||
`td-${providerMachineName}-${permission.title}-${index}-cb`, | ||
attributes.is_admin && attributes.id === 'administrator' ? ( | ||
<input type="checkbox" checked /> | ||
) : ( | ||
<input | ||
type="checkbox" | ||
onChange={() => | ||
this.onPermissionCheck(attributes.id, permission.id) | ||
} | ||
checked={attributes.permissions.includes(permission.id)} | ||
/> | ||
), | ||
]), | ||
], | ||
})), | ||
], | ||
), | ||
); | ||
handleKeyPress = event => { | ||
const input = event.target.value.toLowerCase(); | ||
this.setState(prevState => ({ | ||
...prevState, | ||
renderablePermissions: prevState.rawPermissions.filter( | ||
({ title, description, provider, provider_label: providerLabel }) => | ||
`${title}${description}${provider}${providerLabel}`.includes(input), | ||
), | ||
})); | ||
}; | ||
render() { | ||
return !this.state.loaded ? ( | ||
<Loading /> | ||
) : ( | ||
<Fragment> | ||
<input | ||
type="text" | ||
placeholder="Filter by name, description or module" | ||
onChange={this.handleKeyPress} | ||
onKeyDown={this.handleKeyPress} | ||
/> | ||
<Table zebra> | ||
<THead | ||
data={[ | ||
'PERMISSION', | ||
...this.state.roles.map(({ attributes: { label } }) => | ||
label.toUpperCase(), | ||
), | ||
]} | ||
/> | ||
<TBody | ||
rows={this.createTableRows( | ||
this.groupPermissions(this.state.renderablePermissions), | ||
this.state.roles, | ||
)} | ||
/> | ||
</Table> | ||
</Fragment> | ||
); | ||
} | ||
}; | ||
|
||
export default Permissions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import { css, keyframes } from 'emotion'; | ||
|
||
const rotate = keyframes` | ||
from { | ||
transform: rotate(-10deg); | ||
} | ||
to { | ||
transform: rotate(10deg); | ||
} | ||
`; | ||
|
||
const styles = { | ||
wrap: css` | ||
margin: 100px auto 0; | ||
`, | ||
peace: css` | ||
display: inline-block; | ||
vertical-align: middle; | ||
animation-direction: alternate; | ||
animation-iteration-count: infinite; | ||
animation-duration: 0.5s; | ||
animation-timing-function: cubic-bezier(0, 0, 1, 1); | ||
transform-origin: bottom; | ||
font-size: 50px; | ||
animation-name: ${rotate}; | ||
`, | ||
}; | ||
|
||
const Loading = () => ( | ||
<div className={styles.wrap}> | ||
<span className={styles.peace} role="img" aria-label="Peace Sign"> | ||
✌️ | ||
</span> | ||
</div> | ||
); | ||
|
||
export default Loading; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react'; | ||
import { css } from 'emotion'; | ||
import { | ||
node, | ||
bool, | ||
oneOfType, | ||
arrayOf, | ||
string, | ||
shape, | ||
number, | ||
} from 'prop-types'; | ||
|
||
const TABLE = ({ children, zebra, ...props }) => { | ||
const styles = css` | ||
${zebra ? 'tbody tr:nth-child(odd) {background-color: #e8e8e8;}' : ''}; | ||
`; | ||
return ( | ||
<table className={styles} {...props}> | ||
{children} | ||
</table> | ||
); | ||
}; | ||
TABLE.propTypes = { | ||
children: oneOfType([arrayOf(node), node]).isRequired, | ||
zebra: bool, | ||
}; | ||
TABLE.defaultProps = { | ||
zebra: false, | ||
}; | ||
|
||
const TR = ({ children, ...props }) => <tr {...props}>{children}</tr>; | ||
TR.propTypes = { | ||
children: oneOfType([arrayOf(node), node]).isRequired, | ||
}; | ||
|
||
const TD = ({ children, ...props }) => <td {...props}>{children}</td>; | ||
TD.propTypes = { | ||
children: oneOfType([arrayOf(node), node]).isRequired, | ||
}; | ||
|
||
const THEAD = ({ data }) => ( | ||
<thead> | ||
<TR>{data.map(label => <TD key={`column-${label}`}>{label}</TD>)}</TR> | ||
</thead> | ||
); | ||
THEAD.propTypes = { | ||
data: arrayOf(string).isRequired, | ||
}; | ||
|
||
const TBODY = ({ rows }) => ( | ||
<tbody> | ||
{rows.map(({ colspan, tds, key }) => ( | ||
<TR key={key}> | ||
{tds.map(([tdKey, tdValue]) => ( | ||
<TD key={tdKey} colSpan={colspan || undefined}> | ||
{tdValue} | ||
</TD> | ||
))} | ||
</TR> | ||
))} | ||
</tbody> | ||
); | ||
TBODY.propTypes = { | ||
rows: arrayOf( | ||
shape({ | ||
colspan: number, | ||
key: string, | ||
tds: arrayOf(node).isRequired, | ||
}), | ||
).isRequired, | ||
}; | ||
|
||
export { TR, TD, TABLE as Table, TBODY as TBody, THEAD as THead }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3700,6 +3700,13 @@ interpret@^1.0.0: | |
version "1.1.0" | ||
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614" | ||
|
||
interweave@^8.0.2: | ||
version "8.0.2" | ||
resolved "https://registry.yarnpkg.com/interweave/-/interweave-8.0.2.tgz#51001199c58d311f8b1d0100c4f6b9d494e97480" | ||
dependencies: | ||
babel-runtime "^6.26.0" | ||
prop-types "^15.6.0" | ||
|
||
invariant@^2.2.0, invariant@^2.2.1, invariant@^2.2.2: | ||
version "2.2.3" | ||
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.3.tgz#1a827dfde7dcbd7c323f0ca826be8fa7c5e9d688" | ||
|
@@ -4647,6 +4654,10 @@ make-dir@^1.0.0: | |
dependencies: | ||
pify "^3.0.0" | ||
|
||
makecancelable@^1.0.0: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/makecancelable/-/makecancelable-1.0.0.tgz#c7e2606e59db7a4bf8098ff5b52d7f13173499e5" | ||
|
||
[email protected]: | ||
version "1.0.11" | ||
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to add the
Accept: application/vnd.api+json
header. This will enable 404/403 errors in JSON format (if any). Otherwise they'll come back as HTML errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍