-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
128 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""merge heads | ||
Revision ID: e63f024c23eb | ||
Revises: 0c0978bc2925, 3f455aaf9065 | ||
Create Date: 2024-08-10 11:54:28.435165 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = 'e63f024c23eb' | ||
down_revision: Union[str, None] = ('0c0978bc2925', '3f455aaf9065') | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
pass | ||
|
||
|
||
def downgrade() -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from sqlalchemy import Column, String | ||
from api.v1.models.base_model import BaseTableModel | ||
from uuid_extensions import uuid7 | ||
from api.v1.models.permissions.role_permissions import role_permissions | ||
from sqlalchemy.orm import relationship | ||
|
||
class Permission(BaseTableModel): | ||
__tablename__ = 'permissions' | ||
|
||
title = Column(String, unique=True, nullable=False) | ||
|
||
title = Column(String, unique=True, nullable=False) | ||
|
||
role = relationship('Role', secondary=role_permissions, back_populates='permissions') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
from sqlalchemy import Column, String, Boolean, Text | ||
from sqlalchemy.orm import relationship | ||
from api.v1.models.base_model import BaseTableModel | ||
from uuid_extensions import uuid7 | ||
from api.v1.models.permissions.role_permissions import role_permissions | ||
|
||
class Role(BaseTableModel): | ||
__tablename__ = 'roles' | ||
|
||
name = Column(String, unique=True, nullable=False) | ||
description = Column(Text, nullable=True) | ||
is_builtin = Column(Boolean, default=False) # True for built-in roles, False for custom roles | ||
|
||
permissions = relationship('Permission', secondary=role_permissions, back_populates='role') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from api.db.database import get_db | ||
from api.v1.models.permissions.role import Role | ||
from api.v1.models.permissions.permissions import Permission | ||
|
||
db = next(get_db()) | ||
|
||
def populate_roles_and_permissions(): | ||
'''Function to populate database with roles and permissions''' | ||
|
||
# Define roles | ||
roles = [ | ||
{"name": "admin", "description": "Administrator with full access", "is_builtin": True}, | ||
{"name": "user", "description": "Regular user with limited access", "is_builtin": True}, | ||
# {"name": "Manager", "description": "Manager with management access", "is_builtin": False}, | ||
] | ||
|
||
# Define permissions | ||
permissions = [ | ||
{"title": "create_user"}, | ||
{"title": "delete_user"}, | ||
{"title": "update_user"}, | ||
{"title": "view_user"}, | ||
{"title": "manage_organisation"}, | ||
{"title": "delete_organisation"}, | ||
] | ||
|
||
# Insert roles into the database | ||
for role_data in roles: | ||
if not db.query(Role).filter(Role.name == role_data['name']).first(): | ||
role = Role( | ||
name=role_data["name"], | ||
description=role_data["description"], | ||
is_builtin=role_data["is_builtin"] | ||
) | ||
db.add(role) | ||
db.commit() | ||
db.refresh(role) | ||
|
||
# Insert permissions into the database | ||
for perm_data in permissions: | ||
if not db.query(Permission).filter(Permission.title == perm_data['title']).first(): | ||
permission = Permission(title=perm_data["title"]) | ||
db.add(permission) | ||
db.commit() | ||
db.refresh(permission) | ||
|
||
# Assign permissions to roles (example) | ||
admin_role = db.query(Role).filter_by(name="admin").first() | ||
user_role = db.query(Role).filter_by(name="user").first() | ||
|
||
if not admin_role and not user_role: | ||
admin_permissions = db.query(Permission).all() | ||
user_permissions = db.query(Permission).filter(Permission.title == "view_user").all() | ||
|
||
admin_role.permissions.extend(admin_permissions) | ||
user_role.permissions.extend(user_permissions) | ||
|
||
db.commit() | ||
|
||
db.close() |