Check permissions for playlists
This commit is contained in:
@@ -1,15 +1,16 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {
|
import {
|
||||||
Datagrid,
|
|
||||||
TextField,
|
|
||||||
BooleanField,
|
BooleanField,
|
||||||
NumberField,
|
Datagrid,
|
||||||
DateField,
|
DateField,
|
||||||
Filter,
|
|
||||||
SearchInput,
|
|
||||||
EditButton,
|
EditButton,
|
||||||
|
Filter,
|
||||||
|
NumberField,
|
||||||
|
SearchInput,
|
||||||
|
TextField,
|
||||||
} from 'react-admin'
|
} from 'react-admin'
|
||||||
import { DurationField, List } from '../common'
|
import { DurationField, List } from '../common'
|
||||||
|
import Writable, { isWritable } from './Writable'
|
||||||
|
|
||||||
const PlaylistFilter = (props) => (
|
const PlaylistFilter = (props) => (
|
||||||
<Filter {...props}>
|
<Filter {...props}>
|
||||||
@@ -19,14 +20,17 @@ const PlaylistFilter = (props) => (
|
|||||||
|
|
||||||
const PlaylistList = (props) => (
|
const PlaylistList = (props) => (
|
||||||
<List {...props} exporter={false} filters={<PlaylistFilter />}>
|
<List {...props} exporter={false} filters={<PlaylistFilter />}>
|
||||||
<Datagrid rowClick="show">
|
<Datagrid rowClick="show" isRowSelectable={(r) => isWritable(r.owner)}>
|
||||||
<TextField source="name" />
|
<TextField source="name" />
|
||||||
<TextField source="owner" />
|
<TextField source="owner" />
|
||||||
<BooleanField source="public" />
|
<BooleanField source="public" />
|
||||||
<NumberField source="songCount" />
|
<NumberField source="songCount" />
|
||||||
<DurationField source="duration" />
|
<DurationField source="duration" />
|
||||||
<DateField source="updatedAt" />
|
<DateField source="updatedAt" />
|
||||||
<EditButton />
|
<Writable>
|
||||||
|
<EditButton />
|
||||||
|
</Writable>
|
||||||
|
/>
|
||||||
</Datagrid>
|
</Datagrid>
|
||||||
</List>
|
</List>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { Title } from '../common'
|
|||||||
import PlaylistSongs from './PlaylistSongs'
|
import PlaylistSongs from './PlaylistSongs'
|
||||||
import PlaylistActions from './PlaylistActions'
|
import PlaylistActions from './PlaylistActions'
|
||||||
import PlaylistSongBulkActions from './PlaylistSongBulkActions'
|
import PlaylistSongBulkActions from './PlaylistSongBulkActions'
|
||||||
|
import { isReadOnly } from './Writable'
|
||||||
|
|
||||||
const PlaylistShow = (props) => {
|
const PlaylistShow = (props) => {
|
||||||
const viewVersion = useSelector((s) => s.admin.ui && s.admin.ui.viewVersion)
|
const viewVersion = useSelector((s) => s.admin.ui && s.admin.ui.viewVersion)
|
||||||
@@ -23,6 +24,7 @@ const PlaylistShow = (props) => {
|
|||||||
<PlaylistSongs
|
<PlaylistSongs
|
||||||
{...props}
|
{...props}
|
||||||
playlistId={props.id}
|
playlistId={props.id}
|
||||||
|
readOnly={isReadOnly(record && record.owner)}
|
||||||
title={<Title subTitle={record && record.name} />}
|
title={<Title subTitle={record && record.name} />}
|
||||||
actions={<PlaylistActions />}
|
actions={<PlaylistActions />}
|
||||||
filter={{ playlist_id: props.id }}
|
filter={{ playlist_id: props.id }}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ const useStyles = makeStyles(
|
|||||||
},
|
},
|
||||||
noResults: { padding: 20 },
|
noResults: { padding: 20 },
|
||||||
row: {
|
row: {
|
||||||
cursor: 'move',
|
cursor: (props) => (props.readOnly ? 'arrow' : 'move'),
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
{ name: 'RaList' }
|
{ name: 'RaList' }
|
||||||
@@ -61,6 +61,13 @@ const useStylesListToolbar = makeStyles({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const ReorderableList = ({ readOnly, children, ...rest }) => {
|
||||||
|
if (readOnly) {
|
||||||
|
return children
|
||||||
|
}
|
||||||
|
return <ReactDragListView {...rest}>{children}</ReactDragListView>
|
||||||
|
}
|
||||||
|
|
||||||
const PlaylistSongs = (props) => {
|
const PlaylistSongs = (props) => {
|
||||||
const classes = useStyles(props)
|
const classes = useStyles(props)
|
||||||
const classesToolbar = useStylesListToolbar(props)
|
const classesToolbar = useStylesListToolbar(props)
|
||||||
@@ -70,7 +77,7 @@ const PlaylistSongs = (props) => {
|
|||||||
const dataProvider = useDataProvider()
|
const dataProvider = useDataProvider()
|
||||||
const refresh = useRefresh()
|
const refresh = useRefresh()
|
||||||
const notify = useNotify()
|
const notify = useNotify()
|
||||||
const { bulkActionButtons, expand, className, playlistId } = props
|
const { bulkActionButtons, expand, className, playlistId, readOnly } = props
|
||||||
const { data, ids, version } = controllerProps
|
const { data, ids, version } = controllerProps
|
||||||
|
|
||||||
const anySong = data[ids[0]]
|
const anySong = data[ids[0]]
|
||||||
@@ -136,7 +143,11 @@ const PlaylistSongs = (props) => {
|
|||||||
size={'small'}
|
size={'small'}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<ReactDragListView onDragEnd={handleDragEnd} nodeSelector={'tr'}>
|
<ReorderableList
|
||||||
|
readOnly={readOnly}
|
||||||
|
onDragEnd={handleDragEnd}
|
||||||
|
nodeSelector={'tr'}
|
||||||
|
>
|
||||||
<SongDatagrid
|
<SongDatagrid
|
||||||
classes={classes}
|
classes={classes}
|
||||||
expand={!isXsmall && <SongDetails />}
|
expand={!isXsmall && <SongDetails />}
|
||||||
@@ -158,7 +169,7 @@ const PlaylistSongs = (props) => {
|
|||||||
showStar={false}
|
showStar={false}
|
||||||
/>
|
/>
|
||||||
</SongDatagrid>
|
</SongDatagrid>
|
||||||
</ReactDragListView>
|
</ReorderableList>
|
||||||
)}
|
)}
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { cloneElement } from 'react'
|
||||||
|
|
||||||
|
export const isWritable = (owner) => {
|
||||||
|
return (
|
||||||
|
localStorage.getItem('username') === owner ||
|
||||||
|
localStorage.getItem('role') === 'admin'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isReadOnly = (owner) => {
|
||||||
|
return !isWritable(owner)
|
||||||
|
}
|
||||||
|
|
||||||
|
const Writable = (props) => {
|
||||||
|
const { record, children } = props
|
||||||
|
if (isWritable(record.owner)) {
|
||||||
|
return cloneElement(children, props)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Writable
|
||||||
Reference in New Issue
Block a user