GrantedAuthority cleanRole()

in plugin-ldap/plugin/src/main/groovy/grails/plugin/springsecurity/ldap/userdetails/GrailsLdapAuthoritiesPopulator.groovy [64:110]


	GrantedAuthority cleanRole(GrantedAuthority role) {
		if (!(role instanceof SimpleGrantedAuthority)) {
			return role
		}

		SimpleGrantedAuthority newRole = (SimpleGrantedAuthority) role

		if (roleConvertDashes && newRole.authority.contains('-')) {
			// replace dashes
			newRole = new SimpleGrantedAuthority(newRole.authority.replaceAll('-', '_'))
		}

		if (roleToUpperCase && newRole.authority.toUpperCase() != newRole.authority) {
			// convert to upper case
			newRole = new SimpleGrantedAuthority(newRole.authority.toUpperCase())
		}

		if (roleStripPrefix) {
			// strip prefix if found
			String tempPrefix = rolePrefix + roleStripPrefix
			if (tempPrefix && newRole.authority.startsWith(tempPrefix) && newRole.authority.length() > tempPrefix.length()) {
				// replace dashes
				newRole = new SimpleGrantedAuthority(newRole.authority.replace(tempPrefix, rolePrefix).trim())
			}
		}

		// strip suffix if found
		if (roleStripSuffix && newRole.authority.length() > roleStripSuffix.length() &&
				newRole.authority.endsWith(roleStripSuffix)) {
			int roleLength = newRole.authority.length()
			int suffixLength = roleStripSuffix.length()
			newRole = new SimpleGrantedAuthority(
					newRole.authority.substring(0, roleLength - suffixLength).trim())
		}

		if (newRole.authority.contains(' ')) {
			// replace spaces
			newRole = new SimpleGrantedAuthority(newRole.authority.replaceAll(' ', '_'))
		}

		while (newRole.authority.contains('__')) {
			// replace __
			newRole = new SimpleGrantedAuthority(newRole.authority.replaceAll('__', '_'))
		}

		return newRole
	}