ddd – Domain driven design security


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



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:

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

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)


More about access models here

DDD security discussion here

DDD security question here

Scroll to Top