protected function addPermission(RoleInterface $role, $permissionData)
    {
        $em = $this->getDoctrine()->getManager();
        $alreadyContains = false;

        if (isset($permissionData['id'])) {
            $permission = $em->getRepository(static::ENTITY_NAME_PERMISSION)->find($permissionData['id']);
            if (!$permission) {
                throw new EntityNotFoundException(static::ENTITY_NAME_PERMISSION, $permissionData['id']);
            }
            // only add if not already contains
            $alreadyContains = $role->getPermissions()->contains($permission);
        } else {
            $permission = new Permission();
            $permission->setContext($permissionData['context']);
            $permission->setPermissions(
                $this->get('sulu_security.mask_converter')
                    ->convertPermissionsToNumber($permissionData['permissions'])
            );
        }
        if (false === $alreadyContains) {
            $permission->setRole($role);
            $em->persist($permission);
            $role->addPermission($permission);
        }

        return true;
    }