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