Search in sources :

Example 6 with AuthorizationPersistence

use of cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence in project zoj by licheng.

the class RegisterAction method execute.

/**
     * Register.
     * 
     * @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 {
    if (!Features.register()) {
        context.getResponse().sendError(404);
        return null;
    }
    UserPersistence userPersistence = PersistenceManager.getInstance().getUserPersistence();
    ProfileForm profileForm = (ProfileForm) form;
    if (profileForm.getHandle() == null) {
        return this.handleSuccess(mapping, context, "failure");
    }
    context.getRequest().getSession().invalidate();
    ActionMessages errors = this.validate(userPersistence, profileForm);
    if (errors.size() > 0) {
        return this.handleFailure(mapping, context, errors);
    }
    // create user profile
    UserProfile profile = profileForm.toUserProfile();
    userPersistence.createUserProfile(profile, 0);
    // create user perference
    UserPreference perference = profileForm.toUserPreference();
    perference.setId(profile.getId());
    userPersistence.createUserPreference(perference, 0);
    AuthorizationPersistence authorizationPersistence = PersistenceManager.getInstance().getAuthorizationPersistence();
    authorizationPersistence.addUserRole(profile.getId(), 2);
    context.getRequest().setAttribute("Countries", PersistenceManager.getInstance().getUserPersistence().getAllCountries());
    // get UserSecurity
    UserSecurity security = authorizationPersistence.getUserSecurity(profile.getId());
    context.setUserProfile(profile);
    context.setUserSecurity(security);
    context.setUserPreference(perference);
    ActionMessages messages = new ActionMessages();
    messages.add("message", new ActionMessage("onlinejudge.register.success"));
    this.saveErrors(context.getRequest(), messages);
    context.setAttribute("back", "");
    return this.handleSuccess(mapping, context, "success");
}
Also used : UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) ActionMessages(org.apache.struts.action.ActionMessages) ProfileForm(cn.edu.zju.acm.onlinejudge.form.ProfileForm) ActionMessage(org.apache.struts.action.ActionMessage) AuthorizationPersistence(cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence) UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference) UserPersistence(cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)

Example 7 with AuthorizationPersistence

use of cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence 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 8 with AuthorizationPersistence

use of cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence 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)

Aggregations

AuthorizationPersistence (cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence)8 ActionForward (org.apache.struts.action.ActionForward)5 RoleSecurity (cn.edu.zju.acm.onlinejudge.security.RoleSecurity)4 UserPreference (cn.edu.zju.acm.onlinejudge.bean.UserPreference)3 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)3 UserPersistence (cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)3 UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)3 ActionMessage (org.apache.struts.action.ActionMessage)2 ActionMessages (org.apache.struts.action.ActionMessages)2 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)1 ProfileForm (cn.edu.zju.acm.onlinejudge.form.ProfileForm)1 RoleForm (cn.edu.zju.acm.onlinejudge.form.RoleForm)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ServletException (javax.servlet.ServletException)1 Cookie (javax.servlet.http.Cookie)1