Skip to content

Commit

Permalink
feat: add admin verification in template
Browse files Browse the repository at this point in the history
  • Loading branch information
Márcio Gabriel committed Nov 9, 2024
1 parent 06645dc commit c8b8ec1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
4 changes: 4 additions & 0 deletions flask_backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sqlalchemy.orm import Mapped, relationship

from flask_backend.db import Base
from flask_backend.utils.enums.role import RoleEnum

user_has_roles = Table(
"user_has_roles",
Expand All @@ -23,6 +24,9 @@ class User(Base):
"Role", secondary=user_has_roles, back_populates="users"
)

def is_admin(self):
return RoleEnum.ADMIN in [role.role for role in self.roles]


class Role(Base):
__tablename__ = "roles"
Expand Down
8 changes: 4 additions & 4 deletions flask_backend/seeds/user_seeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

def _build_roles():
user_role = Role(
id=RoleEnum.USER.id,
role=RoleEnum.USER.role,
id=1,
role=RoleEnum.USER,
)

admin_role = Role(
id=RoleEnum.ADMIN.id,
role=RoleEnum.ADMIN.role,
id=2,
role=RoleEnum.ADMIN,
)

return [user_role, admin_role]
Expand Down
6 changes: 2 additions & 4 deletions flask_backend/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<a class="nav-link" href="{{ url_for("movie.index") }}">Filmes</a>
</li>
{% if g.user %}
{% if g.user and RoleEnum.ADMIN.role in g.user.roles %}
{% if g.user.is_admin() %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for("screening.import_screenings") }}">Importar
sessões</a>
Expand Down Expand Up @@ -156,9 +156,7 @@
</li>
<div class="vr mx-3"></div>
{% if g.user %}
<p>{{ g.user.roles }}</p>
<p>{{ RoleEnum.ADMIN.role }}</p>
{% if g.user and RoleEnum.ADMIN.role in g.user.roles %}
{% if g.user.is_admin() %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for("screening.import_screenings") }}">Importar sessões</a>
</li>
Expand Down
20 changes: 4 additions & 16 deletions flask_backend/utils/enums/role.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
from enum import Enum
from enum import StrEnum


class RoleEnum(Enum):
USER = ("USER", 1)
ADMIN = ("ADMIN", 2)

def __init__(self, role_name: str, role_id: int):
self.role_name = role_name
self.role_id = role_id

@property
def role(self):
return self.role_name

@property
def id(self):
return self.role_id
class RoleEnum(StrEnum):
USER = "USER"
ADMIN = "ADMIN"

0 comments on commit c8b8ec1

Please sign in to comment.