class ExternalUserSetupProcessor extends AbstractExternalUserProcessor {

    protected Set<Role> convertGrantedAuthoritiesToRoles(List<GrantedAuthority> authorities, String tenantId) {
        Set<Role> set = new HashSet<Role>();

        if (authorities == null || authorities.isEmpty())
            return set;

		final UserAuthorityService userAuthorityService = getUserAuthorityService();
        for (GrantedAuthority auth : authorities) {
           	String authorityName = auth.getAuthority();
			if (authorityName == null || !isRoleSynchronizable(authorityName))
				continue;

			Role role = userAuthorityService.newRole(new ExecutionContextImpl());
			String internalRoleName = organizationRoleMap.get(authorityName.toUpperCase());
			if (internalRoleName != null) {
				if (internalRoleName.endsWith(ROLE_SUFFIX)) {
					internalRoleName = internalRoleName.substring(0, internalRoleName.length() - ROLE_SUFFIX.length());
					role.setTenantId(tenantId);
				}
				role.setRoleName(internalRoleName);
				role.setExternallyDefined(false);  //role is mapped to internal
			}
			else {
				String authorityNameValid = purgeRoleNameOfInvalidChars(authorityName);
				if (authorityNameValid.length() == 0) {
					if (Level.WARN.isMoreSpecificThan(logger.getLevel())) {
						ProcessorData processorData = ProcessorData.getInstance();
						UserDetails userDetails = (UserDetails) processorData.getData(EXTERNAL_AUTH_DETAILS);

						logger.warn("External role " + authorityName + " has forbidden characters only " +
								"according to permittedExternalRoleNameRegex: " + permittedExternalRoleNameRegex +
								". Skipping it for user: " +
								(userDetails != null && userDetails.getUsername() != null ? userDetails.getUsername() : ""));
					}

					continue;
				}
				authorityName = authorityNameValid.toUpperCase().trim();

				role.setRoleName(authorityName);
				role.setExternallyDefined(true);
				role.setTenantId(tenantId);
			}

            set.add(role);
        }
        return set;
    }

}