Search in sources :

Example 66 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRClassification2Commands method repairMissingParent.

@MCRCommand(syntax = "repair missing parent for classification {0}", help = "restores parentID from information in the given classification, if left right values are correct", order = 130)
public static void repairMissingParent(String classID) {
    Session session = MCRHIBConnection.instance().getSession();
    String sqlQuery = "update {h-schema}MCRCategory cat set cat.parentID=(select parent.internalID from {h-schema}MCRCategory parent WHERE parent.classid='" + classID + "' and parent.leftValue<cat.leftValue and parent.rightValue>cat.rightValue and parent.level=(cat.level-1)) WHERE cat.classid='" + classID + "' and cat.level > 0 and cat.parentID is NULL";
    int updates = session.createNativeQuery(sqlQuery).executeUpdate();
    LOGGER.info(() -> "Repaired " + updates + " parentID columns for classification " + classID);
}
Also used : Session(org.hibernate.Session) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 67 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRAccessCommands method permissionDeleteAllForID.

/**
 * delete all permissions for a given id
 *
 * @param id
 *            String the id of the object the rule is assigned to
 */
@MCRCommand(syntax = "delete all permissions for id {1}", help = "The command delete all access rules for a given id", order = 120)
public static void permissionDeleteAllForID(String id) {
    MCRAccessInterface AI = MCRAccessManager.getAccessImpl();
    AI.removeAllRules(id);
}
Also used : MCRAccessInterface(org.mycore.access.MCRAccessInterface) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 68 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRUserCommands method encryptPasswordsInXMLFile.

/**
 * A given XML file containing user data with cleartext passwords must be converted prior to loading the user data
 * into the system. This method reads all user objects in the given XML file, encrypts the passwords and writes them
 * back to a file with name original-file-name_encrypted.xml.
 *
 * @param oldFile
 *            the filename of the user data input
 * @param newFile
 *            the filename of the user data output (encrypted passwords)
 */
@MCRCommand(syntax = "encrypt passwords in user xml file {0} to file {1}", help = "A migration tool to change old plain text password entries to encrpted entries.", order = 40)
public static void encryptPasswordsInXMLFile(String oldFile, String newFile) throws SAXParseException, IOException {
    File inputFile = getCheckedFile(oldFile);
    if (inputFile == null) {
        return;
    }
    LOGGER.info("Reading file {} ...", inputFile.getAbsolutePath());
    Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(new MCRFileContent(inputFile));
    Element rootelm = doc.getRootElement();
    MCRUser mcrUser = MCRUserTransformer.buildMCRUser(rootelm);
    if (mcrUser == null) {
        throw new MCRException("These data do not correspond to a user.");
    }
    MCRUserManager.updatePasswordHashToSHA256(mcrUser, mcrUser.getPassword());
    FileOutputStream outFile = new FileOutputStream(newFile);
    saveToXMLFile(mcrUser, outFile);
}
Also used : MCRException(org.mycore.common.MCRException) MCRFileContent(org.mycore.common.content.MCRFileContent) Element(org.jdom2.Element) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 69 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRUserCommands method addRole.

/**
 * Loads XML from a user and looks for roles currently not present in the system and creates them.
 *
 * @param fileName
 *            a valid user XML file
 */
@MCRCommand(syntax = "import role from file {0}", help = "Imports a role from file, if that role does not exist", order = 90)
public static void addRole(String fileName) throws SAXParseException, IOException {
    LOGGER.info("Reading file {} ...", fileName);
    Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(new MCRFileContent(fileName));
    MCRRole role = MCRRoleTransformer.buildMCRRole(doc.getRootElement());
    if (MCRRoleManager.getRole(role.getName()) == null) {
        MCRRoleManager.addRole(role);
    } else {
        LOGGER.info("Role {} does already exist.", role.getName());
    }
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) Document(org.jdom2.Document) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 70 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRUserCommands method unassignUserFromRole.

/**
 * This method removes a member user from a role
 *
 * @param userID
 *            the ID of the user which will be removed from the role represented by roleID
 * @param roleID
 *            the ID of the role from which the user with ID mbrUserID will be removed
 */
@MCRCommand(syntax = "unassign user {0} from role {1}", help = "Removes the user {0} as secondary member from the role {1}.", order = 130)
public static void unassignUserFromRole(String userID, String roleID) throws MCRException {
    try {
        MCRUser user = MCRUserManager.getUser(userID);
        user.unassignRole(roleID);
        MCRUserManager.updateUser(user);
    } catch (Exception e) {
        throw new MCRException("Error while unassigning " + userID + " from role " + roleID + ".", e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)106 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)37 MCRException (org.mycore.common.MCRException)34 IOException (java.io.IOException)30 File (java.io.File)22 ArrayList (java.util.ArrayList)18 Document (org.jdom2.Document)17 JDOMException (org.jdom2.JDOMException)17 MCRObject (org.mycore.datamodel.metadata.MCRObject)17 Path (java.nio.file.Path)16 SAXException (org.xml.sax.SAXException)16 EntityManager (javax.persistence.EntityManager)15 MCRAccessException (org.mycore.access.MCRAccessException)15 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)15 MCRPath (org.mycore.datamodel.niofs.MCRPath)15 FileNotFoundException (java.io.FileNotFoundException)13 SAXParseException (org.xml.sax.SAXParseException)12 List (java.util.List)11 Element (org.jdom2.Element)11 MCRPersistenceException (org.mycore.common.MCRPersistenceException)11