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");
}
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");
}
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);
}
}
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);
}
}
Aggregations