mirror of
https://github.com/Cian-H/invenio-theme-iform.git
synced 2026-08-14 23:53:22 +01:00
Added admin panel access with Role Assignment
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2024 I-Form.
|
||||
#
|
||||
# invenio-theme-iform is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the MIT License; see LICENSE file for more
|
||||
# details.
|
||||
|
||||
"""Administration views."""
|
||||
@@ -0,0 +1,78 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2024 I-Form.
|
||||
#
|
||||
# invenio-theme-iform is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the MIT License; see LICENSE file for more
|
||||
# details.
|
||||
|
||||
"""I-Form role management administration views."""
|
||||
|
||||
from flask import flash, redirect, request, url_for
|
||||
from invenio_accounts.models import Role, User
|
||||
from invenio_accounts.proxies import current_accounts
|
||||
from invenio_administration.views.base import AdminView
|
||||
from invenio_db import db
|
||||
from invenio_i18n import lazy_gettext as _
|
||||
|
||||
|
||||
class RoleManagementView(AdminView):
|
||||
"""Admin view for managing user roles."""
|
||||
|
||||
name = "roles"
|
||||
category = "User management"
|
||||
template = "invenio_theme_iform/administration/roles.html"
|
||||
url = "/roles"
|
||||
menu_label = _("Role Assignment")
|
||||
icon = "users"
|
||||
|
||||
def get(self):
|
||||
"""Render the role management template."""
|
||||
roles = Role.query.order_by(Role.name).all()
|
||||
selected_role_id = request.args.get("role_id")
|
||||
|
||||
users = []
|
||||
selected_role = None
|
||||
if selected_role_id:
|
||||
selected_role = Role.query.get(selected_role_id)
|
||||
if selected_role:
|
||||
users = User.query.order_by(User.email).all()
|
||||
|
||||
return self.render(
|
||||
roles=roles,
|
||||
selected_role=selected_role,
|
||||
users=users,
|
||||
)
|
||||
|
||||
def post(self):
|
||||
"""Handle role assignment/revocation."""
|
||||
user_id = request.form.get("user_id")
|
||||
role_id = request.form.get("role_id")
|
||||
action = request.form.get("action")
|
||||
|
||||
if not (user_id and role_id and action):
|
||||
flash(_("Missing required fields."), "error")
|
||||
return redirect(url_for("administration.roles", role_id=role_id))
|
||||
|
||||
try:
|
||||
user = User.query.get(int(user_id))
|
||||
except ValueError:
|
||||
user = None
|
||||
|
||||
role = Role.query.get(role_id)
|
||||
|
||||
if not user or not role:
|
||||
flash(_("User or role not found."), "error")
|
||||
return redirect(url_for("administration.roles", role_id=role_id))
|
||||
|
||||
if action == "enable":
|
||||
if role not in user.roles:
|
||||
current_accounts.datastore.add_role_to_user(user, role)
|
||||
flash(_("Role {role} enabled for {email}.").format(role=role.name, email=user.email), "success")
|
||||
elif action == "disable":
|
||||
if role in user.roles:
|
||||
current_accounts.datastore.remove_role_from_user(user, role)
|
||||
flash(_("Role {role} disabled for {email}.").format(role=role.name, email=user.email), "success")
|
||||
|
||||
db.session.commit()
|
||||
return redirect(url_for("administration.roles", role_id=role_id))
|
||||
@@ -22,6 +22,13 @@
|
||||
<i class="user icon"></i> {{ current_user.email }}
|
||||
</a>
|
||||
</div>
|
||||
{%- for item in current_menu.submenu('profile-admin').children if item.visible %}
|
||||
<div class="short-menu-right-button">
|
||||
<a class="dropdown-item no-decoration" href="{{ item.url }}">
|
||||
{{ item.text|safe }}
|
||||
</a>
|
||||
</div>
|
||||
{%- endfor %}
|
||||
<div class="short-menu-right-button">
|
||||
<a class="dropdown-item no-decoration" href="{{ url_for_security('logout') }}"
|
||||
>{{ _("Sign out") }}</a
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
{#
|
||||
Copyright (C) 2024 I-Form.
|
||||
|
||||
invenio-theme-iform is free software; you can redistribute it and/or modify it
|
||||
under the terms of the MIT License; see LICENSE file for more details.
|
||||
#}
|
||||
{% extends "invenio_administration/base.html" %}
|
||||
|
||||
{% block admin_page_content %}
|
||||
<div class="ui container dashboard rel-mt-5">
|
||||
<div class="ui grid">
|
||||
<div class="sixteen wide column">
|
||||
<h2 class="ui header">
|
||||
<i class="users icon"></i>
|
||||
<div class="content">
|
||||
{{ _("Role Assignment") }}
|
||||
<div class="sub header">{{ _("Select a role and enable or disable it for users.") }}</div>
|
||||
</div>
|
||||
</h2>
|
||||
|
||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||
{% if messages %}
|
||||
{% for category, message in messages %}
|
||||
<div class="ui {{ 'positive' if category == 'success' else 'negative' }} message">
|
||||
{{ message }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
<div class="ui segment">
|
||||
<form method="GET" class="ui form">
|
||||
<div class="field">
|
||||
<label>{{ _("Select a Role") }}</label>
|
||||
<div class="ui action input">
|
||||
<select name="role_id" class="ui dropdown">
|
||||
<option value="">{{ _("-- Select a role --") }}</option>
|
||||
{% for r in roles %}
|
||||
<option value="{{ r.id }}" {% if selected_role and selected_role.id == r.id %}selected{% endif %}>
|
||||
{{ r.name }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<button class="ui button primary" type="submit">{{ _("Select") }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{% if selected_role %}
|
||||
<div class="ui segment">
|
||||
<h3 class="ui header">
|
||||
{{ _("Users assigned to:") }} {{ selected_role.name }}
|
||||
{% if selected_role.description %}
|
||||
<div class="sub header" style="margin-top: 5px;">
|
||||
<i class="info circle icon"></i> {{ selected_role.description }}
|
||||
</div>
|
||||
{% endif %}
|
||||
</h3>
|
||||
<table class="ui celled striped table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ _("Email") }}</th>
|
||||
<th>{{ _("Active") }}</th>
|
||||
<th class="center aligned">{{ _("Has Role?") }}</th>
|
||||
<th class="center aligned">{{ _("Actions") }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for user in users %}
|
||||
<tr>
|
||||
<td>{{ user.email }}</td>
|
||||
<td>
|
||||
{% if user.active %}
|
||||
<div class="ui green horizontal label">{{ _("Active") }}</div>
|
||||
{% else %}
|
||||
<div class="ui grey horizontal label">{{ _("Inactive") }}</div>
|
||||
{% endif %}
|
||||
</td>
|
||||
|
||||
{% set has_role = selected_role in user.roles %}
|
||||
<td class="center aligned">
|
||||
{% if has_role %}
|
||||
<i class="green check large icon"></i>
|
||||
{% else %}
|
||||
<i class="red times large icon"></i>
|
||||
{% endif %}
|
||||
</td>
|
||||
|
||||
<td class="center aligned">
|
||||
<form method="POST" style="display:inline;">
|
||||
<input type="hidden" name="user_id" value="{{ user.id }}">
|
||||
<input type="hidden" name="role_id" value="{{ selected_role.id }}">
|
||||
{% if has_role %}
|
||||
<input type="hidden" name="action" value="disable">
|
||||
<button class="ui tiny red button" type="submit">
|
||||
<i class="minus icon"></i> {{ _("Disable") }}
|
||||
</button>
|
||||
{% else %}
|
||||
<input type="hidden" name="action" value="enable">
|
||||
<button class="ui tiny green button" type="submit">
|
||||
<i class="plus icon"></i> {{ _("Enable") }}
|
||||
</button>
|
||||
{% endif %}
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="4" class="center aligned">{{ _("No users found.") }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -55,6 +55,10 @@ invenio_theme_iform = "invenio_theme_iform.config"
|
||||
[project.entry-points."invenio_base.finalize_app"]
|
||||
invenio_theme_iform = "invenio_theme_iform.ext:finalize_app"
|
||||
|
||||
[project.entry-points."invenio_administration.views"]
|
||||
invenio_theme_iform_roles = "invenio_theme_iform.administration.views:RoleManagementView"
|
||||
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
"hatch>=1.14.1",
|
||||
|
||||
Reference in New Issue
Block a user