Search in sources :

Example 1 with RoleForm

use of cn.edu.zju.acm.onlinejudge.form.RoleForm in project zoj by licheng.

the class EditRoleAction method execute.

/**
     * Edit Role.
     * 
     * <pre>
     * </pre>
     * 
     * @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;
    }
    RoleForm roleForm = (RoleForm) form;
    AuthorizationPersistence authorizationPersistence = PersistenceManager.getInstance().getAuthorizationPersistence();
    if (roleForm.getId() == null || roleForm.getId().trim().length() == 0) {
        long roleId = Utility.parseLong(context.getRequest().getParameter("roleId"));
        RoleSecurity role = authorizationPersistence.getRole(roleId);
        if (role == null) {
            return this.handleSuccess(mapping, context, "success");
        }
        // add contest names
        Map<Long, String> contestNames = new TreeMap<Long, String>();
        for (AbstractContest contest : ContestManager.getInstance().getAllContests()) {
            contestNames.put(contest.getId(), contest.getTitle());
        }
        for (AbstractContest contest : ContestManager.getInstance().getAllProblemsets()) {
            contestNames.put(contest.getId(), contest.getTitle());
        }
        for (AbstractContest contest : ContestManager.getInstance().getAllCourses()) {
            contestNames.put(contest.getId(), contest.getTitle());
        }
        context.setAttribute("ContestNames", contestNames);
        // TODO add forums
        Map<Long, String> forumNames = new TreeMap<Long, String>();
        forumNames.put(1L, "ZOJ Forum");
        context.setAttribute("ForumNames", forumNames);
        roleForm.populate(role);
        return this.handleSuccess(mapping, context, "failure");
    }
    RoleSecurity role = roleForm.toRole();
    authorizationPersistence.updateRole(role, context.getUserProfile().getId());
    if (role.getId() == 1) {
        ContextAdapter.resetDefaultUserSecurity();
    }
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) AuthorizationPersistence(cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence) RoleForm(cn.edu.zju.acm.onlinejudge.form.RoleForm) TreeMap(java.util.TreeMap) ActionForward(org.apache.struts.action.ActionForward) RoleSecurity(cn.edu.zju.acm.onlinejudge.security.RoleSecurity)

Aggregations

AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)1 RoleForm (cn.edu.zju.acm.onlinejudge.form.RoleForm)1 AuthorizationPersistence (cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence)1 RoleSecurity (cn.edu.zju.acm.onlinejudge.security.RoleSecurity)1 TreeMap (java.util.TreeMap)1 ActionForward (org.apache.struts.action.ActionForward)1