Search in sources :

Example 6 with UserSecurity

use of cn.edu.zju.acm.onlinejudge.security.UserSecurity 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 UserSecurity

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

the class BaseAction method checkContestPermission.

protected ActionForward checkContestPermission(ActionMapping mapping, ContextAdapter context, Boolean isProblemset, boolean checkStart, PermissionLevel level) throws Exception {
    // get the contest
    AbstractContest contest = context.getContest();
    if (contest == null || isProblemset != null && (contest instanceof Contest || contest instanceof Course) == isProblemset.booleanValue()) {
        context.setAttribute("contest", null);
        ActionMessages messages = new ActionMessages();
        messages.add("message", new ActionMessage("onlinejudge.showcontest.nocontestid"));
        this.saveErrors(context.getRequest(), messages);
        if (isProblemset != null) {
            context.setAttribute("back", isProblemset ? "showProblemsets.do" : "showContests.do");
        }
        return this.handleFailure(mapping, context, messages, "nopermission");
    }
    context.setAttribute("contest", contest);
    // check contest permission
    UserSecurity userSecurity = context.getUserSecurity();
    boolean hasPermisstion = false;
    if (level == PermissionLevel.ADMIN) {
        hasPermisstion = userSecurity.canAdminContest(contest.getId());
    } else if (level == PermissionLevel.PARTICIPATE) {
        hasPermisstion = userSecurity.canParticipateContest(contest.getId());
    } else if (level == PermissionLevel.VIEW) {
        hasPermisstion = userSecurity.canViewContest(contest.getId());
    } else if (level == PermissionLevel.PARTICIPATECANVIEWSOURCE) {
        hasPermisstion = userSecurity.canViewSource(contest.getId());
    }
    if (!hasPermisstion) {
        ActionMessages messages = new ActionMessages();
        messages.add("message", new ActionMessage("onlinejudge.showcontest.nopermission"));
        this.saveErrors(context.getRequest(), messages);
        if (isProblemset != null) {
            context.setAttribute("back", isProblemset ? "showProblemsets.do" : "showContests.do");
        }
        return this.handleFailure(mapping, context, messages, "nopermission");
    }
    // check start time
    if (checkStart && !userSecurity.canAdminContest(contest.getId())) {
        return this.checkContestStart(mapping, context, contest);
    }
    return null;
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage) AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) Contest(cn.edu.zju.acm.onlinejudge.bean.Contest) Course(cn.edu.zju.acm.onlinejudge.bean.Course)

Example 8 with UserSecurity

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

the class ContextAdapter method getCheckedContests.

protected List<AbstractContest> getCheckedContests(List<AbstractContest> contests) throws PersistenceException {
    UserSecurity userSecurity = this.getUserSecurity();
    List<AbstractContest> ret = new ArrayList<AbstractContest>();
    for (AbstractContest contest : contests) {
        if (userSecurity.canViewContest(contest.getId())) {
            ret.add(contest);
        }
    }
    return ret;
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) ArrayList(java.util.ArrayList)

Aggregations

UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)8 ActionMessage (org.apache.struts.action.ActionMessage)4 ActionMessages (org.apache.struts.action.ActionMessages)4 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)3 UserPreference (cn.edu.zju.acm.onlinejudge.bean.UserPreference)3 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)3 AuthorizationPersistence (cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence)3 UserPersistence (cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)3 Contest (cn.edu.zju.acm.onlinejudge.bean.Contest)2 Course (cn.edu.zju.acm.onlinejudge.bean.Course)2 ArrayList (java.util.ArrayList)2 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)1 ProfileForm (cn.edu.zju.acm.onlinejudge.form.ProfileForm)1 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)1 RoleSecurity (cn.edu.zju.acm.onlinejudge.security.RoleSecurity)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1