This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 03cfc8d03c1be11034b0d3a896c85cf1ea40d6f7 Author: RĂ©mi KOWALSKI <rkowal...@linagora.com> AuthorDate: Tue Feb 4 16:02:05 2020 +0100 JAMES-3032 add a class to check if a user can senda a mail from a given address --- .../java/org/apache/james/rrt/api/CanSendFrom.java | 30 +++++++ .../apache/james/rrt/lib/CanSendFromContract.java | 96 ++++++++++++++++++++++ .../org/apache/james/rrt/lib/CanSendFromImpl.java | 63 ++++++++++++++ server/data/data-memory/pom.xml | 5 ++ .../apache/james/rrt/lib/CanSendFromImplTest.java | 72 ++++++++++++++++ 5 files changed, 266 insertions(+) diff --git a/server/data/data-api/src/main/java/org/apache/james/rrt/api/CanSendFrom.java b/server/data/data-api/src/main/java/org/apache/james/rrt/api/CanSendFrom.java new file mode 100644 index 0000000..41e495c --- /dev/null +++ b/server/data/data-api/src/main/java/org/apache/james/rrt/api/CanSendFrom.java @@ -0,0 +1,30 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.rrt.api; + +import org.apache.james.core.Username; + +public interface CanSendFrom { + + /** + * Indicate if the connectedUser can send a mail using the fromUser in the from clause. + */ + boolean userCanSendFrom(Username connectedUser, Username fromUser); + +} diff --git a/server/data/data-api/src/test/java/org/apache/james/rrt/lib/CanSendFromContract.java b/server/data/data-api/src/test/java/org/apache/james/rrt/lib/CanSendFromContract.java new file mode 100644 index 0000000..b806193 --- /dev/null +++ b/server/data/data-api/src/test/java/org/apache/james/rrt/lib/CanSendFromContract.java @@ -0,0 +1,96 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.rrt.lib; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.core.Domain; +import org.apache.james.core.Username; +import org.apache.james.rrt.api.CanSendFrom; +import org.junit.jupiter.api.Test; + +public interface CanSendFromContract { + + Domain DOMAIN = Domain.of("example.com"); + Domain OTHER_DOMAIN = Domain.of("other.org"); + Username USER = Username.of("u...@example.com"); + Username USER_ALIAS = Username.of("al...@example.com"); + Username OTHER_USER = Username.of("ot...@example.com"); + + CanSendFrom canSendFrom(); + + void addAliasMapping(Username alias, Username user) throws Exception; + + void addDomainMapping(Domain alias, Domain domain) throws Exception; + + void addGroupMapping(String group, Username user) throws Exception; + + + @Test + default void assertUserCanSendFromShouldBeFalseWhenSenderIsNotTheUser() { + assertThat(canSendFrom().userCanSendFrom(USER, OTHER_USER)).isFalse(); + } + + @Test + default void assertUserCanSendFromShouldBeTrueWhenSenderIsTheUser() { + assertThat(canSendFrom().userCanSendFrom(USER, USER)).isTrue(); + } + + @Test + default void assertUserCanSendFromShouldBeFalseWhenSenderIsAnAliasOfAnotherUser() throws Exception { + addAliasMapping(USER_ALIAS, OTHER_USER); + + assertThat(canSendFrom().userCanSendFrom(USER, USER_ALIAS)).isFalse(); + } + + @Test + default void assertUserCanSendFromShouldBeTrueWhenSenderIsAnAliasOfTheUser() throws Exception { + addAliasMapping(USER_ALIAS, USER); + + assertThat(canSendFrom().userCanSendFrom(USER, USER_ALIAS)).isTrue(); + } + + @Test + default void assertUserCanSendFromShouldBeTrueWhenSenderIsAnAliasOfAnAliasOfTheUser() throws Exception { + Username userAliasBis = Username.of("aliasbis@" + DOMAIN.asString()); + addAliasMapping(userAliasBis, USER_ALIAS); + addAliasMapping(USER_ALIAS, USER); + + assertThat(canSendFrom().userCanSendFrom(USER, userAliasBis)).isTrue(); + } + + @Test + default void assertUserCanSendFromShouldBeTrueWhenSenderIsAnAliasOfTheDomainUser() throws Exception { + Username fromUser = Username.of(USER.getLocalPart() + "@" + OTHER_DOMAIN.asString()); + + addDomainMapping(OTHER_DOMAIN, DOMAIN); + + assertThat(canSendFrom().userCanSendFrom(USER, fromUser)).isTrue(); + } + + @Test + default void assertUserCanSendFromShouldBeFalseWhenWhenSenderIsAnAliasOfTheUserFromAGroupAlias() throws Exception { + Username fromGroup = Username.of("gr...@example.com"); + + addGroupMapping("gr...@example.com", USER); + + assertThat(canSendFrom().userCanSendFrom(USER, fromGroup)).isFalse(); + + } +} diff --git a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/CanSendFromImpl.java b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/CanSendFromImpl.java new file mode 100644 index 0000000..e82cbe1 --- /dev/null +++ b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/CanSendFromImpl.java @@ -0,0 +1,63 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.rrt.lib; + +import static org.apache.james.rrt.lib.Mapping.Type.Alias; +import static org.apache.james.rrt.lib.Mapping.Type.Domain; + +import java.util.EnumSet; + +import javax.inject.Inject; + +import org.apache.james.core.Username; +import org.apache.james.rrt.api.CanSendFrom; +import org.apache.james.rrt.api.RecipientRewriteTable; +import org.apache.james.rrt.api.RecipientRewriteTableException; +import org.apache.james.util.OptionalUtils; + +public class CanSendFromImpl implements CanSendFrom { + + public static final EnumSet<Mapping.Type> ALIAS_TYPES_ACCEPTED_IN_FROM = EnumSet.of(Alias, Domain); + private final RecipientRewriteTable recipientRewriteTable; + + @Inject + public CanSendFromImpl(RecipientRewriteTable recipientRewriteTable) { + this.recipientRewriteTable = recipientRewriteTable; + } + + @Override + public boolean userCanSendFrom(Username connectedUser, Username fromUser) { + try { + return connectedUser.equals(fromUser) || emailIsAnAliasOfTheConnectedUser(connectedUser, fromUser); + } catch (RecipientRewriteTableException | RecipientRewriteTable.ErrorMappingException e) { + return false; + } + } + + private boolean emailIsAnAliasOfTheConnectedUser(Username connectedUser, Username fromUser) throws RecipientRewriteTable.ErrorMappingException, RecipientRewriteTableException { + return fromUser.getDomainPart().isPresent() + && recipientRewriteTable.getResolvedMappings(fromUser.getLocalPart(), fromUser.getDomainPart().get(), ALIAS_TYPES_ACCEPTED_IN_FROM) + .asStream() + .map(Mapping::asMailAddress) + .flatMap(OptionalUtils::toStream) + .map(Username::fromMailAddress) + .anyMatch(alias -> alias.equals(connectedUser)); + } + +} diff --git a/server/data/data-memory/pom.xml b/server/data/data-memory/pom.xml index f663747..cf8bd37 100644 --- a/server/data/data-memory/pom.xml +++ b/server/data/data-memory/pom.xml @@ -98,5 +98,10 @@ <artifactId>cucumber-picocontainer</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/server/data/data-memory/src/test/java/org/apache/james/rrt/lib/CanSendFromImplTest.java b/server/data/data-memory/src/test/java/org/apache/james/rrt/lib/CanSendFromImplTest.java new file mode 100644 index 0000000..a38467a --- /dev/null +++ b/server/data/data-memory/src/test/java/org/apache/james/rrt/lib/CanSendFromImplTest.java @@ -0,0 +1,72 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.rrt.lib; + +import static org.mockito.Mockito.mock; + +import org.apache.james.core.Domain; +import org.apache.james.core.Username; +import org.apache.james.dnsservice.api.DNSService; +import org.apache.james.domainlist.lib.DomainListConfiguration; +import org.apache.james.domainlist.memory.MemoryDomainList; +import org.apache.james.rrt.api.CanSendFrom; +import org.apache.james.rrt.memory.MemoryRecipientRewriteTable; +import org.junit.jupiter.api.BeforeEach; + +public class CanSendFromImplTest implements CanSendFromContract { + + AbstractRecipientRewriteTable recipientRewriteTable; + CanSendFrom canSendFrom; + + @BeforeEach + void setup() throws Exception { + recipientRewriteTable = new MemoryRecipientRewriteTable(); + + DNSService dnsService = mock(DNSService.class); + MemoryDomainList domainList = new MemoryDomainList(dnsService); + domainList.configure(DomainListConfiguration.builder() + .autoDetect(false) + .autoDetectIp(false)); + domainList.addDomain(DOMAIN); + domainList.addDomain(OTHER_DOMAIN); + recipientRewriteTable.setDomainList(domainList); + + canSendFrom = new CanSendFromImpl(recipientRewriteTable); + } + + @Override + public CanSendFrom canSendFrom() { + return canSendFrom; + } + + @Override + public void addAliasMapping(Username alias, Username user) throws Exception { + recipientRewriteTable.addAliasMapping(MappingSource.fromUser(alias.getLocalPart(), alias.getDomainPart().get()), user.asString()); + } + + @Override + public void addDomainMapping(Domain alias, Domain domain) throws Exception { + recipientRewriteTable.addAliasDomainMapping(MappingSource.fromDomain(alias), domain); + } + + @Override + public void addGroupMapping(String group, Username user) throws Exception { + recipientRewriteTable.addGroupMapping(MappingSource.fromUser(Username.of(group)), user.asString()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org