Example usage for org.apache.shiro SecurityUtils getSubject

List of usage examples for org.apache.shiro SecurityUtils getSubject

Introduction

In this page you can find the example usage for org.apache.shiro SecurityUtils getSubject.

Prototype

public static Subject getSubject() 

Source Link

Document

Returns the currently accessible Subject available to the calling code depending on runtime environment.

Usage

From source file:au.org.theark.report.web.component.viewReport.phenoFieldDetails.filterForm.FieldDetailsFilterForm.java

License:Open Source License

@Override
protected void initialiseCustomFilterComponents() {
    Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = iArkCommonService.getStudy(sessionStudyId);
    cpModel.getObject().setStudy(study);

    initialisePhenoCollectionDropDown();
    initialiseFieldDataAvailableCheckBox();
}

From source file:au.org.theark.report.web.component.viewReport.ReportSelectPanel.java

License:Open Source License

public void initialisePanel() {
    Subject subject = SecurityUtils.getSubject();
    Long sessionStudyId = (Long) subject.getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = null;//from ww  w.  j  av  a2s  .com

    if (sessionStudyId != null && sessionStudyId > 0) {
        study = iArkCommonService.getStudy(sessionStudyId);
        reportSelectCPM.getObject().setStudy(study);
    }

    try {
        @SuppressWarnings("unused")
        ArkUser arkUser = iArkCommonService.getArkUser(subject.getPrincipal().toString());
        //TODO: Make this method work! :D
        // List<ReportTemplate> resultList = reportService.getReportsAvailableList(arkUser, study);
        List<ReportTemplate> resultList = reportService.getReportsAvailableList(null, study);

        if (resultList == null || (resultList != null && resultList.size() == 0)) {
            this.info(
                    "No reports are available to you under your current role (NB: roles may depend on the study selected.)");
        }
        reportSelectCPM.getObject().setReportsAvailableList(resultList);
    } catch (EntityNotFoundException e) {
        log.error(
                "ReportSelectPanel.initialisePanel() could not load the ArkUser based on username in context.  This should not happen.");
        this.error(
                "A system error has occurred. Please notify the system administrator if this happens again.");
    }

    iModel = new LoadableDetachableModel<Object>() {
        private static final long serialVersionUID = 1L;

        @Override
        protected Object load() {
            pageableListView.removeAll();
            return reportSelectCPM.getObject().getReportsAvailableList();
        }
    };

    pageableListView = buildPageableListView(iModel);
    pageableListView.setReuseItems(true);
    AjaxPagingNavigator pageNavigator = new AjaxPagingNavigator("navigator", pageableListView);
    add(pageNavigator);
    add(pageableListView);
}

From source file:au.org.theark.report.web.component.viewReport.ReportSelectPanel.java

License:Open Source License

/**
 * //from w  w w.j ava  2 s .  c  o m
 * @param iModel
 * @param searchContainer
 * @return
 */
public PageableListView<ReportTemplate> buildPageableListView(IModel iModel) {

    PageableListView<ReportTemplate> sitePageableListView = new PageableListView<ReportTemplate>("reportList",
            iModel, iArkCommonService.getRowsPerPage()) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(final ListItem<ReportTemplate> item) {

            ReportTemplate reportTemplate = item.getModelObject();
            ArkModule module = reportTemplate.getModule();
            ArkFunction function = reportTemplate.getFunction();

            /* The report module */
            // TODO : will need to change to foreign key reference when new ARK security is implemented
            if (reportTemplate.getModule() != null) {
                // Add the study Component Key here
                item.add(new Label("reportTemplate.module.name", module.getName()));
            } else {
                item.add(new Label("reportTemplate.module.name", ""));
            }

            // Perform security check upon selection of the report
            Subject subject = SecurityUtils.getSubject();
            String ldapUserName = subject.getPrincipal().toString();
            boolean securityCheckOk = false;
            try {
                String userRole = iArkCommonService.getUserRole(ldapUserName, function, module,
                        reportSelectCPM.getObject().getStudy());
                if (iArkCommonService.isSuperAdministator(ldapUserName, function, module)) {
                    // Super-Admins can do anything
                    securityCheckOk = true;
                } else {
                    if (userRole != null) {
                        java.util.Collection<String> userRolePermission = iArkCommonService
                                .getArkRolePermission(function, userRole, module);
                        // Enforces that the arkRolePolicyTemplate contain a READ for this report function 
                        if (userRolePermission.contains(PermissionConstants.READ)) {
                            securityCheckOk = true;
                        }
                    }
                }
            } catch (EntityNotFoundException e) {
                // TODO I don't like this kind of code - if there isn't a record, we should just return NULL.
                // Only if it really is an error to not have a record, then we should throw an exception.
            }
            item.setVisible(securityCheckOk);

            /* Component Name Link */
            item.add(buildLink(reportTemplate));

            // TODO when displaying text escape any special characters
            /* Description */
            if (reportTemplate.getDescription() != null) {
                item.add(new Label("reportTemplate.description", reportTemplate.getDescription())
                        .setEscapeModelStrings(false));// the ID here must
                                                                                                                                        // match the ones in
                                                                                                                                        // mark-up
            } else {
                item.add(new Label("reportTemplate.description", ""));// the ID here must match the ones in mark-up
            }

            /* For the alternative stripes */
            item.add(new AttributeModifier("class", new AbstractReadOnlyModel<String>() {
                private static final long serialVersionUID = 1L;

                @Override
                public String getObject() {
                    return (item.getIndex() % 2 == 1) ? "even" : "odd";
                }
            }));

        }
    };
    return sitePageableListView;
}

From source file:au.org.theark.report.web.component.viewReport.ReportSelectPanel.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
private AjaxLink buildLink(final ReportTemplate reportTemplate) {

    AjaxLink link = new AjaxLink("reportTemplate.link") {

        private static final long serialVersionUID = 1L;

        @Override//from www  .j a  v  a2  s .c  o m
        public void onClick(AjaxRequestTarget target) {
            // Perform security check upon selection of the report
            Subject subject = SecurityUtils.getSubject();
            boolean securityCheckOk = false;
            try {
                String userRole = iArkCommonService.getUserRole(subject.getPrincipal().toString(),
                        reportTemplate.getFunction(), reportTemplate.getModule(),
                        reportSelectCPM.getObject().getStudy());
                if (userRole.length() > 0) {
                    securityCheckOk = true;
                }
            } catch (EntityNotFoundException e) {
                // TODO I don't like this kind of code - if there isn't a record, we should just return NULL.
                // Only if it really is an error to not have a record, then we should throw an exception.
            }

            if (securityCheckOk == false) {
                this.error(
                        "You have insufficient privileges to access this report.  If you believe this is incorrect, then please contact the system administrator.");
            } else if (reportTemplate.getName().equals(Constants.STUDY_SUMMARY_REPORT_NAME)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    StudySummaryReportContainer selectedReportPanel = new StudySummaryReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            } else if (reportTemplate.getName().equals(Constants.STUDY_LEVEL_CONSENT_REPORT_NAME)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    StudyLevelConsentReportContainer selectedReportPanel = new StudyLevelConsentReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            } else if (reportTemplate.getName().equals(Constants.STUDY_COMP_CONSENT_REPORT_NAME)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    ConsentDetailsReportContainer selectedReportPanel = new ConsentDetailsReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            } else if (reportTemplate.getName().equals(Constants.PHENO_FIELD_DETAILS_REPORT_NAME)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    PhenoFieldDetailsReportContainer selectedReportPanel = new PhenoFieldDetailsReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            } else if (reportTemplate.getName().equals(Constants.STUDY_USER_ROLE_PERMISSIONS)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    StudyUserRolePermissionsReportContainer selectedReportPanel = new StudyUserRolePermissionsReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            } else if (reportTemplate.getName().equals(Constants.WORK_RESEARCHER_COST_REPORT_NAME)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    WorkResearcherCostReportContainer selectedReportPanel = new WorkResearcherCostReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            } else if (reportTemplate.getName().equals(Constants.WORK_RESEARCHER_DETAIL_COST_REPORT_NAME)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    WorkResearcherDetailCostReportContainer selectedReportPanel = new WorkResearcherDetailCostReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            } else if (reportTemplate.getName().equals(Constants.WORK_STUDY_DETAIL_COST_REPORT_NAME)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    StudyCostReportContainer selectedReportPanel = new StudyCostReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            } else if (reportTemplate.getName().equals(Constants.LIMS_BIOSPECIMEN_SUMMARY_REPORT_NAME)) {
                BiospecimenSummaryReportContainer selectedReportPanel = new BiospecimenSummaryReportContainer(
                        "selectedReportContainerPanel");
                selectedReportPanel.setOutputMarkupId(true);
                // Replace the old selectedReportPanel with this new one
                reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                target.add(reportContainerVO.getSelectedReportContainerWMC());
                this.info(reportTemplate.getName() + " template selected.");

            } else if (reportTemplate.getName().equals(Constants.LIMS_BIOSPECIMEN_DETAIL_REPORT_NAME)) {
                BiospecimenDetailsReportContainer selectedReportPanel = new BiospecimenDetailsReportContainer(
                        "selectedReportContainerPanel");
                selectedReportPanel.setOutputMarkupId(true);
                // Replace the old selectedReportPanel with this new one
                reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                target.add(reportContainerVO.getSelectedReportContainerWMC());
                this.info(reportTemplate.getName() + " template selected.");

            } else if (reportTemplate.getName().equals(Constants.STUDY_COMP_REPORT_NAME)) {
                if (reportSelectCPM.getObject().getStudy() == null) {
                    this.error("This report requires a study to be selected. Please select a study.");
                } else {
                    StudyComponentReportContainer selectedReportPanel = new StudyComponentReportContainer(
                            "selectedReportContainerPanel");
                    selectedReportPanel.setOutputMarkupId(true);
                    // Replace the old selectedReportPanel with this new one
                    reportContainerVO.getSelectedReportPanel().replaceWith(selectedReportPanel);
                    reportContainerVO.setSelectedReportPanel(selectedReportPanel);
                    selectedReportPanel.initialisePanel(reportContainerVO.getFeedbackPanel(), reportTemplate);
                    target.add(reportContainerVO.getSelectedReportContainerWMC());
                    this.info(reportTemplate.getName() + " template selected.");
                }
            }

            else {
                this.error("System error: " + reportTemplate.getName()
                        + " has no implementation or has been deprecated.");
            }
            target.add(reportContainerVO.getFeedbackPanel());
        }
    };

    // Add the label for the link
    Label nameLinkLabel = new Label("reportTemplate.name", reportTemplate.getName());
    link.add(nameLinkLabel);
    return link;

}

From source file:au.org.theark.report.web.component.viewReport.studyLevelConsent.filterForm.StudyLevelConsentDetailsFilterForm.java

License:Open Source License

protected JRResource generateOtherIDResource(AjaxRequestTarget target) {
    Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = iArkCommonService.getStudy(sessionStudyId);
    cpModel.getObject().getLinkSubjectStudy().setStudy(study);

    String consentType = "Study-level Consent OtherIDs";
    String reportTitle = study.getName() + " - Consent Details Report - " + consentType;

    //      ReportTemplate reportTemplate = cpModel.getObject().getSelectedReportTemplate();
    ReportOutputFormat reportOutputFormat = cpModel.getObject().getSelectedOutputFormat();
    ReportTemplate reportTemplate = new ReportTemplate();
    reportTemplate.setName("OtherID");
    reportTemplate.setTemplatePath("otherid.jrxml");

    // show report
    ServletContext context = ((WebApplication) getApplication()).getServletContext();
    File reportFile = null;//from   ww  w  .  j  av  a2 s  .  c  o m

    reportFile = new File(context.getRealPath("/reportTemplates/" + reportTemplate.getTemplatePath()));
    log.info("" + reportFile.getAbsoluteFile());

    JasperDesign design = null;
    JasperReport report = null;
    try {
        design = JRXmlLoader.load(reportFile);
        // System.out.println(" design -- created " );
        if (design != null) {
            design.setName(reportTitle); // set the output file name to match report title
            if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.CSV_REPORT_FORMAT)) {
                design.setIgnorePagination(true); // don't paginate CSVs
            }
            report = JasperCompileManager.compileReport(design);
            // System.out.println(" design -- compiled " );
        }
    } catch (JRException e) {
        reportFile = null;
        e.printStackTrace();
    }
    log.info("" + reportFile.getAbsoluteFile());
    // templateIS = getClass().getResourceAsStream("/reportTemplates/WebappReport.jrxml");
    final Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("BaseDir", new File(context.getRealPath("/reportTemplates")));
    parameters.put("ReportTitle", reportTitle);
    Subject currentUser = SecurityUtils.getSubject();
    String userName = "(unknown)";
    if (currentUser.getPrincipal() != null) {
        userName = (String) currentUser.getPrincipal();
    }
    parameters.put("UserName", userName);
    //      StudyLevelConsentReportDataSource reportDS = new StudyLevelConsentReportDataSource(reportService, cpModel.getObject());
    StudyLevelConsentOtherIDReportDataSource reportDS = new StudyLevelConsentOtherIDReportDataSource(
            reportService, cpModel.getObject());

    JRResource reportResource = null;
    if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.PDF_REPORT_FORMAT)) {
        final JRResource pdfResource = new JRConcreteResource<PdfResourceHandler>(new PdfResourceHandler());
        pdfResource.setJasperReport(report);
        pdfResource.setReportParameters(parameters).setReportDataSource(reportDS);
        reportResource = pdfResource;
    } else if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.CSV_REPORT_FORMAT)) {
        final JRResource csvResource = new JRConcreteResource<CsvResourceHandler>(new CsvResourceHandler());
        csvResource.setJasperReport(report);
        csvResource.setReportParameters(parameters).setReportDataSource(reportDS);
        reportResource = csvResource;
    }
    return reportResource;
}

From source file:au.org.theark.report.web.component.viewReport.studyLevelConsent.filterForm.StudyLevelConsentDetailsFilterForm.java

License:Open Source License

protected void onGenerateProcess(AjaxRequestTarget target) {
    Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = iArkCommonService.getStudy(sessionStudyId);
    cpModel.getObject().getLinkSubjectStudy().setStudy(study);

    String consentType = "Study-level Consent";
    String reportTitle = study.getName() + " - Consent Details Report - " + consentType;

    ReportTemplate reportTemplate = cpModel.getObject().getSelectedReportTemplate();
    ReportOutputFormat reportOutputFormat = cpModel.getObject().getSelectedOutputFormat();

    // show report
    ServletContext context = ((WebApplication) getApplication()).getServletContext();
    File reportFile = null;//from  w ww . j  a  va  2  s . c  om

    reportFile = new File(context.getRealPath("/reportTemplates/" + reportTemplate.getTemplatePath()));
    JasperDesign design = null;
    JasperReport report = null;
    try {
        design = JRXmlLoader.load(reportFile);
        // System.out.println(" design -- created " );
        if (design != null) {
            design.setName(reportTitle); // set the output file name to match report title
            if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.CSV_REPORT_FORMAT)) {
                design.setIgnorePagination(true); // don't paginate CSVs
            }
            report = JasperCompileManager.compileReport(design);
            // System.out.println(" design -- compiled " );
        }
    } catch (JRException e) {
        reportFile = null;
        e.printStackTrace();
    }
    // templateIS = getClass().getResourceAsStream("/reportTemplates/WebappReport.jrxml");
    final Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("BaseDir", new File(context.getRealPath("/reportTemplates")));
    parameters.put("ReportTitle", reportTitle);
    Subject currentUser = SecurityUtils.getSubject();
    String userName = "(unknown)";
    if (currentUser.getPrincipal() != null) {
        userName = (String) currentUser.getPrincipal();
    }
    parameters.put("UserName", userName);
    StudyLevelConsentReportDataSource reportDS = new StudyLevelConsentReportDataSource(reportService,
            cpModel.getObject());

    JRResource reportResource = null;
    if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.PDF_REPORT_FORMAT)) {
        final JRResource pdfResource = new JRConcreteResource<PdfResourceHandler>(new PdfResourceHandler());
        pdfResource.setJasperReport(report);
        pdfResource.setReportParameters(parameters).setReportDataSource(reportDS);
        reportResource = pdfResource;
    } else if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.CSV_REPORT_FORMAT)) {
        final JRResource csvResource = new JRConcreteResource<CsvResourceHandler>(new CsvResourceHandler());
        csvResource.setJasperReport(report);
        csvResource.setReportParameters(parameters).setReportDataSource(reportDS);
        reportResource = csvResource;
    }
    if (reportResource != null) {
        reportOutputPanel.setReportResource(reportResource);
        JRResource other = generateOtherIDResource(target);
        if (other != null) {
            reportOutputPanel.setOtherIDReportResource(generateOtherIDResource(target));
        }
        reportOutputPanel.setVisible(true);
        target.add(reportOutputPanel);
    }
}

From source file:au.org.theark.report.web.component.viewReport.studySummary.filterForm.StudySummaryFilterForm.java

License:Open Source License

protected void onGenerateProcess(AjaxRequestTarget target) {

    Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = iArkCommonService.getStudy(sessionStudyId);

    String reportTitle = study.getName() + " - Study Summary Report";

    ReportTemplate reportTemplate = cpModel.getObject().getSelectedReportTemplate();
    ReportOutputFormat reportOutputFormat = cpModel.getObject().getSelectedOutputFormat();

    // show report
    ServletContext context = ((WebApplication) getApplication()).getServletContext();
    File reportFile = null;//from   w w w .j av  a  2 s. co  m

    reportFile = new File(context.getRealPath("/reportTemplates/" + reportTemplate.getTemplatePath()));
    JasperDesign design = null;
    JasperReport report = null;
    try {
        design = JRXmlLoader.load(reportFile);
        System.out.println(" design -- created ");
        if (design != null) {
            design.setName(reportTitle); // set the output file name to match report title
            if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.CSV_REPORT_FORMAT)) {
                design.setIgnorePagination(true); // don't paginate CSVs
            }
            report = JasperCompileManager.compileReport(design);
            System.out.println(" design -- compiled ");
        }
    } catch (JRException e) {
        reportFile = null;
        e.printStackTrace();
    }
    // templateIS = getClass().getResourceAsStream("/reportTemplates/WebappReport.jrxml");
    final Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("BaseDir", new File(context.getRealPath("/reportTemplates")));
    parameters.put("ReportTitle", reportTitle);
    Subject currentUser = SecurityUtils.getSubject();
    String userName = "(unknown)";
    if (currentUser.getPrincipal() != null) {
        userName = (String) currentUser.getPrincipal();
    }
    parameters.put("UserName", userName);
    StudySummaryReportDataSource reportDS = new StudySummaryReportDataSource(reportService, study);

    JRResource reportResource = null;
    if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.PDF_REPORT_FORMAT)) {
        final JRResource pdfResource = new JRConcreteResource<PdfResourceHandler>(new PdfResourceHandler());
        pdfResource.setJasperReport(report);
        pdfResource.setReportParameters(parameters).setReportDataSource(reportDS);
        // This code would emulate a file download as if clicked the user
        // clicked on the download link, but unfortunately it seems to
        // stuff up the Indicator (not hidden upon completion).
        // ResourceReference ref = new ResourceReference(study.getName() + "/" + report.getName() + "." + reportOutputFormat.getName()) {
        // protected Resource newResource() {
        // return pdfResource;
        // }
        // };
        // String url = getRequestCycle().urlFor(ref).toString();
        // getRequestCycle().setRequestTarget(new RedirectRequestTarget(url));
        // add(new ResourceLink<Void>("linkToPdf", pdfResource));
        reportResource = pdfResource;
    } else if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.CSV_REPORT_FORMAT)) {
        final JRResource csvResource = new JRConcreteResource<CsvResourceHandler>(new CsvResourceHandler());
        csvResource.setJasperReport(report);
        csvResource.setReportParameters(parameters).setReportDataSource(reportDS);
        // This code would emulate a file download as if clicked the user
        // clicked on the download link, but unfortunately it seems to
        // stuff up the Indicator (not hidden upon completion).
        // ResourceReference ref = new ResourceReference(study.getName() + "/" + report.getName() + "." + reportOutputFormat.getName()) {
        // protected Resource newResource() {
        // return csvResource;
        // }
        // };
        // String url = getRequestCycle().urlFor(ref).toString();
        // getRequestCycle().setRequestTarget(new RedirectRequestTarget(url));
        // add(new ResourceLink<Void>("linkToCsv", csvResource));
        reportResource = csvResource;
    }
    if (reportResource != null) {
        reportOutputPanel.setReportResource(reportResource);
        reportOutputPanel.setVisible(true);
        target.add(reportOutputPanel);
    }
}

From source file:au.org.theark.report.web.component.viewReport.studyUserRolePermissions.filterForm.StudyUserRolePermissionsFilterForm.java

License:Open Source License

protected void onGenerateProcess(AjaxRequestTarget target) {

    Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = iArkCommonService.getStudy(sessionStudyId);

    String reportTitle = study.getName() + " - Study User Role Permissions Report";

    ReportTemplate reportTemplate = cpModel.getObject().getSelectedReportTemplate();

    ReportOutputFormat reportOutputFormat = cpModel.getObject().getSelectedOutputFormat();

    // show report
    ServletContext context = ((WebApplication) getApplication()).getServletContext();
    File reportFile = null;/* w  w  w. j ava  2s  .c  o m*/

    reportFile = new File(context.getRealPath("/reportTemplates/" + reportTemplate.getTemplatePath()));
    JasperDesign design = null;
    JasperReport report = null;
    try {
        design = JRXmlLoader.load(reportFile);
        // System.out.println(" design -- created " );
        if (design != null) {
            design.setName(reportTitle); // set the output file name to match report title
            if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.CSV_REPORT_FORMAT)) {
                design.setIgnorePagination(true); // don't paginate CSVs
            }
            report = JasperCompileManager.compileReport(design);
            // System.out.println(" design -- compiled " );
        }
    } catch (JRException e) {
        reportFile = null;
        e.printStackTrace();
    }
    // templateIS = getClass().getResourceAsStream("/reportTemplates/WebappReport.jrxml");
    final Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("BaseDir", new File(context.getRealPath("/reportTemplates")));
    parameters.put("ReportTitle", reportTitle);
    Subject currentUser = SecurityUtils.getSubject();
    String userName = "(unknown)";
    if (currentUser.getPrincipal() != null) {
        userName = (String) currentUser.getPrincipal();
    }
    parameters.put("UserName", userName);
    StudyUserRolePermissionsReportDataSource reportDS = new StudyUserRolePermissionsReportDataSource(
            reportService, study);

    JRResource reportResource = null;
    if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.PDF_REPORT_FORMAT)) {
        final JRResource pdfResource = new JRConcreteResource<PdfResourceHandler>(new PdfResourceHandler());
        pdfResource.setJasperReport(report);
        pdfResource.setReportParameters(parameters).setReportDataSource(reportDS);
        // This code would emulate a file download as if clicked the user
        // clicked on the download link, but unfortunately it seems to
        // stuff up the Indicator (not hidden upon completion).
        // ResourceReference ref = new ResourceReference(study.getName() + "/" + report.getName() + "." + reportOutputFormat.getName()) {
        // protected Resource newResource() {
        // return pdfResource;
        // }
        // };
        // String url = getRequestCycle().urlFor(ref).toString();
        // getRequestCycle().setRequestTarget(new RedirectRequestTarget(url));
        // add(new ResourceLink<Void>("linkToPdf", pdfResource));
        reportResource = pdfResource;
    } else if (reportOutputFormat.getName().equals(au.org.theark.report.service.Constants.CSV_REPORT_FORMAT)) {
        final JRResource csvResource = new JRConcreteResource<CsvResourceHandler>(new CsvResourceHandler());
        csvResource.setJasperReport(report);
        csvResource.setReportParameters(parameters).setReportDataSource(reportDS);
        // This code would emulate a file download as if clicked the user
        // clicked on the download link, but unfortunately it seems to
        // stuff up the Indicator (not hidden upon completion).
        // ResourceReference ref = new ResourceReference(study.getName() + "/" + report.getName() + "." + reportOutputFormat.getName()) {
        // protected Resource newResource() {
        // return csvResource;
        // }
        // };
        // String url = getRequestCycle().urlFor(ref).toString();
        // getRequestCycle().setRequestTarget(new RedirectRequestTarget(url));
        // add(new ResourceLink<Void>("linkToCsv", csvResource));
        reportResource = csvResource;
    }
    if (reportResource != null) {
        reportOutputPanel.setReportResource(reportResource);
        reportOutputPanel.setVisible(true);
        target.add(reportOutputPanel);
    }
}

From source file:au.org.theark.security.CustomAuthorizationStrategy.java

License:Open Source License

private SecurityConstraint checkInvalidInstantiation(Annotation[] annotationList, ShiroAction action) {

    for (Annotation annotation : annotationList) {

        if (annotation instanceof SecurityConstraint) {

            SecurityConstraint constraint = (SecurityConstraint) annotation;
            //ACTION
            if (constraint.action() == action) {
                System.out.println("Constraints match");
                //The component in context has this action annotated.
                //Check if the current user has the rights/access by checking the subjects  
                SecurityManager securityManager = ThreadContext.getSecurityManager();
                Subject subject = SecurityUtils.getSubject();//The subject in session maintained by Shiro Security Manager

                //CONSTRAINT
                //Checks if the subject has one of the below constraints, if the subject does not then return the constraint that 
                //is violated
                switch (constraint.constraint()) {

                case HasRole: {
                    //Check if the Shiro Security Manager if the user has role
                    if (!securityManager.hasRole(subject.getPrincipals(), constraint.value())) {
                        System.out.println("Subject does not have role: " + constraint.value());
                        return constraint;
                    }/*  www.j  a  va  2 s  . c  o m*/
                    break;
                }
                case HasPermission: {

                    if (!securityManager.isPermitted(subject.getPrincipals(), constraint.value())) {
                        System.out.println("Subject does not have Permission");
                        return constraint;
                    }
                    break;
                }
                case IsAuthenticated: {

                    if (!subject.isAuthenticated()) {
                        System.out.println("Subject is Not Authenticated");
                        return constraint;
                    }
                    break;
                }
                case LoggedIn: {

                    if (subject.getPrincipal() == null) {
                        System.out.println("Subject is not LoggedIn");
                        return constraint;
                    }
                    break;
                }
                }

            } else {
                System.out.println("Actions do not match ");
            }
        }
    }
    return null;
}

From source file:au.org.theark.study.model.dao.LdapUserDao.java

License:Open Source License

/**
 * Use when you want to return ALL users from LDAP. Applies for a Super User and Study Admin only. The criteria is supplied in the userVO
 * /*from   w  w w  .  j av  a  2s  .c  o  m*/
 * @param userCriteriaVO
 * @return
 * @throws InvalidNameException
 */
public List<ArkUserVO> searchAllUsers(ArkUserVO userCriteriaVO) throws ArkSystemException {

    SecurityManager securityManager = ThreadContext.getSecurityManager();
    Subject currentUser = SecurityUtils.getSubject();
    List<ArkUserVO> userList = new ArrayList<ArkUserVO>();

    try {
        List<ArkUserRole> adminUserNameList = arkAuthorisationService.getArkSuperAdministratorList();
        if (securityManager.isPermitted(currentUser.getPrincipals(), PermissionConstants.CREATE)
                && securityManager.isPermitted(currentUser.getPrincipals(), PermissionConstants.UPDATE)
                && securityManager.isPermitted(currentUser.getPrincipals(), PermissionConstants.READ)) {

            log.debug("getBaseDn() " + ldapDataContextSource.getBasePeopleDn());// ou=arkUsers or whatever is configured in the context file.
            LdapName ldapName;
            try {

                AndFilter andFilter = new AndFilter();
                andFilter.and(new EqualsFilter("objectClass", "person"));

                ldapName = new LdapName(ldapDataContextSource.getBasePeopleDn());
                // if userId was specified
                /* User ID */
                if (StringUtils.hasText(userCriteriaVO.getUserName())) {
                    ldapName.add(new Rdn(Constants.CN, userCriteriaVO.getUserName()));
                    andFilter.and(new WhitespaceWildcardsFilter(Constants.CN, userCriteriaVO.getUserName()));
                }
                /* Given Name */
                if (StringUtils.hasText(userCriteriaVO.getFirstName())) {
                    ldapName.add(new Rdn(Constants.GIVEN_NAME, userCriteriaVO.getFirstName()));
                    andFilter.and(
                            new WhitespaceWildcardsFilter(Constants.GIVEN_NAME, userCriteriaVO.getFirstName()));
                }

                /* Surname Name */
                if (StringUtils.hasText(userCriteriaVO.getLastName())) {
                    ldapName.add(new Rdn(Constants.LAST_NAME, userCriteriaVO.getLastName()));
                    andFilter.and(
                            new WhitespaceWildcardsFilter(Constants.LAST_NAME, userCriteriaVO.getLastName()));
                }

                /* Email */
                if (StringUtils.hasText(userCriteriaVO.getEmail())) {
                    ldapName.add(new Rdn(Constants.EMAIL, userCriteriaVO.getEmail()));
                    andFilter.and(new WhitespaceWildcardsFilter(Constants.EMAIL, userCriteriaVO.getEmail()));
                }

                for (ArkUserRole superAdmin : adminUserNameList) {
                    ldapName.add(new Rdn(Constants.CN, superAdmin.getArkUser().getLdapUserName()));
                    Filter filter = new NotFilter(
                            new EqualsFilter(Constants.CN, superAdmin.getArkUser().getLdapUserName()));
                    andFilter.and(filter);
                }

                /* Status is not defined as yet in the schema */
                userList = ldapDataContextSource.getLdapTemplate().search(
                        ldapDataContextSource.getBasePeopleDn(), andFilter.encode(), new PersonContextMapper());
                log.debug("Size of list " + userList.size());
            } catch (InvalidNameException ine) {

                log.error("Exception occured in searchAllUsers " + ine);
                throw new ArkSystemException("A system errror occured");
            }
        }

    } catch (EntityNotFoundException e) {

        log.error("Exception occured in searchAllUsers " + e);
        throw new ArkSystemException("A system errror occured. ");
    }

    return userList;
}