Search in sources :

Example 6 with RegistrarModule

use of cz.metacentrum.perun.registrar.RegistrarModule 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

RegistrarModule (cz.metacentrum.perun.registrar.RegistrarModule)6 SQLException (java.sql.SQLException)4 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Application (cz.metacentrum.perun.registrar.model.Application)2 ApplicationFormItem (cz.metacentrum.perun.registrar.model.ApplicationFormItem)2 ResultSet (java.sql.ResultSet)2 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)2 SingleColumnRowMapper (org.springframework.jdbc.core.SingleColumnRowMapper)2 AppType (cz.metacentrum.perun.registrar.model.Application.AppType)1 Type (cz.metacentrum.perun.registrar.model.ApplicationFormItem.Type)1 ApplicationFormItemData (cz.metacentrum.perun.registrar.model.ApplicationFormItemData)1 ApplicationFormItemWithPrefilledValue (cz.metacentrum.perun.registrar.model.ApplicationFormItemWithPrefilledValue)1 MailType (cz.metacentrum.perun.registrar.model.ApplicationMail.MailType)1 RowMapper (org.springframework.jdbc.core.RowMapper)1