-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- добвлен декоратор require_access для проверки доступа вызвавшего сетевой метод пользователя
- Loading branch information
1 parent
336c5af
commit d1a7fa5
Showing
5 changed files
with
80 additions
and
37 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from .main.client import Client | ||
from .main.server import Server | ||
from .main.utils.cl_unit import ClUnit | ||
from .main.utils.decorator import require_access | ||
from .main.utils.server_db import ServerDB | ||
|
||
__all__ = ["Client", "Server", "ClUnit", "ServerDB"] | ||
__version__ = "0.2.5" | ||
__all__ = ["Client", "Server", "ClUnit", "require_access", "ServerDB"] | ||
__version__ = "0.2.6" |
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,34 @@ | ||
from typing import List | ||
|
||
from .cl_unit import ClUnit | ||
from .server_db import ServerDB | ||
|
||
def require_access(req_access: List[str] | str): | ||
""" | ||
A decorator that ensures the user has the required access level(s) before executing the function. | ||
Args: | ||
req_access (List[str] | str): The required access level(s). Can be a single string or a list of strings | ||
representing the access levels needed to execute the function. | ||
Returns: | ||
function: The decorated function that checks user access before execution. | ||
Raises: | ||
PermissionError: If the user does not have the necessary access permissions, | ||
this exception is raised with a message indicating the missing permissions. | ||
""" | ||
|
||
if isinstance(req_access, str): | ||
req_access = [req_access] | ||
|
||
def decorator(func): | ||
async def wrapper(cl_unit: ClUnit, *args, **kwargs): | ||
if await ServerDB.check_access_login(cl_unit.login, req_access): | ||
return await func(cl_unit, *args, **kwargs) | ||
else: | ||
raise PermissionError(f"Access error. Insufficient permissions for the following: {'; '.join(req_access)}") | ||
|
||
return wrapper | ||
|
||
return decorator |
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