Commit 8c1d311f authored by Noe Nieto's avatar Noe Nieto 💬

Merge branch 'master' of source.puri.sm:liberty/ldh_middleware

parents cd8779ee 2e4fb63e
Pipeline #5843 failed with stage
......@@ -15,6 +15,10 @@
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
{% if is_billing %}
<input type="submit" value="{% trans 'Billing >>' %}" />
{% else %}
<input type="submit" value="{% trans 'Register >>' %}" />
{% endif %}
</form>
{% endblock %}
......@@ -4,6 +4,7 @@ from django.contrib.auth import logout
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django import forms
from django.contrib.auth import get_user_model
......@@ -60,6 +61,16 @@ class CartRegistrationView(RegistrationView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['DEBUG_REGISTER_STATUS'] = settings.DEBUG_REGISTER_STATUS
is_billing = False
billing_carts = list(
set().union(settings.WOO_CART_BASIC,
settings.WOO_CART_COMPLETE,
settings.WOO_CART_GROUP_BASIC,
settings.WOO_CART_GROUP_COMPLETE)
)
if self.reward in billing_carts:
is_billing = True
context['is_billing'] = is_billing
return context
def get(self, *args, **kwargs):
......@@ -67,7 +78,8 @@ class CartRegistrationView(RegistrationView):
valid_cart_numbers = list(
set().union(settings.WOO_CART_BASIC,
settings.WOO_CART_COMPLETE,
settings.WOO_CART_GROUP)
settings.WOO_CART_GROUP_BASIC,
settings.WOO_CART_GROUP_COMPLETE)
)
if settings.WOO_CART_ZERO:
......@@ -76,6 +88,12 @@ class CartRegistrationView(RegistrationView):
if settings.WOO_CART_999:
valid_cart_numbers.append("999")
if settings.WOO_CART_5000:
valid_cart_numbers.append("5000")
if settings.WOO_CART_5999:
valid_cart_numbers.append("5999")
self.reward = kwargs.get('reward', None)
if self.reward not in valid_cart_numbers:
......@@ -89,7 +107,7 @@ class CartRegistrationView(RegistrationView):
def get_success_url(self, user):
if self.reward in ["0", "999"]:
if self.reward in ["0", "999", "5000", "5999"]:
# skip billing page for magic cart numbers
# (but still force authentication for login purposes)
......@@ -114,20 +132,67 @@ class CartRegistrationView(RegistrationView):
def register(self, form):
user = super().register(form)
if self.reward == 0 or self.reward in settings.WOO_CART_BASIC:
reward = ChosenReward(user=user, reward=self.reward)
magic_carts = ["0", "999", "5000", "5999"]
basic_carts = list(set().union(["0", "5000"],
settings.WOO_CART_BASIC,
settings.WOO_CART_GROUP_BASIC))
complete_carts = list(set().union(["999", "5999"],
settings.WOO_CART_COMPLETE,
settings.WOO_CART_GROUP_COMPLETE))
group_carts = list(set().union(["5000", "5999"],
settings.WOO_CART_GROUP_BASIC,
settings.WOO_CART_GROUP_COMPLETE))
is_magic = self.reward in magic_carts
if is_magic:
reward.is_pending = False
# List of services that need to be activated for the new user
to_activate = []
if self.reward in basic_carts:
# Register as BASIC user
user.account_type = AccountType.BASIC
elif self.reward == 999 or self.reward in settings.WOO_CART_COMPLETE:
if is_magic:
reward.is_pending = False
# Activate Chat and Social
to_activate.append(settings.LM_SERVICES.SOCIAL)
to_activate.append(settings.LM_SERVICES.CHAT)
for lgroup in ['chat', 'social']:
user.set_ldap_group(lgroup)
elif self.reward in complete_carts:
# Register as COMPLETE user
user.account_type = AccountType.COMPLETE
elif self.reward == 3 or self.reward in settings.WOO_CART_GROUP:
if is_magic:
reward.is_pending = False
# Activate all but Group
to_activate.append(settings.LM_SERVICES.SOCIAL)
to_activate.append(settings.LM_SERVICES.CHAT)
to_activate.append(settings.LM_SERVICES.TUNNEL)
to_activate.append(settings.LM_SERVICES.MAIL)
to_activate.append(settings.LM_SERVICES.XMPP)
for lgroup in ['chat', 'social', 'mail', 'xmpp']:
user.set_ldap_group(lgroup)
if self.reward in group_carts:
user.account_type = AccountType.GROUP
else:
# Register as UNDEFINED user
user.account_type = AccountType.UNDEFINED
reward = ChosenReward(user=user, reward=self.reward)
if is_magic:
reward.is_pending = False
# Activate Group
to_activate.append(settings.LM_SERVICES.GROUP)
now = timezone.now()
next_month = now + timezone.timedelta(days=30)
limits = user.limit_set.all()
for limit in limits:
if limit.service in to_activate:
limit.renewal_date = next_month
limit.is_active = True
# Tunnel special case
if limit.service == settings.LM_SERVICES.TUNNEL:
user.generate_tunnel_account()
limit.save()
user.save()
reward.save()
return user
......@@ -29,19 +29,29 @@ AUTH_LDAP_BIND_DN=cn=admin,dc=example,dc=com
AUTH_LDAP_USER_SEARCH_BASE_DN=ou=people,dc=example,dc=com
SQLITE_DB_PATH=/var/opt/purist/middleware/db.sqlite3
STATICFILES_DIRS=/var/opt/purist/brand,/var/opt/purist/downloads
WOO_URL=https://example.com
WOO_WP_API=True
WOO_VERSION=wc/v1
WOO_QUERY_STRING_AUTH = True
WOOSUB1_PRODUCT_LIST = 123,124
WOO_CART_PATH=cart
WOO_CART_BASIC = 1
WOO_CART_COMPLETE = 2
WOO_CART_GROUP_BASIC = 3
WOO_CART_GROUP_COMPLETE = 4
WOO_CART_ZERO = True
WOO_CART_999 = True
WOO_CART_5000 = True
WOO_CART_5999 = True
WOO_CART_BILLING_PATH = add_to_cart
WOO_CART_THANKS_PATH = get-started
OVPN_HOSTNAME=ssh.example.com
OVPN_PORT=22
OVPN_USERNAME=username
OVPN_FILEPATH="/path/to/{IDENTITY}/{IDENTITY}.ovpn"
TUNNEL_HOST=https://example.com
PASSWORD_RESET_TOKEN_EXPIRES=1800
WOO_CART_GROUP = 1,2,4
# Email configuration example to use Mailtrap for development (https://source.puri.sm/snippets/56)
# EMAIL_HOST = '127.0.0.1'
......
-
name: BASIC
services:
- Chat
- Social
ids:
- 62889
- 54390
-
name: COMPLETE
services:
- Chat
- Social
- Xmpp
- Mail
- Tunnel
ids:
- 62123
- 56584
- 64655
-
name: GROUP_COMPLETE
services:
- Chat
- Social
- Xmpp
- Mail
- Tunnel
- Group
ids:
- 62124
- 56586
- 64657
-
name: GROUP_BASIC
services:
- Chat
- Social
ids:
- 62887
- 54393
......@@ -15,6 +15,6 @@
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
<input type="submit" value="{% trans 'Register >>' %}" />
</form>
{% endblock %}
......@@ -100,18 +100,6 @@ SPDX-License-Identifier: AGPL-3.0
</table>
<h2>{% trans "Downloads" %}</h2>
<ul>
{% if has_limit["TUNNEL"] %}
<li><a href="{% url 'ovpn_userfile' %}">{% trans "Tunnel certificate" %}</a></li>
</li>
{% endif %}
{% if has_limit["NONE"] %}
<li>None. If you have just subscribed, please refresh after 5 mins and your downloads will appear.</li>
{% endif %}
</ul>
{% if has_limit["GROUP"]%}
<h2>{% trans "Group members" %}</h2>
......
from django.core.management.base import BaseCommand
from django.conf import settings
from django.utils import timezone
from limitmonitor.models import ExternalBundle
import argparse
import strictyaml
from purist.limitmonitor import ServicesContainer
class Command(BaseCommand):
help = 'Generates ExternalBundle entries as specified in yaml input file'
def add_arguments(self, parser):
parser.add_argument('file', type=argparse.FileType('r'))
def handle(self, *args, **options):
infile = options['file']
with open(infile.name, 'r') as stream:
bundles = strictyaml.load(stream.read()).data
# ordered dict with uppercase service name as keys used to get
# service value from name
ser_values = {n.upper(): v for v, n in ServicesContainer.MAP.items()}
now = timezone.now()
for bundle in bundles:
msg = 'Proccessing {} bundle.'.format(bundle['name'])
self.stdout.write(msg)
count = 0
for id in bundle['ids']:
for service in bundle['services']:
ser_val = ser_values[service.upper()]
cur_bund = ExternalBundle.objects.filter(
external_key=id).filter(service=ser_val)
if len(cur_bund) == 0:
eb = ExternalBundle(
parser=settings.LM_PARSERS.WOO_SUBSCRIPTION_V1,
external_key=id,
service=ser_val,
created_date=now
)
eb.save()
count += 1
msg = ' {} entries added.'.format(count)
self.stdout.write(msg)
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-04-08 09:40
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('limitmonitor', '0006_delete_credit_model'),
]
operations = [
migrations.AlterField(
model_name='externalbundle',
name='service',
field=models.IntegerField(choices=[(0, 'Undefined'), (1, 'Tunnel'), (2, 'Chat'), (3, 'Mail'), (4, 'Social'), (5, 'XMPP'), (6, 'Group')], default=0),
),
migrations.AlterField(
model_name='limit',
name='service',
field=models.IntegerField(choices=[(0, 'Undefined'), (1, 'Tunnel'), (2, 'Chat'), (3, 'Mail'), (4, 'Social'), (5, 'XMPP'), (6, 'Group')], default=0),
),
]
......@@ -128,6 +128,8 @@ def activate_single_limit(limit, credit_timedelta=None, renewal_date=None):
limit.user.set_ldap_group("social")
elif limit.service == settings.LM_SERVICES.XMPP:
limit.user.set_ldap_group("xmpp")
elif limit.service == settings.LM_SERVICES.GROUP:
pass # Nothing to do for GROUP
else:
raise Exception("Invalid activation attempt. Service not recognised.")
......
......@@ -170,9 +170,12 @@ WOO_CONSUMER_SECRET = secret_config("WOO_CONSUMER_SECRET")
WOO_CART_BASIC = config("WOO_CART_BASIC", cast=Csv(str))
WOO_CART_COMPLETE = config("WOO_CART_COMPLETE", cast=Csv(str))
WOO_CART_GROUP = config("WOO_CART_GROUP", cast=Csv(str))
WOO_CART_GROUP_BASIC = config("WOO_CART_GROUP_BASIC", cast=Csv(str))
WOO_CART_GROUP_COMPLETE = config("WOO_CART_GROUP_COMPLETE", cast=Csv(str))
WOO_CART_ZERO = config("WOO_CART_ZERO", cast=bool)
WOO_CART_999 = config("WOO_CART_999", cast=bool)
WOO_CART_5000 = config("WOO_CART_5000", cast=bool)
WOO_CART_5999 = config("WOO_CART_5999", cast=bool)
WOO_CART_BILLING_PATH = config("WOO_CART_BILLING_PATH")
WOO_CART_THANKS_PATH = config("WOO_CART_THANKS_PATH")
......
......@@ -17,7 +17,6 @@ from limitmonitor.tunnel import TunnelManager
from cryptography.fernet import Fernet
from choicesenum import ChoicesEnum
from choicesenum.django.fields import EnumIntegerField
from invitation.models import Invitation
from woocommerce import API as WOO_API
......@@ -117,10 +116,16 @@ class User(AbstractUser):
ldap_person.change_password(raw_password)
def set_ldap_group(self, group_id):
pass
ldap_person = self.get_ldap()
ldap_group = LdapGroup.objects.get(cn=group_id)
ldap_group.members.append(ldap_person.build_dn())
ldap_group.save()
def remove_ldap_group(self, group_id):
pass
ldap_person = self.get_ldap()
ldap_group = LdapGroup.objects.get(cn=group_id)
ldap_group.members.remove(ldap_person.build_dn())
ldap_group.save()
def woo_get_json(self, query):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment