CCM NG: GroupManager finished
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3531 8810af33-2d31-482b-a856-94f89814c4dfpull/2/head
parent
7c43e82068
commit
aa0f9df7ee
|
|
@ -19,6 +19,8 @@
|
|||
package org.libreccm.core;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -27,38 +29,108 @@ import javax.enterprise.context.RequestScoped;
|
|||
@RequestScoped
|
||||
public class GroupManager {
|
||||
|
||||
@Inject
|
||||
private transient EntityManager entityManager;
|
||||
|
||||
@Inject
|
||||
private transient GroupRepository groupRepository;
|
||||
|
||||
@Inject
|
||||
private transient UserRepository userRepository;
|
||||
|
||||
/**
|
||||
* Add a user to a group. If the user is already a member of the group, this
|
||||
* method will do nothing.
|
||||
*
|
||||
* @param user The user to add to the group
|
||||
* @param group The group to add the user to.
|
||||
* @param user The user to add to the group. If the value is {@code null}
|
||||
* an {@link IllegalArgumentException} will be thrown.
|
||||
* @param group The group to add the user to. If the value is {@code null}
|
||||
* an {@link IllegalArgumentException} will be thrown.
|
||||
*/
|
||||
public void addUserToGroup(final User user, final Group group) {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't add null as user to a group.");
|
||||
}
|
||||
|
||||
if (group == null) {
|
||||
throw new IllegalArgumentException("Can't add a user to group null");
|
||||
}
|
||||
|
||||
if (isMemberOfGroup(user, group)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final GroupMembership membership = new GroupMembership();
|
||||
membership.setGroup(group);
|
||||
membership.setUser(user);
|
||||
|
||||
group.addMember(membership);
|
||||
user.addGroupMembership(membership);
|
||||
|
||||
entityManager.persist(membership);
|
||||
groupRepository.save(group);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a user from a group. If the user is not member of the group this
|
||||
* method will do nothing.
|
||||
*
|
||||
* @param user The user to remove from the group.
|
||||
* @param group The group to remove the user from.
|
||||
* @param user The user to remove from the group. If the value is
|
||||
* {@code null} an {@link IllegalArgumentException} will be
|
||||
* thrown.
|
||||
* @param group The group to remove the user from. If the value is
|
||||
* {@code null} an {@link IllegalArgumentException} will be
|
||||
* thrown.
|
||||
*/
|
||||
public void removeUserFromGroup(final User user, final Group group) {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't remove user null from a group.");
|
||||
}
|
||||
|
||||
if (group == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't remove a user from group null");
|
||||
}
|
||||
|
||||
GroupMembership membershipToDelete = null;
|
||||
for(final GroupMembership membership : group.getMembers()) {
|
||||
if (membership.getUser().equals(user)) {
|
||||
membershipToDelete = membership;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (membershipToDelete != null) {
|
||||
group.removeMember(membershipToDelete);
|
||||
user.removeGroupMembership(membershipToDelete);
|
||||
|
||||
entityManager.remove(membershipToDelete);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a user is member of a group.
|
||||
*
|
||||
* @param user The user to check
|
||||
* @param group The group to check
|
||||
* @param user The user to check. If the value is {@code null} an
|
||||
* {@link IllegalArgumentException} will be thrown.
|
||||
* @param group The group to check. If the value is {@code null} an
|
||||
* {@link IllegalArgumentException} will be thrown.
|
||||
*
|
||||
* @return {@code true} if the provided user is a member of the provided
|
||||
* group, {@code false} otherwise.
|
||||
*/
|
||||
public boolean isMemberOfGroup(final User user, final Group group) {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Can't check null user");
|
||||
}
|
||||
|
||||
if (group == null) {
|
||||
throw new IllegalArgumentException("Can't check null group");
|
||||
}
|
||||
|
||||
boolean result = false;
|
||||
|
||||
for (final GroupMembership membership : user.getGroupMemberships()) {
|
||||
|
|
|
|||
|
|
@ -47,6 +47,8 @@ public class DatasetsTest extends DatasetsVerifier {
|
|||
"/datasets/org/libreccm/core/CcmObjectRepositoryTest/after-delete.json",
|
||||
"/datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json",
|
||||
"/datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-new.json",
|
||||
"/datasets/org/libreccm/core/GroupManagerTest/after-add-to-group.json",
|
||||
"/datasets/org/libreccm/core/GroupManagerTest/after-remove-from-group.json",
|
||||
"/datasets/org/libreccm/core/GroupManagerTest/users-groups.json",
|
||||
"/datasets/org/libreccm/core/GroupRepositoryTest/data.json",
|
||||
"/datasets/org/libreccm/core/GroupRepositoryTest/after-delete.json",
|
||||
|
|
|
|||
|
|
@ -21,9 +21,11 @@ package org.libreccm.core;
|
|||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.container.test.api.ShouldThrowException;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.junit.InSequence;
|
||||
import org.jboss.arquillian.persistence.PersistenceTest;
|
||||
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
|
||||
import org.jboss.arquillian.persistence.UsingDataSet;
|
||||
import org.jboss.arquillian.transaction.api.annotation.TransactionMode;
|
||||
import org.jboss.arquillian.transaction.api.annotation.Transactional;
|
||||
|
|
@ -60,13 +62,13 @@ public class GroupManagerTest {
|
|||
|
||||
@Inject
|
||||
private transient GroupManager groupManager;
|
||||
|
||||
|
||||
@Inject
|
||||
private transient UserRepository userRepository;
|
||||
|
||||
|
||||
@Inject
|
||||
private transient GroupRepository groupRepository;
|
||||
|
||||
|
||||
public GroupManagerTest() {
|
||||
}
|
||||
|
||||
|
|
@ -122,18 +124,223 @@ public class GroupManagerTest {
|
|||
.addAsWebInfResource("test-web.xml", "WEB-INF/web.xml")
|
||||
.addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Verify the
|
||||
* {@link GroupManager#isMemberOfGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* method.
|
||||
*/
|
||||
@Test
|
||||
@InSequence(10)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/users-groups.json")
|
||||
public void verifyIsMemberOfGroup() {
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
public void isMemberOfGroup() {
|
||||
final User jdoe = userRepository.findByScreenName("jdoe");
|
||||
final Group admins = groupRepository.findByGroupName("admins");
|
||||
final Group users = groupRepository.findByGroupName("users");
|
||||
final Group authors = groupRepository.findByGroupName("authors");
|
||||
|
||||
|
||||
assertThat(groupManager.isMemberOfGroup(jdoe, admins), is(false));
|
||||
assertThat(groupManager.isMemberOfGroup(jdoe, users), is(true));
|
||||
assertThat(groupManager.isMemberOfGroup(jdoe, authors), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that
|
||||
* {@link GroupManager#isMemberOfGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* throws an {@link IllegalArgumentException} if the provided {@code user}
|
||||
* is {@code null}.
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@InSequence(20)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldThrowException(IllegalArgumentException.class)
|
||||
public void isMemberOfGroupNullUser() {
|
||||
final Group admins = groupRepository.findByGroupName("admins");
|
||||
|
||||
groupManager.isMemberOfGroup(null, admins);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that
|
||||
* {@link GroupManager#isMemberOfGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* throws an {@link IllegalArgumentException} if the provided {@code group}
|
||||
* is {@code null}.
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@InSequence(30)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldThrowException(IllegalArgumentException.class)
|
||||
public void isMemberOfGroupNullGroup() {
|
||||
final User jdoe = userRepository.findByScreenName("jdoe");
|
||||
|
||||
groupManager.isMemberOfGroup(jdoe, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the
|
||||
* {@link GroupManager#addUserToGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* method adds an user to a group and stores the changed group and user
|
||||
* correctly to the database.
|
||||
*/
|
||||
@Test
|
||||
@InSequence(40)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldMatchDataSet(value = "datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "after-add-to-group.json",
|
||||
excludeColumns = {"membership_id"})
|
||||
public void addUserToGroup() {
|
||||
final User jdoe = userRepository.findByScreenName("jdoe");
|
||||
final Group admins = groupRepository.findByGroupName("admins");
|
||||
|
||||
groupManager.addUserToGroup(jdoe, admins);
|
||||
|
||||
assertThat(groupManager.isMemberOfGroup(jdoe, admins), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that
|
||||
* {@link GroupManager#addUserToGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* throws an {@link IllegalArgumentException} if the provided {@code user}
|
||||
* is {@code null}.
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@InSequence(50)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldMatchDataSet(value = "datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json",
|
||||
excludeColumns = {"membership_id"})
|
||||
@ShouldThrowException(IllegalArgumentException.class)
|
||||
public void addUserToGroupNullUser() {
|
||||
final Group admins = groupRepository.findByGroupName("admins");
|
||||
|
||||
groupManager.addUserToGroup(null, admins);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that
|
||||
* {@link GroupManager#addUserToGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* throws an {@link IllegalArgumentException} if the provided {@code group}
|
||||
* is {@code null}.
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@InSequence(60)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldMatchDataSet(value = "datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json",
|
||||
excludeColumns = {"membership_id"})
|
||||
@ShouldThrowException(IllegalArgumentException.class)
|
||||
public void addUserToGroupNullGroup() {
|
||||
final User jdoe = userRepository.findByScreenName("jdoe");
|
||||
|
||||
groupManager.addUserToGroup(jdoe, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the
|
||||
* {@link GroupManager#addUserToGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* does nothing if the provided user is already a member of the provided
|
||||
* group.
|
||||
*/
|
||||
@Test
|
||||
@InSequence(70)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldMatchDataSet(value = "datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json",
|
||||
excludeColumns = {"membership_id"})
|
||||
public void addUserToGroupAlreadyMember() {
|
||||
final User jdoe = userRepository.findByScreenName("jdoe");
|
||||
final Group authors = groupRepository.findByGroupName("authors");
|
||||
|
||||
groupManager.addUserToGroup(jdoe, authors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that
|
||||
* {@link GroupManager#removeUserFromGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* really removes a user from a group and stores the changed user and group
|
||||
* in the database.
|
||||
*/
|
||||
@Test
|
||||
@InSequence(80)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldMatchDataSet(value = "datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "after-remove-from-group.json",
|
||||
excludeColumns = {"membership_id"})
|
||||
public void removeUserFromGroup() {
|
||||
final User jdoe = userRepository.findByScreenName("jdoe");
|
||||
final Group authors = groupRepository.findByGroupName("authors");
|
||||
|
||||
groupManager.removeUserFromGroup(jdoe, authors);
|
||||
|
||||
assertThat(groupManager.isMemberOfGroup(jdoe, authors), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that
|
||||
* {@link GroupManager#removeUserFromGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* throws an {@link IllegalArgumentException} if the provided {@code user}
|
||||
* is {@code null}.
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@InSequence(90)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldMatchDataSet(value = "datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json",
|
||||
excludeColumns = {"membership_id"})
|
||||
@ShouldThrowException(IllegalArgumentException.class)
|
||||
public void removeUserFromGroupNullUser() {
|
||||
final Group authors = groupRepository.findByGroupName("authors");
|
||||
|
||||
groupManager.removeUserFromGroup(null, authors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that
|
||||
* {@link GroupManager#removeUserFromGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* throws an {@link IllegalArgumentException} if the provided {@code group}
|
||||
* is {@code null}.
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@InSequence(100)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldMatchDataSet(value = "datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json",
|
||||
excludeColumns = {"membership_id"})
|
||||
@ShouldThrowException(IllegalArgumentException.class)
|
||||
public void removeUserFromGroupNullGroup() {
|
||||
final User jdoe = userRepository.findByScreenName("jdoe");
|
||||
|
||||
groupManager.removeUserFromGroup(jdoe, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that
|
||||
* {@link GroupManager#removeUserFromGroup(org.libreccm.core.User, org.libreccm.core.Group)}
|
||||
* does nothing if the provided {@code user} is not a member of the provided
|
||||
* {@code group}.
|
||||
*/
|
||||
@Test
|
||||
@InSequence(110)
|
||||
@UsingDataSet("datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json")
|
||||
@ShouldMatchDataSet(value = "datasets/org/libreccm/core/GroupManagerTest/"
|
||||
+ "users-groups.json",
|
||||
excludeColumns = {"membership_id"})
|
||||
public void removeUserFromGroupNotMember() {
|
||||
final User jdoe = userRepository.findByScreenName("jdoe");
|
||||
final Group admins = groupRepository.findByGroupName("admins");
|
||||
|
||||
groupManager.removeUserFromGroup(jdoe, admins);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
{
|
||||
"subjects":
|
||||
[
|
||||
{
|
||||
"subject_id": -10
|
||||
},
|
||||
{
|
||||
"subject_id": -20
|
||||
},
|
||||
{
|
||||
"subject_id": -30
|
||||
},
|
||||
{
|
||||
"subject_id": -40
|
||||
}
|
||||
],
|
||||
"ccm_users":
|
||||
[
|
||||
{
|
||||
"banned": false,
|
||||
"hash_algorithm": "SHA-512",
|
||||
"family_name": "Doe",
|
||||
"given_name": "John",
|
||||
"password": "C+o2w6mp+eLrbluMEgKMVSdP50A9BMethXN8R3yihtkbzt7WfWsde2nmq/t5gq6im3J8i3jw4Y3YrKHou8JQ2A==",
|
||||
"password_reset_required": false,
|
||||
"salt": "Fu8FPgqAal4GZp1hDjkOB+t6ITRCcO7HBoN5Xqf29UnVj5NUdUFZRTyKYMBEx6JmZGmHcMDG9OGVCKcEM9oyScSRreJs4B51wM44NM6KeRwbCf+VhBn14DkBrl40ygraNf+AJacKpMyCpFI0O/Am7mMDWL4flskBsylkxaQn3vKfzgN5MVG2szW//I6Q6YEH9AuL8LauS6fKaVynMzzu3xzD8Hjqvvlnzym898eom2lqScPfg5g4e8Ww13HCHAYe6twupAW/BjUNax5HSioEisZN/P1UGrde8uFEj+hbbavrWYZuilPuEu25+/98jyXx6542agqrWN8j0SFYcIyOgA==",
|
||||
"screen_name": "jdoe",
|
||||
"subject_id": -10
|
||||
}
|
||||
],
|
||||
"ccm_groups":
|
||||
[
|
||||
{
|
||||
"name": "admins",
|
||||
"subject_id": -20
|
||||
},
|
||||
{
|
||||
"name": "users",
|
||||
"subject_id": -30
|
||||
},
|
||||
{
|
||||
"name": "authors",
|
||||
"subject_id": -40
|
||||
}
|
||||
],
|
||||
"user_email_addresses":
|
||||
[
|
||||
{
|
||||
"user_id": -10,
|
||||
"email_address": "john.doe@example.com",
|
||||
"bouncing": false,
|
||||
"verified": true
|
||||
}
|
||||
],
|
||||
"group_memberships":
|
||||
[
|
||||
{
|
||||
"membership_id": -10,
|
||||
"group_subject_id": -30,
|
||||
"user_subject_id": -10
|
||||
},
|
||||
{
|
||||
"membership_id": -20,
|
||||
"group_subject_id": -40,
|
||||
"user_subject_id": -10
|
||||
},
|
||||
{
|
||||
"membership_id": -30,
|
||||
"group_subject_id": -20,
|
||||
"user_subject_id": -10
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
{
|
||||
"subjects":
|
||||
[
|
||||
{
|
||||
"subject_id": -10
|
||||
},
|
||||
{
|
||||
"subject_id": -20
|
||||
},
|
||||
{
|
||||
"subject_id": -30
|
||||
},
|
||||
{
|
||||
"subject_id": -40
|
||||
}
|
||||
],
|
||||
"ccm_users":
|
||||
[
|
||||
{
|
||||
"banned": false,
|
||||
"hash_algorithm": "SHA-512",
|
||||
"family_name": "Doe",
|
||||
"given_name": "John",
|
||||
"password": "C+o2w6mp+eLrbluMEgKMVSdP50A9BMethXN8R3yihtkbzt7WfWsde2nmq/t5gq6im3J8i3jw4Y3YrKHou8JQ2A==",
|
||||
"password_reset_required": false,
|
||||
"salt": "Fu8FPgqAal4GZp1hDjkOB+t6ITRCcO7HBoN5Xqf29UnVj5NUdUFZRTyKYMBEx6JmZGmHcMDG9OGVCKcEM9oyScSRreJs4B51wM44NM6KeRwbCf+VhBn14DkBrl40ygraNf+AJacKpMyCpFI0O/Am7mMDWL4flskBsylkxaQn3vKfzgN5MVG2szW//I6Q6YEH9AuL8LauS6fKaVynMzzu3xzD8Hjqvvlnzym898eom2lqScPfg5g4e8Ww13HCHAYe6twupAW/BjUNax5HSioEisZN/P1UGrde8uFEj+hbbavrWYZuilPuEu25+/98jyXx6542agqrWN8j0SFYcIyOgA==",
|
||||
"screen_name": "jdoe",
|
||||
"subject_id": -10
|
||||
}
|
||||
],
|
||||
"ccm_groups":
|
||||
[
|
||||
{
|
||||
"name": "admins",
|
||||
"subject_id": -20
|
||||
},
|
||||
{
|
||||
"name": "users",
|
||||
"subject_id": -30
|
||||
},
|
||||
{
|
||||
"name": "authors",
|
||||
"subject_id": -40
|
||||
}
|
||||
],
|
||||
"user_email_addresses":
|
||||
[
|
||||
{
|
||||
"user_id": -10,
|
||||
"email_address": "john.doe@example.com",
|
||||
"bouncing": false,
|
||||
"verified": true
|
||||
}
|
||||
],
|
||||
"group_memberships":
|
||||
[
|
||||
{
|
||||
"membership_id": -10,
|
||||
"group_subject_id": -30,
|
||||
"user_subject_id": -10
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Reference in New Issue