Search in sources :

Example 1 with AuthorizeException

use of org.dspace.authorize.AuthorizeException in project DSpace by DSpace.

the class EPersonCLITool method cmdAdd.

/**
 * Command to create an EPerson.
 */
private static int cmdAdd(Context context, String[] argv) throws AuthorizeException, SQLException {
    Options options = new Options();
    options.addOption(VERB_ADD);
    final OptionGroup identityOptions = new OptionGroup();
    identityOptions.addOption(OPT_EMAIL);
    identityOptions.addOption(OPT_NETID);
    options.addOptionGroup(identityOptions);
    options.addOption(OPT_GIVENNAME);
    options.addOption(OPT_SURNAME);
    options.addOption(OPT_PHONE);
    options.addOption(OPT_LANGUAGE);
    options.addOption(OPT_REQUIRE_CERTIFICATE);
    Option option = new Option("p", "password", true, "password to match the EPerson name");
    options.addOption(option);
    options.addOption("h", "help", false, "explain --add options");
    // Rescan the command for more details.
    CommandLineParser parser = new DefaultParser();
    CommandLine command;
    try {
        command = parser.parse(options, argv);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        return 1;
    }
    if (command.hasOption('h')) {
        new HelpFormatter().printHelp("user --add [options]", options);
        return 0;
    }
    // Check that we got sufficient credentials to define a user.
    if ((!command.hasOption(OPT_EMAIL.getOpt())) && (!command.hasOption(OPT_NETID.getOpt()))) {
        System.err.println("You must provide an email address or a netid to identify the new user.");
        return 1;
    }
    if (!command.hasOption('p')) {
        System.err.println("You must provide a password for the new user.");
        return 1;
    }
    // Create!
    EPerson eperson = null;
    try {
        eperson = ePersonService.create(context);
    } catch (SQLException | AuthorizeException ex) {
        context.abort();
        System.err.println(ex.getMessage());
        return 1;
    }
    eperson.setCanLogIn(true);
    eperson.setSelfRegistered(false);
    eperson.setEmail(command.getOptionValue(OPT_EMAIL.getOpt()));
    eperson.setFirstName(context, command.getOptionValue(OPT_GIVENNAME.getOpt()));
    eperson.setLastName(context, command.getOptionValue(OPT_SURNAME.getOpt()));
    eperson.setLanguage(context, command.getOptionValue(OPT_LANGUAGE.getOpt(), Locale.getDefault().getLanguage()));
    ePersonService.setMetadataSingleValue(context, eperson, MD_PHONE, command.getOptionValue(OPT_PHONE.getOpt()), null);
    eperson.setNetid(command.getOptionValue(OPT_NETID.getOpt()));
    ePersonService.setPassword(eperson, command.getOptionValue('p'));
    if (command.hasOption(OPT_REQUIRE_CERTIFICATE.getOpt())) {
        eperson.setRequireCertificate(Boolean.valueOf(command.getOptionValue(OPT_REQUIRE_CERTIFICATE.getOpt())));
    } else {
        eperson.setRequireCertificate(false);
    }
    try {
        ePersonService.update(context, eperson);
        System.out.printf("Created EPerson %s\n", eperson.getID().toString());
    } catch (SQLException | AuthorizeException ex) {
        context.abort();
        System.err.println(ex.getMessage());
        return 1;
    }
    return 0;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) OptionGroup(org.apache.commons.cli.OptionGroup) SQLException(java.sql.SQLException) AuthorizeException(org.dspace.authorize.AuthorizeException) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 2 with AuthorizeException

use of org.dspace.authorize.AuthorizeException in project DSpace by DSpace.

the class EPersonCLITool method cmdDelete.

/**
 * Command to delete an EPerson.
 */
private static int cmdDelete(Context context, String[] argv) {
    Options options = new Options();
    options.addOption(VERB_DELETE);
    final OptionGroup identityOptions = new OptionGroup();
    identityOptions.addOption(OPT_EMAIL);
    identityOptions.addOption(OPT_NETID);
    options.addOptionGroup(identityOptions);
    options.addOption("h", "help", false, "explain --delete options");
    CommandLineParser parser = new DefaultParser();
    CommandLine command;
    try {
        command = parser.parse(options, argv);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        return 1;
    }
    if (command.hasOption('h')) {
        new HelpFormatter().printHelp("user --delete [options]", options);
        return 0;
    }
    // Delete!
    EPerson eperson = null;
    try {
        if (command.hasOption(OPT_NETID.getOpt())) {
            eperson = ePersonService.findByNetid(context, command.getOptionValue(OPT_NETID.getOpt()));
        } else if (command.hasOption(OPT_EMAIL.getOpt())) {
            eperson = ePersonService.findByEmail(context, command.getOptionValue(OPT_EMAIL.getOpt()));
        } else {
            System.err.println("You must specify the user's email address or netid.");
            return 1;
        }
    } catch (SQLException e) {
        System.err.append(e.getMessage());
        return 1;
    }
    if (null == eperson) {
        System.err.println("No such EPerson");
        return 1;
    }
    try {
        List<String> tableList = ePersonService.getDeleteConstraints(context, eperson);
        if (!tableList.isEmpty()) {
            System.out.printf("The EPerson with ID: %s is referenced by the following database tables:%n", eperson.getID().toString());
            tableList.forEach((s) -> {
                System.out.println(s);
            });
        }
        System.out.printf("Are you sure you want to delete this EPerson with ID: %s? (y or n): ", eperson.getID().toString());
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.flush();
        String s = input.readLine();
        if (s != null && s.trim().toLowerCase().startsWith("y")) {
            ePersonService.delete(context, eperson);
            System.out.printf("%nDeleted EPerson with ID: %s", eperson.getID().toString());
        } else {
            System.out.printf("%nAbort Deletion of EPerson with ID: %s %n", eperson.getID().toString());
        }
    } catch (SQLException | AuthorizeException | IOException ex) {
        System.err.println(ex.getMessage());
        return 1;
    }
    return 0;
}
Also used : Options(org.apache.commons.cli.Options) InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) AuthorizeException(org.dspace.authorize.AuthorizeException) IOException(java.io.IOException) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) OptionGroup(org.apache.commons.cli.OptionGroup) BufferedReader(java.io.BufferedReader) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 3 with AuthorizeException

use of org.dspace.authorize.AuthorizeException in project DSpace by DSpace.

the class Groomer method aging.

/**
 * Find and optionally delete accounts not logged in recently.
 *
 * @param command a parsed command line.
 * @throws SQLException from callees.
 */
private static void aging(CommandLine command) throws SQLException {
    if (!command.hasOption('b')) {
        System.err.println("A last login date is required.");
        System.exit(1);
    }
    Date before = null;
    try {
        before = dateFormat.get().parse(command.getOptionValue('b'));
    } catch (java.text.ParseException ex) {
        System.err.println(ex.getMessage());
        System.exit(1);
    }
    boolean delete = command.hasOption('d');
    Context myContext = new Context();
    List<EPerson> epeople = ePersonService.findNotActiveSince(myContext, before);
    myContext.turnOffAuthorisationSystem();
    for (EPerson account : epeople) {
        System.out.print(account.getID());
        System.out.print('\t');
        System.out.print(account.getLastActive());
        System.out.print('\t');
        System.out.print(account.getEmail());
        System.out.print('\t');
        System.out.print(account.getNetid());
        System.out.print('\t');
        System.out.print(account.getFullName());
        System.out.println();
        if (delete) {
            List<String> whyNot = ePersonService.getDeleteConstraints(myContext, account);
            if (!whyNot.isEmpty()) {
                System.out.print("\tCannot be deleted; referenced in");
                for (String table : whyNot) {
                    System.out.print(' ');
                    System.out.print(table);
                }
                System.out.println();
            } else {
                try {
                    ePersonService.delete(myContext, account);
                } catch (AuthorizeException | IOException ex) {
                    System.err.println(ex.getMessage());
                }
            }
        }
    }
    myContext.restoreAuthSystemState();
    myContext.complete();
}
Also used : Context(org.dspace.core.Context) AuthorizeException(org.dspace.authorize.AuthorizeException) Context(org.dspace.core.Context) IOException(java.io.IOException) Date(java.util.Date)

Example 4 with AuthorizeException

use of org.dspace.authorize.AuthorizeException in project DSpace by DSpace.

the class GroupServiceImpl method create.

@Override
public Group create(Context context) throws SQLException, AuthorizeException {
    // FIXME - authorization?
    if (!authorizeService.isAdmin(context)) {
        throw new AuthorizeException("You must be an admin to create an EPerson Group");
    }
    // Create a table row
    Group g = groupDAO.create(context, new Group());
    log.info(LogHelper.getHeader(context, "create_group", "group_id=" + g.getID()));
    context.addEvent(new Event(Event.CREATE, Constants.GROUP, g.getID(), null, getIdentifiers(context, g)));
    update(context, g);
    return g;
}
Also used : AuthorizeException(org.dspace.authorize.AuthorizeException) Event(org.dspace.event.Event)

Example 5 with AuthorizeException

use of org.dspace.authorize.AuthorizeException in project DSpace by DSpace.

the class LogicalFilterTest method init.

/**
 * This method will be run before every test as per @Before. It will
 * initialize resources required for the tests.
 *
 * Other methods can be annotated with @Before here or in subclasses
 * but no execution order is guaranteed
 */
@Before
@Override
public void init() {
    super.init();
    try {
        context.turnOffAuthorisationSystem();
        // Set up logical statement lists for operator testing
        setUpStatements();
        // Set up DSpace resources for condition and filter testing
        // Set up first community, collection and item
        this.communityOne = communityService.create(null, context);
        this.collectionOne = collectionService.create(context, communityOne);
        WorkspaceItem workspaceItem = workspaceItemService.create(context, collectionOne, false);
        this.itemOne = installItemService.installItem(context, workspaceItem);
        // Add one bitstream to item one, but put it in THUMBNAIL bundle
        bundleService.addBitstream(context, bundleService.create(context, itemOne, "THUMBNAIL"), bitstreamService.create(context, new ByteArrayInputStream("Item 1 Thumbnail 1".getBytes(StandardCharsets.UTF_8))));
        // Set up second community, collection and item, and third item
        this.communityTwo = communityService.create(null, context);
        this.collectionTwo = collectionService.create(context, communityTwo);
        // Item two
        workspaceItem = workspaceItemService.create(context, collectionTwo, false);
        this.itemTwo = installItemService.installItem(context, workspaceItem);
        // Add two bitstreams to item two
        Bundle bundleTwo = bundleService.create(context, itemTwo, "ORIGINAL");
        bundleService.addBitstream(context, bundleTwo, bitstreamService.create(context, new ByteArrayInputStream("Item 2 Bitstream 1".getBytes(StandardCharsets.UTF_8))));
        bundleService.addBitstream(context, bundleTwo, bitstreamService.create(context, new ByteArrayInputStream("Item 2 Bitstream 2".getBytes(StandardCharsets.UTF_8))));
        // Item three
        workspaceItem = workspaceItemService.create(context, collectionTwo, false);
        this.itemThree = installItemService.installItem(context, workspaceItem);
        // Add three bitstreams to item three
        Bundle bundleThree = bundleService.create(context, itemThree, "ORIGINAL");
        bundleService.addBitstream(context, bundleThree, bitstreamService.create(context, new ByteArrayInputStream("Item 3 Bitstream 1".getBytes(StandardCharsets.UTF_8))));
        bundleService.addBitstream(context, bundleThree, bitstreamService.create(context, new ByteArrayInputStream("Item 3 Bitstream 2".getBytes(StandardCharsets.UTF_8))));
        bundleService.addBitstream(context, bundleThree, bitstreamService.create(context, new ByteArrayInputStream("Item 3 Bitstream 2".getBytes(StandardCharsets.UTF_8))));
        // Withdraw the second item for later testing
        itemService.withdraw(context, itemTwo);
        // Initialise metadata field for later testing with both items
        this.metadataField = metadataFieldService.findByElement(context, MetadataSchemaEnum.DC.getName(), element, qualifier);
        context.restoreAuthSystemState();
    } catch (AuthorizeException | SQLException | IOException e) {
        log.error("Error encountered during init", e);
        fail("Error encountered during init: " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) Bundle(org.dspace.content.Bundle) AuthorizeException(org.dspace.authorize.AuthorizeException) IOException(java.io.IOException) WorkspaceItem(org.dspace.content.WorkspaceItem) Before(org.junit.Before)

Aggregations

AuthorizeException (org.dspace.authorize.AuthorizeException)214 SQLException (java.sql.SQLException)143 IOException (java.io.IOException)88 Context (org.dspace.core.Context)63 Item (org.dspace.content.Item)51 Bitstream (org.dspace.content.Bitstream)29 Test (org.junit.Test)29 WorkspaceItem (org.dspace.content.WorkspaceItem)25 Collection (org.dspace.content.Collection)24 Bundle (org.dspace.content.Bundle)22 EPerson (org.dspace.eperson.EPerson)18 Community (org.dspace.content.Community)17 ArrayList (java.util.ArrayList)15 DSpaceObject (org.dspace.content.DSpaceObject)15 ContextException (org.dspace.rest.exceptions.ContextException)15 AccessDeniedException (org.springframework.security.access.AccessDeniedException)15 RESTAuthorizationException (org.dspace.app.rest.exception.RESTAuthorizationException)14 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 Path (javax.ws.rs.Path)13 MetadataValue (org.dspace.content.MetadataValue)13