Пример #1
0
def _make_newuser(request, form, context):
    """Creates a new user in LDAP and runs config_newuser
    if config_newuser fails, the user account is deleted to prevent the user
    account from being in limbo."""
    enroll_jobs = "true" if form.cleaned_data["enroll_jobs"] else "false"
    success, uid = create_new_user(
        form.cleaned_data["username"],
        form.cleaned_data["full_name"],
        form.cleaned_data["email"],
        form.cleaned_data["student_id"],
        form.cleaned_data["password"],
    )
    email = shlex.quote(form.cleaned_data["email"])
    username = shlex.quote(form.cleaned_data["username"])
    if success:
        config_newuser_process = subprocess.run(
            [
                "sudo",
                str(newuser_script), username, email,
                str(uid), enroll_jobs
            ],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )
        if config_newuser_process.returncode == 0:
            logger.info(
                f"New CSUA member! Username: {username} Email: {email}")
            return render(request, "create_success.html")
        else:
            messages.error(
                request,
                "Failed to run config_newuser. Please contact #website on slack for assistance.",
            )
            if delete_user(form.cleaned_data["username"]):
                logger.error(
                    f"Failed to run config_newuser. Username: {username} Email: {email}",
                    exc_info=None,
                    extra={"request": request},
                )
            else:
                logger.error(
                    f"Failed to run config_newuser and failed to delete user. Username: {username} Email: {email}"
                )
    else:
        if uid == -1:
            logger.error(
                f"Failed to bind as newuser. Username: {username} Email: {email}"
            )
            messages.error(
                request,
                "Internal error, failed to bind as newuser. Please report this to #website.",
            )
        else:
            messages.error(request, "Your username is already taken.")

    return render(request, "newuser.html", context)
Пример #2
0
    def test_create_new_user(self):
        max_uid = utils.get_max_uid()
        self.assertEquals(max_uid, 31337)

        success, uid_num = utils.create_new_user(
            "pnunez1",
            "Phillip E. Nunez",
            "*****@*****.**",
            3116969,
            "il0vedangengdg!",
        )
        self.assertTrue(success)
        self.assertEquals(uid_num, 31338)

        max_uid = utils.get_max_uid()
        self.assertEquals(max_uid, 31338)
Пример #3
0
def _make_newuser(request, form, context):
    """Creates a new user in LDAP and runs config_newuser"""
    enroll_jobs = "true" if form.cleaned_data["enroll_jobs"] else "false"
    success, uid = create_new_user(
        form.cleaned_data["username"],
        form.cleaned_data["full_name"],
        form.cleaned_data["email"],
        form.cleaned_data["student_id"],
        form.cleaned_data["password"],
    )
    if success:
        email = shlex.quote(form.cleaned_data["email"])
        username = shlex.quote(form.cleaned_data["username"])
        exit_code = subprocess.call(
            ["sudo", str(newuser_script), username, email, uid, enroll_jobs], shell=True
        ).returncode
        if exit_code == 0:
            logger.info("New user created: {0}".format(uid))
            return render(request, "create_success.html")
        else:
            messages.error(
                request,
                "Account created, but failed to run config_newuser. Please contact #website for assistance.",
            )
            logger.error("Account created, but failed to run config_newuser.")
            # TODO: delete user to roll back the newuser operation.
    else:
        if uid == -1:
            messages.error(
                request,
                "Internal error, failed to bind as newuser. Please report this to #website.",
            )
        else:
            messages.error(request, "Your username is already taken.")

    return render(request, "newuser.html", context)