All files / src/resources users.tsx

100% Statements 31/31
87.5% Branches 7/8
100% Functions 10/10
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345                                                                                                                    3x         3x         3x 5x   5x                     3x   3x             3x 3x                   3x 3x                                                   3x 3x   3x 2x 2x   2x                           3x 1x                                             3x 2x 2x   2x                   3x 2x 2x   2x                                                                                                                                                                                                                   2x                                                                       3x           4x        
import AssignmentIndIcon from "@mui/icons-material/AssignmentInd";
import ContactMailIcon from "@mui/icons-material/ContactMail";
import DevicesIcon from "@mui/icons-material/Devices";
import GetAppIcon from "@mui/icons-material/GetApp";
import UserIcon from "@mui/icons-material/Group";
import NotificationsIcon from "@mui/icons-material/Notifications";
import PermMediaIcon from "@mui/icons-material/PermMedia";
import PersonPinIcon from "@mui/icons-material/PersonPin";
import SettingsInputComponentIcon from "@mui/icons-material/SettingsInputComponent";
import ViewListIcon from "@mui/icons-material/ViewList";
import {
  ArrayField,
  ArrayInput,
  BooleanField,
  BooleanInput,
  BulkDeleteButton,
  Button,
  Create,
  CreateButton,
  CreateProps,
  DataTable,
  DateField,
  DeleteButton,
  Edit,
  EditProps,
  ExportButton,
  FormTab,
  List,
  ListProps,
  NumberField,
  Pagination,
  PasswordInput,
  ReferenceField,
  ReferenceManyField,
  ResourceProps,
  SearchInput,
  SelectInput,
  SimpleForm,
  SimpleFormIterator,
  TabbedForm,
  TextField,
  TextInput,
  TopToolbar,
  maxLength,
  regex,
  required,
  useCreatePath,
  useListContext,
  useRecordContext,
  useTranslate,
} from "react-admin";
 
import AvatarField from "../components/AvatarField";
import { ServerNoticeBulkButton, ServerNoticeButton } from "../components/ServerNotices";
import { DATE_FORMAT } from "../components/date";
import { DeviceRemoveButton } from "../components/devices";
import { MediaIDField, ProtectMediaButton, QuarantineMediaButton } from "../components/media";
 
const choices_medium = [
  { id: "email", name: "resources.users.email" },
  { id: "msisdn", name: "resources.users.msisdn" },
];
 
const choices_type = [
  { id: "bot", name: "bot" },
  { id: "support", name: "support" },
];
 
const UserListActions = () => {
  const { isPending, total } = useListContext();
 
  return (
    <TopToolbar>
      <CreateButton />
      <ExportButton disabled={isPending || total === 0} maxResults={10000} />
      <Button to="/import_users" label="CSV Import">
        <GetAppIcon sx={{ transform: "rotate(180deg)", fontSize: "20px" }} />
      </Button>
    </TopToolbar>
  );
};
 
const UserPagination = () => <Pagination rowsPerPageOptions={[10, 25, 50, 100, 500, 1000]} />;
 
const userFilters = [
  <SearchInput source="name" alwaysOn />,
  <BooleanInput source="guests" alwaysOn />,
  <BooleanInput label="resources.users.fields.show_deactivated" source="deactivated" alwaysOn />,
  <BooleanInput label="resources.users.fields.show_locked" source="locked" alwaysOn />,
];
 
const UserBulkActionButtons = () => (
  <>
    <ServerNoticeBulkButton />
    <BulkDeleteButton
      label="resources.users.action.erase"
      confirmTitle="resources.users.helper.erase"
      mutationMode="pessimistic"
    />
  </>
);
 
export const UserList = (props: ListProps) => (
  <List
    {...props}
    filters={userFilters}
    filterDefaultValues={{ guests: true, deactivated: false, locked: false }}
    sort={{ field: "name", order: "ASC" }}
    actions={<UserListActions />}
    pagination={<UserPagination />}
  >
    <DataTable bulkActionButtons={<UserBulkActionButtons />} rowClick="edit">
      <DataTable.Col source="avatar_src" label="resources.users.fields.avatar">
        <AvatarField source="avatar_src" sx={{ height: "40px", width: "40px" }} />
      </DataTable.Col>
      <DataTable.Col source="id" />
      <DataTable.Col source="displayname" />
      <DataTable.Col source="is_guest" field={BooleanField} />
      <DataTable.Col source="admin" field={BooleanField} />
      <DataTable.Col source="deactivated" field={BooleanField} />
      <DataTable.Col source="locked" field={BooleanField} />
      <DataTable.Col source="erased" field={BooleanField} />
      <DataTable.Col source="creation_ts" label="resources.users.fields.creation_ts_ms">
        <DateField source="creation_ts" showTime options={DATE_FORMAT} />
      </DataTable.Col>
    </DataTable>
  </List>
);
 
const validateUser = [required(), maxLength(253), regex(/^[a-z0-9._=\-/]+$/, "synapseadmin.users.invalid_user_id")];
const validateAddress = [required(), maxLength(255)];
 
const UserEditActions = () => {
  const record = useRecordContext();
  const translate = useTranslate();
 
  return (
    <TopToolbar>
      {!record?.deactivated && <ServerNoticeButton />}
      <DeleteButton
        label="resources.users.action.erase"
        confirmTitle={translate("resources.users.helper.erase", {
          smart_count: 1,
        })}
        mutationMode="pessimistic"
      />
    </TopToolbar>
  );
};
 
export const UserCreate = (props: CreateProps) => (
  <Create {...props} redirect="edit">
    <SimpleForm>
      <TextInput source="id" autoComplete="off" validate={validateUser} />
      <TextInput source="displayname" validate={maxLength(256)} />
      <PasswordInput source="password" autoComplete="new-password" validate={maxLength(512)} />
      <SelectInput source="user_type" choices={choices_type} translateChoice={false} resettable />
      <BooleanInput source="admin" />
      <ArrayInput source="threepids">
        <SimpleFormIterator disableReordering>
          <SelectInput source="medium" choices={choices_medium} validate={required()} />
          <TextInput source="address" validate={validateAddress} />
        </SimpleFormIterator>
      </ArrayInput>
      <ArrayInput source="external_ids" label="synapseadmin.users.tabs.sso">
        <SimpleFormIterator disableReordering>
          <TextInput source="auth_provider" validate={required()} />
          <TextInput source="external_id" label="resources.users.fields.id" validate={required()} />
        </SimpleFormIterator>
      </ArrayInput>
    </SimpleForm>
  </Create>
);
 
const UserTitle = () => {
  const record = useRecordContext();
  const translate = useTranslate();
 
  return (
    <span>
      {translate("resources.users.name", {
        smart_count: 1,
      })}{" "}
      {record ? `"${record.displayname}"` : ""}
    </span>
  );
};
 
export const UserEdit = (props: EditProps) => {
  const translate = useTranslate();
  const createPath = useCreatePath();
 
  return (
    <Edit {...props} title={<UserTitle />} actions={<UserEditActions />}>
      <TabbedForm>
        <FormTab label={translate("resources.users.name", { smart_count: 1 })} icon={<PersonPinIcon />}>
          <AvatarField source="avatar_src" sortable={false} sx={{ height: "120px", width: "120px", float: "right" }} />
          <TextInput source="id" disabled />
          <TextInput source="displayname" />
          <PasswordInput source="password" autoComplete="new-password" helperText="resources.users.helper.password" />
          <SelectInput source="user_type" choices={choices_type} translateChoice={false} resettable />
          <BooleanInput source="admin" />
          <BooleanInput source="locked" />
          <BooleanInput source="deactivated" helperText="resources.users.helper.deactivate" />
          <BooleanInput source="erased" disabled />
          <DateField source="creation_ts_ms" showTime options={DATE_FORMAT} />
          <TextField source="consent_version" />
        </FormTab>
 
        <FormTab label="resources.users.threepid" icon={<ContactMailIcon />} path="threepid">
          <ArrayInput source="threepids">
            <SimpleFormIterator disableReordering>
              <SelectInput source="medium" choices={choices_medium} />
              <TextInput source="address" />
            </SimpleFormIterator>
          </ArrayInput>
        </FormTab>
 
        <FormTab label="synapseadmin.users.tabs.sso" icon={<AssignmentIndIcon />} path="sso">
          <ArrayInput source="external_ids" label={false}>
            <SimpleFormIterator disableReordering>
              <TextInput source="auth_provider" validate={required()} />
              <TextInput source="external_id" label="resources.users.fields.id" validate={required()} />
            </SimpleFormIterator>
          </ArrayInput>
        </FormTab>
 
        <FormTab label={translate("resources.devices.name", { smart_count: 2 })} icon={<DevicesIcon />} path="devices">
          <ReferenceManyField reference="devices" target="user_id" label={false}>
            <DataTable>
              <DataTable.Col source="device_id" />
              <DataTable.Col source="display_name" />
              <DataTable.Col source="last_seen_ip" />
              <DataTable.Col source="last_seen_ts">
                <DateField source="last_seen_ts" showTime options={DATE_FORMAT} />
              </DataTable.Col>
              <DataTable.Col>
                <DeviceRemoveButton />
              </DataTable.Col>
            </DataTable>
          </ReferenceManyField>
        </FormTab>
 
        <FormTab label="resources.connections.name" icon={<SettingsInputComponentIcon />} path="connections">
          <ReferenceField reference="connections" source="id" label={false} link={false}>
            <ArrayField source="devices[].sessions[0].connections" label="resources.connections.name">
              <DataTable bulkActionButtons={false}>
                <DataTable.Col source="ip" />
                <DataTable.Col source="last_seen">
                  <DateField source="last_seen" showTime options={DATE_FORMAT} />
                </DataTable.Col>
                <DataTable.Col source="user_agent" />
              </DataTable>
            </ArrayField>
          </ReferenceField>
        </FormTab>
 
        <FormTab
          label={translate("resources.users_media.name", { smart_count: 2 })}
          icon={<PermMediaIcon />}
          path="media"
        >
          <ReferenceManyField
            reference="users_media"
            target="user_id"
            label={false}
            pagination={<UserPagination />}
            perPage={50}
            sort={{ field: "created_ts", order: "DESC" }}
          >
            <DataTable>
              <DataTable.Col source="media_id" field={MediaIDField} />
              <DataTable.Col source="created_ts">
                <DateField source="created_ts" showTime options={DATE_FORMAT} />
              </DataTable.Col>
              <DataTable.Col source="last_access_ts">
                <DateField source="last_access_ts" showTime options={DATE_FORMAT} />
              </DataTable.Col>
              <DataTable.Col source="media_length" field={NumberField} />
              <DataTable.Col source="media_type" />
              <DataTable.Col source="upload_name" />
              <DataTable.Col source="quarantined_by" />
              <DataTable.Col label="resources.quarantine_media.action.name">
                <QuarantineMediaButton label="resources.quarantine_media.action.name" />
              </DataTable.Col>
              <DataTable.Col label="resources.users_media.fields.safe_from_quarantine">
                <ProtectMediaButton label="resources.users_media.fields.safe_from_quarantine" />
              </DataTable.Col>
              <DataTable.Col>
                <DeleteButton mutationMode="pessimistic" redirect={false} />
              </DataTable.Col>
            </DataTable>
          </ReferenceManyField>
        </FormTab>
 
        <FormTab label={translate("resources.rooms.name", { smart_count: 2 })} icon={<ViewListIcon />} path="rooms">
          <ReferenceManyField reference="joined_rooms" target="user_id" label={false}>
            <DataTable
              rowClick={id => createPath({ resource: "rooms", id, type: "show" })}
              bulkActionButtons={false}
            >
              <DataTable.Col source="id" label="resources.rooms.fields.room_id" />
              <DataTable.Col label="resources.rooms.fields.name">
                <ReferenceField source="id" reference="rooms" link={false}>
                  <TextField source="name" />
                </ReferenceField>
              </DataTable.Col>
            </DataTable>
          </ReferenceManyField>
        </FormTab>
 
        <FormTab
          label={translate("resources.pushers.name", { smart_count: 2 })}
          icon={<NotificationsIcon />}
          path="pushers"
        >
          <ReferenceManyField reference="pushers" target="user_id" label={false}>
            <DataTable bulkActionButtons={false}>
              <DataTable.Col source="kind" />
              <DataTable.Col source="app_display_name" />
              <DataTable.Col source="app_id" />
              <DataTable.Col source="data.url" />
              <DataTable.Col source="device_display_name" />
              <DataTable.Col source="lang" />
              <DataTable.Col source="profile_tag" />
              <DataTable.Col source="pushkey" />
            </DataTable>
          </ReferenceManyField>
        </FormTab>
      </TabbedForm>
    </Edit>
  );
};
 
const resource = {
  name: "users",
  icon: UserIcon,
  list: UserList,
  edit: UserEdit,
  create: UserCreate,
  recordRepresentation: (record: { displayname?: string; id: string }) => record.displayname || record.id,
} satisfies ResourceProps;
 
export default resource;