Search in sources :

Example 1 with ServiceUserMapper

use of org.apache.sling.serviceusermapping.ServiceUserMapper in project sling by apache.

the class AbstractSlingRepository2 method loginService.

// service login
/**
     * Actual implementation of the {@link #loginService(String, String)} method
     * taking into account the bundle calling this method.
     * <p/>
     * This method uses the
     * {@link AbstractSlingRepositoryManager#getServiceUserMapper()
     * ServiceUserMapper} service to map the named service to a user and then
     * calls the {@link #createServiceSession(String, String)} method actually
     * create a session for that user.
     *
     * @param subServiceName An optional subService identifier (may be
     *            {@code null})
     * @param workspace The workspace to access or {@code null} to access the
     *            {@link #getDefaultWorkspace() default workspace}
     * @return A session authenticated with the service user
     * @throws LoginException if the service name cannot be derived or if
     *             logging is as the user to which the service name maps is not
     *             allowed
     * @throws RepositoryException If a general error occurs while creating the
     *             session
     */
@Override
public final Session loginService(final String subServiceName, final String workspace) throws LoginException, RepositoryException {
    final ServiceUserMapper serviceUserMapper = this.getSlingRepositoryManager().getServiceUserMapper();
    final String userName = (serviceUserMapper != null) ? serviceUserMapper.getServiceUserID(this.usingBundle, subServiceName) : null;
    if (userName == null) {
        throw new LoginException("Cannot derive user name for bundle " + usingBundle + " and sub service " + subServiceName);
    }
    return createServiceSession(userName, workspace);
}
Also used : ServiceUserMapper(org.apache.sling.serviceusermapping.ServiceUserMapper) LoginException(javax.jcr.LoginException)

Example 2 with ServiceUserMapper

use of org.apache.sling.serviceusermapping.ServiceUserMapper in project sling by apache.

the class AbstractSlingRepository2 method impersonateFromService.

/**
     * Default implementation of the {@link #impersonateFromService(Credentials, String, String)}
     * method taking into account the bundle calling this method.
     * <p/>
     * This method uses the
     * {@link AbstractSlingRepositoryManager#getServiceUserMapper()
     * ServiceUserMapper} service to map the named service to a user and then
     * calls the {@link #createServiceSession(String, String)} method actually
     * create a session for that user. This service session is then impersonated
     * to the subject identified by the specified {@code credentials}.
     *
     * @param subServiceName An optional subService identifier (may be {@code null})
     * @param credentials    A valid non-null {@code Credentials} object
     * @param workspaceName  The workspace to access or {@code null} to access the
     *            {@link #getDefaultWorkspace() default workspace}
     * @return a new {@code Session} object
     * @throws LoginException If the current session does not have sufficient access to perform the operation.
     * @throws RepositoryException If another error occurs.
     * @since 2.4
     */
@Override
public Session impersonateFromService(final String subServiceName, final Credentials credentials, final String workspaceName) throws LoginException, RepositoryException {
    final ServiceUserMapper serviceUserMapper = this.getSlingRepositoryManager().getServiceUserMapper();
    final String userName = (serviceUserMapper != null) ? serviceUserMapper.getServiceUserID(this.usingBundle, subServiceName) : null;
    if (userName == null) {
        throw new LoginException("Cannot derive user name for bundle " + usingBundle + " and sub service " + subServiceName);
    }
    Session serviceSession = null;
    try {
        serviceSession = createServiceSession(userName, workspaceName);
        return serviceSession.impersonate(credentials);
    } finally {
        if (serviceSession != null) {
            serviceSession.logout();
        }
    }
}
Also used : ServiceUserMapper(org.apache.sling.serviceusermapping.ServiceUserMapper) LoginException(javax.jcr.LoginException) Session(javax.jcr.Session)

Aggregations

LoginException (javax.jcr.LoginException)2 ServiceUserMapper (org.apache.sling.serviceusermapping.ServiceUserMapper)2 Session (javax.jcr.Session)1