Search in sources :

Example 16 with UserProfile

use of cn.edu.zju.acm.onlinejudge.bean.UserProfile in project zoj by licheng.

the class LoginAction method authenticate.

/**
 * Authenticate.
 *
 * @param form
 * @return
 * @throws Exception
 */
private ActionMessages authenticate(LoginForm form, ContextAdapter context) throws PersistenceException {
    context.getRequest().getSession().invalidate();
    ActionMessages errors = new ActionMessages();
    UserPersistence userPersistence = PersistenceManager.getInstance().getUserPersistence();
    UserProfile profile = userPersistence.login(form.getHandle(), form.getPassword());
    // no such user
    if (profile == null) {
        errors.add("password", new ActionMessage("LoginForm.password.invalid"));
        return errors;
    }
    // deactivated
    if (!profile.isActive()) {
        errors.add("password", new ActionMessage("LoginForm.password.deactivated"));
        return errors;
    }
    AuthorizationPersistence authorizationPersistence = PersistenceManager.getInstance().getAuthorizationPersistence();
    // get UserSecurity
    UserSecurity security = authorizationPersistence.getUserSecurity(profile.getId());
    // get UserPreference
    UserPreference perference = userPersistence.getUserPreference(profile.getId());
    context.setUserProfile(profile);
    context.setUserSecurity(security);
    if (context.getAllCourses().size() != 0) {
        security.setHasCourses(true);
    } else {
        security.setHasCourses(false);
    }
    context.setUserPreference(perference);
    return errors;
}
Also used : UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) ActionMessages(org.apache.struts.action.ActionMessages) 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 17 with UserProfile

use of cn.edu.zju.acm.onlinejudge.bean.UserProfile in project zoj by licheng.

the class CookieFilter method doFilter.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest r = (HttpServletRequest) request;
    if (r.getAttribute(ContextAdapter.SECURITY_SESSION_KEY) == null) {
        Cookie[] cookies = r.getCookies();
        String handle = null;
        String password = null;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("oj_handle")) {
                    handle = cookie.getValue();
                }
                if (cookie.getName().equals("oj_password")) {
                    password = cookie.getValue();
                }
            }
        }
        if (handle != null && password != null) {
            try {
                UserPersistence userPersistence = PersistenceManager.getInstance().getUserPersistence();
                UserProfile profile = userPersistence.login(handle, password);
                if (profile != null && profile.isActive()) {
                    AuthorizationPersistence authorizationPersistence = PersistenceManager.getInstance().getAuthorizationPersistence();
                    // get UserSecurity
                    UserSecurity security = authorizationPersistence.getUserSecurity(profile.getId());
                    // get UserPreference
                    UserPreference perference = userPersistence.getUserPreference(profile.getId());
                    r.getSession().setAttribute(ContextAdapter.USER_PROFILE_SESSION_KEY, profile);
                    r.getSession().setAttribute(ContextAdapter.SECURITY_SESSION_KEY, security);
                    r.getSession().setAttribute(ContextAdapter.PREFERENCE_SESSION_KEY, perference);
                } else {
                    Cookie ch = new Cookie("oj_handle", "");
                    ch.setMaxAge(0);
                    ch.setPath("/");
                    ((HttpServletResponse) response).addCookie(ch);
                    Cookie cp = new Cookie("oj_password", "");
                    cp.setMaxAge(0);
                    cp.setPath("/");
                    ((HttpServletResponse) response).addCookie(cp);
                }
            } catch (Exception e) {
                throw new ServletException("failed to auth with cookie.", e);
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) ServletException(javax.servlet.ServletException) UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) AuthorizationPersistence(cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence) HttpServletResponse(javax.servlet.http.HttpServletResponse) UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference) UserPersistence(cn.edu.zju.acm.onlinejudge.persistence.UserPersistence) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 18 with UserProfile

use of cn.edu.zju.acm.onlinejudge.bean.UserProfile in project zoj by licheng.

the class SubmissionPersistenceImpl method getProblemsetRankList.

public ProblemsetRankList getProblemsetRankList(long contestId, int offset, int count, String sort) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        String sql = null;
        if (sort.equalsIgnoreCase("submit")) {
            sql = "SELECT u.user_profile_id, u.handle, u.nickname, up.plan, ua.solved, ua.tiebreak " + "FROM user_ac ua " + "LEFT JOIN user_profile u ON ua.user_profile_id = u.user_profile_id " + "LEFT JOIN user_preference up ON ua.user_profile_id = up.user_profile_id " + "WHERE contest_id=? ORDER BY ua.tiebreak DESC, ua.solved DESC " + "LIMIT " + offset + "," + count;
        } else {
            sql = "SELECT u.user_profile_id, u.handle, u.nickname, up.plan, ua.solved, ua.tiebreak " + "FROM user_ac ua " + "LEFT JOIN user_profile u ON ua.user_profile_id = u.user_profile_id " + "LEFT JOIN user_preference up ON ua.user_profile_id = up.user_profile_id " + "WHERE contest_id=? ORDER BY ua.solved DESC, ua.tiebreak ASC " + "LIMIT " + offset + "," + count;
        }
        List<UserProfile> users = new ArrayList<UserProfile>();
        List<Integer> solved = new ArrayList<Integer>();
        List<Integer> total = new ArrayList<Integer>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setLong(1, contestId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                UserProfile user = new UserProfile();
                user.setId(rs.getLong(1));
                user.setHandle(rs.getString(2));
                user.setNickName(rs.getString(3));
                user.setDeclaration(rs.getString(4));
                users.add(user);
                solved.add(rs.getInt(5));
                total.add(rs.getInt(6));
            }
        } finally {
            Database.dispose(ps);
        }
        int[] solvedArray = new int[solved.size()];
        int[] totalArray = new int[solved.size()];
        for (int i = 0; i < solvedArray.length; ++i) {
            solvedArray[i] = solved.get(i);
            totalArray[i] = total.get(i);
        }
        ProblemsetRankList r = new ProblemsetRankList(offset, count);
        r.setUsers(users.toArray(new UserProfile[0]));
        r.setSolved(solvedArray);
        r.setTotal(totalArray);
        return r;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the rank list", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) ProblemsetRankList(cn.edu.zju.acm.onlinejudge.util.ProblemsetRankList)

Example 19 with UserProfile

use of cn.edu.zju.acm.onlinejudge.bean.UserProfile in project zoj by licheng.

the class ProfileForm method toUserProfile.

/**
 * Converts the form bean to UserProfile bean.
 *
 * @return the UserProfile bean.
 * @throws PersistenceException
 *             if failed to convert
 */
public UserProfile toUserProfile() throws PersistenceException {
    UserProfile profile = new UserProfile();
    if (this.nick != null && this.nick.trim().length() > 0) {
        profile.setNickName(this.nick);
    } else {
        profile.setNickName(this.handle);
    }
    profile.setAddressLine1(this.addressLine1);
    profile.setAddressLine2(this.addressLine2);
    profile.setBirthDate(this.parseDate(this.birthday));
    profile.setCity(this.city);
    profile.setCountry(PersistenceManager.getInstance().getCountry(this.country));
    profile.setEmail(this.email.trim());
    profile.setFirstName(this.firstName.trim());
    profile.setLastName(this.lastName.trim());
    profile.setGender(this.gender.charAt(0));
    profile.setGraduateStudent(this.graduateStudent);
    profile.setGraduationYear(this.graduationYear == null || this.graduationYear.trim().length() == 0 ? 0 : Integer.parseInt(this.graduationYear));
    profile.setHandle(this.handle.trim());
    profile.setMajor(this.major);
    profile.setPhoneNumber(this.phone);
    profile.setSchool(this.school);
    profile.setState(this.state);
    profile.setStudentNumber(this.studentNumber);
    profile.setZipCode(this.zipCode);
    profile.setPassword(this.newPassword);
    return profile;
}
Also used : UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile)

Example 20 with UserProfile

use of cn.edu.zju.acm.onlinejudge.bean.UserProfile in project zoj by licheng.

the class TestSubmitAction method execute.

/**
 * SubmitAction.
 *
 * @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 (!this.isLogin(context, true)) {
        return this.handleSuccess(mapping, context, "login");
    }
    boolean isProblemset = context.getRequest().getRequestURI().endsWith("submit.do");
    ActionForward forward = this.checkProblemParticipatePermission(mapping, context, isProblemset);
    if (forward != null) {
        return forward;
    }
    AbstractContest contest = context.getContest();
    Problem problem = context.getProblem();
    long languageId = Utility.parseLong(context.getRequest().getParameter("languageId"));
    Language language = PersistenceManager.getInstance().getLanguagePersistence().getLanguage(languageId);
    if (language == null) {
        return this.handleSuccess(mapping, context, "submit");
    }
    String source = context.getRequest().getParameter("source");
    if (source == null || source.length() == 0) {
        return this.handleSuccess(mapping, context, "submit");
    }
    List refrance = PersistenceManager.getInstance().getReferencePersistence().getProblemReferences(problem.getId(), ReferenceType.HEADER);
    if (refrance.size() != 0) {
        Reference r = (Reference) refrance.get(0);
        String percode = new String(r.getContent());
        source = percode + "\n" + source;
    }
    UserProfile user = context.getUserProfile();
    if (submitCache != null && submitCache.contains(user.getId())) {
        ActionMessages messages = new ActionMessages();
        messages.add("message", new ActionMessage("onlinejudge.submit.interval"));
        this.saveErrors(context.getRequest(), messages);
        context.setAttribute("source", source);
        return handleSuccess(mapping, context, "submit");
    }
    if (contest.isCheckIp()) {
        forward = this.checkLastLoginIP(mapping, context, isProblemset);
        if (forward != null) {
            return forward;
        }
    }
    Submission submission = new Submission();
    submission.setContestId(contest.getId());
    submission.setLanguage(language);
    submission.setProblemId(problem.getId());
    submission.setUserProfileId(user.getId());
    submission.setContent(source);
    submission.setMemoryConsumption(0);
    submission.setTimeConsumption(0);
    submission.setSubmitDate(new Date());
    SubmissionPersistence submissionPersistence = PersistenceManager.getInstance().getSubmissionPersistence();
    if (contest.getEndTime() != null && new Date().after(contest.getEndTime())) {
        submission.setJudgeReply(JudgeReply.OUT_OF_CONTEST_TIME);
        submissionPersistence.createSubmission(submission, user.getId());
    } else if (source.getBytes().length > problem.getLimit().getSubmissionLimit() * 1024) {
        submission.setContent(source.substring(0, problem.getLimit().getSubmissionLimit() * 1024));
        submission.setJudgeReply(JudgeReply.SUBMISSION_LIMIT_EXCEEDED);
        submissionPersistence.createSubmission(submission, user.getId());
    } else {
        Random ran = new Random();
        submission.setJudgeReply(ran.nextInt() % 2 == 0 ? JudgeReply.WRONG_ANSWER : JudgeReply.ACCEPTED);
        submissionPersistence.createSubmission(submission, user.getId());
        JudgeService.getInstance().judge(submission, Priority.NORMAL);
    }
    context.setAttribute("contestOrder", submission.getContestOrder());
    if (submitCache != null) {
        submitCache.put(user.getId(), user.getId());
    }
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) SubmissionPersistence(cn.edu.zju.acm.onlinejudge.persistence.SubmissionPersistence) ActionForward(org.apache.struts.action.ActionForward) Date(java.util.Date) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) Random(java.util.Random) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) List(java.util.List)

Aggregations

UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)32 ActionForward (org.apache.struts.action.ActionForward)9 UserPersistence (cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)7 ActionMessage (org.apache.struts.action.ActionMessage)6 ActionMessages (org.apache.struts.action.ActionMessages)6 UserPreference (cn.edu.zju.acm.onlinejudge.bean.UserPreference)5 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)4 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)3 Country (cn.edu.zju.acm.onlinejudge.bean.enumeration.Country)3 AuthorizationPersistence (cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence)3 UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)3 List (java.util.List)3