Search in sources :

Example 1 with SecurityDao

use of com.joliciel.jochre.security.SecurityDao in project jochre by urieli.

the class ImageController method doAfterCompose.

@Override
public void doAfterCompose(Window window) throws Exception {
    super.doAfterCompose(window);
    Session session = Sessions.getCurrent();
    currentUser = (User) session.getAttribute(LoginController.SESSION_JOCHRE_USER);
    if (currentUser == null)
        Executions.sendRedirect("login.zul");
    // comp.setVariable(comp.getId() + "Ctrl", this, true);
    hebrewAccentsSpan.setContent("var hebrewAccents=\"" + HEBREW_ACCENTS + "\";");
    rowGrid.setRowRenderer(new ImageGridRowRenderer());
    HttpServletRequest request = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
    imageId = Integer.parseInt(request.getParameter("imageId"));
    GraphicsDao graphicsDao = GraphicsDao.getInstance(jochreSession);
    currentImage = graphicsDao.loadJochreImage(imageId);
    docId = currentImage.getPage().getDocumentId();
    currentImageOwner = (currentUser.getRole().equals(UserRole.ADMIN) || currentImage.getOwner().equals(currentUser));
    if (!currentImageOwner) {
        btnSave.setVisible(false);
        btnSave2.setVisible(false);
        btnSaveAndExit.setVisible(false);
        btnSaveAndExit2.setVisible(false);
        cmbStatus.setVisible(false);
        lblImageStatus.setVisible(true);
    } else {
        btnSave.setVisible(true);
        btnSave2.setVisible(true);
        btnSaveAndExit.setVisible(true);
        btnSaveAndExit2.setVisible(true);
        cmbStatus.setVisible(true);
        lblImageStatus.setVisible(false);
    }
    if (currentUser.getRole().equals(UserRole.ADMIN)) {
        cmbOwner.setVisible(true);
        lblOwner.setVisible(false);
        SecurityDao securityDao = SecurityDao.getInstance(jochreSession);
        List<User> users = securityDao.findUsers();
        List<Comboitem> cmbOwnerItems = cmbOwner.getItems();
        Comboitem selectedUser = null;
        for (User user : users) {
            Comboitem item = new Comboitem(user.getFullName());
            item.setValue(user);
            if (currentImage.getOwner().equals(user))
                selectedUser = item;
            cmbOwnerItems.add(item);
        }
        cmbOwner.setSelectedItem(selectedUser);
    } else {
        cmbOwner.setVisible(false);
        lblOwner.setVisible(true);
        lblOwner.setValue(currentImage.getOwner().getFullName());
    }
    String pageTitle = Labels.getLabel("image.title");
    winJochreImage.getPage().setTitle(pageTitle);
    String windowTitle = Labels.getLabel("image.winJochreImage.title", new Object[] { currentImage.getPage().getDocument().getName(), currentImage.getPage().getIndex() });
    winJochreImage.setTitle(windowTitle);
    List<Comboitem> cmbStatusItems = cmbStatus.getItems();
    Comboitem selectedItem = null;
    List<ImageStatus> imageStatuses = new ArrayList<ImageStatus>();
    if (currentUser.getRole().equals(UserRole.ADMIN)) {
        for (ImageStatus imageStatus : ImageStatus.values()) {
            imageStatuses.add(imageStatus);
        }
    } else if (currentImage.getImageStatus().equals(ImageStatus.AUTO_NEW) || currentImage.getImageStatus().equals(ImageStatus.AUTO_VALIDATED)) {
        imageStatuses.add(ImageStatus.AUTO_NEW);
        imageStatuses.add(ImageStatus.AUTO_VALIDATED);
    } else {
        // a bit dangerous - leaving the image as "training" and allowing
        // modifications, but oh well!
        imageStatuses.add(currentImage.getImageStatus());
    }
    for (ImageStatus imageStatus : imageStatuses) {
        String statusLabel = Labels.getLabel("ImageStatus." + imageStatus.getCode());
        Comboitem item = new Comboitem(statusLabel);
        item.setValue(imageStatus.getId());
        if (currentImage.getImageStatus().equals(imageStatus))
            selectedItem = item;
        cmbStatusItems.add(item);
    }
    cmbStatus.setSelectedItem(selectedItem);
    lblImageStatus.setValue(Labels.getLabel("ImageStatus." + currentImage.getImageStatus().getCode()));
    reloadRowGrid();
}
Also used : User(com.joliciel.jochre.security.User) ImageStatus(com.joliciel.jochre.graphics.ImageStatus) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) GraphicsDao(com.joliciel.jochre.graphics.GraphicsDao) Comboitem(org.zkoss.zul.Comboitem) JochreSession(com.joliciel.jochre.JochreSession) Session(org.zkoss.zk.ui.Session) SecurityDao(com.joliciel.jochre.security.SecurityDao)

Example 2 with SecurityDao

use of com.joliciel.jochre.security.SecurityDao in project jochre by urieli.

the class LoginController method doAfterCompose.

@Override
public void doAfterCompose(Window window) throws Exception {
    super.doAfterCompose(window);
    String pageTitle = Labels.getLabel("login.title");
    winLogin.getPage().setTitle(pageTitle);
    Session session = Sessions.getCurrent();
    session.removeAttribute(SESSION_JOCHRE_USER);
    HttpServletRequest request = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
    String failed = request.getParameter("failed");
    if (failed == null)
        lblError.setVisible(false);
    else
        lblError.setVisible(true);
    SecurityDao securityDao = SecurityDao.getInstance(jochreSession);
    Parameters parameters = securityDao.loadParameters();
    Date lastFailedLoginAttempt = parameters.getLastFailedLoginAttempt();
    Date now = new Date();
    long diff = now.getTime() - lastFailedLoginAttempt.getTime();
    LOG.debug("time since last failed login: " + diff);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Parameters(com.joliciel.jochre.security.Parameters) Date(java.util.Date) Session(org.zkoss.zk.ui.Session) JochreSession(com.joliciel.jochre.JochreSession) SecurityDao(com.joliciel.jochre.security.SecurityDao)

Example 3 with SecurityDao

use of com.joliciel.jochre.security.SecurityDao in project jochre by urieli.

the class LoginController method onClick$btnLogin.

@Listen("onClick = #btnLogin")
public void onClick$btnLogin(Event event) {
    try {
        LOG.debug("onClick$btnLogin");
        Session session = Sessions.getCurrent();
        SecurityDao securityDao = SecurityDao.getInstance(jochreSession);
        User user = null;
        try {
            user = securityDao.findUser(txtUserName.getValue());
        } catch (EntityNotFoundException enfe) {
            LOG.debug("Unknown user: " + txtUserName.getValue());
            lblError.setVisible(true);
        }
        if (user != null) {
            boolean success = user.login(txtPassword.getValue());
            if (!success) {
                LOG.debug("Login failed");
                lblError.setVisible(true);
                Parameters parameters = securityDao.loadParameters();
                Date lastFailedLoginAttempt = parameters.getLastFailedLoginAttempt();
                Date now = new Date();
                long diff = now.getTime() - lastFailedLoginAttempt.getTime();
                LOG.debug("time since last failed login: " + diff);
            } else {
                LOG.debug("Login success");
                session.setAttribute(SESSION_JOCHRE_USER, user);
                Executions.sendRedirect("docs.zul");
            }
        }
    } catch (Exception e) {
        LOG.error("Failure in onClick$btnLogin", e);
        throw new RuntimeException(e);
    }
}
Also used : User(com.joliciel.jochre.security.User) Parameters(com.joliciel.jochre.security.Parameters) EntityNotFoundException(com.joliciel.jochre.EntityNotFoundException) Date(java.util.Date) EntityNotFoundException(com.joliciel.jochre.EntityNotFoundException) Session(org.zkoss.zk.ui.Session) JochreSession(com.joliciel.jochre.JochreSession) SecurityDao(com.joliciel.jochre.security.SecurityDao) Listen(org.zkoss.zk.ui.select.annotation.Listen)

Example 4 with SecurityDao

use of com.joliciel.jochre.security.SecurityDao in project jochre by urieli.

the class Jochre method doCommandSegment.

/**
 * Segment a file, without analysing it.
 *
 * @param filename
 *          the path of the file to load
 * @param userFriendlyName
 *          a name to store against this file in the database
 * @param save
 *          should we save this file to the database?
 * @param pages
 *          the pages to process, empty means all
 */
public void doCommandSegment(String filename, String userFriendlyName, File outputDir, boolean save, Set<Integer> pages) {
    if (filename.length() == 0)
        throw new RuntimeException("Missing argument: file");
    if (userId < 0 && save)
        throw new RuntimeException("Missing argument (for save=true): userId");
    User user = null;
    if (userId >= 0) {
        SecurityDao securityDao = SecurityDao.getInstance(jochreSession);
        user = securityDao.loadUser(userId);
    }
    File file = new File(filename);
    JochreDocumentGenerator jochreDocumentGenerator = new JochreDocumentGenerator(file.getName(), userFriendlyName, jochreSession);
    if (save)
        jochreDocumentGenerator.requestSave(user);
    if (jochreDocumentGenerator.isDrawSegmentedImage()) {
        if (outputDir != null) {
            jochreDocumentGenerator.requestSegmentation(outputDir);
        }
    }
    if (filename.toLowerCase().endsWith(".pdf")) {
        PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor(file, pages, jochreDocumentGenerator);
        pdfDocumentProcessor.process();
    } else if (filename.toLowerCase().endsWith(".png") || filename.toLowerCase().endsWith(".jpg") || filename.toLowerCase().endsWith(".jpeg") || filename.toLowerCase().endsWith(".gif")) {
        ImageDocumentExtractor extractor = new ImageDocumentExtractor(file, jochreDocumentGenerator);
        extractor.extractDocument();
    } else {
        throw new RuntimeException("Unrecognised file extension");
    }
}
Also used : PdfDocumentProcessor(com.joliciel.jochre.pdf.PdfDocumentProcessor) User(com.joliciel.jochre.security.User) File(java.io.File) SecurityDao(com.joliciel.jochre.security.SecurityDao) JochreDocumentGenerator(com.joliciel.jochre.doc.JochreDocumentGenerator) ImageDocumentExtractor(com.joliciel.jochre.doc.ImageDocumentExtractor)

Example 5 with SecurityDao

use of com.joliciel.jochre.security.SecurityDao in project jochre by urieli.

the class JochreDocument method getOwner.

/**
 * The User who uploaded this document in the first place.
 */
public User getOwner() {
    if (this.owner == null && this.ownerId != 0) {
        SecurityDao securityDao = SecurityDao.getInstance(jochreSession);
        this.owner = securityDao.loadUser(this.ownerId);
    }
    return owner;
}
Also used : SecurityDao(com.joliciel.jochre.security.SecurityDao)

Aggregations

SecurityDao (com.joliciel.jochre.security.SecurityDao)6 JochreSession (com.joliciel.jochre.JochreSession)3 User (com.joliciel.jochre.security.User)3 Session (org.zkoss.zk.ui.Session)3 Parameters (com.joliciel.jochre.security.Parameters)2 Date (java.util.Date)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 EntityNotFoundException (com.joliciel.jochre.EntityNotFoundException)1 ImageDocumentExtractor (com.joliciel.jochre.doc.ImageDocumentExtractor)1 JochreDocumentGenerator (com.joliciel.jochre.doc.JochreDocumentGenerator)1 GraphicsDao (com.joliciel.jochre.graphics.GraphicsDao)1 ImageStatus (com.joliciel.jochre.graphics.ImageStatus)1 PdfDocumentProcessor (com.joliciel.jochre.pdf.PdfDocumentProcessor)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Listen (org.zkoss.zk.ui.select.annotation.Listen)1 Comboitem (org.zkoss.zul.Comboitem)1