/** * Gets the user executing this session. * * @return not null */ public User getUser() { return User.fromUsername(userName); }
public String getUser() { return user.asString(); } }
@Test public void withDefaultDomainShouldAppendDefaultDomainWhenNone() { assertThat(User.fromUsername("user") .withDefaultDomain(Domain.LOCALHOST)) .isEqualTo(User.fromLocalPartWithDomain("user", Domain.LOCALHOST)); }
public User withDefaultDomain(Optional<Domain> defaultDomain) { if (hasDomainPart()) { return this; } return new User(localPart, defaultDomain); }
@VisibleForTesting boolean areDomainsDifferent(String user, String otherUser) { Optional<Domain> domain = User.fromUsername(user).getDomainPart(); Optional<Domain> otherDomain = User.fromUsername(otherUser).getDomainPart(); return !domain.equals(otherDomain); }
private User retrieveUsername(MailAddress mailAddress) { try { return User.fromUsername(usersRepository.getUser(mailAddress)); } catch (UsersRepositoryException e) { return User.fromMailAddress(mailAddress); } }
@Override public QuotaRoot getQuotaRoot(MailboxPath mailboxPath) { Preconditions.checkArgument(!mailboxPath.getNamespace().contains(SEPARATOR), "Namespace should not contain " + SEPARATOR); return Optional.ofNullable(mailboxPath.getUser()) .map(user -> { Preconditions.checkArgument(!mailboxPath.getUser().contains(SEPARATOR), "Username should not contain " + SEPARATOR); return User.fromUsername(mailboxPath.getUser()); }) .map(user -> QuotaRoot.quotaRoot(mailboxPath.getNamespace() + SEPARATOR + user.asString(), user.getDomainPart())) .orElseGet(() -> QuotaRoot.quotaRoot(mailboxPath.getNamespace(), Optional.empty())); }
@Override public boolean test(String name, final String password) throws UsersRepositoryException { return Optional.ofNullable(userByName.get(org.apache.james.core.User.fromUsername(name).asString())) .map(user -> user.verifyPassword(password)) .orElse(false); }
@Test public void withDefaultDomainShouldNotAppendDefaultDomainWhenDomainIsPresent() { assertThat(User.fromUsername("user@domain") .withDefaultDomain(Domain.LOCALHOST)) .isEqualTo(User.fromUsername("user@domain")); }
@Test public void withDefaultDomainFromUserShouldPreserveUserWhenAlreadyHasADomain() { assertThat(User.fromUsername("user@domain") .withDefaultDomainFromUser(User.fromUsername("bob@tld"))) .isEqualTo(User.fromUsername("user@domain")); }
protected void isValidUsername(String username) throws UsersRepositoryException { User user = User.fromUsername(username); if (supportVirtualHosting()) { // need a @ in the username if (!user.hasDomainPart()) { throw new UsersRepositoryException("Given Username needs to contain a @domainpart"); } else { Domain domain = user.getDomainPart().get(); try { if (!domainList.containsDomain(domain)) { throw new UsersRepositoryException("Domain does not exist in DomainList"); } } catch (DomainListException e) { throw new UsersRepositoryException("Unable to query DomainList", e); } } } else { // @ only allowed when virtualhosting is supported if (user.hasDomainPart()) { throw new UsersRepositoryException("Given Username contains a @domainpart but virtualhosting support is disabled"); } } }
@Override public QuotaRoot forUser(User user) { return QuotaRoot.quotaRoot(MailboxConstants.USER_NAMESPACE + SEPARATOR + user.asString(), user.getDomainPart()); }
@Test public void fromUsernameShouldParseUsernameWithDomain() { assertThat(User.fromUsername("aa@bb")) .isEqualTo(User.from("aa", Optional.of("bb"))); }
public static User fromLocalPartWithDomain(String localPart, String domain) { Preconditions.checkNotNull(domain); return fromLocalPartWithDomain(localPart, Domain.of(domain)); }
@Override public MailAddress getMailAddressFor(User user) throws UsersRepositoryException { try { if (supportVirtualHosting()) { return new MailAddress(user.asString()); } return new MailAddress(user.getLocalPart(), domainList.getDefaultDomain()); } catch (Exception e) { throw new UsersRepositoryException("Failed to compute mail address associated with the user", e); } } }
private Mappings getMappings(User user, int mappingLimit) throws ErrorMappingException, RecipientRewriteTableException { // We have to much mappings throw ErrorMappingException to avoid // infinity loop if (mappingLimit == 0) { throw new TooManyMappingException("554 Too many mappings to process"); } Mappings targetMappings = mapAddress(user.getLocalPart(), user.getDomainPart().get()); try { return MappingsImpl.fromMappings( targetMappings.asStream() .flatMap(Throwing.function((Mapping target) -> convertAndRecurseMapping(user, target, mappingLimit)).sneakyThrow())); } catch (SkipMappingProcessingException e) { return MappingsImpl.empty(); } }
@Test public void hasDomainPartShouldReturnTrueWhenHasADomain() { assertThat(User.fromLocalPartWithDomain("aa", "domain").hasDomainPart()) .isTrue(); }
@Test public void fromLocalPartWithDomainShouldReturnAValidUser() { assertThat(User.fromLocalPartWithDomain("aa", "bb")) .isEqualTo(User.from("aa", Optional.of("bb"))); }
public static User fromUsername(String username) { Preconditions.checkNotNull(username); Preconditions.checkArgument(!Strings.isNullOrEmpty(username)); List<String> parts = ImmutableList.copyOf(Splitter.on('@').split(username)); switch (parts.size()) { case 1: return fromLocalPartWithoutDomain(username); case 2: return fromLocalPartWithDomain(parts.get(0), parts.get(1)); } throw new IllegalArgumentException("The username should not contain multiple domain delimiter."); }
@Test public void hasDomainPartShouldReturnFalseWhenNoDomain() { assertThat(User.fromLocalPartWithoutDomain("aa").hasDomainPart()) .isFalse(); }