Search in sources :

Example 6 with RoleSecurity

use of cn.edu.zju.acm.onlinejudge.security.RoleSecurity in project zoj by licheng.

the class ShowRolesAction method execute.

/**
     * ShowRolesAction.
     * 
     * @param mapping
     *            action mapping
     * @param form
     *            action form
     * @param request
     *            http servlet request
     * @param response
     *            http servlet response
     * 
     * @return action forward instance
     * 
     * @throws Exception
     *             any errors happened
     */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    ActionForward forward = this.checkAdmin(mapping, context);
    if (forward != null) {
        return forward;
    }
    AuthorizationPersistence authorizationPersistence = PersistenceManager.getInstance().getAuthorizationPersistence();
    List<RoleSecurity> roles = authorizationPersistence.getAllRoles();
    context.setAttribute("Roles", roles);
    return this.handleSuccess(mapping, context, "success");
}
Also used : AuthorizationPersistence(cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence) ActionForward(org.apache.struts.action.ActionForward) RoleSecurity(cn.edu.zju.acm.onlinejudge.security.RoleSecurity)

Example 7 with RoleSecurity

use of cn.edu.zju.acm.onlinejudge.security.RoleSecurity in project zoj by licheng.

the class AddRoleAction method execute.

/**
     * AddRoleAction.
     * 
     * @param mapping
     *            action mapping
     * @param form
     *            action form
     * @param request
     *            http servlet request
     * @param response
     *            http servlet response
     * 
     * @return action forward instance
     * 
     * @throws Exception
     *             any errors happened
     */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    // check admin
    ActionForward forward = this.checkAdmin(mapping, context);
    if (forward != null) {
        return forward;
    }
    String name = context.getRequest().getParameter("name");
    if (name == null || name.trim().length() == 0) {
        return this.handleSuccess(mapping, context, "success");
    }
    String description = context.getRequest().getParameter("description");
    if (description == null) {
        description = "";
    }
    RoleSecurity role = new RoleSecurity(-1, name, description);
    AuthorizationPersistence ap = PersistenceManager.getInstance().getAuthorizationPersistence();
    ap.createRole(role, context.getUserProfile().getId());
    return this.handleSuccess(mapping, context, "success");
}
Also used : AuthorizationPersistence(cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence) ActionForward(org.apache.struts.action.ActionForward) RoleSecurity(cn.edu.zju.acm.onlinejudge.security.RoleSecurity)

Example 8 with RoleSecurity

use of cn.edu.zju.acm.onlinejudge.security.RoleSecurity in project zoj by licheng.

the class AuthorizationPersistenceImpl method getRole.

/**
     * <p>
     * Gets a role with given id from persistence layer.
     * </p>
     * 
     * @return a role with given id from persistence layer
     * @param id
     *            the id of the role
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public RoleSecurity getRole(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        RoleSecurity role;
        try {
            ps = conn.prepareStatement("SELECT role_id, name, description FROM role WHERE role_id=?");
            ps.setLong(1, id);
            rs = ps.executeQuery();
            if (!rs.next()) {
                return null;
            }
            role = new RoleSecurity(rs.getLong(1), rs.getString(2), rs.getString(3));
        } finally {
            Database.dispose(ps);
        }
        try {
            // select the contest permissions
            ps = conn.prepareStatement("SELECT role_id, contest_id, permission_level_id FROM contest_permission WHERE role_id=?");
            ps.setLong(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                role.getContestPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
            }
        } finally {
            Database.dispose(ps);
        }
        try {
            // select the forum permissions
            ps = conn.prepareStatement("SELECT role_id, forum_id, permission_level_id FROM forum_permission WHERE role_id=?");
            ps.setLong(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                role.getForumPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
            }
        } finally {
            Database.dispose(ps);
        }
        return role;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the role with id " + id, e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) RoleSecurity(cn.edu.zju.acm.onlinejudge.security.RoleSecurity)

Example 9 with RoleSecurity

use of cn.edu.zju.acm.onlinejudge.security.RoleSecurity in project zoj by licheng.

the class AuthorizationPersistenceImpl method getAllRoles.

/**
     * <p>
     * Gets all roles from persistence layer.
     * </p>
     * 
     * @return a list of RoleSecurity instances
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public List<RoleSecurity> getAllRoles() throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<RoleSecurity> roles = new ArrayList<RoleSecurity>();
        Map<Long, RoleSecurity> roleIds = new HashMap<Long, RoleSecurity>();
        try {
            // select the roles;
            ps = conn.prepareStatement("SELECT role_id, name, description FROM role");
            rs = ps.executeQuery();
            while (rs.next()) {
                RoleSecurity role = new RoleSecurity(rs.getLong(1), rs.getString(2), rs.getString(3));
                roles.add(role);
                roleIds.put(role.getId(), role);
            }
        } finally {
            Database.dispose(ps);
        }
        try {
            // select the contests permissions
            ps = conn.prepareStatement("SELECT role_id, contest_id, permission_level_id FROM contest_permission");
            rs = ps.executeQuery();
            while (rs.next()) {
                RoleSecurity role = roleIds.get(rs.getLong(1));
                role.getContestPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
            }
        } finally {
            Database.dispose(ps);
        }
        try {
            // select the forum permissions
            ps = conn.prepareStatement("SELECT role_id, forum_id, permission_level_id FROM forum_permission");
            rs = ps.executeQuery();
            while (rs.next()) {
                RoleSecurity role = roleIds.get(rs.getLong(1));
                role.getForumPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
            }
        } finally {
            Database.dispose(ps);
        }
        return roles;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get all roles", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) RoleSecurity(cn.edu.zju.acm.onlinejudge.security.RoleSecurity)

Aggregations

RoleSecurity (cn.edu.zju.acm.onlinejudge.security.RoleSecurity)9 ArrayList (java.util.ArrayList)5 AuthorizationPersistence (cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence)4 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 ActionForward (org.apache.struts.action.ActionForward)4 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)2 HashMap (java.util.HashMap)2 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)1 RoleForm (cn.edu.zju.acm.onlinejudge.form.RoleForm)1 UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)1 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1