use of org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal in project jackrabbit by apache.
the class ACLEditor method getPrincipal.
/**
* Returns the principal for the given path or null.
*
* @param pathToACNode
* @return
* @throws RepositoryException
*/
private Principal getPrincipal(final String pathToACNode) throws RepositoryException {
final String id = getPathName(pathToACNode);
UserManager uMgr = session.getUserManager();
Authorizable authorizable = uMgr.getAuthorizable(id);
if (authorizable == null) {
// use workaround to retrieve the principal
if (pathToACNode.startsWith(acRootPath)) {
final String principalPath = pathToACNode.substring(acRootPath.length());
if (principalPath.indexOf('/', 1) > 0) {
// safe to build an item based principal
authorizable = uMgr.getAuthorizable(new ItemBasedPrincipal() {
public String getPath() throws RepositoryException {
return principalPath;
}
public String getName() {
return Text.getName(principalPath);
}
});
} else {
// see getPathToAcNode above -> try to retrieve principal by name.
return session.getPrincipalManager().getPrincipal(Text.getName(principalPath));
}
}
// else: path doesn't start with acRootPath -> return null.
}
return (authorizable == null) ? null : authorizable.getPrincipal();
}
use of org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal in project jackrabbit by apache.
the class TokenProvider method getUserId.
static String getUserId(NodeImpl tokenNode, UserManager userManager) throws RepositoryException {
if (tokenNode != null) {
final NodeImpl userNode = (NodeImpl) tokenNode.getParent().getParent();
final String principalName = userNode.getProperty(UserImpl.P_PRINCIPAL_NAME).getString();
if (userNode.isNodeType(UserImpl.NT_REP_USER)) {
Authorizable a = userManager.getAuthorizable(new ItemBasedPrincipal() {
public String getPath() throws RepositoryException {
return userNode.getPath();
}
public String getName() {
return principalName;
}
});
if (a != null && !a.isGroup() && !((User) a).isDisabled()) {
return a.getID();
}
} else {
throw new RepositoryException("Failed to calculate userId from token credentials");
}
}
return null;
}
use of org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal in project jackrabbit by apache.
the class TokenProvider method getTokenParent.
private NodeImpl getTokenParent(User user) throws RepositoryException {
NodeImpl tokenParent = null;
String parentPath = null;
try {
if (user != null) {
Principal pr = user.getPrincipal();
if (pr instanceof ItemBasedPrincipal) {
String userPath = ((ItemBasedPrincipal) pr).getPath();
NodeImpl userNode = (NodeImpl) session.getNode(userPath);
if (userNode.hasNode(TOKENS_NODE_NAME)) {
tokenParent = (NodeImpl) userNode.getNode(TOKENS_NODE_NAME);
} else {
tokenParent = userNode.addNode(session.getQName(TOKENS_NODE_NAME), TOKENS_NT_NAME, NodeId.randomId());
parentPath = userPath + '/' + TOKENS_NODE_NAME;
session.save();
}
}
} else {
log.debug("Cannot create login token: No user specified. (null)");
}
} catch (RepositoryException e) {
// conflict while creating token store for this user -> refresh and
// try to get the tree from the updated root.
log.debug("Conflict while creating token store -> retrying", e);
session.refresh(false);
if (parentPath != null && session.nodeExists(parentPath)) {
tokenParent = (NodeImpl) session.getNode(parentPath);
}
}
return tokenParent;
}
use of org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal in project jackrabbit by apache.
the class TokenBasedLoginTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
if (superuser instanceof JackrabbitSession) {
UserManager umgr = ((JackrabbitSession) superuser).getUserManager();
String uid = "test";
while (umgr.getAuthorizable(uid) != null) {
uid += "_";
}
testuser = umgr.createUser(uid, uid);
Principal p = testuser.getPrincipal();
if (p instanceof ItemBasedPrincipal) {
testuserPath = ((ItemBasedPrincipal) p).getPath();
} else {
throw new NotExecutableException();
}
creds = new SimpleCredentials(uid, uid.toCharArray());
if (!umgr.isAutoSave()) {
doSave = true;
superuser.save();
}
} else {
throw new NotExecutableException();
}
}
use of org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal in project jackrabbit by apache.
the class GroupImplTest method testEveryoneGroup.
public void testEveryoneGroup() throws RepositoryException, NotExecutableException {
Group g = null;
try {
g = userMgr.createGroup(EveryonePrincipal.NAME);
save(superuser);
assertEquals(EveryonePrincipal.NAME, g.getPrincipal().getName());
assertEquals(EveryonePrincipal.getInstance(), g.getPrincipal());
assertTrue(g.isDeclaredMember(getTestUser(superuser)));
assertTrue(g.isMember(getTestUser(superuser)));
Iterator<Authorizable> it = g.getDeclaredMembers();
assertTrue(it.hasNext());
Set<Authorizable> members = new HashSet<Authorizable>();
while (it.hasNext()) {
members.add(it.next());
}
it = g.getMembers();
assertTrue(it.hasNext());
while (it.hasNext()) {
assertTrue(members.contains(it.next()));
}
assertFalse(g.addMember(getTestUser(superuser)));
assertFalse(g.removeMember(getTestUser(superuser)));
PrincipalManager pMgr = ((JackrabbitSession) superuser).getPrincipalManager();
Principal everyone = pMgr.getEveryone();
assertTrue(everyone instanceof ItemBasedPrincipal);
assertEquals(everyone, EveryonePrincipal.getInstance());
} finally {
if (g != null) {
g.remove();
save(superuser);
}
}
}
Aggregations