use of com.enonic.xp.security.IdProvider in project xp by enonic.
the class SecurityServiceImplTest method testUpdateIdProvider.
@Test
public void testUpdateIdProvider() throws Exception {
runAsAdmin(() -> {
// setup
final PrincipalKey userKey = PrincipalKey.ofUser(SYSTEM, "User1");
final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "Group-a");
final PrincipalKey groupKey2 = PrincipalKey.ofGroup(SYSTEM, "group-b");
final IdProviderAccessControlList permissions = IdProviderAccessControlList.of(IdProviderAccessControlEntry.create().principal(userKey).access(CREATE_USERS).build(), IdProviderAccessControlEntry.create().principal(groupKey1).access(ADMINISTRATOR).build(), IdProviderAccessControlEntry.create().principal(groupKey2).access(WRITE_USERS).build());
final CreateIdProviderParams createIdProvider = CreateIdProviderParams.create().key(IdProviderKey.from("enonic")).displayName("Enonic Id Provider").permissions(permissions).description("old id provider description").build();
final IdProvider idProviderCreated = securityService.createIdProvider(createIdProvider);
// exercise
final IdProviderAccessControlList updatePermissions = IdProviderAccessControlList.of(IdProviderAccessControlEntry.create().principal(userKey).access(CREATE_USERS).build(), IdProviderAccessControlEntry.create().principal(groupKey1).access(ADMINISTRATOR).build());
final UpdateIdProviderParams updateIdProvider = UpdateIdProviderParams.create().key(IdProviderKey.from("enonic")).displayName("Enonic Id Provider updated").permissions(updatePermissions).description("new id provider description").build();
final IdProvider idProviderUpdated = securityService.updateIdProvider(updateIdProvider);
// verify
assertNotNull(idProviderUpdated);
assertEquals("enonic", idProviderUpdated.getKey().toString());
assertEquals("Enonic Id Provider updated", idProviderUpdated.getDisplayName());
assertEquals("new id provider description", idProviderUpdated.getDescription());
final IdProviderAccessControlList updatedPermissions = securityService.getIdProviderPermissions(IdProviderKey.from("enonic"));
assertNotNull(idProviderCreated);
assertEquals(CREATE_USERS, updatedPermissions.getEntry(userKey).getAccess());
assertEquals(ADMINISTRATOR, updatedPermissions.getEntry(groupKey1).getAccess());
assertNull(updatedPermissions.getEntry(groupKey2));
});
}
use of com.enonic.xp.security.IdProvider in project xp by enonic.
the class SecurityServiceImplTest method testUpdateIdProviderWithEditor.
@Test
public void testUpdateIdProviderWithEditor() throws Exception {
runAsAdmin(() -> {
// setup
final CreateIdProviderParams createIdProvider = CreateIdProviderParams.create().key(IdProviderKey.from("enonic")).displayName("Enonic Id Provider").description("old id provider description").build();
final IdProvider idProviderCreated = securityService.createIdProvider(createIdProvider);
// exercise
final UpdateIdProviderParams updateIdProvider = UpdateIdProviderParams.create(idProviderCreated).editor(edit -> {
edit.key = IdProviderKey.from("newEnonic");
edit.displayName = "Enonic Id Provider updated";
edit.description = "new id provider description";
}).displayName("Display name from parameters").description("Description from parameters").build();
final IdProvider idProviderUpdated = securityService.updateIdProvider(updateIdProvider);
// verify
assertNotNull(idProviderUpdated);
assertEquals("enonic", idProviderUpdated.getKey().toString());
assertEquals("Enonic Id Provider updated", idProviderUpdated.getDisplayName());
assertEquals("new id provider description", idProviderUpdated.getDescription());
});
}
use of com.enonic.xp.security.IdProvider in project xp by enonic.
the class SecurityServiceImpl method updateIdProvider.
@Override
public IdProvider updateIdProvider(final UpdateIdProviderParams updateIdProviderParams) {
return callWithContext(() -> {
final NodePath idProviderNodePath = IdProviderNodeTranslator.toIdProviderNodePath(updateIdProviderParams.getKey());
final Node node = this.nodeService.getByPath(idProviderNodePath);
if (node == null) {
return null;
}
final IdProvider existingIdProvider = IdProviderNodeTranslator.fromNode(node);
final IdProvider idProviderToUpdate = updateIdProviderParams.update(existingIdProvider);
final UpdateNodeParams updateNodeParams = IdProviderNodeTranslator.toUpdateNodeParams(idProviderToUpdate, node.id());
final Node idProviderNode = nodeService.update(updateNodeParams);
if (updateIdProviderParams.getIdProviderPermissions() != null) {
final Node usersNode = nodeService.getByPath(IdProviderNodeTranslator.toIdProviderUsersNodePath(updateIdProviderParams.getKey()));
final Node groupsNode = nodeService.getByPath(IdProviderNodeTranslator.toIdProviderGroupsNodePath(updateIdProviderParams.getKey()));
final IdProviderAccessControlList permissions = updateIdProviderParams.getIdProviderPermissions();
AccessControlList idProviderNodePermissions = IdProviderNodeTranslator.idProviderPermissionsToIdProviderNodePermissions(permissions);
AccessControlList usersNodePermissions = IdProviderNodeTranslator.idProviderPermissionsToUsersNodePermissions(permissions);
AccessControlList groupsNodePermissions = IdProviderNodeTranslator.idProviderPermissionsToGroupsNodePermissions(permissions);
final Node rootNode = nodeService.getRoot();
idProviderNodePermissions = mergeWithRootPermissions(idProviderNodePermissions, rootNode.getPermissions());
usersNodePermissions = mergeWithRootPermissions(usersNodePermissions, rootNode.getPermissions());
groupsNodePermissions = mergeWithRootPermissions(groupsNodePermissions, rootNode.getPermissions());
setNodePermissions(idProviderNode.id(), idProviderNodePermissions);
setNodePermissions(usersNode.id(), usersNodePermissions);
setNodePermissions(groupsNode.id(), groupsNodePermissions);
final ApplyNodePermissionsParams applyPermissions = ApplyNodePermissionsParams.create().nodeId(idProviderNode.id()).overwriteChildPermissions(false).build();
nodeService.applyPermissions(applyPermissions);
}
this.nodeService.refresh(RefreshMode.SEARCH);
return IdProviderNodeTranslator.fromNode(idProviderNode);
});
}
use of com.enonic.xp.security.IdProvider in project xp by enonic.
the class SecurityServiceImpl method authenticate.
@Override
public AuthenticationInfo authenticate(final AuthenticationToken token) {
if (!(token instanceof VerifiedUsernameAuthToken) && !(token instanceof VerifiedEmailAuthToken)) {
addRandomDelay();
}
if (isSuAuthenticationEnabled(token)) {
return authenticateSu((UsernamePasswordAuthToken) token);
}
if (token.getIdProvider() != null) {
return doAuthenticate(token);
} else {
final IdProviders idProviders = callAsAuthenticated(this::getIdProviders);
for (IdProvider idProvider : idProviders) {
token.setIdProvider(idProvider.getKey());
final AuthenticationInfo authInfo = doAuthenticate(token);
if (authInfo.isAuthenticated()) {
return authInfo;
}
}
return AuthenticationInfo.unAuthenticated();
}
}
use of com.enonic.xp.security.IdProvider in project xp by enonic.
the class IdProviderNodeTranslator method createIdProviderFromNode.
private static IdProvider createIdProviderFromNode(final Node node) {
if (node.name().toString().equalsIgnoreCase(PrincipalKey.ROLES_NODE_NAME)) {
return null;
}
final PropertySet nodeAsSet = node.data().getRoot();
final IdProviderKey idProviderKey = IdProviderNodeTranslator.toKey(node);
final IdProvider.Builder idProvider = IdProvider.create().displayName(nodeAsSet.getString(IdProviderPropertyNames.DISPLAY_NAME_KEY)).key(idProviderKey).description(nodeAsSet.getString(IdProviderPropertyNames.DESCRIPTION_KEY));
if (nodeAsSet.hasProperty(IdProviderPropertyNames.APPLICATION)) {
final String applicationKey = nodeAsSet.getString(IdProviderPropertyNames.ID_PROVIDER_APPLICATION_KEY);
final PropertySet config = nodeAsSet.getSet(IdProviderPropertyNames.ID_PROVIDER_CONFIG_FORM_KEY);
final IdProviderConfig idProviderConfig = IdProviderConfig.create().applicationKey(ApplicationKey.from(applicationKey)).config(config.toTree()).build();
idProvider.idProviderConfig(idProviderConfig);
} else if (IdProviderKey.system().equals(idProviderKey)) {
// TODO Remove after next dump upgrade
final IdProviderConfig idProviderConfig = IdProviderConfig.create().applicationKey(SYSTEM_ID_PROVIDER_KEY).build();
idProvider.idProviderConfig(idProviderConfig);
}
return idProvider.build();
}
Aggregations