use of org.exist.security.internal.aider.UserAider in project exist by eXist-db.
the class SecurityManagerTest method setup.
@BeforeClass
public static void setup() throws EXistException, PermissionDeniedException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
final SecurityManager securityManager = brokerPool.getSecurityManager();
// create the personal group
final Group group = new GroupAider(TEST_GROUP_NAME);
group.setMetadataValue(EXistSchemaType.DESCRIPTION, "Personal group for " + TEST_GROUP_NAME);
try (final DBBroker broker = brokerPool.get(Optional.of(securityManager.getSystemSubject()))) {
securityManager.addGroup(broker, group);
// create the account
final Account user = new UserAider(TEST_USER_NAME);
user.setPassword(TEST_USER_NAME);
user.addGroup(TEST_GROUP_NAME);
securityManager.addAccount(user);
// add the new account as a manager of their personal group
final Group personalGroup = securityManager.getGroup(TEST_GROUP_NAME);
personalGroup.addManager(securityManager.getAccount(TEST_USER_NAME));
securityManager.updateGroup(personalGroup);
}
}
use of org.exist.security.internal.aider.UserAider in project exist by eXist-db.
the class FnCollectionSecurityTest method createUser.
private static void createUser(final SecurityManager securityManager, final DBBroker broker, final String username) throws PermissionDeniedException, EXistException {
final UserAider user = new UserAider(username);
user.setPassword(username);
Group group = new GroupAider(username);
group.setMetadataValue(EXistSchemaType.DESCRIPTION, "Personal group for " + username);
group.addManager(user);
securityManager.addGroup(broker, group);
// add the personal group as the primary group
user.addGroup(username);
securityManager.addAccount(user);
// add the new account as a manager of their personal group
group = securityManager.getGroup(username);
group.addManager(securityManager.getAccount(username));
securityManager.updateGroup(group);
}
use of org.exist.security.internal.aider.UserAider in project exist by eXist-db.
the class AbstractSecurityManagerRoundtripTest method checkGroupMembership.
@Test
public void checkGroupMembership() throws XMLDBException, PermissionDeniedException, EXistException, IOException, DatabaseConfigurationException {
UserManagementService ums = (UserManagementService) getRoot().getService("UserManagementService", "1.0");
final String group1Name = "testGroup1";
final String group2Name = "testGroup2";
final String userName = "testUser";
Group group1 = new GroupAider(group1Name);
Group group2 = new GroupAider(group2Name);
Account user = new UserAider(userName, group1);
try {
ums.addGroup(group1);
ums.addGroup(group2);
ums.addAccount(user);
ums.getAccount(userName);
user.addGroup(group2);
ums.updateAccount(user);
/**
* RESTART THE SERVER **
*/
restartServer();
/**
***********************
*/
ums = (UserManagementService) getRoot().getService("UserManagementService", "1.0");
user = ums.getAccount(userName);
assertNotNull(user);
Group defaultGroup = user.getDefaultGroup();
assertNotNull(defaultGroup);
assertEquals(group1Name, defaultGroup.getName());
String[] groups = user.getGroups();
assertNotNull(groups);
assertEquals(2, groups.length);
assertEquals(group1Name, groups[0]);
assertEquals(group2Name, groups[1]);
} finally {
// cleanup
final Account u1 = ums.getAccount(userName);
if (u1 != null) {
ums.removeAccount(u1);
}
final Group g1 = ums.getGroup(group1Name);
if (g1 != null) {
ums.removeGroup(g1);
}
final Group g2 = ums.getGroup(group2Name);
if (g2 != null) {
ums.removeGroup(g2);
}
}
}
use of org.exist.security.internal.aider.UserAider in project exist by eXist-db.
the class AbstractSecurityManagerRoundtripTest method checkPrimaryGroupStability.
@Test
public void checkPrimaryGroupStability() throws XMLDBException, PermissionDeniedException, EXistException, IOException, DatabaseConfigurationException {
UserManagementService ums = (UserManagementService) getRoot().getService("UserManagementService", "1.0");
final String group1Name = "testGroupA";
final String group2Name = "testGroupB";
final String userName = "testUserA";
Group group1 = new GroupAider(group1Name);
Group group2 = new GroupAider(group2Name);
// set users primary group as group1
Account user = new UserAider(userName, group1);
try {
ums.addGroup(group1);
ums.addGroup(group2);
ums.addAccount(user);
ums.getAccount(userName);
user.addGroup(group2Name);
ums.updateAccount(user);
/**
* RESTART THE SERVER **
*/
restartServer();
/**
***********************
*/
ums = (UserManagementService) getRoot().getService("UserManagementService", "1.0");
user = ums.getAccount(userName);
assertNotNull(user);
Group defaultGroup = user.getDefaultGroup();
assertNotNull(defaultGroup);
assertEquals(group1Name, defaultGroup.getName());
String[] groups = user.getGroups();
assertNotNull(groups);
assertEquals(2, groups.length);
assertEquals(group1Name, groups[0]);
assertEquals(group2Name, groups[1]);
} finally {
// cleanup
final Account u1 = ums.getAccount(userName);
if (u1 != null) {
ums.removeAccount(u1);
}
final Group g1 = ums.getGroup(group1Name);
if (g1 != null) {
ums.removeGroup(g1);
}
final Group g2 = ums.getGroup(group2Name);
if (g2 != null) {
ums.removeGroup(g2);
}
}
}
use of org.exist.security.internal.aider.UserAider in project exist by eXist-db.
the class ActiveDirectoryRealm method authenticate.
/*
* (non-Javadoc)
*
* @see org.exist.security.Realm#authenticate(java.lang.String,
* java.lang.Object)
*/
@Override
public Subject authenticate(final String username, Object credentials) throws AuthenticationException {
String[] returnedAtts = { "sn", "givenName", "mail" };
String searchFilter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
// Create the search controls
SearchControls searchCtls = new SearchControls();
searchCtls.setReturningAttributes(returnedAtts);
// Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
LdapContext ctxGC = null;
boolean ldapUser = false;
try {
ctxGC = ensureContextFactory().getLdapContext(username, String.valueOf(credentials));
// Search objects in GC using filters
NamingEnumeration<SearchResult> answer = ctxGC.search(((ContextFactory) ensureContextFactory()).getSearchBase(), searchFilter, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes attrs = sr.getAttributes();
Map<String, Object> amap = null;
if (attrs != null) {
amap = new HashMap<>();
NamingEnumeration<? extends Attribute> ne = attrs.getAll();
while (ne.hasMore()) {
Attribute attr = ne.next();
amap.put(attr.getID(), attr.get());
ldapUser = true;
}
ne.close();
}
}
} catch (NamingException e) {
e.printStackTrace();
throw new AuthenticationException(AuthenticationException.UNNOWN_EXCEPTION, e.getMessage());
}
if (ldapUser) {
AbstractAccount account = (AbstractAccount) getAccount(username);
if (account == null) {
try (final DBBroker broker = getDatabase().get(Optional.of(getSecurityManager().getSystemSubject()))) {
// perform as SYSTEM user
account = (AbstractAccount) getSecurityManager().addAccount(new UserAider(ID, username));
} catch (Exception e) {
throw new AuthenticationException(AuthenticationException.UNNOWN_EXCEPTION, e.getMessage(), e);
}
}
return new SubjectAccreditedImpl(account, ctxGC);
}
return null;
}
Aggregations