2020-02-07 19:44:48 +03:00
|
|
|
import React from "react";
|
|
|
|
import {
|
|
|
|
Datagrid,
|
|
|
|
Create,
|
|
|
|
Edit,
|
|
|
|
List,
|
|
|
|
Filter,
|
|
|
|
SimpleForm,
|
|
|
|
BooleanField,
|
|
|
|
BooleanInput,
|
|
|
|
ImageField,
|
|
|
|
PasswordInput,
|
|
|
|
TextField,
|
|
|
|
TextInput,
|
|
|
|
ReferenceField,
|
2020-02-07 18:25:32 +03:00
|
|
|
SaveButton,
|
|
|
|
Toolbar,
|
2020-02-07 19:44:48 +03:00
|
|
|
regex,
|
|
|
|
} from "react-admin";
|
|
|
|
|
|
|
|
const UserFilter = props => (
|
|
|
|
<Filter {...props}>
|
|
|
|
<BooleanInput source="guests" alwaysOn />
|
|
|
|
<BooleanInput
|
|
|
|
label="resources.users.fields.show_deactivated"
|
|
|
|
source="deactivated"
|
|
|
|
alwaysOn
|
|
|
|
/>
|
|
|
|
</Filter>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const UserList = props => (
|
|
|
|
<List
|
|
|
|
{...props}
|
|
|
|
filters={<UserFilter />}
|
|
|
|
filterDefaultValues={{ guests: true, deactivated: false }}
|
|
|
|
bulkActionButtons={false}
|
|
|
|
>
|
|
|
|
<Datagrid rowClick="edit">
|
|
|
|
<ReferenceField
|
|
|
|
source="Avatar"
|
|
|
|
reference="users"
|
|
|
|
link={false}
|
|
|
|
sortable={false}
|
|
|
|
>
|
|
|
|
<ImageField source="avatar_url" title="displayname" />
|
|
|
|
</ReferenceField>
|
|
|
|
<TextField source="id" />
|
|
|
|
{/* Hack since the users endpoint does not give displaynames in the list*/}
|
|
|
|
<ReferenceField
|
|
|
|
source="name"
|
|
|
|
reference="users"
|
|
|
|
link={false}
|
|
|
|
sortable={false}
|
|
|
|
>
|
|
|
|
<TextField source="displayname" />
|
|
|
|
</ReferenceField>
|
|
|
|
<BooleanField source="is_guest" sortable={false} />
|
|
|
|
<BooleanField source="admin" sortable={false} />
|
|
|
|
<BooleanField source="deactivated" sortable={false} />
|
|
|
|
</Datagrid>
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
|
2020-02-04 13:04:56 +03:00
|
|
|
function generateRandomUser() {
|
|
|
|
const homeserver = localStorage.getItem("home_server");
|
|
|
|
const user_id =
|
|
|
|
"@" +
|
|
|
|
Array(8)
|
|
|
|
.fill("0123456789abcdefghijklmnopqrstuvwxyz")
|
|
|
|
.map(
|
|
|
|
x =>
|
|
|
|
x[
|
|
|
|
Math.floor(
|
|
|
|
(crypto.getRandomValues(new Uint32Array(1))[0] /
|
|
|
|
(0xffffffff + 1)) *
|
|
|
|
x.length
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.join("") +
|
|
|
|
":" +
|
|
|
|
homeserver;
|
|
|
|
|
|
|
|
const password = Array(20)
|
|
|
|
.fill(
|
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@-#$"
|
|
|
|
)
|
|
|
|
.map(
|
|
|
|
x =>
|
|
|
|
x[
|
|
|
|
Math.floor(
|
|
|
|
(crypto.getRandomValues(new Uint32Array(1))[0] / (0xffffffff + 1)) *
|
|
|
|
x.length
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.join("");
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: user_id,
|
|
|
|
password: password,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:25:32 +03:00
|
|
|
// redirect to the related Author show page
|
|
|
|
const redirect = (basePath, id, data) => {
|
|
|
|
return {
|
|
|
|
pathname: "/showpdf",
|
|
|
|
state: {
|
|
|
|
id: data.id,
|
|
|
|
displayname: data.displayname,
|
|
|
|
password: data.password,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const UserCreateToolbar = props => (
|
|
|
|
<Toolbar {...props}>
|
|
|
|
<SaveButton
|
|
|
|
label="synapseadmin.action.save_and_show"
|
|
|
|
redirect={redirect}
|
|
|
|
submitOnEnter={true}
|
|
|
|
/>
|
|
|
|
<SaveButton
|
|
|
|
label="synapseadmin.action.save_only"
|
2020-02-10 20:16:16 +03:00
|
|
|
redirect="list"
|
2020-02-07 18:25:32 +03:00
|
|
|
submitOnEnter={false}
|
|
|
|
variant="text"
|
|
|
|
/>
|
|
|
|
</Toolbar>
|
|
|
|
);
|
|
|
|
|
2020-02-07 19:44:48 +03:00
|
|
|
// https://matrix.org/docs/spec/appendices#user-identifiers
|
|
|
|
const validateUser = regex(
|
|
|
|
/^@[a-z0-9._=\-/]+:.*/,
|
|
|
|
"synapseadmin.users.invalid_user_id"
|
|
|
|
);
|
|
|
|
|
|
|
|
export const UserCreate = props => (
|
2020-02-04 13:04:56 +03:00
|
|
|
<Create record={generateRandomUser()} {...props}>
|
2020-02-07 18:25:32 +03:00
|
|
|
<SimpleForm toolbar={<UserCreateToolbar />}>
|
2020-02-07 19:44:48 +03:00
|
|
|
<TextInput source="id" autoComplete="off" validate={validateUser} />
|
|
|
|
<TextInput source="displayname" />
|
|
|
|
<PasswordInput source="password" autoComplete="new-password" />
|
|
|
|
<BooleanInput source="admin" />
|
|
|
|
</SimpleForm>
|
|
|
|
</Create>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const UserEdit = props => (
|
|
|
|
<Edit {...props}>
|
2020-02-07 18:25:32 +03:00
|
|
|
<SimpleForm toolbar={<UserCreateToolbar />}>
|
2020-02-07 19:44:48 +03:00
|
|
|
<TextInput source="id" disabled />
|
|
|
|
<TextInput source="displayname" />
|
|
|
|
<PasswordInput source="password" autoComplete="new-password" />
|
|
|
|
<BooleanInput source="admin" />
|
|
|
|
<BooleanInput source="deactivated" />
|
|
|
|
</SimpleForm>
|
|
|
|
</Edit>
|
|
|
|
);
|