Example #1
0
    def test_reset_user_password_link(self):
        # Set password
        CreateTestData.create_user(name='bob', email='*****@*****.**', password='******')

        # Set password to something new
        model.User.by_name(u'bob').password = '******'
        model.repo.commit_and_remove()
        test2_encoded = model.User.by_name(u'bob').password
        assert test2_encoded != 'test2'
        assert model.User.by_name(u'bob').password == test2_encoded

        # Click link from reset password email
        create_reset_key(model.User.by_name(u'bob'))
        reset_password_link = get_reset_link(model.User.by_name(u'bob'))
        offset = reset_password_link.replace('http://test.ckan.net', '')
        print offset
        res = self.app.get(offset)

        # Reset password form
        fv = res.forms['user-reset']
        fv['password1'] = 'test1'
        fv['password2'] = 'test1'
        res = fv.submit('save', status=302)

        # Check a new password is stored
        assert model.User.by_name(u'bob').password != test2_encoded
def new_user_content(admin_user, new_user):
    if new_user.fullname and len(new_user.fullname) > 0:
        username = new_user.fullname
    else:
        username = new_user.name
    if admin_user.fullname and len(admin_user.fullname) > 0:
        admin_username = admin_user.fullname
    else:
        admin_username = admin_user.name
    login_username = new_user.name
    reset_link = ckan_mailer.get_reset_link(new_user)
    site_title = gobar_helpers.get_theme_config('title.site-title',
                                                'Portal Andino')
    plain_body = new_user_plain_body.format(admin_username=admin_username,
                                            username=username,
                                            reset_link=reset_link,
                                            site_title=site_title,
                                            login_username=login_username)
    html_body = new_user_html_body.format(admin_username=admin_username,
                                          username=username,
                                          reset_link=reset_link,
                                          site_title=site_title,
                                          login_username=login_username)
    subject = new_user_subject.format(admin_username=admin_username)
    return subject, plain_body, html_body
Example #3
0
    def test_reset_user_password_link(self):
        # Set password
        CreateTestData.create_user(name='bob', email='*****@*****.**', password='******')

        # Set password to something new
        model.User.by_name(u'bob').password = '******'
        model.repo.commit_and_remove()
        test2_encoded = model.User.by_name(u'bob').password
        assert test2_encoded != 'test2'
        assert model.User.by_name(u'bob').password == test2_encoded

        # Click link from reset password email
        create_reset_key(model.User.by_name(u'bob'))
        reset_password_link = get_reset_link(model.User.by_name(u'bob'))
        offset = reset_password_link.replace('http://test.ckan.net', '')
        print offset
        res = self.app.get(offset)

        # Reset password form
        fv = res.forms['user-reset']
        fv['password1'] = 'test1'
        fv['password2'] = 'test1'
        res = fv.submit('save', status=302)

        # Check a new password is stored
        assert model.User.by_name(u'bob').password != test2_encoded
Example #4
0
    def test_reset_user_password_link(self):
        # Set password
        CreateTestData.create_user(name="bob", email="*****@*****.**", password="******")

        # Set password to something new
        model.User.by_name(u"bob").password = "******"
        model.repo.commit_and_remove()
        test2_encoded = model.User.by_name(u"bob").password
        assert test2_encoded != "test2"
        assert model.User.by_name(u"bob").password == test2_encoded

        # Click link from reset password email
        create_reset_key(model.User.by_name(u"bob"))
        reset_password_link = get_reset_link(model.User.by_name(u"bob"))
        offset = reset_password_link.replace("http://test.ckan.net", "")
        res = self.app.get(offset)

        # Reset password form
        fv = res.forms["user-reset"]
        fv["password1"] = "test1"
        fv["password2"] = "test1"
        res = fv.submit("save", status=302)

        # Check a new password is stored
        assert model.User.by_name(u"bob").password != test2_encoded
Example #5
0
def _get_reset_link_body(user):
    extra_vars = {
        'reset_link': get_reset_link(user),
        'site_title_dk': config_option_show('ckan.site_title', 'da_DK'),
        'site_title_en': config_option_show('ckan.site_title', 'en'),
        'site_url': config.get('ckan.site_url'),
        'user_name': user.name,
    }
    return render_jinja2('emails/reset_password.txt', extra_vars)
Example #6
0
def get_reg_link_body(user):
    extra_vars = {
        'reg_link': get_reset_link(user),
        'site_title': config.get('ckan.site_title'),
        'site_url': config.get('ckan.site_url') + '/' + h.lang(),
        'user_name': user.name,
        'tou_version': get_latest_legal_version('tou'),
        'privacy_version': get_latest_legal_version('privacy')
        }
    return render_jinja2('emails/user_registration.txt', extra_vars)
Example #7
0
def reset_mail_content(user):
    if user.fullname:
        username = user.fullname
    else:
        username = user.name
    reset_link = ckan_mailer.get_reset_link(user)
    site_title = gobar_helpers.get_theme_config('title.site-title', 'Portal Andino')
    plain_body = reset_password_plain_body.format(username=username, reset_link=reset_link, site_title=site_title)
    html_body = reset_password_html_body.format(username=username, reset_link=reset_link, site_title=site_title)
    subject = reset_password_subject.format(site_title=site_title)
    return subject, plain_body, html_body
Example #8
0
    def test_reset_password_custom_body(self):
        user = factories.User()
        user_obj = model.User.by_name(user['name'])

        mailer.send_reset_link(user_obj)

        # check it went to the mock smtp server
        msgs = self.get_smtp_messages()
        assert_equal(len(msgs), 1)
        msg = msgs[0]
        extra_vars = {'reset_link': mailer.get_reset_link(user_obj)}
        expected = render_jinja2('emails/reset_password.txt', extra_vars)
        body = self.get_email_body(msg[3])
        assert_equal(expected, body)
        assert_in('**test**', body)
Example #9
0
    def test_reset_password_custom_body(self):
        user = factories.User()
        user_obj = model.User.by_name(user["name"])

        mailer.send_reset_link(user_obj)

        # check it went to the mock smtp server
        msgs = self.get_smtp_messages()
        assert len(msgs) == 1
        msg = msgs[0]
        extra_vars = {"reset_link": mailer.get_reset_link(user_obj)}
        expected = render_jinja2("emails/reset_password.txt", extra_vars)
        body = self.get_email_body(msg[3])
        assert expected == body
        assert "**test**" in body
Example #10
0
    def _sso_user_reset_notification(self):
        import ckan.lib.mailer as mailer
        from ckanext.saml2.model.saml2_user import SAML2User
        import time
        saml2_users = model.Session.query(SAML2User.id).all()
        if len(self.args) > 1:
            users = model.Session.query(model.User)\
               .filter(model.User.name == self.args[1])\
               .filter(model.User.id.in_(saml2_users)).limit(1).all()
        else:
            users = model.Session.query(model.User)\
                .filter(model.User.id.in_(saml2_users))\
                .all()
        for user in users:
            time.sleep(4)
            if user:
                print('*' * 100)
                mailer.create_reset_key(user)
                reset_link = mailer.get_reset_link(user)
                extra_link = h.url_for('/user/reset', qualified=True)
                subject = 'Data.NSW & IAR ID Hub decommissioning'
                msg = ('Dear {0},\n\n'

                'The Department of Finance, Services and Innovation has been making a range of improvements to Data NSW and as part of that roadmap, we are making changes to the login process.\n\n'

                'In order to maintain your access to Data NSW and the Information Access Register, you will need to reset your password to login to Data NSW.\n\n'

                'To reset your password, as soon as possible please visit: {1} \n\n'

                'If the link above doesn\'t work, please visit {2} and reset your password manually using the following username: {3} \n\n'

                'Once your password is reset, you will be able to use this new password and the login functionality on the Data NSW homepage to access your datasets. Please note, your Data NSW user name is used in the salutation of this message.\n\n'

                'To continue to access Data NSW to administer your agency\'s datasets, please make these login changes by 6 February.\n\n'

                'If you have any questions or concerns about these changes, please contact the Information and Data Policy team at the Department of Finance, Services and Innovation at [email protected]\n\n'

                'Kind Regards,\n'
                'The Data NSW team\n'
                'Department Finance, Services and Innovation').format(user.name, reset_link, extra_link, user.name)
                if user.email:
                    mailer.mail_recipient(user.name, user.email, subject, msg)
                    log.info("User pass reset email should be sent to {0} user.".format(user.name))
                    print("User pass reset email should be sent to {0} user.".format(user.name))
                else:
                    log.error("User {0} don't have email".format(user.name))
                    print("User {0} don't have email".format(user.name))
                print('*' * 100)
Example #11
0
    def test_reset_password_custom_body(self):
        user = factories.User()
        user_obj = model.User.by_name(user['name'])

        mailer.send_reset_link(user_obj)

        # check it went to the mock smtp server
        msgs = self.get_smtp_messages()
        assert_equal(len(msgs), 1)
        msg = msgs[0]
        extra_vars = {
            'reset_link': mailer.get_reset_link(user_obj)
        }
        expected = render_jinja2('emails/reset_password.txt',
                                 extra_vars)
        body = self.get_email_body(msg[3])
        assert_equal(expected, body)
        assert_in('**test**', body)
Example #12
0
    def test_invite_user_custom_body(self):
        user = factories.User()
        user_obj = model.User.by_name(user['name'])

        mailer.send_invite(user_obj)

        # check it went to the mock smtp server
        msgs = self.get_smtp_messages()
        assert_equal(len(msgs), 1)
        msg = msgs[0]
        extra_vars = {
            'reset_link': mailer.get_reset_link(user_obj),
            'user_name': user['name'],
            'site_title': config.get('ckan.site_title'),
        }
        expected = render_jinja2('emails/invite_user.txt', extra_vars)
        body = self.get_email_body(msg[3])
        assert_equal(expected, body)
        assert_in('**test**', body)
def passwordless_get_reset_link_body(user):
    login_link = toolkit.url_for(controller='user',
                                 action='login',
                                 qualified=True)
    reset_link = mailer.get_reset_link(user)
    reset_key = user.reset_key[2:-1]
    extra_vars = {
        'login_link': login_link,
        'reset_link': reset_link,
        'site_title': config.get('ckan.site_title'),
        'site_url': config.get('ckan.site_url'),
        'user_name': user.name,
        'user_fullname': user.fullname,
        'user_email': user.email,
        'key': reset_key
    }
    log.debug("KEY {0}".format(reset_key))
    # NOTE: This template is translated
    return render('emails/reset_password.txt', extra_vars)
Example #14
0
    def test_invite_user_custom_body(self):
        user = factories.User()
        user_obj = model.User.by_name(user["name"])

        mailer.send_invite(user_obj)

        # check it went to the mock smtp server
        msgs = self.get_smtp_messages()
        assert len(msgs) == 1
        msg = msgs[0]
        extra_vars = {
            "reset_link": mailer.get_reset_link(user_obj),
            "user_name": user["name"],
            "site_title": config.get("ckan.site_title"),
        }
        expected = render_jinja2("emails/invite_user.txt", extra_vars)
        body = self.get_email_body(msg[3])
        assert expected == body
        assert "**test**" in body
Example #15
0
    def test_invite_user_custom_body(self):
        user = factories.User()
        user_obj = model.User.by_name(user['name'])

        mailer.send_invite(user_obj)

        # check it went to the mock smtp server
        msgs = self.get_smtp_messages()
        assert_equal(len(msgs), 1)
        msg = msgs[0]
        extra_vars = {
            'reset_link': mailer.get_reset_link(user_obj),
            'user_name': user['name'],
            'site_title': config.get('ckan.site_title'),
        }
        expected = render_jinja2('emails/invite_user.txt',
                                 extra_vars)
        body = self.get_email_body(msg[3])
        assert_equal(expected, body)
        assert_in('**test**', body)
Example #16
0
def user_invite(context, data_dict):
    '''Invite a new user.

    You must be authorized to create group members.

    :param email: the email of the user to be invited to the group
    :type email: string
    :param group_id: the id or name of the group
    :type group_id: string
    :param role: role of the user in the group. One of ``member``, ``editor``,
        or ``admin``
    :type role: string

    :returns: the newly created yser
    :rtype: dictionary
    '''
    toolkit.check_access('user_invite', context, data_dict)

    schema = context.get('schema',
                         logic.schema.default_user_invite_schema())
    data, errors = toolkit.navl_validate(data_dict, schema, context)
    if errors:
        raise toolkit.ValidationError(errors)

    model = context['model']
    group = model.Group.get(data['group_id'])
    if not group:
        raise toolkit.ObjectNotFound()

    name = logic.action.create._get_random_username_from_email(data['email'])
    password = str(random.SystemRandom().random())
    data['name'] = name
    data['password'] = password
    data['state'] = model.State.PENDING
    user_dict = toolkit.get_action('user_create')(context, data)
    user = model.User.get(user_dict['id'])
    member_dict = {
        'username': user.id,
        'id': data['group_id'],
        'role': data['role']
    }
    toolkit.get_action('group_member_create')(context, member_dict)

    if group.is_organization:
        group_dict = toolkit.get_action('organization_show')(context,
            {'id': data['group_id']})
    else:
        group_dict = toolkit.get_action('group_show')(context,
            {'id': data['group_id']})

    mailer.create_reset_key(user)

    # Email body
    group_type = (toolkit._('organization') if group_dict['is_organization']
                  else toolkit._('group'))
    role = data['role']
    extra_vars = {
        'reset_link': mailer.get_reset_link(user),
        'site_title': config.get('ckan.site_title'),
        'site_url': config.get('ckan.site_url'),
        'user_name': user.name,
        'role_name': authz.roles_trans().get(role, toolkit._(role)),
        'group_type': group_type,
        'group_title': group_dict.get('title'),
    }

    # NOTE: This template is translated
    body = render_jinja2('emails/invite_user.txt', extra_vars)
    subject = toolkit._('Invite for {site_title}').format(
        site_title=config.get('ckan.site_title'))

    mailer.mail_user(user, subject, body)

    return model_dictize.user_dictize(user, context)