Search in sources :

Example 1 with Account

use of org.eclipse.che.account.shared.model.Account in project che-server by eclipse-che.

the class ResourceServicePermissionsFilter method filter.

@Override
protected void filter(GenericResourceMethod genericMethodResource, Object[] arguments) throws ApiException {
    String accountId;
    switch(genericMethodResource.getMethod().getName()) {
        case GET_TOTAL_RESOURCES_METHOD:
        case GET_AVAILABLE_RESOURCES_METHOD:
        case GET_USED_RESOURCES_METHOD:
        case GET_RESOURCES_DETAILS_METHOD:
            Subject currentSubject = EnvironmentContext.getCurrent().getSubject();
            if (currentSubject.hasPermission(SystemDomain.DOMAIN_ID, null, SystemDomain.MANAGE_SYSTEM_ACTION)) {
                // user is admin and he is able to see resources of all accounts
                return;
            }
            accountId = ((String) arguments[0]);
            break;
        default:
            throw new ForbiddenException("The user does not have permission to perform this operation");
    }
    final Account account = accountManager.getById(accountId);
    final AccountPermissionsChecker resourcesPermissionsChecker = permissionsCheckers.get(account.getType());
    if (resourcesPermissionsChecker != null) {
        resourcesPermissionsChecker.checkPermissions(accountId, AccountOperation.SEE_RESOURCE_INFORMATION);
    } else {
        throw new ForbiddenException("User is not authorized to perform given operation");
    }
}
Also used : Account(org.eclipse.che.account.shared.model.Account) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) AccountPermissionsChecker(org.eclipse.che.multiuser.api.permission.server.account.AccountPermissionsChecker) Subject(org.eclipse.che.commons.subject.Subject)

Example 2 with Account

use of org.eclipse.che.account.shared.model.Account in project che-server by eclipse-che.

the class ResourceManager method getResourceDetails.

/**
 * Returns detailed information about resources which given account can use.
 *
 * @param accountId account id
 * @return detailed information about resources which can be used by given account
 * @throws NotFoundException when account with specified id was not found
 * @throws ServerException when some exception occurs
 */
public ResourcesDetails getResourceDetails(String accountId) throws NotFoundException, ServerException {
    final List<ProvidedResources> resources = new ArrayList<>();
    for (ResourcesProvider resourcesProvider : resourcesProviders) {
        resources.addAll(resourcesProvider.getResources(accountId));
    }
    final List<Resource> allResources = resources.stream().flatMap(providedResources -> providedResources.getResources().stream()).collect(Collectors.toList());
    return new ResourcesDetailsImpl(accountId, resources, new ArrayList<>(resourceAggregator.aggregateByType(allResources).values()));
}
Also used : ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker) Resource(org.eclipse.che.multiuser.resource.model.Resource) Set(java.util.Set) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) AvailableResourcesProvider(org.eclipse.che.multiuser.resource.api.AvailableResourcesProvider) NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ProvidedResources(org.eclipse.che.multiuser.resource.model.ProvidedResources) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ResourcesDetailsImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourcesDetailsImpl) List(java.util.List) ServerException(org.eclipse.che.api.core.ServerException) ResourcesProvider(org.eclipse.che.multiuser.resource.api.ResourcesProvider) Map(java.util.Map) Optional(java.util.Optional) ResourcesDetails(org.eclipse.che.multiuser.resource.model.ResourcesDetails) AccountManager(org.eclipse.che.account.api.AccountManager) Account(org.eclipse.che.account.shared.model.Account) ResourceAggregator(org.eclipse.che.multiuser.resource.api.ResourceAggregator) AvailableResourcesProvider(org.eclipse.che.multiuser.resource.api.AvailableResourcesProvider) ResourcesProvider(org.eclipse.che.multiuser.resource.api.ResourcesProvider) ProvidedResources(org.eclipse.che.multiuser.resource.model.ProvidedResources) ArrayList(java.util.ArrayList) Resource(org.eclipse.che.multiuser.resource.model.Resource) ResourcesDetailsImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourcesDetailsImpl)

Example 3 with Account

use of org.eclipse.che.account.shared.model.Account in project che-server by eclipse-che.

the class RuntimeResourceUsageTracker method getUsedResource.

@Override
public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException {
    final Account account = accountManager.getById(accountId);
    final long currentlyUsedRuntimes = Pages.stream((maxItems, skipCount) -> workspaceManagerProvider.get().getByNamespace(account.getName(), false, maxItems, skipCount)).filter(ws -> STOPPED != ws.getStatus()).count();
    if (currentlyUsedRuntimes > 0) {
        return Optional.of(new ResourceImpl(RuntimeResourceType.ID, currentlyUsedRuntimes, RuntimeResourceType.UNIT));
    } else {
        return Optional.empty();
    }
}
Also used : WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker) RuntimeResourceType(org.eclipse.che.multiuser.resource.api.type.RuntimeResourceType) Provider(javax.inject.Provider) Resource(org.eclipse.che.multiuser.resource.model.Resource) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Singleton(javax.inject.Singleton) NotFoundException(org.eclipse.che.api.core.NotFoundException) Inject(javax.inject.Inject) ServerException(org.eclipse.che.api.core.ServerException) Optional(java.util.Optional) Pages(org.eclipse.che.api.core.Pages) AccountManager(org.eclipse.che.account.api.AccountManager) Account(org.eclipse.che.account.shared.model.Account) STOPPED(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED) Account(org.eclipse.che.account.shared.model.Account) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)

Example 4 with Account

use of org.eclipse.che.account.shared.model.Account in project che-server by eclipse-che.

the class FreeResourcesProvider method getDefaultResources.

private List<ResourceImpl> getDefaultResources(String accountId) throws NotFoundException, ServerException {
    List<ResourceImpl> defaultResources = new ArrayList<>();
    final Account account = accountManager.getById(accountId);
    final DefaultResourcesProvider defaultResourcesProvider = defaultResourcesProviders.get(account.getType());
    if (defaultResourcesProvider != null) {
        defaultResources.addAll(defaultResourcesProvider.getResources(accountId));
    }
    return defaultResources;
}
Also used : Account(org.eclipse.che.account.shared.model.Account) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) ArrayList(java.util.ArrayList)

Example 5 with Account

use of org.eclipse.che.account.shared.model.Account in project che-server by eclipse-che.

the class JpaWorkerDaoTest method shouldThrowServerExceptionOnExistsWhenRuntimeExceptionOccursInDoGetMethod.

@Test(expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "Database exception")
public void shouldThrowServerExceptionOnExistsWhenRuntimeExceptionOccursInDoGetMethod() throws Exception {
    final Account account = new AccountImpl("accountId", "namespace", "test");
    final WorkspaceImpl workspace = WorkspaceImpl.builder().setId("workspaceId").setAccount(account).build();
    // Persist the account
    manager.getTransaction().begin();
    manager.persist(account);
    manager.getTransaction().commit();
    manager.clear();
    // Persist the workspace
    manager.getTransaction().begin();
    manager.persist(workspace);
    manager.getTransaction().commit();
    manager.clear();
    final UserImpl user = new UserImpl("user0", "user0@com.com", "usr0");
    // Persist the user
    manager.getTransaction().begin();
    manager.persist(user);
    manager.getTransaction().commit();
    manager.clear();
    // Persist the worker
    WorkerImpl worker = new WorkerImpl("workspaceId", "user0", Collections.singletonList(SET_PERMISSIONS));
    manager.getTransaction().begin();
    manager.persist(worker);
    manager.getTransaction().commit();
    manager.clear();
    workerDao.exists("user0", "workspaceId", SET_PERMISSIONS);
}
Also used : Account(org.eclipse.che.account.shared.model.Account) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) AccountImpl(org.eclipse.che.account.spi.AccountImpl) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) WorkerImpl(org.eclipse.che.multiuser.permission.workspace.server.model.impl.WorkerImpl) Test(org.testng.annotations.Test)

Aggregations

Account (org.eclipse.che.account.shared.model.Account)26 ServerException (org.eclipse.che.api.core.ServerException)14 NotFoundException (org.eclipse.che.api.core.NotFoundException)12 Optional (java.util.Optional)10 Inject (javax.inject.Inject)10 Singleton (javax.inject.Singleton)10 AccountManager (org.eclipse.che.account.api.AccountManager)10 Resource (org.eclipse.che.multiuser.resource.model.Resource)10 List (java.util.List)8 WorkspaceManager (org.eclipse.che.api.workspace.server.WorkspaceManager)8 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)8 ResourceUsageTracker (org.eclipse.che.multiuser.resource.api.ResourceUsageTracker)8 ResourceImpl (org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)8 Collectors (java.util.stream.Collectors)6 Provider (javax.inject.Provider)6 Pages (org.eclipse.che.api.core.Pages)6 ArrayList (java.util.ArrayList)4 ConflictException (org.eclipse.che.api.core.ConflictException)4 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)4 STOPPED (org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED)4