Ejemplo n.º 1
0
def deactivate(user_id):
    from art17.auth.common import set_user_active
    user = models.RegisteredUser.query.get(user_id)
    set_user_active(user, False)
    print "user", user.id, "has been deactivated"
    if not user.is_ldap:
        print "user", user.id, "has been removed from Zope"
Ejemplo n.º 2
0
def activate(user_id):
    from art17.auth.common import set_user_active
    user = models.RegisteredUser.query.get(user_id)
    set_user_active(user, True)
    print "user", user.id, "has been activated"
    if not user.is_ldap:
        print "user", user.id, "has been created in Zope"
Ejemplo n.º 3
0
def admin_user(user_id):
    user = models.RegisteredUser.query.get_or_404(user_id)
    current_user_roles = [r.name for r in user.roles]
    all_roles = (models.Role.query.with_entities(
        models.Role.name,
        models.Role.description).order_by(models.Role.id).all())

    if request.method == 'POST':
        if request.form.get('btn') == u'delete':
            # delete from local database
            user = models.RegisteredUser.query.get(user_id)
            models.db.session.delete(user)
            models.db.session.commit()
            flash("User %s has successfully been deleted" % user_id, 'success')
            return redirect(url_for('.users'))
        else:
            user_form = Art17AdminEditUserForm(request.form, obj=user)
            if user_form.validate():
                # manage status
                set_user_active(user, user_form.active.data)

                # manage roles
                datastore = current_app.extensions['security'].datastore
                new_roles = request.form.getlist('roles')
                expandable_roles = filter(lambda k: k not in new_roles,
                                          current_user_roles)
                for role in new_roles:
                    datastore.add_role_to_user(user_id, role)
                for role in expandable_roles:
                    datastore.remove_role_from_user(user_id, role)
                datastore.commit()

                # manage user info
                user_form.populate_obj(user)
                models.db.session.commit()

                # manage role notifications
                if request.form.get('notify_user', type=bool):
                    send_role_change_notification(user, new_roles)

                flash("User information updated for %s" % user_id, 'success')
                return redirect(url_for('.users'))
    else:
        user_form = Art17AdminEditUserForm(obj=user)

    return render_template(
        'auth/admin_user.html', **{
            'user': user,
            'user_form': user_form,
            'current_user_roles': current_user_roles,
            'all_roles': dict(all_roles),
        })
Ejemplo n.º 4
0
def admin_create_local():
    form = Art17LocalRegisterForm(request.form)

    if form.validate_on_submit():
        kwargs = form.to_dict()
        plaintext_password = kwargs['password']
        encrypted_password = encrypt_password(plaintext_password)
        datastore = current_app.extensions['security'].datastore
        user = datastore.create_user(**kwargs)
        user.confirmed_at = datetime.utcnow()
        set_user_active(user, True)
        user.password = encrypted_password
        datastore.commit()
        send_welcome_email(user, plaintext_password)
        flash("User %s created successfully." % kwargs['id'], 'success')
        return redirect(url_for('.users'))

    return render_template('auth/register_local.html', **{
        'register_user_form': form,
    })
Ejemplo n.º 5
0
def admin_create_ldap():
    user_id = request.form.get('user_id')
    if user_id is None:
        return render_template('auth/register_ldap_enter_user_id.html')

    if models.RegisteredUser.query.get(user_id) is not None:
        flash('User "%s" already registered.' % user_id, 'error')
        return redirect(url_for('.admin_create_ldap'))

    initial_data = _get_initial_ldap_data(user_id)
    if '_fields_from_ldap' in request.form:
        if initial_data is None:
            flash('User "%s" not found in Eionet.' % user_id, 'error')
            return redirect(url_for('.admin_create_ldap'))
        form = Art17LDAPRegisterForm(ImmutableMultiDict(initial_data))
    else:
        form = Art17LDAPRegisterForm(request.form)
        form.name.data = initial_data.get('name', '')
        form.email.data = initial_data.get('email', '') or form.email.data
        if form.validate():
            kwargs = form.to_dict()
            kwargs['id'] = user_id
            kwargs['is_ldap'] = True
            datastore = current_app.extensions['security'].datastore
            user = datastore.create_user(**kwargs)
            user.confirmed_at = datetime.utcnow()
            set_user_active(user, True)
            datastore.commit()
            send_welcome_email(user)
            flash(
                "User %s created successfully." % kwargs['id'],
                'success',
            )
            return redirect(url_for('.users'))

    return render_template(
        'auth/register_ldap.html', **{
            'user_id': user_id,
            'register_user_form': form,
        })
Ejemplo n.º 6
0
def deactivate(user_id):
    from art17.auth.common import set_user_active
    user = models.RegisteredUser.query.get(user_id)
    set_user_active(user, False)
    print "user", user.id, "has been deactivated"