Files
directus/docs/extensions/services/working-with-users.md
2023-12-19 18:16:17 +01:00

3.7 KiB

description, contributors
description contributors
Learn about the required services when working with users and granting them access control. Esther Agbaje

Working with Users

There are various services related to users:

  • UsersService: Used to manage user accounts and perform CRUD operations on user profiles.
  • RolesServices: Used to assign roles to users.
  • PermissionsServices: Used to manage the permissions associated with each role.

All three of these services extend the ItemsService and are used in a very similar way.

export default defineEndpoint((router, context) => {
  const { services, getSchema } = context;
  const { UsersService, RolesService, PermissionsService } = services;

  router.get('/', async (req, res) => {
    const schema = await getSchema();
    const usersService = new UsersService({ schema, accountability: req.accountability });
    const rolesService = new RolesService({ schema, accountability: req.accountability });
    const permissionsService = new PermissionsService({ schema, accountability: req.accountability });

    // Your route handler logic
  });
});

Get a User

router.get('/', async (req, res) => {
  const usersService = new UsersService({
    schema: await getSchema(),
    accountability: req.accountability
  });

  const data = await usersService.getUserByEmail('email');

  res.json(data);
});

Create a Role

router.post('/', async (req, res) => {
  const rolesService = new RolesService({
    schema: await getSchema(),
    accountability: req.accountability
  });

  const data = await rolesService.createOne({
    name: 'Interns',
    icon: 'verified_user',
    description: null,
    admin_access: false,
    app_access: true,
  });

  res.json(data);
});

Create a User and Assign a Role

router.post('/', async (req, res) => {
  const rolesService = new RolesService({
    schema: await getSchema(),
    accountability: req.accountability
  });

  const roles = await rolesService.readByQuery({
    fields: ['*'],
  });

  const foundRole = roles.find((item) => item.name == 'role');

  const data = await usersService.createOne({
    icon: 'attractions',
    role: foundRole.id,
  });

  res.json(data);
});

::: warning Creating a User

A role is required when creating a user.

:::

Update a User

router.patch('/', async (req, res) => {
  const usersService = new UsersService({
    schema: await getSchema(),
    accountability: req.accountability
  });

  const data = await usersService.updateOne('user_id', {
    title: 'CTO'
  });

  res.json(data);
});

Delete a User

router.delete('/', async (req, res) => {
  const usersService = new UsersService({
    schema: await getSchema(),
    accountability: req.accountability
  });

  const data = await usersService.deleteOne('user_id');

  res.json(data);
});

Assign Permission to a Role

router.post('/', async (req, res) => {
  const schema = await getSchema();
  const rolesService = new RolesService({ schema,	accountability: req.accountability });
  const permissionsService = new PermissionsService({ schema, accountability: req.accountability });

  const roles = await rolesService.readByQuery({
	  fields: ['*'],
	  filter: {
		  name: {
			  _eq: 'role'
		  }
	  }
  });

  const foundRole = roles[0];

  const data = await permissionsService.createOne({
	  collection: 'pages',
	  action: 'read',
	  role: 'c86c2761-65d3-43c3-897f-6f74ad6a5bd7',
	  fields: ['id', 'title'],
	  role: foundRole.id,
  });

  res.json(data);
});

::: tip Explore Services In-Depth

Refer to the full list of methods in our codebase.

:::