Search in sources :

Example 11 with Application

use of cz.metacentrum.perun.registrar.model.Application in project perun by CESNET.

the class RegistrarManagerImpl method rejectApplication.

@Override
@Transactional(rollbackFor = Exception.class)
public Application rejectApplication(PerunSession sess, int appId, String reason) throws PerunException {
    Application app = getApplicationById(appId);
    if (app == null)
        throw new RegistrarException("Application with ID=" + appId + " doesn't exists.");
    // authz
    if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, app.getVo())) {
        if (app.getGroup() != null) {
            if (!AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, app.getGroup())) {
                throw new PrivilegeException(sess, "rejectApplication");
            }
        } else {
            throw new PrivilegeException(sess, "rejectApplication");
        }
    }
    // only VERIFIED applications can be rejected
    if (AppState.APPROVED.equals(app.getState())) {
        throw new RegistrarException("Approved application can't be rejected ! Try to refresh the view to see changes.");
    } else if (AppState.REJECTED.equals(app.getState())) {
        throw new RegistrarException("Application is already rejected. Try to refresh the view to see changes.");
    }
    // mark as rejected
    int result = jdbc.update("update application set state=?, modified_by=?, modified_at=? where id=?", AppState.REJECTED.toString(), sess.getPerunPrincipal().getActor(), new Date(), appId);
    if (result == 0) {
        throw new RegistrarException("Application with ID=" + appId + " not found.");
    } else if (result > 1) {
        throw new ConsistencyErrorException("More than one application is stored under ID=" + appId + ".");
    }
    // set back as rejected
    app.setState(AppState.REJECTED);
    log.info("Application {} marked as REJECTED.", appId);
    // get all reserved logins
    List<Pair<String, String>> logins = jdbc.query("select namespace,login from application_reserved_logins where app_id=?", new RowMapper<Pair<String, String>>() {

        @Override
        public Pair<String, String> mapRow(ResultSet rs, int arg1) throws SQLException {
            return new Pair<String, String>(rs.getString("namespace"), rs.getString("login"));
        }
    }, appId);
    // delete passwords for reserved logins
    for (Pair<String, String> login : logins) {
        try {
            // left = namespace / right = login
            usersManager.deletePassword(registrarSession, login.getRight(), login.getLeft());
        } catch (LoginNotExistsException ex) {
            log.error("[REGISTRAR] Login: {} not exists while deleting passwords in rejected application: {}", login.getLeft(), appId);
        }
    }
    // free any login from reservation when application is rejected
    jdbc.update("delete from application_reserved_logins where app_id=?", appId);
    // log
    perun.getAuditer().log(sess, "{} rejected.", app);
    // call registrar module
    RegistrarModule module;
    if (app.getGroup() != null) {
        module = getRegistrarModule(getFormForGroup(app.getGroup()));
    } else {
        module = getRegistrarModule(getFormForVo(app.getVo()));
    }
    if (module != null) {
        module.rejectApplication(sess, app, reason);
    }
    // send mail
    getMailManager().sendMessage(app, MailType.APP_REJECTED_USER, reason, null);
    perun.getAuditer().log(sess, "Application ID=" + app.getId() + " voID=" + app.getVo().getId() + ((app.getGroup() != null) ? (" groupID=" + app.getGroup().getId()) : "") + " has been rejected.");
    // return updated application
    return app;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RegistrarModule(cz.metacentrum.perun.registrar.RegistrarModule) Application(cz.metacentrum.perun.registrar.model.Application) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Application (cz.metacentrum.perun.registrar.model.Application)11 SQLException (java.sql.SQLException)6 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)6 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)4 RegistrarModule (cz.metacentrum.perun.registrar.RegistrarModule)2 RegistrarException (cz.metacentrum.perun.registrar.exceptions.RegistrarException)2 ApplicationForm (cz.metacentrum.perun.registrar.model.ApplicationForm)2 ApplicationFormItemData (cz.metacentrum.perun.registrar.model.ApplicationFormItemData)2 ApplicationMail (cz.metacentrum.perun.registrar.model.ApplicationMail)2 MailText (cz.metacentrum.perun.registrar.model.ApplicationMail.MailText)2 ResultSet (java.sql.ResultSet)2 MailException (org.springframework.mail.MailException)2 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)1 PerunException (cz.metacentrum.perun.core.api.exceptions.PerunException)1 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)1 ApplicationFormItem (cz.metacentrum.perun.registrar.model.ApplicationFormItem)1 ApplicationFormItemWithPrefilledValue (cz.metacentrum.perun.registrar.model.ApplicationFormItemWithPrefilledValue)1 RowMapper (org.springframework.jdbc.core.RowMapper)1