Esempio n. 1
0
    def put(self):
        """Handles POST requests."""
        update_type = self.payload.get('update_type')
        data = self.payload.get('data')

        if update_type == 'user_bio':
            user_services.update_user_bio(self.user_id, data)
        elif update_type == 'subject_interests':
            user_services.update_subject_interests(self.user_id, data)
        elif update_type == 'preferred_language_codes':
            user_services.update_preferred_language_codes(self.user_id, data)
        elif update_type == 'preferred_site_language_code':
            user_services.update_preferred_site_language_code(
                self.user_id, data)
        elif update_type == 'profile_picture_data_url':
            user_services.update_profile_picture_data_url(self.user_id, data)
        elif update_type == 'email_preferences':
            user_services.update_email_preferences(
                self.user_id, data['can_receive_email_updates'],
                data['can_receive_editor_role_email'],
                data['can_receive_feedback_message_email'])
        else:
            raise self.InvalidInputException(
                'Invalid update type: %s' % update_type)

        self.render_json({})
Esempio n. 2
0
    def test_set_and_get_user_email_preferences(self):
        user_id = 'someUser'
        username = '******'
        user_email = '*****@*****.**'

        user_services.get_or_create_user(user_id, user_email)
        user_services.set_username(user_id, username)

        # When UserEmailPreferencesModel is yet to be created,
        # the value returned by get_email_preferences() should be True.
        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEquals(email_preferences['can_receive_editor_role_email'],
                          feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE)

        # The user retrieves their email preferences. This initializes
        # a UserEmailPreferencesModel instance with the default values.
        user_services.update_email_preferences(
            user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE,
            feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE)

        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEquals(email_preferences['can_receive_editor_role_email'],
                          feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE)

        # The user sets their membership email preference to False.
        user_services.update_email_preferences(
            user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE, False)

        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEquals(email_preferences['can_receive_editor_role_email'],
                          False)
Esempio n. 3
0
    def post(self, _):
        """Handles POST requests."""
        if self.request.get('data[list_id]') != feconf.MAILCHIMP_AUDIENCE_ID:
            self.render_json({})
            return

        email = self.request.get('data[email]')
        user_settings = user_services.get_user_settings_from_email(email)

        # Ignore the request if the user does not exist in Oppia.
        if user_settings is None:
            self.render_json({})
            return

        user_id = user_settings.user_id
        user_email_preferences = user_services.get_email_preferences(user_id)
        if self.request.get('type') == 'subscribe':
            user_services.update_email_preferences(
                user_id,
                True,
                user_email_preferences.can_receive_editor_role_email,
                user_email_preferences.can_receive_feedback_message_email,
                user_email_preferences.can_receive_subscription_email,
                bulk_email_db_already_updated=True)
        elif self.request.get('type') == 'unsubscribe':
            user_services.update_email_preferences(
                user_id,
                False,
                user_email_preferences.can_receive_editor_role_email,
                user_email_preferences.can_receive_feedback_message_email,
                user_email_preferences.can_receive_subscription_email,
                bulk_email_db_already_updated=True)
        self.render_json({})
Esempio n. 4
0
    def put(self):
        """Handles POST requests."""
        update_type = self.payload.get('update_type')
        data = self.payload.get('data')

        if update_type == 'user_bio':
            user_services.update_user_bio(self.user_id, data)
        elif update_type == 'subject_interests':
            user_services.update_subject_interests(self.user_id, data)
        elif update_type == 'preferred_language_codes':
            user_services.update_preferred_language_codes(self.user_id, data)
        elif update_type == 'preferred_site_language_code':
            user_services.update_preferred_site_language_code(
                self.user_id, data)
        elif update_type == 'profile_picture_data_url':
            user_services.update_profile_picture_data_url(self.user_id, data)
        elif update_type == 'email_preferences':
            user_services.update_email_preferences(
                self.user_id, data['can_receive_email_updates'],
                data['can_receive_editor_role_email'],
                data['can_receive_feedback_message_email'])
        else:
            raise self.InvalidInputException('Invalid update type: %s' %
                                             update_type)

        self.render_json({})
Esempio n. 5
0
    def setUp(self):
        super(EmailDashboardResultTests, self).setUp()
        # User A has one created exploration.
        self.signup(self.USER_A_EMAIL, self.USER_A_USERNAME)
        self.signup(feconf.SYSTEM_EMAIL_ADDRESS, 'systemUser')
        self.user_a_id = self.get_user_id_from_email(self.USER_A_EMAIL)
        user_services.update_email_preferences(self.user_a_id, True, True,
                                               True, True)
        self.save_new_valid_exploration(self.EXP_ID_1,
                                        self.user_a_id,
                                        end_state_name='End')
        # User B has one created exploration.
        self.signup(self.USER_B_EMAIL, self.USER_B_USERNAME)
        self.user_b_id = self.get_user_id_from_email(self.USER_B_EMAIL)
        user_services.update_email_preferences(self.user_b_id, True, True,
                                               True, True)
        self.save_new_valid_exploration(self.EXP_ID_2,
                                        self.user_b_id,
                                        end_state_name='End')

        # Submitter and new_submitter are submitter of query.
        self.signup(self.SUBMITTER_EMAIL, self.SUBMITTER_USERNAME)
        self.submitter_id = self.get_user_id_from_email(self.SUBMITTER_EMAIL)
        self.signup(self.NEW_SUBMITTER_EMAIL, self.NEW_SUBMITTER_USERNAME)
        self.new_submitter_id = self.get_user_id_from_email(
            self.NEW_SUBMITTER_EMAIL)
        self.set_curriculum_admins(
            [self.SUBMITTER_USERNAME, self.NEW_SUBMITTER_USERNAME])
Esempio n. 6
0
    def test_set_and_get_user_email_preferences(self):
        user_id = 'someUser'
        username = '******'
        user_email = '*****@*****.**'

        user_services.get_or_create_user(user_id, user_email)
        user_services.set_username(user_id, username)

        # When UserEmailPreferencesModel is yet to be created,
        # the value returned by get_email_preferences() should be True.
        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEquals(
            email_preferences['can_receive_editor_role_email'],
            feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE)

        # The user retrieves their email preferences. This initializes
        # a UserEmailPreferencesModel instance with the default values.
        user_services.update_email_preferences(
            user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE,
            feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE)

        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEquals(
            email_preferences['can_receive_editor_role_email'],
            feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE)

        # The user sets their membership email preference to False.
        user_services.update_email_preferences(
            user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE, False)

        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEquals(
            email_preferences['can_receive_editor_role_email'],
            False)
Esempio n. 7
0
    def post(self):
        """Handles POST requests."""
        username = self.payload.get("username")
        agreed_to_terms = self.payload.get("agreed_to_terms")
        can_receive_email_updates = self.payload.get("can_receive_email_updates")

        has_ever_registered = user_services.has_ever_registered(self.user_id)
        has_fully_registered = user_services.has_fully_registered(self.user_id)

        if has_fully_registered:
            self.render_json({})
            return

        if not isinstance(agreed_to_terms, bool) or not agreed_to_terms:
            raise self.InvalidInputException(
                "In order to edit explorations on this site, you will " "need to accept the license terms."
            )
        else:
            user_services.record_agreement_to_terms(self.user_id)

        if not user_services.get_username(self.user_id):
            try:
                user_services.set_username(self.user_id, username)
            except utils.ValidationError as e:
                raise self.InvalidInputException(e)

        if can_receive_email_updates is not None:
            user_services.update_email_preferences(self.user_id, can_receive_email_updates)

        # Note that an email is only sent when the user registers for the first
        # time.
        if feconf.CAN_SEND_EMAILS_TO_USERS and not has_ever_registered:
            email_manager.send_post_signup_email(self.user_id)

        self.render_json({})
Esempio n. 8
0
    def setUp(self):
        super(CronMailReviewersContributorDashboardSuggestionsHandlerTests,
              self).setUp()
        self.signup(self.ADMIN_EMAIL, self.ADMIN_USERNAME)
        self.admin_id = self.get_user_id_from_email(self.ADMIN_EMAIL)
        self.set_admins([self.ADMIN_USERNAME])
        self.signup(self.AUTHOR_EMAIL, self.AUTHOR_USERNAME)
        self.author_id = self.get_user_id_from_email(self.AUTHOR_EMAIL)
        self.signup(self.REVIEWER_EMAIL, self.REVIEWER_USERNAME)
        self.reviewer_id = self.get_user_id_from_email(self.REVIEWER_EMAIL)
        user_services.update_email_preferences(self.reviewer_id, True, False,
                                               False, False)
        self.save_new_valid_exploration(self.target_id, self.author_id)
        # Give reviewer rights to review translations in the given language
        # code.
        user_services.allow_user_to_review_translation_in_language(
            self.reviewer_id, self.language_code)
        # Create a translation suggestion so that the reviewer has something
        # to be notified about.
        translation_suggestion = self._create_translation_suggestion()
        self.expected_reviewable_suggestion_email_info = (
            suggestion_services.
            create_reviewable_suggestion_email_info_from_suggestion(
                translation_suggestion))

        self.can_send_emails = self.swap(feconf, 'CAN_SEND_EMAILS', True)
        self.cannot_send_emails = self.swap(feconf, 'CAN_SEND_EMAILS', False)
        self.testapp_swap = self.swap(self, 'testapp',
                                      webtest.TestApp(main_cron.app))

        self.reviewers_suggestion_email_infos = []
        self.reviewer_ids = []
Esempio n. 9
0
def pre_delete_user(user_id):
    """Prepare user for the full deletion.
        1. Mark all the activities that are private and solely owned by the user
           being deleted as deleted.
        2. Disable all the email preferences.
        3. Mark the user as to be deleted.
        4. Create PendingDeletionRequestModel for the user.

    Args:
        user_id: str. The id of the user to be deleted. If the user_id
            corresponds to a profile user then only that profile is deleted.
            For a full user, all of its associated profile users are deleted
            too.
    """
    pending_deletion_requests = []
    user_settings = user_services.get_user_settings(user_id, strict=True)

    linked_profile_user_ids = [
        user.user_id for user in
        user_services.get_all_profiles_auth_details_by_parent_user_id(user_id)
    ]
    profile_users_settings_list = user_services.get_users_settings(
        linked_profile_user_ids)
    for profile_user_settings in profile_users_settings_list:
        profile_id = profile_user_settings.user_id
        user_services.mark_user_for_deletion(profile_id)
        pending_deletion_requests.append(
            wipeout_domain.PendingDeletionRequest.create_default(
                profile_id, profile_user_settings.email,
                profile_user_settings.role))
    if user_settings.role != feconf.ROLE_ID_LEARNER:
        taskqueue_services.defer(
            taskqueue_services.FUNCTION_ID_REMOVE_USER_FROM_RIGHTS_MODELS,
            taskqueue_services.QUEUE_NAME_ONE_OFF_JOBS,
            user_id,
        )
        # Set all the user's email preferences to False in order to disable all
        # ordinary emails that could be sent to the users.
        user_services.update_email_preferences(user_id, False, False, False,
                                               False)

    date_now = datetime.datetime.utcnow()
    date_before_which_username_should_be_saved = (
        date_now - PERIOD_AFTER_WHICH_USERNAME_CANNOT_BE_REUSED)
    user_services.mark_user_for_deletion(user_id)

    normalized_long_term_username = (
        user_settings.normalized_username if
        user_settings.created_on < date_before_which_username_should_be_saved
        else None)
    pending_deletion_requests.append(
        wipeout_domain.PendingDeletionRequest.create_default(
            user_id,
            user_settings.email,
            user_settings.role,
            normalized_long_term_username=normalized_long_term_username))

    save_pending_deletion_requests(pending_deletion_requests)
Esempio n. 10
0
    def test_email_is_not_sent_if_recipient_has_declined_such_emails(self):
        user_services.update_email_preferences(self.new_user_id, True, False)

        with self.can_send_emails_ctx, self.can_send_editor_role_email_ctx:
            email_manager.send_role_notification_email(
                self.editor_id, self.new_user_id, rights_manager.ROLE_OWNER,
                self.exploration.id, self.exploration.title)

            messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL)
            self.assertEqual(len(messages), 0)
Esempio n. 11
0
    def test_email_is_not_sent_if_recipient_has_declined_such_emails(self):
        user_services.update_email_preferences(self.new_user_id, True, False)

        with self.can_send_emails_ctx, self.can_send_editor_role_email_ctx:
            email_manager.send_role_notification_email(
                self.editor_id, self.new_user_id, rights_manager.ROLE_OWNER,
                self.exploration.id, self.exploration.title)

            messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL)
            self.assertEqual(len(messages), 0)
Esempio n. 12
0
    def setUp(self):
        super(UserEmailPreferencesModelValidatorTests, self).setUp()

        self.signup(USER_EMAIL, USER_NAME)
        self.user_id = self.get_user_id_from_email(USER_EMAIL)
        user_services.update_email_preferences(self.user_id, True, True, False,
                                               True)

        self.model_instance = user_models.UserEmailPreferencesModel.get_by_id(
            self.user_id)
        self.job_class = (prod_validation_jobs_one_off.
                          UserEmailPreferencesModelAuditOneOffJob)
Esempio n. 13
0
 def setUp(self):
     super(BulkEmailWebhookEndpointTests, self).setUp()
     self.signup(self.EDITOR_EMAIL, self.EDITOR_USERNAME)
     self.editor_id = self.get_user_id_from_email(self.EDITOR_EMAIL)
     self.swap_secret = self.swap(
         feconf, 'MAILCHIMP_WEBHOOK_SECRET', 'secret')
     self.swap_audience_id = (
         self.swap(feconf, 'MAILCHIMP_AUDIENCE_ID', 'audience_id'))
     user_services.update_email_preferences(
         self.editor_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE,
         feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE,
         feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE,
         feconf.DEFAULT_SUBSCRIPTION_EMAIL_PREFERENCE)
Esempio n. 14
0
    def post(self):
        """Handles POST requests."""
        username = self.payload.get('username')
        agreed_to_terms = self.payload.get('agreed_to_terms')
        default_dashboard = self.payload.get('default_dashboard')
        can_receive_email_updates = self.payload.get(
            'can_receive_email_updates')

        has_ever_registered = user_services.has_ever_registered(self.user_id)
        has_fully_registered_account = (
            user_services.has_fully_registered_account(self.user_id))

        if has_fully_registered_account:
            self.render_json({})
            return

        if not isinstance(agreed_to_terms, bool) or not agreed_to_terms:
            raise self.InvalidInputException(
                'In order to edit explorations on this site, you will '
                'need to accept the license terms.')
        else:
            user_services.record_agreement_to_terms(self.user_id)

        if not user_services.get_username(self.user_id):
            try:
                user_services.set_username(self.user_id, username)
            except utils.ValidationError as e:
                raise self.InvalidInputException(e)

        if can_receive_email_updates is not None:
            user_services.update_email_preferences(
                self.user_id, can_receive_email_updates,
                feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE,
                feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE,
                feconf.DEFAULT_SUBSCRIPTION_EMAIL_PREFERENCE)

        # Note that an email is only sent when the user registers for the first
        # time.
        if feconf.CAN_SEND_EMAILS and not has_ever_registered:
            email_manager.send_post_signup_email(self.user_id)

        user_services.generate_initial_profile_picture(self.user_id)

        if not has_ever_registered:
            # Set the default dashboard for new users.
            user_services.update_user_default_dashboard(
                self.user_id, default_dashboard)

        self.render_json({})
Esempio n. 15
0
    def test_email_is_not_sent_if_recipient_has_declined_such_emails(self):
        user_services.update_email_preferences(
            self.editor_id, True, False, False)

        with self.can_send_emails_ctx, self.can_send_feedback_email_ctx:
            feedback_services.create_thread(
                self.exploration.id, 'a_state_name', self.user_id_a,
                'a subject', 'some text')

            # Note: the job in the taskqueue represents the realtime
            # event emitted by create_thread().
            self.assertEqual(self.count_jobs_in_taskqueue(), 1)
            self.process_and_flush_pending_tasks()
            messages = self.mail_stub.get_sent_messages(to=self.EDITOR_EMAIL)
            self.assertEqual(len(messages), 0)
Esempio n. 16
0
    def put(self):
        """Handles POST requests."""
        update_type = self.payload.get("update_type")
        data = self.payload.get("data")

        if update_type == "user_bio":
            user_services.update_user_bio(self.user_id, data)
        elif update_type == "preferred_language_codes":
            user_services.update_preferred_language_codes(self.user_id, data)
        elif update_type == "profile_picture_data_url":
            user_services.update_profile_picture_data_url(self.user_id, data)
        elif update_type == "can_receive_email_updates":
            user_services.update_email_preferences(self.user_id, data)
        else:
            raise self.InvalidInputException("Invalid update type: %s" % update_type)
Esempio n. 17
0
    def put(self):
        """Handles POST requests."""
        update_type = self.payload.get('update_type')
        data = self.payload.get('data')

        if update_type == 'user_bio':
            user_services.update_user_bio(self.user_id, data)
        elif update_type == 'preferred_language_codes':
            user_services.update_preferred_language_codes(self.user_id, data)
        elif update_type == 'profile_picture_data_url':
            user_services.update_profile_picture_data_url(self.user_id, data)
        elif update_type == 'can_receive_email_updates':
            user_services.update_email_preferences(self.user_id, data)
        else:
            raise self.InvalidInputException(
                'Invalid update type: %s' % update_type)
Esempio n. 18
0
    def put(self):
        """Handles POST requests."""
        update_type = self.payload.get('update_type')
        data = self.payload.get('data')

        if update_type == 'user_bio':
            user_services.update_user_bio(self.user_id, data)
        elif update_type == 'preferred_language_codes':
            user_services.update_preferred_language_codes(self.user_id, data)
        elif update_type == 'profile_picture_data_url':
            user_services.update_profile_picture_data_url(self.user_id, data)
        elif update_type == 'can_receive_email_updates':
            user_services.update_email_preferences(self.user_id, data)
        else:
            raise self.InvalidInputException('Invalid update type: %s' %
                                             update_type)
Esempio n. 19
0
    def post(self):
        """Handles POST requests."""
        username = self.normalized_payload.get('username')
        agreed_to_terms = self.normalized_payload.get('agreed_to_terms')
        default_dashboard = self.normalized_payload.get('default_dashboard')
        can_receive_email_updates = self.normalized_payload.get(
            'can_receive_email_updates')
        bulk_email_signup_message_should_be_shown = False

        if can_receive_email_updates is not None:
            bulk_email_signup_message_should_be_shown = (
                user_services.update_email_preferences(
                    self.user_id, can_receive_email_updates,
                    feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE,
                    feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE,
                    feconf.DEFAULT_SUBSCRIPTION_EMAIL_PREFERENCE)
            )
            if bulk_email_signup_message_should_be_shown:
                self.render_json({
                    'bulk_email_signup_message_should_be_shown': (
                        bulk_email_signup_message_should_be_shown)
                })
                return
        # Ruling out the possibility of None for mypy type checking.
        assert self.user_id is not None
        has_ever_registered = user_services.has_ever_registered(self.user_id)
        has_fully_registered_account = (
            user_services.has_fully_registered_account(self.user_id))

        if has_fully_registered_account:
            self.render_json({})
            return

        if not agreed_to_terms:
            raise self.InvalidInputException(
                'In order to edit explorations on this site, you will '
                'need to accept the license terms.')

        user_services.record_agreement_to_terms(self.user_id)

        if not user_services.get_username(self.user_id):
            user_services.set_username(self.user_id, username)

        # Note that an email is only sent when the user registers for the first
        # time.
        if feconf.CAN_SEND_EMAILS and not has_ever_registered:
            email_manager.send_post_signup_email(self.user_id)

        user_services.generate_initial_profile_picture(self.user_id)

        if not has_ever_registered:
            # Set the default dashboard for new users.
            user_services.update_user_default_dashboard(
                self.user_id, default_dashboard)

        self.render_json({
            'bulk_email_signup_message_should_be_shown': (
                bulk_email_signup_message_should_be_shown)
        })
Esempio n. 20
0
def pre_delete_user(user_id):
    """Prepare user for the full deletion.
        1. Mark all the activities that are private and solely owned by the user
           being deleted as deleted.
        2. Disable all the email preferences.
        3. Mark the user as to be deleted.
        4. Create PendingDeletionRequestModel for the user.

    Args:
        user_id: str. The id of the user to be deleted. If the user_id
            corresponds to a profile user then only that profile is deleted.
            For a full user, all of its associated profile users are deleted
            too.
    """
    pending_deletion_requests = []
    user_settings = user_services.get_user_settings(user_id, strict=True)

    linked_profile_user_ids = [
        user.user_id for user in
        user_services.get_all_profiles_auth_details_by_parent_user_id(user_id)
    ]
    profile_users_settings_list = user_services.get_users_settings(
        linked_profile_user_ids)
    for profile_user_settings in profile_users_settings_list:
        profile_id = profile_user_settings.user_id
        user_services.mark_user_for_deletion(profile_id)
        pending_deletion_requests.append(
            wipeout_domain.PendingDeletionRequest.create_default(
                profile_id, profile_user_settings.email,
                profile_user_settings.role))
    if user_settings.role != feconf.ROLE_ID_LEARNER:
        _remove_user_from_activities_with_associated_rights_models(
            user_id, True)

        # Set all the user's email preferences to False in order to disable all
        # ordinary emails that could be sent to the users.
        user_services.update_email_preferences(user_id, False, False, False,
                                               False)

    user_services.mark_user_for_deletion(user_id)
    pending_deletion_requests.append(
        wipeout_domain.PendingDeletionRequest.create_default(
            user_id, user_settings.email, user_settings.role))

    save_pending_deletion_requests(pending_deletion_requests)
Esempio n. 21
0
def pre_delete_user(user_id):
    """Prepare user for the full deletion.
        1. Mark all the activities that are private and solely owned by the user
           being deleted as deleted.
        2. Disable all the email preferences.
        3. Mark the user as to be deleted.
        4. Create PendingDeletionRequestModel for the user.

    Args:
        user_id: str. The id of the user to be deleted.
    """
    subscribed_exploration_summaries = (
        exp_fetchers.get_exploration_summaries_subscribed_to(user_id))
    explorations_to_be_deleted_ids = [
        exp_summary.id for exp_summary in subscribed_exploration_summaries
        if exp_summary.is_private() and
        exp_summary.is_solely_owned_by_user(user_id)]
    exp_services.delete_explorations(user_id, explorations_to_be_deleted_ids)

    subscribed_collection_summaries = (
        collection_services.get_collection_summaries_subscribed_to(user_id))
    collections_to_be_deleted_ids = [
        col_summary.id for col_summary in subscribed_collection_summaries
        if col_summary.is_private() and
        col_summary.is_solely_owned_by_user(user_id)]
    collection_services.delete_collections(
        user_id, collections_to_be_deleted_ids)

    # Set all the user's email preferences to False in order to disable all
    # ordinary emails that could be sent to the users.
    user_services.update_email_preferences(user_id, False, False, False, False)

    email = user_services.get_user_settings(user_id, strict=True).email
    user_services.mark_user_for_deletion(user_id)

    save_pending_deletion_request(
        wipeout_domain.PendingDeletionRequest.create_default(
            user_id,
            email,
            explorations_to_be_deleted_ids,
            collections_to_be_deleted_ids
        )
    )
Esempio n. 22
0
    def test_set_and_get_user_email_preferences(self):
        user_id = 'someUser'
        username = '******'
        user_email = '*****@*****.**'

        user_services.create_new_user(user_id, user_email)
        user_services.set_username(user_id, username)

        # When UserEmailPreferencesModel is yet to be created,
        # the value returned by get_email_preferences() should be True.
        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEqual(email_preferences.can_receive_editor_role_email,
                         feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE)

        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEqual(email_preferences.can_receive_feedback_message_email,
                         feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE)

        # The user retrieves their email preferences. This initializes
        # a UserEmailPreferencesModel instance with the default values.
        user_services.update_email_preferences(
            user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE,
            feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE,
            feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE,
            feconf.DEFAULT_SUBSCRIPTION_EMAIL_PREFERENCE)

        email_preferences = user_services.get_email_preferences(user_id)
        self.assertEqual(email_preferences.can_receive_editor_role_email,
                         feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE)
        self.assertEqual(email_preferences.can_receive_feedback_message_email,
                         feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE)

        # The user sets their membership email preference to False.
        user_services.update_email_preferences(
            user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE, False, False,
            False)

        email_preferences = user_services.get_email_preferences(user_id)

        self.assertFalse(email_preferences.can_receive_editor_role_email)
        self.assertFalse(email_preferences.can_receive_feedback_message_email)
        self.assertFalse(email_preferences.can_receive_subscription_email)
Esempio n. 23
0
def pre_delete_user(user_id):
    """Prepare user for the full deletion.
        1. Mark all the activities that are private and solely owned by the user
           being deleted as deleted.
        2. Disable all the email preferences.
        3. Mark the user as to be deleted.
        4. Create PendingDeletionRequestModel for the user.

    Args:
        user_id: str. The id of the user to be deleted.
    """
    subscribed_exploration_summaries = (
        exp_fetchers.get_exploration_summaries_subscribed_to(user_id))
    explorations_to_be_deleted_ids = [
        exp_summary.id for exp_summary in subscribed_exploration_summaries
        if exp_summary.is_private()
        and exp_summary.is_solely_owned_by_user(user_id)
    ]
    # TODO(#8301): Implement delete_explorations to make this efficient.
    for exp_id in explorations_to_be_deleted_ids:
        exp_services.delete_exploration(user_id, exp_id)

    subscribed_collection_summaries = (
        collection_services.get_collection_summaries_subscribed_to(user_id))
    collections_to_be_deleted_ids = [
        col_summary.id for col_summary in subscribed_collection_summaries
        if col_summary.is_private()
        and col_summary.is_solely_owned_by_user(user_id)
    ]
    # TODO(#8301): Implement delete_collections to make this efficient.
    for col_id in collections_to_be_deleted_ids:
        collection_services.delete_collection(user_id, col_id)

    # Set all the user's email preferences to False in order to disable all
    # ordinary emails that could be sent to the users.
    user_services.update_email_preferences(user_id, False, False, False, False)

    user_services.mark_user_for_deletion(
        user_id,
        explorations_to_be_deleted_ids,
        collections_to_be_deleted_ids,
    )
Esempio n. 24
0
    def post(self):
        """Handles POST requests."""
        username = self.payload.get('username')
        agreed_to_terms = self.payload.get('agreed_to_terms')
        can_receive_email_updates = self.payload.get(
            'can_receive_email_updates')

        has_ever_registered = user_services.has_ever_registered(self.user_id)
        has_fully_registered = user_services.has_fully_registered(self.user_id)

        if has_fully_registered:
            self.render_json({})
            return

        if not isinstance(agreed_to_terms, bool) or not agreed_to_terms:
            raise self.InvalidInputException(
                'In order to edit explorations on this site, you will '
                'need to accept the license terms.')
        else:
            user_services.record_agreement_to_terms(self.user_id)

        if not user_services.get_username(self.user_id):
            try:
                user_services.set_username(self.user_id, username)
            except utils.ValidationError as e:
                raise self.InvalidInputException(e)

        if can_receive_email_updates is not None:
            user_services.update_email_preferences(
                self.user_id, can_receive_email_updates,
                feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE,
                feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE,
                feconf.DEFAULT_SUBSCRIPTION_EMAIL_PREFERENCE)

        # Note that an email is only sent when the user registers for the first
        # time.
        if feconf.CAN_SEND_EMAILS and not has_ever_registered:
            email_manager.send_post_signup_email(self.user_id)

        user_services.generate_initial_profile_picture(self.user_id)

        self.render_json({})
Esempio n. 25
0
    def post(self):
        """Handles POST requests."""
        username = self.payload.get('username')
        agreed_to_terms = self.payload.get('agreed_to_terms')
        can_receive_email_updates = self.payload.get(
            'can_receive_email_updates')

        has_ever_registered = user_services.has_ever_registered(self.user_id)
        has_fully_registered = user_services.has_fully_registered(self.user_id)

        if has_fully_registered:
            self.render_json({})
            return

        if not isinstance(agreed_to_terms, bool) or not agreed_to_terms:
            raise self.InvalidInputException(
                'In order to edit explorations on this site, you will '
                'need to accept the license terms.')
        else:
            user_services.record_agreement_to_terms(self.user_id)

        if not user_services.get_username(self.user_id):
            try:
                user_services.set_username(self.user_id, username)
            except utils.ValidationError as e:
                raise self.InvalidInputException(e)

        if can_receive_email_updates is not None:
            user_services.update_email_preferences(self.user_id,
                                                   can_receive_email_updates)

        # Note that an email is only sent when the user registers for the first
        # time.
        if feconf.CAN_SEND_EMAILS_TO_USERS and not has_ever_registered:
            email_manager.send_post_signup_email(self.user_id)

        self.render_json({})
Esempio n. 26
0
    def put(self):
        """Handles POST requests."""
        update_type = self.payload.get('update_type')
        data = self.payload.get('data')

        if update_type == 'user_bio':
            if len(data) > feconf.MAX_BIO_LENGTH_IN_CHARS:
                raise self.InvalidInputException(
                    'User bio exceeds maximum character limit: %s' %
                    feconf.MAX_BIO_LENGTH_IN_CHARS)
            else:
                user_services.update_user_bio(self.user_id, data)
        elif update_type == 'subject_interests':
            user_services.update_subject_interests(self.user_id, data)
        elif update_type == 'preferred_language_codes':
            user_services.update_preferred_language_codes(self.user_id, data)
        elif update_type == 'preferred_site_language_code':
            user_services.update_preferred_site_language_code(
                self.user_id, data)
        elif update_type == 'preferred_audio_language_code':
            user_services.update_preferred_audio_language_code(
                self.user_id, data)
        elif update_type == 'profile_picture_data_url':
            user_services.update_profile_picture_data_url(self.user_id, data)
        elif update_type == 'default_dashboard':
            user_services.update_user_default_dashboard(self.user_id, data)
        elif update_type == 'email_preferences':
            user_services.update_email_preferences(
                self.user_id, data['can_receive_email_updates'],
                data['can_receive_editor_role_email'],
                data['can_receive_feedback_message_email'],
                data['can_receive_subscription_email'])
        else:
            raise self.InvalidInputException('Invalid update type: %s' %
                                             update_type)

        self.render_json({})
Esempio n. 27
0
    def setUp(self):
        super(UserQueryJobOneOffTests, self).setUp()
        # User A has no created or edited explorations.
        # User B has one created exploration.
        # User C has one edited exploration.
        # User D has created an exploration and then edited it.
        # User E has created an exploration 10 days before.
        # User F has one created exploration but is not subscribed to emails.
        # Submitter is the user who submits the query.
        self.signup(self.USER_A_EMAIL, self.USER_A_USERNAME)
        self.user_a_id = self.get_user_id_from_email(self.USER_A_EMAIL)
        user_services.update_email_preferences(self.user_a_id, True, True,
                                               True, True)
        self.signup(self.USER_B_EMAIL, self.USER_B_USERNAME)
        self.user_b_id = self.get_user_id_from_email(self.USER_B_EMAIL)
        user_services.update_email_preferences(self.user_b_id, True, True,
                                               True, True)
        self.signup(self.USER_C_EMAIL, self.USER_C_USERNAME)
        self.user_c_id = self.get_user_id_from_email(self.USER_C_EMAIL)
        user_services.update_email_preferences(self.user_c_id, True, True,
                                               True, True)
        self.signup(self.USER_D_EMAIL, self.USER_D_USERNAME)
        self.user_d_id = self.get_user_id_from_email(self.USER_D_EMAIL)
        user_services.update_email_preferences(self.user_d_id, True, True,
                                               True, True)
        self.signup(self.USER_E_EMAIL, self.USER_E_USERNAME)
        self.user_e_id = self.get_user_id_from_email(self.USER_E_EMAIL)
        user_services.update_email_preferences(self.user_e_id, True, True,
                                               True, True)
        self.signup(self.USER_F_EMAIL, self.USER_F_USERNAME)
        self.user_f_id = self.get_user_id_from_email(self.USER_F_EMAIL)
        user_services.update_email_preferences(self.user_f_id, False, True,
                                               True, True)
        self.signup(self.USER_SUBMITTER_EMAIL, self.USER_SUBMITTER_USERNAME)
        self.submitter_id = self.get_user_id_from_email(
            self.USER_SUBMITTER_EMAIL)
        user_services.update_email_preferences(self.submitter_id, True, True,
                                               True, True)

        self.save_new_valid_exploration(self.EXP_ID_1,
                                        self.user_b_id,
                                        end_state_name='End')

        exp_services.update_exploration(self.user_c_id, self.EXP_ID_1, [
            exp_domain.ExplorationChange({
                'cmd': 'edit_exploration_property',
                'property_name': 'objective',
                'new_value': 'the objective'
            })
        ], 'Test edit')

        self.save_new_valid_exploration(self.EXP_ID_2,
                                        self.user_d_id,
                                        end_state_name='End')

        exp_services.update_exploration(self.user_d_id, self.EXP_ID_2, [
            exp_domain.ExplorationChange({
                'cmd': 'edit_exploration_property',
                'property_name': 'objective',
                'new_value': 'the objective'
            })
        ], 'Test edit')

        self.save_new_valid_exploration(self.EXP_ID_3,
                                        self.user_e_id,
                                        end_state_name='End')
        user_e_settings = user_services.get_user_settings(self.user_e_id)
        user_e_settings.last_created_an_exploration = (
            user_e_settings.last_created_an_exploration -
            datetime.timedelta(days=10))
        # Last edited time also changes when user creates an exploration.
        user_e_settings.last_edited_an_exploration = (
            datetime.datetime.utcnow() - datetime.timedelta(days=10))
        user_services.update_last_logged_in(
            user_e_settings,
            user_e_settings.last_logged_in - datetime.timedelta(days=10))

        self.save_new_valid_exploration(self.EXP_ID_4,
                                        self.user_f_id,
                                        end_state_name='End')

        user_a_settings = user_services.get_user_settings(self.user_a_id)
        user_services.update_last_logged_in(
            user_a_settings,
            user_a_settings.last_logged_in - datetime.timedelta(days=3))

        # Set tmpsuperadm1n as admin in ADMIN_USERNAMES config property.
        self.set_admins(['tmpsuperadm1n'])
Esempio n. 28
0
def pre_delete_user(user_id):
    """Prepare user for the full deletion.
        1. Mark all the activities that are private and solely owned by the user
           being deleted as deleted.
        2. Disable all the email preferences.
        3. Mark the user as to be deleted.
        4. Create PendingDeletionRequestModel for the user.

    Args:
        user_id: str. The id of the user to be deleted. If the user_id
            corresponds to a profile user then only that profile is deleted.
            For a full user, all of its associated profile users are deleted
            too.
    """
    pending_deletion_requests = []
    user_settings = user_services.get_user_settings(user_id, strict=True)

    linked_profile_user_ids = [
        user.user_id for user in
        user_services.get_all_profiles_auth_details_by_parent_user_id(user_id)
    ]
    profile_users_settings_list = user_services.get_users_settings(
        linked_profile_user_ids)
    for profile_user_settings in profile_users_settings_list:
        profile_id = profile_user_settings.user_id
        user_services.mark_user_for_deletion(profile_id)
        pending_deletion_requests.append(
            wipeout_domain.PendingDeletionRequest.create_default(
                profile_id, profile_user_settings.email,
                profile_user_settings.role, [], []))
    explorations_to_be_deleted_ids = []
    collections_to_be_deleted_ids = []
    if user_settings.role != feconf.ROLE_ID_LEARNER:
        subscribed_exploration_summaries = (
            exp_fetchers.get_exploration_summaries_subscribed_to(user_id))

        explorations_to_be_deleted_ids = [
            exp_summary.id for exp_summary in subscribed_exploration_summaries
            if exp_summary.is_private()
            and exp_summary.is_solely_owned_by_user(user_id)
        ]
        exp_services.delete_explorations(user_id,
                                         explorations_to_be_deleted_ids)

        # Release ownership of explorations that are public and are solely owned
        # by the to-be-deleted user.
        explorations_to_release_ownership_ids = [
            exp_summary.id for exp_summary in subscribed_exploration_summaries
            if not exp_summary.is_private()
            and exp_summary.is_solely_owned_by_user(user_id)
        ]
        for exp_id in explorations_to_release_ownership_ids:
            rights_manager.release_ownership_of_exploration(
                user_services.get_system_user(), exp_id)

        explorations_to_remove_user_from_ids = [
            exp_summary.id for exp_summary in subscribed_exploration_summaries
            if not exp_summary.is_solely_owned_by_user(user_id)
        ]
        for exp_id in explorations_to_remove_user_from_ids:
            rights_manager.deassign_role_for_exploration(
                user_services.get_system_user(), exp_id, user_id)

        subscribed_collection_summaries = (
            collection_services.get_collection_summaries_subscribed_to(user_id)
        )
        collections_to_be_deleted_ids = [
            col_summary.id for col_summary in subscribed_collection_summaries
            if col_summary.is_private()
            and col_summary.is_solely_owned_by_user(user_id)
        ]
        collection_services.delete_collections(user_id,
                                               collections_to_be_deleted_ids)

        # Release ownership of collections that are public and are solely owned
        # by the to-be-deleted user.
        collections_to_release_ownership_ids = [
            col_summary.id for col_summary in subscribed_collection_summaries
            if not col_summary.is_private()
            and col_summary.is_solely_owned_by_user(user_id)
        ]
        for col_id in collections_to_release_ownership_ids:
            rights_manager.release_ownership_of_collection(
                user_services.get_system_user(), col_id)

        collections_to_remove_user_from_ids = [
            col_summary.id for col_summary in subscribed_collection_summaries
            if not col_summary.is_solely_owned_by_user(user_id)
        ]
        for col_id in collections_to_remove_user_from_ids:
            rights_manager.deassign_role_for_collection(
                user_services.get_system_user(), col_id, user_id)

        topic_services.deassign_user_from_all_topics(
            user_services.get_system_user(), user_id)

        # Set all the user's email preferences to False in order to disable all
        # ordinary emails that could be sent to the users.
        user_services.update_email_preferences(user_id, False, False, False,
                                               False)

    user_services.mark_user_for_deletion(user_id)
    pending_deletion_requests.append(
        wipeout_domain.PendingDeletionRequest.create_default(
            user_id, user_settings.email, user_settings.role,
            explorations_to_be_deleted_ids, collections_to_be_deleted_ids))

    save_pending_deletion_requests(pending_deletion_requests)