我已经为管理成员创建了一个管理仪表板。作为管理员,我可以登录做任何事情。
我还需要允许这些成员(普通用户)登录,重定向到他们的个人资料页面,并能够编辑他们的属性的子集。
我搜索了文档,但一直无法确定如何最好地完成此操作。
有没有标准的方法,如果有可以做到这一点?
Authorization section of the documentation上提供了管理仪表板上用户权限所需的全部内容。
首先,在验证步骤中,您必须使用set the current user permission(基本上是您的选择字符串,由API给出和/或存储在本地存储中,例如:user
,admin
,user_X
等):
if (type === AUTH_GET_PERMISSIONS) {
const { role, id } = params;
return Promise.resolve(`${role}_${id}`);
}
然后,你可以check this permission inside your resource declaration:
export const UserEdit = ({ ...props }) =>
<Edit title={<UserTitle />} {...props}>
{permissions =>
<TabbedForm defaultValue={{ role: 'user' }}>
<FormTab label="user.form.summary">
<DisabledInput source="id" />
{permissions === `user_${props.record.id}` &&
<TextInput source="name" validate={required} />
}
</FormTab>
</TabbedForm>}
</Edit>;
你也可以restrict an entire resource to administrators:
<Admin
restClient={restClient}
authClient={authClient}
>
{permissions => [
// Restrict access to the remove view to admin only
<Resource
name="customers"
list={UserList}
edit={UserEdit}
remove={permissions.startsWith('admin') ? UserDelete : null}
/>,
// Only include the categories resource for admin users
permissions.startsWith('admin')
? <Resource name="categories" list={CategoryList} edit={CategoryEdit} />
: null,
]}
</Admin>
您也可以尝试使用我的ra-component-factory以非常优雅的方式解决这些问题:https://github.com/zifnab87/ra-component-factory它现在列在admin-on-rest(https://github.com/marmelab/admin-on-rest/blob/master/docs/Ecosystem.md)的贡献中
以下是配置的一般框架 - 它可以帮助您将字段设置为不可见或只读,菜单项可见性,操作按钮可见性,甚至每个角色和每个操作的标签排列。
export default {
props: {
id: {
input: (<TextInput source="id"/>),
field: (<TextField source="id"/>),
},
name: {
input: (<TextInput label="Name" source="name"/>),
field: (<TextField label="Name" source="name"/>),
},
date: {
input: (<DateInput source="date" parse={dateParser} label="Post Date"/>),
field: (<DateField source="date" type="date" label="Post Date"/>),
},
dateGte: { //date Greater than equal
input: (<DateInput source="dateGte" parse={dateParser} label="Date from"/>),
},
dateLte: { // date Less than equal
input: (<DateInput source="dateLte" parse={dateParser} label="Date to"/>),
},
author: {
input: <ReferenceInput label="Author" source="author" reference="authors" allowEmpty>
<SelectInput optionText="name" translate={false}/>
</ReferenceInput>,
field: <ReferenceField label="Author" source="author" reference="authors" sortable={false} linkType={false} allowEmpty={true}>
<ChipField source="name"/>
</ReferenceField>
},
},
role1: {
create: {
props: ["name", "author", "date"],
action: true
},
edit: {
props: ["_id", "name", "author", "date"],
action: true
},
list: {
props: ["id", "name", "author", "date"],
action: true
},
filter: {
props: ["q", "id", "author", "dateGte", "dateLte"],
action: true
},
show: {
props: ["id", "name", "author"],
action: true
},
search: {
action: true
},
delete: {
action: true
},
},
role2: {
create: {
props: [],
action: false
},
edit: {
props: [],
action: false
},
list: {
props: ["id", "name", "author", "date"],
action: false
},
filter: {
props: ["q", "id", "author", "dateGte", "dateLte"],
action: true
},
show: {
props: ["id", "name", "author"],
action: true
},
search: {
action: true
},
delete: {
action: false
},
}
}