Search in sources :

Example 1 with UserPreference

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

the class UserPersistenceImplTest method testGetUserPreference.

/**
 * Tests getGetUserPreference method
 * @throws Exception to JUnit
 */
public void testGetUserPreference() throws Exception {
    persistence.createUserProfile(profile, 1);
    perference.setId(profile.getId());
    persistence.createUserPreference(perference, 1);
    UserPreference perference1 = persistence.getUserPreference(perference.getId());
    checkUserPreference(perference, perference1);
}
Also used : UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference)

Example 2 with UserPreference

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

the class ShowUserStatusAction method execute.

/**
 * Method execute
 *
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    UserProfile user = null;
    String handle = context.getRequest().getParameter("handle");
    if (handle != null && handle.length() > 0) {
        // TODO cache?
        user = PersistenceManager.getInstance().getUserPersistence().getUserProfileByHandle(handle);
    } else if (context.getRequest().getParameter("userId") != null) {
        long userId = Utility.parseLong(context.getRequest().getParameter("userId"));
        if (userId != -1) {
            user = PersistenceManager.getInstance().getUserPersistence().getUserProfile(userId);
        }
    } else {
        user = context.getUserProfile();
    }
    AbstractContest contest = null;
    if (user != null) {
        long contestId = Utility.parseLong(context.getRequest().getParameter("contestId"));
        if (contestId == -1) {
            contestId = ShowUserStatusAction.defaultProblemSetId;
        }
        contest = ContestManager.getInstance().getContest(contestId);
    }
    if (contest != null) {
        context.setAttribute("contest", contest);
        ActionForward forward = this.checkContestViewPermission(mapping, context, null, true);
        if (forward != null) {
            contest = null;
        }
    }
    UserStatistics statistics = null;
    UserPreference pref = null;
    if (contest != null && user != null) {
        // TODO cache?
        pref = PersistenceManager.getInstance().getUserPersistence().getUserPreference(user.getId());
        statistics = StatisticsManager.getInstance().getUserStatistics(contest.getId(), user.getId());
    }
    context.setAttribute("user", user);
    context.setAttribute("preference", pref);
    context.setAttribute("contest", contest);
    context.setAttribute("UserStatistics", statistics);
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserStatistics(cn.edu.zju.acm.onlinejudge.util.UserStatistics) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference) ActionForward(org.apache.struts.action.ActionForward)

Example 3 with UserPreference

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

the class EditProfileAction method execute.

/**
 * Edit Profile.
 *
 * @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.editProfile()) {
        context.getResponse().sendError(404);
        return null;
    }
    if (!this.isLogin(context)) {
        return this.handleSuccess(mapping, context, "login");
    }
    UserPersistence userPersistence = PersistenceManager.getInstance().getUserPersistence();
    ProfileForm profileForm = (ProfileForm) form;
    UserProfile profile = context.getUserProfile();
    UserPreference perference = userPersistence.getUserPreference(profile.getId());
    if (profileForm.getHandle() == null) {
        profileForm.populate(profile, perference);
        context.setAttribute("ProfileForm", profileForm);
        return this.handleSuccess(mapping, context, "failure");
    }
    if (userPersistence.login(profileForm.getHandle(), profileForm.getPassword()) == null) {
        return this.handleFailure(mapping, context, "password", "ProfileForm.password.invalid");
    }
    UserProfile newProfile = profileForm.toUserProfile();
    newProfile.setId(profile.getId());
    newProfile.setRegDate(profile.getRegDate());
    if (!profile.getHandle().equals(newProfile.getHandle())) {
        return this.handleFailure(mapping, context, "handle", "ProfileForm.handle.changed");
    }
    if (!profile.getEmail().equals(newProfile.getEmail())) {
        UserProfile temp = userPersistence.getUserProfileByEmail(newProfile.getEmail());
        if (temp != null && temp.getId() != profile.getId()) {
            return this.handleFailure(mapping, context, "email", "ProfileForm.email.used");
        }
    }
    userPersistence.updateUserProfile(newProfile, profile.getId());
    UserPreference newPerference = profileForm.toUserPreference();
    newPerference.setId(profile.getId());
    userPersistence.updateUserPreference(newPerference, profile.getId());
    context.setUserProfile(newProfile);
    context.getRequest().setAttribute("Countries", PersistenceManager.getInstance().getUserPersistence().getAllCountries());
    ActionMessages messages = new ActionMessages();
    messages.add("message", new ActionMessage("onlinejudge.editProfile.success"));
    this.saveErrors(context.getRequest(), messages);
    context.setAttribute("back", "");
    return this.handleSuccess(mapping, context, "success");
}
Also used : 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) UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference) UserPersistence(cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)

Example 4 with UserPreference

use of cn.edu.zju.acm.onlinejudge.bean.UserPreference 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 5 with UserPreference

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

Aggregations

UserPreference (cn.edu.zju.acm.onlinejudge.bean.UserPreference)10 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)5 UserPersistence (cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)4 AuthorizationPersistence (cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence)3 UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)3 ActionMessage (org.apache.struts.action.ActionMessage)3 ActionMessages (org.apache.struts.action.ActionMessages)3 ProfileForm (cn.edu.zju.acm.onlinejudge.form.ProfileForm)2 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)1 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)1 UserStatistics (cn.edu.zju.acm.onlinejudge.util.UserStatistics)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 ServletException (javax.servlet.ServletException)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1