Beispiel #1
0
    def handle(self, user_id):
        user = self.kqueen_request('user', 'get', fnargs=(user_id,))
        if user['active']:
            flash('User {} is already active.'.format(user['username']), 'warning')
            return redirect(request.environ.get('HTTP_REFERER', url_for('ui.organization_manage')))

        # Init mail handler
        mail.init_app(app)
        token = generate_confirmation_token(user['email'])
        html = render_template(
            'ui/email/user_invitation.html',
            username=user['username'],
            token=token,
            organization=user['organization']['name']
        )
        msg = Message(
            '[KQueen] Organization invitation',
            recipients=[user['email']],
            html=html
        )
        try:
            mail.send(msg)
        except Exception as e:
            self.logger('error', repr(e))
            self.kqueen_request('user', 'delete', fnargs=(user['id'],))
            flash('Could not send activation e-mail, please try again later.', 'danger')
            return redirect(request.environ.get('HTTP_REFERER', url_for('ui.organization_manage')))

        flash('Activation e-mail sent to user {}.'.format(user['username']), 'success')
        return redirect(request.environ.get('HTTP_REFERER', url_for('ui.organization_manage')))
Beispiel #2
0
    def handle(self, user_id):
        user = self.kqueen_request('user', 'get', fnargs=(user_id,))
        logger.debug('User {} from {} with id {} re-invited.'.format(user['username'], user['organization'], user['id']))
        if user['active']:
            logger.debug('User {} from {} with id {} is already active.'.format(user['username'], user['organization'], user['id']))
            flash('User {} is already active.'.format(user['username']), 'warning')
            return redirect(request.environ.get('HTTP_REFERER', url_for('ui.organization_manage')))

        # send mail
        token = generate_confirmation_token(user['email'])
        html = render_template(
            'ui/email/user_invitation.html',
            username=user['username'],
            token=token,
            organization=user['organization']['name'],
            year=datetime.utcnow().year
        )
        email = EmailMessage(
            '[KQueen] Organization invitation',
            recipients=[user['email']],
            html=html
        )
        try:
            email.send()
        except Exception as e:
            logger.exception('User {} from {} with id {} will be removed.'.format(user['username'], user['organization'], user['id']))
            self.kqueen_request('user', 'delete', fnargs=(user['id'],))
            flash('Could not send activation e-mail, please try again later.', 'danger')
            return redirect(request.environ.get('HTTP_REFERER', url_for('ui.organization_manage')))

        logger.debug('Activation e-mail sent to user {} from {} with id {} will be removed.'.format(user['username'], user['organization'], user['id']))
        flash('Activation e-mail sent to user {}.'.format(user['username']), 'success')
        return redirect(request.environ.get('HTTP_REFERER', url_for('ui.organization_manage')))
Beispiel #3
0
    def handle(self):
        form = UserInviteForm()
        if form.validate_on_submit():
            organization = 'Organization:{}'.format(
                session['user']['organization']['id'])
            password = generate_password()
            user_kw = {
                'username': form.email.data,
                'password': password,
                'email': form.email.data,
                'organization': organization,
                'role': 'member',
                'created_at': datetime.utcnow(),
                'active': False
            }
            logger.debug('User {} from {} invited.'.format(
                user_kw['username'], user_kw['organization']))
            user = self.kqueen_request('user', 'create', fnargs=(user_kw, ))

            # send mail
            token = generate_confirmation_token(user['email'])
            html = render_template('ui/email/user_invitation.html',
                                   username=user['username'],
                                   token=token,
                                   organization=user['organization']['name'])
            email = EmailMessage('[KQueen] Organization invitation',
                                 recipients=[user['email']],
                                 html=html)
            try:
                email.send()
            except Exception as e:
                logger.exception(
                    'User {} from {} with id {} will be removed.'.format(
                        user_kw['username'], user_kw['organization'],
                        user['id']))
                self.kqueen_request('user', 'delete', fnargs=(user['id'], ))
                flash(
                    'Could not send invitation e-mail, please try again later.',
                    'danger')
                return render_template('ui/user_invite.html', form=form)

            logger.debug('User {} from {} created with id {}.'.format(
                user_kw['username'], user_kw['organization'], user['id']))
            flash('User {} successfully created.'.format(user['username']),
                  'success')
            return redirect(url_for('ui.organization_manage'))
        return render_template('ui/user_invite.html', form=form)
Beispiel #4
0
    def handle(self, organization_id):
        form = MemberCreateForm()
        if form.validate_on_submit():
            user_kw = {
                'username': form.email.data,
                'email': form.email.data,
                'password': generate_password(),
                'organization': 'Organization:{}'.format(organization_id),
                'created_at': datetime.utcnow(),
                'role': form.role.data,
                'active': True,
                'metadata': {}
            }
            user = self.kqueen_request('user',
                                       'create',
                                       fnkwargs={'payload': user_kw})

            # send mail
            token = generate_confirmation_token(user['email'])
            html = render_template('ui/email/user_invitation.html',
                                   username=user['username'],
                                   token=token,
                                   organization=user['organization']['name'])
            email = EmailMessage('[KQueen] Organization invitation',
                                 recipients=[user['email']],
                                 html=html)
            try:
                email.send()
            except Exception as e:
                msg = 'Could not send invitation e-mail, please try again later.'
                logger.exception(msg)
                self.kqueen_request('user',
                                    'delete',
                                    fnargs={'uuid', user['id']})
                flash(msg, 'danger')
                return render_template('manager/member_create.html', form=form)

            msg = 'Member {} successfully added.'.format(user['username'])
            user_logger.debug('{}:{}'.format(user_prefix(session), msg))
            flash(msg, 'success')
            return redirect(
                url_for('manager.organization_detail',
                        organization_id=organization_id))
        return render_template('manager/member_create.html',
                               form=form,
                               organization_id=organization_id)
Beispiel #5
0
 def handle(self):
     form = RequestPasswordResetForm()
     if form.validate_on_submit():
         # Init mail handler
         mail.init_app(app)
         token = generate_confirmation_token(form.email.data)
         html = render_template('ui/email/user_request_password_reset.html', token=token)
         msg = Message(
             '[KQueen] Password reset',
             recipients=[form.email.data],
             html=html
         )
         try:
             mail.send(msg)
         except Exception as e:
             self.logger('error', repr(e))
             flash('Could not send password reset e-mail, please try again later.', 'danger')
         flash('Password reset link was sent to your e-mail address.', 'success')
         return redirect(url_for('ui.index'))
     return render_template('ui/user_request_password_reset.html', form=form)
Beispiel #6
0
 def handle(self):
     form = RequestPasswordResetForm()
     if form.validate_on_submit():
         # send mail
         token = generate_confirmation_token(form.email.data)
         html = render_template('ui/email/user_request_password_reset.html', token=token)
         email = EmailMessage(
             '[KQueen] Password reset',
             recipients=[form.email.data],
             html=html
         )
         try:
             email.send()
         except Exception as e:
             msg = 'Could not send password reset e-mail, please try again later.'
             logger.exception(msg)
             flash(msg, 'danger')
         else:
             flash('Password reset link was sent to your e-mail address.', 'success')
         return redirect(url_for('ui.index'))
     return render_template('ui/user_request_password_reset.html', form=form)
Beispiel #7
0
    def handle(self):
        if not app.config['ENABLE_PUBLIC_REGISTRATION']:
            flash('Public registration is disabled.', 'warning')
            return redirect(url_for('ui.index'))
        form = UserRegistrationForm()
        if form.validate_on_submit():
            organization_kw = {
                'name': form.organization_name.data,
                'namespace': slugify(form.organization_name.data),
                'created_at': datetime.utcnow()
            }
            organization = self.kqueen_request('organization',
                                               'create',
                                               fnargs=(organization_kw, ),
                                               service=True)
            organization_ref = 'Organization:{}'.format(organization['id'])
            user_kw = {
                'username': form.email.data,
                'password': form.password_1.data,
                'email': form.email.data,
                'organization': organization_ref,
                'role': 'admin',
                'created_at': datetime.utcnow(),
                'active': False
            }
            try:
                user = self.kqueen_request('user',
                                           'create',
                                           fnargs=(user_kw, ),
                                           service=True)
            except KQueenAPIException:
                self.kqueen_request('organization',
                                    'delete',
                                    fnargs=(organization['id'], ),
                                    service=True)
                return render_template('registration/register.html', form=form)

            # Init mail handler
            mail.init_app(app)
            token = generate_confirmation_token(user['email'])
            html = render_template('registration/email/verify_email.html',
                                   token=token)
            msg = Message('[KQueen] E-mail verification',
                          recipients=[user['email']],
                          html=html)
            try:
                mail.send(msg)
            except Exception as e:
                self.logger('error', repr(e))
                self.kqueen_request('user',
                                    'delete',
                                    fnargs=(user['id'], ),
                                    service=True)
                self.kqueen_request('organization',
                                    'delete',
                                    fnargs=(organization['id'], ),
                                    service=True)
                flash(
                    'Could not send verification e-mail, please try again later.',
                    'danger')
                return render_template('registration/register.html', form=form)

            flash(
                'Registration successful. Check your e-mail for the activation link!',
                'success')
            return redirect(url_for('ui.login'))
        return render_template('registration/register.html', form=form)
Beispiel #8
0
    def handle(self):
        # TODO: Add CN name validator (for LDAp username)

        form_cls = UserInviteForm

        auth_config = self.kqueen_request('configuration', 'auth')

        for auth_type, options in auth_config.items():
            ui_parameters = options['ui_parameters']
            # Add tag to field names to enable dynamic field switching
            ui_parameters = {k + '__' + auth_type: v for k, v in ui_parameters.items()}
            form_cls.append_fields(ui_parameters, switchtag=auth_type)

        form = form_cls()
        form.auth_method.choices = [(k, v['name']) for k, v in auth_config.items()]
        if form.validate_on_submit():

            organization = 'Organization:{}'.format(session['user']['organization']['id'])

            # Filter out populated tagged fields and get their data
            try:
                ui_filled_parameters = {
                    k.split('__')[0]: v.data
                    for (k, v)
                    in form._fields.items()
                    if (hasattr(v, 'switchtag') and v.switchtag) and form.auth_method.data in k
                }
            except Exception as e:
                msg = 'Failed to invite user: Invalid parameters.'
                user_logger.exception('{}:{}'.format(user_prefix(session), msg))
                flash(msg, 'danger')
                render_template('ui/user_invite.html', form=form)

            chosen_auth_type = form.auth_method.data
            username_field_descr = auth_config[chosen_auth_type]['ui_parameters']['username']
            username = ui_filled_parameters['username']

            user_kw = {
                'username': username,
                'password': generate_password() if username_field_descr.get('generate_password', True) else '',
                'email': username if username_field_descr['type'] == 'email' else '',
                'organization': organization,
                'role': 'member',
                'created_at': datetime.utcnow(),
                'auth': chosen_auth_type,
                'active': username_field_descr.get('active', True),
                'metadata': {}
            }
            logger.debug('User {} from {} invited.'.format(user_kw['username'], user_kw['organization']))
            user = self.kqueen_request('user', 'create', fnargs=(user_kw,))

            # send mail
            notify = username_field_descr.get('notify')
            if notify:
                logger.debug('User {} from {} with id {} will be notified '
                             'through email.'.format(user_kw['username'], user_kw['organization'], user['id']))
                token = generate_confirmation_token(user['email'])
                html = render_template(
                    'ui/email/user_invitation.html',
                    username=user['username'],
                    token=token,
                    organization=user['organization']['name'],
                    year=datetime.utcnow().year
                )
                email = EmailMessage(
                    '[KQueen] Organization invitation',
                    recipients=[user['email']],
                    html=html
                )
                try:
                    email.send()
                except Exception:
                    logger.exception('User {} from {} with id {} will be removed.'.format(user_kw['username'],
                                                                                          user_kw['organization'],
                                                                                          user['id']))
                    self.kqueen_request('user', 'delete', fnargs=(user['id'],))
                    flash('Could not send invitation e-mail, please try again later.', 'danger')
                    return render_template('ui/user_invite.html', form=form)

            logger.debug('User {} from {} created with id {}.'.format(user_kw['username'],
                                                                      user_kw['organization'],
                                                                      user['id']))
            flash('User {} successfully created.'.format(user['username']), 'success')
            return redirect(url_for('ui.organization_manage'))
        return render_template('ui/user_invite.html', form=form)
Beispiel #9
0
def email_token():
    _user = user()
    return auth.generate_confirmation_token(_user['email'])