如何允许会员仅访问和编辑个人资料页面?

时间:2018-01-03 15:52:43

标签: admin-on-rest

我已经为管理成员创建了一个管理信息中心。作为管理员,我可以登录进行所有操作。

我还需要允许这些成员(普通用户)登录,重定向到他们的个人资料页面,并能够编辑其属性的子集。

我搜索了文档,但一直无法确定如何最好地完成此任务。

是否有标准方式,如果有可行的方法?

2 个答案:

答案 0 :(得分:1)

Authorization section of the documentation上提供了管理信息中心用户权限所需的全部内容。

首先,在身份验证步骤中,您必须set the current user permission(基本上是您的选择字符串,由API提供和/或存储在本地存储中,例如:user,{{1 },admin等):

user_X

然后,你可以check this permission inside your resource declaration

if (type === AUTH_GET_PERMISSIONS) {
    const { role, id } = params;
    return Promise.resolve(`${role}_${id}`);
}

您还可以restrict an entire resource to administrators

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>;

答案 1 :(得分:0)

你也可以尝试使用我的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)<的贡献中/ p>

以下是配置的一般框架 - 它可以帮助您将字段设置为不可见或只读,菜单项可见性,操作按钮可见性,甚至每个角色和每个操作的标签排列。

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
        },
    }
}