Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 10 additions & 98 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,16 @@ on:
types: [ created ]

jobs:

# -------------------------
# AMD64 Build
# -------------------------
build-amd64:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Normalize repo and sanitize ref
run: |
echo "REPO_LOWER=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV
SAFE_REF=$(echo "${GITHUB_REF_NAME}" | tr / -)
echo "SAFE_REF=$SAFE_REF" >> $GITHUB_ENV

- name: Build and push AMD64 image
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile
build-args: SOURCE_REPO=https://github.com/${{ github.repository }}
platforms: linux/amd64
push: true
tags: |
ghcr.io/${{ env.REPO_LOWER }}:${{ env.SAFE_REF }}-amd64
${{ github.ref_name == 'main' && format('ghcr.io/{0}:latest-amd64', env.REPO_LOWER) || '' }}
cache-from: type=gha
cache-to: type=gha,mode=max

# -------------------------
# ARM64 Build
# -------------------------
build-arm64:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker daemon (containerd features)
uses: docker/setup-docker-action@v4
with:
daemon-config: |
{
"debug": true,
"features": { "containerd-snapshotter": true }
}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

Expand All @@ -82,60 +31,23 @@ jobs:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Normalize repo and sanitize ref
- name: Normalise repo and sanitise ref
run: |
echo "REPO_LOWER=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV
SAFE_REF=$(echo "${GITHUB_REF_NAME}" | tr / -)
echo "SAFE_REF=$SAFE_REF" >> $GITHUB_ENV

- name: Build and push ARM64 image
- name: Build and push multi-arch image
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile
build-args: BUILDKIT_PROGRESS=plain,SOURCE_REPO=https://github.com/${{ github.repository }}
platforms: linux/arm64
build-args: |
SOURCE_REPO=https://github.com/${{ github.repository }}
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: |
ghcr.io/${{ env.REPO_LOWER }}:${{ env.SAFE_REF }}-arm64
${{ github.ref_name == 'main' && format('ghcr.io/{0}:latest-arm64', env.REPO_LOWER) || '' }}
cache-from: type=gha,scope=arm64
cache-to: type=gha,scope=arm64,mode=max

# -------------------------
# Create multi-arch manifest
# -------------------------
manifest:
needs: [ build-amd64, build-arm64 ]
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx (for imagetools)
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Normalize repo and sanitize ref
run: |
echo "REPO_LOWER=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV
SAFE_REF=$(echo "${GITHUB_REF_NAME}" | tr / -)
echo "SAFE_REF=$SAFE_REF" >> $GITHUB_ENV

- name: Create multi-arch manifest for ref tag
run: |
docker buildx imagetools create \
--tag ghcr.io/${{ env.REPO_LOWER }}:${{ env.SAFE_REF }} \
ghcr.io/${{ env.REPO_LOWER }}:${{ env.SAFE_REF }}-amd64 \
ghcr.io/${{ env.REPO_LOWER }}:${{ env.SAFE_REF }}-arm64

- name: Also tag :latest on main
if: ${{ github.ref_name == 'main' }}
run: |
docker buildx imagetools create \
--tag ghcr.io/${{ env.REPO_LOWER }}:latest \
ghcr.io/${{ env.REPO_LOWER }}:latest-amd64 \
ghcr.io/${{ env.REPO_LOWER }}:latest-arm64
ghcr.io/${{ env.REPO_LOWER }}:${{ env.SAFE_REF }}
${{ github.ref_name == 'main' && format('ghcr.io/{0}:latest', env.REPO_LOWER) || '' }}
cache-from: type=gha
cache-to: type=gha,mode=max
2 changes: 2 additions & 0 deletions aegis/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# aegis/admin.py

from django.contrib import admin, messages
from django.contrib.auth.admin import UserAdmin
from django.utils.html import format_html
Expand Down
2 changes: 2 additions & 0 deletions aegis/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# aegis/forms.py

from django import forms
from django.contrib.auth.validators import UnicodeUsernameValidator
from django.utils.translation import gettext_lazy as _
Expand Down
2 changes: 2 additions & 0 deletions aegis/management/commands/initial_setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# aegis/management/commands/initial_setup.py

import os

from django.core.management import call_command
Expand Down
52 changes: 27 additions & 25 deletions aegis/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# aegis/models.py

import uuid

from django.conf import settings
Expand Down Expand Up @@ -104,31 +106,31 @@ def __str__(self):
return self.service_name


class AdminMenuMaster(BaseModel):
id = models.SmallAutoField(primary_key=True, db_column='id', db_index=True, editable=False, unique=True,
blank=False, null=False, verbose_name='ID')
parent_id = models.ForeignKey('self', null=True, blank=True, related_name='submenus', db_column='parent_id',
on_delete=models.CASCADE)
menu_name = models.CharField(max_length=30, null=False, blank=False, unique=True,
validators=[RegexValidator(regex=r'^[a-zA-Z0-9()\s]+$', message="Invalid characters")])
menu_icon = models.CharField(max_length=20, null=True, blank=True, default='list',
validators=[RegexValidator(regex=r'^[a-z0-9-]+$', message="Invalid characters")])
menu_route = models.CharField(max_length=30, null=True, blank=True,
validators=[RegexValidator(regex=r'^[a-zA-Z0-9\s-]+$', message="Invalid characters")])
menu_access = models.CharField(max_length=30, null=True, blank=True,
validators=[RegexValidator(regex=r'^[a-zA-Z0-9\s-]+$', message="Invalid characters")])
menu_order = models.SmallIntegerField(null=True, blank=True,
validators=[RegexValidator(regex=r'^[0-9]+$', message="Invalid characters")])

history = HistoricalRecords(table_name="admin_menu_master_history")

class Meta:
db_table = "admin_menu_master"
verbose_name = "Admin Menu"
verbose_name_plural = "Admin Menus"

def __str__(self):
return f"{self.menu_name} ({self.menu_route})"
# class AdminMenuMaster(BaseModel):
# id = models.SmallAutoField(primary_key=True, db_column='id', db_index=True, editable=False, unique=True,
# blank=False, null=False, verbose_name='ID')
# parent_id = models.ForeignKey('self', null=True, blank=True, related_name='submenus', db_column='parent_id',
# on_delete=models.CASCADE)
# menu_name = models.CharField(max_length=30, null=False, blank=False, unique=True,
# validators=[RegexValidator(regex=r'^[a-zA-Z0-9()\s]+$', message="Invalid characters")])
# menu_icon = models.CharField(max_length=20, null=True, blank=True, default='list',
# validators=[RegexValidator(regex=r'^[a-z0-9-]+$', message="Invalid characters")])
# menu_route = models.CharField(max_length=30, null=True, blank=True,
# validators=[RegexValidator(regex=r'^[a-zA-Z0-9\s-]+$', message="Invalid characters")])
# menu_access = models.CharField(max_length=30, null=True, blank=True,
# validators=[RegexValidator(regex=r'^[a-zA-Z0-9\s-]+$', message="Invalid characters")])
# menu_order = models.SmallIntegerField(null=True, blank=True,
# validators=[RegexValidator(regex=r'^[0-9]+$', message="Invalid characters")])
#
# history = HistoricalRecords(table_name="admin_menu_master_history")
#
# class Meta:
# db_table = "admin_menu_master"
# verbose_name = "Admin Menu"
# verbose_name_plural = "Admin Menus"
#
# def __str__(self):
# return f"{self.menu_name} ({self.menu_route})"


class PermissionMaster(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion aegis/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# serializers.py
# aegis/serializers.py

from typing import cast, TYPE_CHECKING

Expand Down
39 changes: 0 additions & 39 deletions aegis/signals.py
Original file line number Diff line number Diff line change
@@ -1,39 +0,0 @@
'''
Use Case: Signals are more suited for logging events related to model changes, such as creation, update, or deletion of
Django model instances.

Granularity: Signals can be more specific, logging only when certain actions occur on models.

Decoupling: Signals offer a higher degree of decoupling since they are not tied to the request-response cycle.

Signal Registration:

The @receiver(post_save, sender=UserActivityLog) decorator is used to register the log_user_activity function as a
receiver for the post_save signal for the UserActivityLog model. This means that log_user_activity will be called every
time a UserActivityLog instance is saved.
Signal Receiver Function - log_user_activity:

This function is triggered after a UserActivityLog instance is saved (after a new record is created or an existing
record is updated).
The function checks if a new record was created (if created:). If true, it formats a log entry string with details from
the UserActivityLog instance, such as user, method, activity, timestamp, and user_timezone.
Logging the Activity:

The formatted log entry is then logged using logger.info(log_entry).
The logger named 'backend.models' is configured to handle this log. According to LOGGING configuration in
settings.py, this logger writes the logs to the file specified by the 'file' handler (user_activity.log).
'''

# from django.db.models.signals import post_save
# from django.dispatch import receiver
# from backend.models import UserActivityLog
# import logging
#
# logger = logging.getLogger('backend.models')
#
#
# @receiver(post_save, sender=UserActivityLog)
# def log_user_activity(sender, instance, created, **kwargs):
# if created:
# log_entry = f"{instance.user} - {instance.method} {instance.activity} - {instance.timestamp} - {instance.user_timezone}" # Include user_timezone here
# logger.info(log_entry)
4 changes: 3 additions & 1 deletion aegis/templatetags/custom_filters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# aegis/templatetags/custom_filters.py

from django import template

register = template.Library()

register = template.Library()

@register.filter
def remove_chars(value):
Expand Down
78 changes: 40 additions & 38 deletions aegis/templatetags/permission_tags.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
from django import template
from aegis.models import CustomPermissions, GroupCustomPermissions, PermissionMaster, AdminMenuMaster

register = template.Library()


@register.simple_tag
def has_view_permission(user, menu_route):
if user.is_superuser or user.is_staff:
return True

if menu_route is None:
return False # Handle NULL menu_route values as you see fit

try:
# Get the AdminMenuMaster instance for the relevant menu
menus = AdminMenuMaster.objects.filter(menu_route=menu_route)
# menu = AdminMenuMaster.objects.get(menu_route=menu_route)

if menus.exists():
menu = menus.first()
else:
return False
# Get the PermissionMaster instance for 'view' action
permission_master = PermissionMaster.objects.get(menu=menu, action='view')
except (AdminMenuMaster.DoesNotExist, PermissionMaster.DoesNotExist):
return False

# Check if the user has the permission
has_permission = CustomPermissions.objects.filter(
user=user, permission_name=permission_master
).exists()

has_group_permission = GroupCustomPermissions.objects.filter(
group__in=user.groups.all(), permission_names__in=[permission_master]
).exists()

return has_permission or has_group_permission
# aegis/templatetags/permission_tags.py

# from django import template
# from aegis.models import CustomPermissions, GroupCustomPermissions, PermissionMaster, AdminMenuMaster
#
# register = template.Library()
#
#
# @register.simple_tag
# def has_view_permission(user, menu_route):
# if user.is_superuser or user.is_staff:
# return True
#
# if menu_route is None:
# return False # Handle NULL menu_route values as you see fit
#
# try:
# # Get the AdminMenuMaster instance for the relevant menu
# menus = AdminMenuMaster.objects.filter(menu_route=menu_route)
# # menu = AdminMenuMaster.objects.get(menu_route=menu_route)
#
# if menus.exists():
# menu = menus.first()
# else:
# return False
# # Get the PermissionMaster instance for 'view' action
# permission_master = PermissionMaster.objects.get(menu=menu, action='view')
# except (AdminMenuMaster.DoesNotExist, PermissionMaster.DoesNotExist):
# return False
#
# # Check if the user has the permission
# has_permission = CustomPermissions.objects.filter(
# user=user, permission_name=permission_master
# ).exists()
#
# has_group_permission = GroupCustomPermissions.objects.filter(
# group__in=user.groups.all(), permission_names__in=[permission_master]
# ).exists()
#
# return has_permission or has_group_permission
18 changes: 10 additions & 8 deletions aegis/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.urls import path
from .views import DashboardView

app_name = "aegis"

urlpatterns = [
path("dashboard/", DashboardView.as_view(), name="dashboard")
]
# # aegis/urls.py
#
# from django.urls import path
# # from .views import DashboardView
#
# app_name = "aegis"
#
# urlpatterns = [
# path("dashboard/", DashboardView.as_view(), name="dashboard")
# ]
Loading