All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.restclient.models.PermissionsService Maven / Gradle / Ivy

/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.restclient.models;

import com.google.common.collect.Lists;
import javax.inject.Inject;
import org.graylog2.restclient.lib.APIException;
import org.graylog2.restclient.lib.ApiClient;
import org.graylog2.restclient.models.api.responses.ReaderPermissionsResponse;
import org.graylog2.restclient.models.api.responses.RestPermissionsResponse;
import org.graylog2.restroutes.generated.routes;

import java.io.IOException;
import java.util.List;

/**
 * Combines the permissions of both the server and the web-interface.
 *
 * This does not take into account different server versions yet!
 */
public class PermissionsService {

    private final ApiClient api;

    @Inject
    private PermissionsService(ApiClient api) {
        this.api = api;
    }

    public List all() {
        List permissions = Lists.newArrayList();
        try {
            RestPermissionsResponse response = api.path(routes.PermissionsResource().permissions(), RestPermissionsResponse.class).execute();
            for (String group : response.permissions.keySet()) {
                permissions.add(group + ":*");
                for (String action : response.permissions.get(group)) {
                    permissions.add(group + ":" + action);
                }
            }
        } catch (APIException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return permissions;
    }

    public List readerPermissions(String username) {
        List permissions = Lists.newArrayList();

        try {
            final ReaderPermissionsResponse response = api
                    .path(routes.PermissionsResource().readerPermissions(username), ReaderPermissionsResponse.class)
                    .execute();
            permissions.addAll(response.permissions);
        } catch (APIException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return permissions;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy