mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-05 14:51:13 +02:00
Base API and tables for RBAC support.
This commit is contained in:
1 parent
eb0f8c6174
commit
6d4da98b8e
15 files changed
+1196
-42
No files matched your search
@@ -17,6 +17,8 @@
|
||||
|
||||
from .base import Base
|
||||
from .users import User, UserGroup
|
||||
from .roles import Role
|
||||
from .permissions import Permission
|
||||
from .computes import Compute
|
||||
from .templates import (
|
||||
Template,
|
||||
|
||||
@@ -19,7 +19,7 @@ import uuid
|
||||
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from sqlalchemy import Column, DateTime, func, inspect
|
||||
from sqlalchemy.types import TypeDecorator, CHAR
|
||||
from sqlalchemy.types import TypeDecorator, CHAR, VARCHAR
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.ext.declarative import as_declarative
|
||||
|
||||
@@ -72,6 +72,37 @@ class GUID(TypeDecorator):
|
||||
return value
|
||||
|
||||
|
||||
class ListException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ListType(TypeDecorator):
|
||||
"""
|
||||
Save/restore a Python list to/from a database column.
|
||||
"""
|
||||
|
||||
impl = VARCHAR
|
||||
cache_ok = True
|
||||
|
||||
def __init__(self, separator=',', *args, **kwargs):
|
||||
|
||||
self._separator = separator
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
if value is not None:
|
||||
if any(self._separator in str(item) for item in value):
|
||||
raise ListException(f"List values cannot contain '{self._separator}'"
|
||||
f"Please use a different separator.")
|
||||
return self._separator.join(map(str, value))
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
if value is None:
|
||||
return []
|
||||
else:
|
||||
return list(map(str, value.split(self._separator)))
|
||||
|
||||
|
||||
class BaseTable(Base):
|
||||
|
||||
__abstract__ = True
|
||||
@@ -79,6 +110,8 @@ class BaseTable(Base):
|
||||
created_at = Column(DateTime, server_default=func.current_timestamp())
|
||||
updated_at = Column(DateTime, server_default=func.current_timestamp(), onupdate=func.current_timestamp())
|
||||
|
||||
__mapper_args__ = {"eager_defaults": True}
|
||||
|
||||
|
||||
def generate_uuid():
|
||||
return str(uuid.uuid4())
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2021 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from sqlalchemy import Table, Column, String, ForeignKey, Boolean
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .base import Base, BaseTable, generate_uuid, GUID, ListType
|
||||
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
permission_role_link = Table(
|
||||
"permissions_roles_link",
|
||||
Base.metadata,
|
||||
Column("permission_id", GUID, ForeignKey("permissions.permission_id", ondelete="CASCADE")),
|
||||
Column("role_id", GUID, ForeignKey("roles.role_id", ondelete="CASCADE"))
|
||||
|
||||
)
|
||||
|
||||
|
||||
class Permission(BaseTable):
|
||||
|
||||
__tablename__ = "permissions"
|
||||
|
||||
permission_id = Column(GUID, primary_key=True, default=generate_uuid)
|
||||
methods = Column(ListType)
|
||||
path = Column(String)
|
||||
action = Column(String)
|
||||
roles = relationship("Role", secondary=permission_role_link, back_populates="permissions")
|
||||
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2021 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from sqlalchemy import Table, Column, String, Boolean, ForeignKey, event
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .base import Base, BaseTable, generate_uuid, GUID
|
||||
from .permissions import permission_role_link
|
||||
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
role_group_link = Table(
|
||||
"roles_groups_link",
|
||||
Base.metadata,
|
||||
Column("role_id", GUID, ForeignKey("roles.role_id", ondelete="CASCADE")),
|
||||
Column("user_group_id", GUID, ForeignKey("user_groups.user_group_id", ondelete="CASCADE"))
|
||||
)
|
||||
|
||||
|
||||
class Role(BaseTable):
|
||||
|
||||
__tablename__ = "roles"
|
||||
|
||||
role_id = Column(GUID, primary_key=True, default=generate_uuid)
|
||||
name = Column(String)
|
||||
description = Column(String)
|
||||
is_updatable = Column(Boolean, default=True)
|
||||
permissions = relationship("Permission", secondary=permission_role_link, back_populates="roles")
|
||||
groups = relationship("UserGroup", secondary=role_group_link, back_populates="roles")
|
||||
|
||||
|
||||
@event.listens_for(Role.__table__, 'after_create')
|
||||
def create_default_roles(target, connection, **kw):
|
||||
|
||||
default_roles = [
|
||||
{"name": "Administrator", "description": "Administrator role", "is_updatable": False},
|
||||
{"name": "User", "description": "User role", "is_updatable": False},
|
||||
]
|
||||
|
||||
stmt = target.insert().values(default_roles)
|
||||
connection.execute(stmt)
|
||||
connection.commit()
|
||||
log.info("The default roles have been created in the database")
|
||||
@@ -19,6 +19,8 @@ from sqlalchemy import Table, Boolean, Column, String, ForeignKey, event
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .base import Base, BaseTable, generate_uuid, GUID
|
||||
from .roles import role_group_link
|
||||
|
||||
from gns3server.config import Config
|
||||
from gns3server.services import auth_service
|
||||
|
||||
@@ -26,11 +28,11 @@ import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
users_group_members = Table(
|
||||
"users_group_members",
|
||||
user_group_link = Table(
|
||||
"users_groups_link",
|
||||
Base.metadata,
|
||||
Column("user_id", GUID, ForeignKey("users.user_id", ondelete="CASCADE")),
|
||||
Column("user_group_id", GUID, ForeignKey("users_group.user_group_id", ondelete="CASCADE"))
|
||||
Column("user_group_id", GUID, ForeignKey("user_groups.user_group_id", ondelete="CASCADE"))
|
||||
)
|
||||
|
||||
|
||||
@@ -45,7 +47,9 @@ class User(BaseTable):
|
||||
hashed_password = Column(String)
|
||||
is_active = Column(Boolean, default=True)
|
||||
is_superadmin = Column(Boolean, default=False)
|
||||
groups = relationship("UserGroup", secondary=users_group_members, back_populates="users")
|
||||
groups = relationship("UserGroup", secondary=user_group_link, back_populates="users")
|
||||
permission_id = Column(GUID, ForeignKey('permissions.permission_id', ondelete="CASCADE"))
|
||||
permissions = relationship("Permission")
|
||||
|
||||
|
||||
@event.listens_for(User.__table__, 'after_create')
|
||||
@@ -68,12 +72,13 @@ def create_default_super_admin(target, connection, **kw):
|
||||
|
||||
class UserGroup(BaseTable):
|
||||
|
||||
__tablename__ = "users_group"
|
||||
__tablename__ = "user_groups"
|
||||
|
||||
user_group_id = Column(GUID, primary_key=True, default=generate_uuid)
|
||||
name = Column(String, unique=True, index=True)
|
||||
is_updatable = Column(Boolean, default=True)
|
||||
users = relationship("User", secondary=users_group_members, back_populates="groups")
|
||||
users = relationship("User", secondary=user_group_link, back_populates="groups")
|
||||
roles = relationship("Role", secondary=role_group_link, back_populates="groups")
|
||||
|
||||
|
||||
@event.listens_for(UserGroup.__table__, 'after_create')
|
||||
@@ -91,11 +96,11 @@ def create_default_user_groups(target, connection, **kw):
|
||||
log.info("The default user groups have been created in the database")
|
||||
|
||||
|
||||
@event.listens_for(users_group_members, 'after_create')
|
||||
@event.listens_for(user_group_link, 'after_create')
|
||||
def add_admin_to_group(target, connection, **kw):
|
||||
|
||||
users_group_table = UserGroup.__table__
|
||||
stmt = users_group_table.select().where(users_group_table.c.name == "Administrators")
|
||||
user_groups_table = UserGroup.__table__
|
||||
stmt = user_groups_table.select().where(user_groups_table.c.name == "Administrators")
|
||||
result = connection.execute(stmt)
|
||||
user_group_id = result.first().user_group_id
|
||||
|
||||
|
||||
Reference in new issue
Block a user