ddd – Domain driven design security

Question:

In a DDD architecture, in which layer is security implemented (access control)? What would the structure look like?

Answer:

Hi,

The security part must be before the creation of domain events. It is not recommended to leave the security part for the interface as you will have code repetition.

Let's use the following use case (user story):

O usuário pode editar seu perfil

We would have the following example Domain Model:

UsuarioService
editarperfil(EditarUsuarioCommand command)
    Usuario usuario = usuarioRepository.getOneById(command.id)
    usuario.alterarNome(command.nome)

Access control should be before calling UsuarioService.editarPerfil()

Alternatives to perform this access control:

  • (IBAC) based on identity list – recommended for when we have the list of users and permissions

     UsuarioService @AccessControlList[listaUsuarios] editarperfil(EditarUsuarioCommand command)
  • (LBAC) recommended for access levels

     @posseses[level=5] userteste UserService @requires(level>=3) editarperfil(EditarUsuarioCommand command)
  • (RBAC) based on roles

     @roles[admin] userTest UsuarioService @requires(role=admin) editarperfil(EditarUsuarioCommand command)

Sources

More about access models here

DDD security discussion here

DDD security question here

Scroll to Top