예제 #1
0
def create_group():
    if not authorize.has_role("admin", "group manager"):
        return render_template("401.html")

    # We have to add these fields dynamically based on the projects in the database
    projects = Project.query.all()

    form_copy = deepcopy(EditGroupForm)
    form_copy, project_names = _bind_special_projects_to_form(
        form_copy, projects)

    form = form_copy()
    users = User.query.all()
    form.group_members.choices = [(user.username, user.username)
                                  for user in users]

    if form.validate_on_submit():

        _process_group_form_data(form)

        flash(
            f"The group {form.data['group_name']} has been successfully created"
        )
        return render_template("admin/manage_groups.html")

    return render_template("admin/create_group.html",
                           form=form,
                           special_project_names=project_names)
예제 #2
0
def my_account():

    current_user = get_current_user()

    # Redirect to admin version if logged in user is admin
    if authorize.has_role("admin"):
        return redirect(url_for("admin.manage_user", user_id=current_user.id))

    if current_user is None:
        return redirect(url_for("auth.login"))

    project_association = UserProjectAssociation.query.filter_by(
        entity_id=current_user.id
    ).all()

    projects = []
    if project_association:
        projects = [x.projects for x in project_association]

    form = UpdateAccountInfoForm
    form, project_names = _bind_user_projects_to_form(
        form, projects, user=current_user, new=False
    )

    form = form()

    if request.method == "GET":
        form.first_name.data = current_user.first_name
        form.last_name.data = current_user.last_name
        form.email.data = current_user.email

    if request.method == "POST":

        try:
            form.email.validators.remove(_validate_email)
        except ValueError:
            pass

        if form.validate():
            current_user.first_name = form.first_name.data
            current_user.last_name = form.last_name.data
            current_user.email = form.email.data

            if form.password:
                current_user.password = form.password.data

            db.session.add(current_user)
            db.session.commit()

            flash("Your account has been updated.")

            return redirect(url_for("main.index"))

    return render_template(
        "auth/manage_account.html", form=form, user=current_user, projects=project_names
    )
예제 #3
0
def delete_group(group_id):
    # Permissions check
    if not authorize.has_role("admin", "group manager"):
        return render_template("401.html")

    form = DeleteGroupForm()

    if request.method == "POST":

        specified_group = Group.query.filter_by(
            name=form.group_name.data).one_or_none()

        if specified_group is None or str(specified_group.id) != group_id:
            form.group_name.validators.append(_validate_user_delete)
            flash("The input group name did not match the requested group.")

        if form.validate():
            db.session.delete(specified_group)
            db.session.commit()
            flash(f"Group {specified_group.name} removed from the dashboard.")

            return render_template("admin/manage_groups.html")

    return render_template("admin/delete_group.html", form=form)
예제 #4
0
def delete_user(user_id):
    # Permissions check
    if not authorize.has_role("admin"):
        return render_template("401.html")

    user_remove = User.query.filter(User.id == user_id).one()

    if user_remove.id == get_current_user().id:
        flash("You cannot remove your own account from the dashboard.")
        return render_template("admin/manage_users.html")

    form = DeleteUserForm()

    try:
        form.username.validators.remove(_validate_username)
    except ValueError:
        pass

    if request.method == "POST":

        specified_user = User.query.filter_by(
            username=form.username.data).one_or_none()

        if specified_user is None or str(specified_user.id) != user_id:
            form.username.validators.append(_validate_user_delete)
            flash("The input username did not match the requested user.")

        if form.validate():
            db.session.delete(specified_user)
            db.session.commit()
            flash(
                f"User {specified_user.username} removed from the dashboard.")

            return render_template("admin/manage_users.html")

    return render_template("admin/delete_user.html", form=form)
예제 #5
0
def create_user():

    if not authorize.has_role("admin"):
        return render_template("401.html")

    projects = Project.query.all()

    form = deepcopy(CreateUserForm)
    form, project_names = _bind_user_projects_to_form(form, projects=projects)
    form = form()
    form.groups.choices = [(g.name, g.name) for g in Group.query.all()]
    form.roles.choices = [(r.name, r.name) for r in Role.query.all()]

    if form.validate_on_submit():
        processed_form = _process_user_body(form.data)

        if isinstance(processed_form, Response):
            flash(
                "Creating the user failed because of problems with the input data. "
                "Please check the inputs and try again.")
            return redirect(url_for("admin.create_user"))

        else:
            db.session.add(processed_form)
            db.session.commit()

            _process_user_permissions(form, processed_form)

            flash(
                f"The user {form.data['username']} has been successfully created"
            )
            return render_template("admin/manage_users.html")

    return render_template("admin/create_user.html",
                           form=form,
                           projects=project_names)
예제 #6
0
def manage_user(user_id):

    # Permissions check
    if not authorize.has_role("admin"):
        return render_template("401.html")

    # Get the user information
    user = User.query.get(user_id)

    if not user:
        return render_template("404.html")

    projects = Project.query.all()

    form = deepcopy(ManageUserFormAdmin)
    form, project_names = _bind_user_projects_to_form(form,
                                                      projects=projects,
                                                      user=user,
                                                      new=False)
    form = form()

    form.groups.choices = [(g.name, g.name) for g in Group.query.all()]
    form.roles.choices = [(r.name, r.name) for r in Role.query.all()]

    # Set defaults based on user
    if request.method == "GET":
        form.username.data = user.username
        form.groups.data = [g.name for g in user.groups]
        form.roles.data = [r.name for r in user.roles]
        form.first_name.data = user.first_name
        form.last_name.data = user.last_name
        form.email.data = user.email

    if request.method == "POST":

        # Each of these has to be tried - two separate try statements
        try:
            form.username.validators.remove(_validate_username)
        except ValueError:
            pass

        try:
            form.email.validators.remove(_validate_email)
        except ValueError:
            pass

        # field has been attempted to be updated and we
        # must check the input
        if form.username.data != user.username:
            if User.query.filter(User.username == form.username.data).first():
                form.username.validators.append(_validate_username)

        if form.email.data != user.email:
            if User.query.filter(User.email == form.email.data).first():
                form.email.validators.append(_validate_email)

        # use validate instead of validate_on_submit so we can add our own validators
        # (lines above)
        if form.validate():

            user = _process_user_body(form.data, original_user_data=user)
            db.session.add(user)
            db.session.commit()
            _process_user_permissions(form, user)
            flash(
                f"The user {form.data['username']} has been successfully updated."
            )
            return render_template("admin/manage_users.html")

    return render_template(
        "admin/create_user.html",
        form=form,
        username=user.username,
        user_id=user.id,
        projects=project_names,
    )
예제 #7
0
def manage_group(group_id):
    # Permissions check
    if not authorize.has_role("admin", "group manager"):
        return render_template("401.html")

    # Get the group information
    group = Group.query.get(group_id)

    if not group:
        return render_template("404.html")

    # We have to add project fields dynamically based on the projects in the database

    # Get projects owned by the group
    owned_projects = Project.query.filter_by(group_id=group.id).all()

    # Get all projects in db
    projects = Project.query.all()

    # special projects
    special_projects = list(set(projects) - set(owned_projects))

    form_copy = deepcopy(EditGroupForm)
    form_copy, special_projects = _bind_special_projects_to_form(
        form_copy, special_projects, new=False, group=group)
    form_copy, owned_projects = _bind_owned_projects_to_form(form_copy,
                                                             owned_projects,
                                                             new=False,
                                                             group=group)

    form = form_copy()
    users = User.query.all()
    form.group_members.choices = [(user.username, user.username)
                                  for user in users]

    # Set defaults
    if request.method == "GET":
        form.group_name.data = group.name
        form.group_members.data = [g.username for g in group.users]

    if request.method == "POST":
        try:
            form.group_name.validators.remove(_validate_group)
        except ValueError:
            pass

        # field has been attempted to be updated and we
        # must check the input
        if form.group_name.data != group.name:
            if Group.query.filter(Group.name == form.group_name.data).first():
                form.group_name.validators.append(_validate_group)

        # use validate instead of validate_on_submit so we can add our own validators
        # (lines above)
        if form.validate():

            _process_group_form_data(form)

            db.session.commit()
            flash(
                f"The group {form.data['group_name']} has been successfully updated."
            )
            return render_template("admin/manage_groups.html")

    return render_template(
        "admin/create_group.html",
        form=form,
        owned_project_names=owned_projects,
        special_project_names=special_projects,
        group_name=group.name,
        group_id=group.id,
    )
예제 #8
0
def manage_groups():
    if not authorize.has_role("admin", "group manager"):
        return render_template("401.html")
    return render_template("admin/manage_groups.html")
예제 #9
0
def manage_users():
    if not authorize.has_role("admin"):
        return render_template("401.html")
    return render_template("admin/manage_users.html")