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);
}
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");
}
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");
}
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;
}
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);
}
Aggregations