Search in sources :

Example 56 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class CoursesLogic method createCourseAndInstructor.

/**
 * Creates a Course object and an Instructor object for the Course.
 */
public void createCourseAndInstructor(String instructorGoogleId, String courseId, String courseName, String courseTimeZone) throws InvalidParametersException, EntityAlreadyExistsException {
    AccountAttributes courseCreator = accountsLogic.getAccount(instructorGoogleId);
    Assumption.assertNotNull("Trying to create a course for a non-existent instructor :" + instructorGoogleId, courseCreator);
    Assumption.assertTrue("Trying to create a course for a person who doesn't have instructor privileges :" + instructorGoogleId, courseCreator.isInstructor);
    createCourse(courseId, courseName, courseTimeZone);
    /* Create the initial instructor for the course */
    InstructorPrivileges privileges = new InstructorPrivileges(Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER);
    InstructorAttributes instructor = InstructorAttributes.builder(instructorGoogleId, courseId, courseCreator.name, courseCreator.email).withPrivileges(privileges).build();
    try {
        instructorsLogic.createInstructor(instructor);
    } catch (EntityAlreadyExistsException | InvalidParametersException e) {
        // roll back the transaction
        coursesDb.deleteCourse(courseId);
        String errorMessage = "Unexpected exception while trying to create instructor for a new course " + System.lineSeparator() + instructor.toString() + System.lineSeparator() + TeammatesException.toStringWithStackTrace(e);
        Assumption.fail(errorMessage);
    }
}
Also used : AccountAttributes(teammates.common.datatransfer.attributes.AccountAttributes) EntityAlreadyExistsException(teammates.common.exception.EntityAlreadyExistsException) InvalidParametersException(teammates.common.exception.InvalidParametersException) InstructorPrivileges(teammates.common.datatransfer.InstructorPrivileges) InstructorAttributes(teammates.common.datatransfer.attributes.InstructorAttributes)

Example 57 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class StringHelper method decrypt.

/*
     * Decrypts the supplied string.
     *
     * @param message the ciphertext as a hexadecimal string
     * @return the plaintext
     * @throws InvalidParameterException if the ciphertext is invalid.
     * @throws RuntimeException if the decryption fails for any other reason, such as {@code Cipher} initialization failure.
     */
public static String decrypt(String message) throws InvalidParametersException {
    try {
        SecretKeySpec sks = new SecretKeySpec(hexStringToByteArray(Config.ENCRYPTION_KEY), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, sks);
        byte[] decrypted = cipher.doFinal(hexStringToByteArray(message));
        return new String(decrypted);
    } catch (NumberFormatException | IllegalBlockSizeException | BadPaddingException e) {
        log.warning("Attempted to decrypt invalid ciphertext: " + message);
        throw new InvalidParametersException(e);
    } catch (Exception e) {
        Assumption.fail(TeammatesException.toStringWithStackTrace(e));
        return null;
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) InvalidParametersException(teammates.common.exception.InvalidParametersException) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) TeammatesException(teammates.common.exception.TeammatesException) InvalidParametersException(teammates.common.exception.InvalidParametersException)

Example 58 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class InstructorFeedbackEditCopyAction method execute.

@Override
protected ActionResult execute() throws EntityDoesNotExistException {
    String newFeedbackSessionName = getRequestParamValue(Const.ParamsNames.COPIED_FEEDBACK_SESSION_NAME);
    String[] coursesIdToCopyTo = getRequestParamValues(Const.ParamsNames.COPIED_COURSES_ID);
    String originalFeedbackSessionName = getRequestParamValue(Const.ParamsNames.FEEDBACK_SESSION_NAME);
    String originalCourseId = getRequestParamValue(Const.ParamsNames.COURSE_ID);
    String nextUrl = getRequestParamValue(Const.ParamsNames.NEXT_URL);
    Assumption.assertPostParamNotNull(Const.ParamsNames.COURSE_ID, originalCourseId);
    Assumption.assertPostParamNotNull(Const.ParamsNames.FEEDBACK_SESSION_NAME, originalFeedbackSessionName);
    Assumption.assertPostParamNotNull(Const.ParamsNames.COPIED_FEEDBACK_SESSION_NAME, newFeedbackSessionName);
    if (nextUrl == null) {
        nextUrl = Const.ActionURIs.INSTRUCTOR_FEEDBACK_SESSIONS_PAGE;
    }
    if (coursesIdToCopyTo == null || coursesIdToCopyTo.length == 0) {
        return createAjaxResultWithErrorMessage(Const.StatusMessages.FEEDBACK_SESSION_COPY_NONESELECTED);
    }
    InstructorAttributes instructor = logic.getInstructorForGoogleId(originalCourseId, account.googleId);
    FeedbackSessionAttributes fsa = logic.getFeedbackSession(originalFeedbackSessionName, originalCourseId);
    gateKeeper.verifyAccessible(instructor, logic.getCourse(originalCourseId), Const.ParamsNames.INSTRUCTOR_PERMISSION_VIEW_SESSION_IN_SECTIONS);
    gateKeeper.verifyAccessible(instructor, fsa, false);
    try {
        // Check if there are no conflicting feedback sessions in all the courses
        List<String> conflictCourses = filterConflictsInCourses(newFeedbackSessionName, coursesIdToCopyTo);
        if (!conflictCourses.isEmpty()) {
            String commaSeparatedListOfCourses = StringHelper.toString(conflictCourses, ",");
            String errorToUser = String.format(Const.StatusMessages.FEEDBACK_SESSION_COPY_ALREADYEXISTS, newFeedbackSessionName, commaSeparatedListOfCourses);
            return createAjaxResultWithErrorMessage(errorToUser);
        }
        FeedbackSessionAttributes fs = null;
        // TODO: consider doing this as a batch insert
        for (String courseIdToCopyTo : coursesIdToCopyTo) {
            InstructorAttributes instructorForCourse = logic.getInstructorForGoogleId(courseIdToCopyTo, account.googleId);
            gateKeeper.verifyAccessible(instructorForCourse, logic.getCourse(courseIdToCopyTo), Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION);
            fs = logic.copyFeedbackSession(newFeedbackSessionName, courseIdToCopyTo, originalFeedbackSessionName, originalCourseId, instructor.email);
        }
        List<String> courses = Arrays.asList(coursesIdToCopyTo);
        String commaSeparatedListOfCourses = StringHelper.toString(courses, ",");
        statusToUser.add(new StatusMessage(Const.StatusMessages.FEEDBACK_SESSION_COPIED, StatusMessageColor.SUCCESS));
        statusToAdmin = "Copying to multiple feedback sessions.<br>" + "New Feedback Session <span class=\"bold\">(" + fs.getFeedbackSessionName() + ")</span> " + "for Courses: <br>" + commaSeparatedListOfCourses + "<br>" + "<span class=\"bold\">From:</span> " + fs.getStartTime() + "<span class=\"bold\"> to</span> " + fs.getEndTime() + "<br>" + "<span class=\"bold\">Session visible from:</span> " + fs.getSessionVisibleFromTime() + "<br>" + "<span class=\"bold\">Results visible from:</span> " + fs.getResultsVisibleFromTime() + "<br><br>" + "<span class=\"bold\">Instructions:</span> " + fs.getInstructions() + "<br>" + "Copied from <span class=\"bold\">(" + originalFeedbackSessionName + ")</span> for Course " + "<span class=\"bold\">[" + originalCourseId + "]</span> created.<br>";
        // so that the instructor can see the new feedback sessions
        return createAjaxResultWithoutClearingStatusMessage(new InstructorFeedbackEditCopyData(account, sessionToken, Config.getAppUrl(nextUrl).withParam(Const.ParamsNames.ERROR, Boolean.FALSE.toString()).withParam(Const.ParamsNames.USER_ID, account.googleId)));
    } catch (EntityAlreadyExistsException e) {
        // If conflicts are checked above, this will only occur via race condition
        setStatusForException(e, Const.StatusMessages.FEEDBACK_SESSION_EXISTS);
        return createAjaxResultWithErrorMessage(Const.StatusMessages.FEEDBACK_SESSION_EXISTS);
    } catch (InvalidParametersException e) {
        setStatusForException(e);
        return createAjaxResultWithErrorMessage(e.getMessage());
    }
}
Also used : FeedbackSessionAttributes(teammates.common.datatransfer.attributes.FeedbackSessionAttributes) EntityAlreadyExistsException(teammates.common.exception.EntityAlreadyExistsException) InstructorFeedbackEditCopyData(teammates.ui.pagedata.InstructorFeedbackEditCopyData) InvalidParametersException(teammates.common.exception.InvalidParametersException) InstructorAttributes(teammates.common.datatransfer.attributes.InstructorAttributes) StatusMessage(teammates.common.util.StatusMessage)

Example 59 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class InstructorFeedbackQuestionCopyAction method execute.

@Override
protected ActionResult execute() {
    String courseId = getRequestParamValue(Const.ParamsNames.COURSE_ID);
    String feedbackSessionName = getRequestParamValue(Const.ParamsNames.FEEDBACK_SESSION_NAME);
    InstructorAttributes instructorDetailForCourse = logic.getInstructorForGoogleId(courseId, account.googleId);
    gateKeeper.verifyAccessible(instructorDetailForCourse, logic.getFeedbackSession(feedbackSessionName, courseId), false, Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION);
    String instructorEmail = instructorDetailForCourse.email;
    try {
        int index = 0;
        String feedbackQuestionId = getRequestParamValue(Const.ParamsNames.FEEDBACK_QUESTION_ID + "-" + index);
        statusToAdmin = "";
        while (feedbackQuestionId != null) {
            FeedbackQuestionAttributes feedbackQuestion = logic.copyFeedbackQuestion(feedbackQuestionId, feedbackSessionName, courseId, instructorEmail);
            index++;
            feedbackQuestionId = getRequestParamValue(Const.ParamsNames.FEEDBACK_QUESTION_ID + "-" + index);
            statusToAdmin += "Created Feedback Question for Feedback Session:<span class=\"bold\">(" + feedbackQuestion.feedbackSessionName + ")</span> for Course <span class=\"bold\">[" + feedbackQuestion.courseId + "]</span> created.<br>" + "<span class=\"bold\">" + feedbackQuestion.getQuestionDetails().getQuestionTypeDisplayName() + ":</span> " + SanitizationHelper.sanitizeForHtml(feedbackQuestion.getQuestionDetails().getQuestionText());
        }
        if (index > 0) {
            statusToUser.add(new StatusMessage(Const.StatusMessages.FEEDBACK_QUESTION_ADDED, StatusMessageColor.SUCCESS));
        } else {
            statusToUser.add(new StatusMessage("No questions are indicated to be copied", StatusMessageColor.DANGER));
            isError = true;
        }
    } catch (InvalidParametersException e) {
        // This part is not tested because GateKeeper handles if this happens, would be
        // extremely difficult to replicate a situation whereby it gets past GateKeeper
        statusToUser.add(new StatusMessage(e.getMessage(), StatusMessageColor.DANGER));
        statusToAdmin = e.getMessage();
        isError = true;
    }
    return createRedirectResult(new PageData(account, sessionToken).getInstructorFeedbackEditLink(courseId, feedbackSessionName));
}
Also used : PageData(teammates.ui.pagedata.PageData) FeedbackQuestionAttributes(teammates.common.datatransfer.attributes.FeedbackQuestionAttributes) InvalidParametersException(teammates.common.exception.InvalidParametersException) InstructorAttributes(teammates.common.datatransfer.attributes.InstructorAttributes) StatusMessage(teammates.common.util.StatusMessage)

Example 60 with InvalidParametersException

use of teammates.common.exception.InvalidParametersException in project teammates by TEAMMATES.

the class InstructorFeedbackQuestionEditAction method execute.

@Override
protected ActionResult execute() throws EntityDoesNotExistException {
    String courseId = getNonNullRequestParamValue(Const.ParamsNames.COURSE_ID);
    String feedbackSessionName = getNonNullRequestParamValue(Const.ParamsNames.FEEDBACK_SESSION_NAME);
    gateKeeper.verifyAccessible(logic.getInstructorForGoogleId(courseId, account.googleId), logic.getFeedbackSession(feedbackSessionName, courseId), false, Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION);
    String editType = getNonNullRequestParamValue(Const.ParamsNames.FEEDBACK_QUESTION_EDITTYPE);
    try {
        if ("edit".equals(editType)) {
            String questionText = getNonNullRequestParamValue(Const.ParamsNames.FEEDBACK_QUESTION_TEXT);
            Assumption.assertNotEmpty("Empty question text", questionText);
            FeedbackQuestionAttributes updatedQuestion = extractFeedbackQuestionData();
            editQuestion(updatedQuestion);
        } else if ("delete".equals(editType)) {
            // branch not tested because if it's not edit or delete, Assumption.fail will cause test failure
            deleteQuestion();
        } else {
            // Assumption.fails are not tested
            Assumption.fail("Invalid editType");
        }
    } catch (InvalidParametersException e) {
        // This part is not tested because GateKeeper handles if this happens, would be
        // extremely difficult to replicate a situation whereby it gets past GateKeeper
        setStatusForException(e);
    }
    return createRedirectResult(new PageData(account, sessionToken).getInstructorFeedbackEditLink(courseId, feedbackSessionName));
}
Also used : PageData(teammates.ui.pagedata.PageData) FeedbackQuestionAttributes(teammates.common.datatransfer.attributes.FeedbackQuestionAttributes) InvalidParametersException(teammates.common.exception.InvalidParametersException)

Aggregations

InvalidParametersException (teammates.common.exception.InvalidParametersException)83 EntityDoesNotExistException (teammates.common.exception.EntityDoesNotExistException)37 InstructorAttributes (teammates.common.datatransfer.attributes.InstructorAttributes)24 StatusMessage (teammates.common.util.StatusMessage)21 Test (org.testng.annotations.Test)19 EntityAlreadyExistsException (teammates.common.exception.EntityAlreadyExistsException)19 FeedbackSessionAttributes (teammates.common.datatransfer.attributes.FeedbackSessionAttributes)13 StudentAttributes (teammates.common.datatransfer.attributes.StudentAttributes)12 CourseAttributes (teammates.common.datatransfer.attributes.CourseAttributes)9 FeedbackSession (teammates.storage.entity.FeedbackSession)9 Text (com.google.appengine.api.datastore.Text)8 FeedbackQuestionAttributes (teammates.common.datatransfer.attributes.FeedbackQuestionAttributes)8 AccountAttributes (teammates.common.datatransfer.attributes.AccountAttributes)6 FeedbackResponseAttributes (teammates.common.datatransfer.attributes.FeedbackResponseAttributes)6 ArrayList (java.util.ArrayList)5 InstructorPrivileges (teammates.common.datatransfer.InstructorPrivileges)5 VoidWork (com.googlecode.objectify.VoidWork)4 StudentProfileAttributes (teammates.common.datatransfer.attributes.StudentProfileAttributes)4 PageData (teammates.ui.pagedata.PageData)4 FeedbackResponseCommentAttributes (teammates.common.datatransfer.attributes.FeedbackResponseCommentAttributes)3