Example #1
0
def add_member(request):
    """Add kiberpipa memeber with all the stuff"""
    if not request.user.is_staff:
        return

    form = NewMemberForm(request.POST or None)

    if request.method == "POST" and form.is_valid():
        # create ldap record
        password = ''.join(random.sample(string.letters + string.digits, 8))
        password_hash = ldap_salted_sha1.encrypt(password)

        ldif_template = get_template('org/member_add.ldif').render(Context(dict(
            data=form.cleaned_data,
            password_hash=password_hash,
        )))

        with tempfile.NamedTemporaryFile() as f:
            f.write(ldif_template.encode('utf-8'))
            f.flush()
            
            p = subprocess.Popen('sudo -u root ldapadd -D cn=admin,dc=kiberpipa,dc=org -f %s -w %s' % (f.name, settings.LDAP_PASSWORD),
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 shell=True)
            
            stdout, stderr = p.communicate()
            if p.returncode != 0:
                raise Exception("Failed adding a member to opendalp.")

        # add him to pipa-org
        if form.cleaned_data['add_to_private_mailinglist']:
            mailman_list = List.objects.get(id=2)
            try:
                mailman_list.subscribe(form.cleaned_data['email'])
            except:
                pass  # member is already subscribed

        # send email to new user
        html = get_template('mail/member_add_welcome_email.html').render(
            Context(dict(
                username=form.cleaned_data['username'],
                password=password,
        )))
        send_mail(u'Dobrodošel/a v Kiberpipi!',
                  html,
                  settings.DEFAULT_FROM_EMAIL,
                  [form.cleaned_data['email']])

        # add a diary we added a member
        diary = Diary(
            log_formal=u"Dodal novega člana: %s (%s %s)" % (form.cleaned_data['username'],
                                                            form.cleaned_data['firstname'],
                                                            form.cleaned_data['surname']),
            author=request.user,
            length=datetime.time(1),  # 1h
            task=Project.objects.get(id=2),
        )
        diary.save()

        return render_to_response(
            'org/member_add_success.html',
            {'email': form.cleaned_data['email']},
            context_instance=RequestContext(request))

    return render_to_response(
        'org/member_add.html',
        {'form': form},
        context_instance=RequestContext(request))
Example #2
0
def add_member(request):
    """Add kiberpipa memeber with all the stuff"""
    if not request.user.is_staff:
        return

    form = NewMemberForm(request.POST or None)

    if request.method == "POST" and form.is_valid():
        # create ldap record
        password = "".join(random.sample(string.letters + string.digits, 8))
        password_hash = ldap_salted_sha1.encrypt(password)

        uid = int(
            subprocess.Popen(
                "getent passwd | awk -F: '$3 < 3000 { print $3 }' | sort -n | tail -1",
                stdout=subprocess.PIPE,
                shell=True,
            )
            .communicate()[0]
            .strip()
        )
        uid += 1
        gid = int(
            subprocess.Popen(
                "getent group | awk -F: '$3 < 3000 { print $3 }' | sort -n | tail -1",
                stdout=subprocess.PIPE,
                shell=True,
            )
            .communicate()[0]
            .strip()
        )
        gid += 1

        ldif_template = get_template("org/member_add.ldif").render(
            Context(dict(data=form.cleaned_data, password_hash=password_hash, uid=uid, gid=gid))
        )

        with tempfile.NamedTemporaryFile() as f:
            f.write(ldif_template.encode("utf-8"))
            f.flush()
            subprocess.check_call(
                "sudo -u root ldapadd -D cn=admin,dc=kiberpipa,dc=org -f %s -w %s" % (f.name, settings.LDAP_PASSWORD),
                shell=True,
            )

        # create home folder
        # TODO: dogbert login
        # subprocess.check_call('sudo -u root mkdir -p /home/%s' % form.cleaned_data['username'],
        #                      shell=True)
        # TODO: chown it (sudoers should be very strict about this)
        # subprocess.check_call('sudo -u root chown -p /home/%s' % form.cleaned_data['username'],
        #                      shell=True)

        # TODO: add member to redmine group

        # add him to pipa-org
        if form.cleaned_data["add_to_private_mailinglist"]:
            mailman_list = List.objects.get(id=2)
            try:
                mailman_list.subscribe(form.cleaned_data["email"])
            except:
                pass  # member is already subscribed

        # send email to new user
        html = get_template("mail/member_add_welcome_email.html").render(
            Context(dict(username=form.cleaned_data["username"], password=password))
        )
        send_mail(u"Dobrodošel/a v Kiberpipi!", html, settings.DEFAULT_FROM_EMAIL, [form.cleaned_data["email"]])

        # add a diary we added a member
        diary = Diary(
            log_formal=u"Dodal novega člana: %s (%s %s)"
            % (form.cleaned_data["username"], form.cleaned_data["firstname"], form.cleaned_data["surname"]),
            author=request.user,
            length=datetime.time(1),  # 1h
            task=Project.objects.get(id=2),
        )
        diary.save()

        return render_to_response(
            "org/member_add_success.html",
            {"email": form.cleaned_data["email"]},
            context_instance=RequestContext(request),
        )

    return render_to_response("org/member_add.html", {"form": form}, context_instance=RequestContext(request))