Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files refactor to include repositories logic #1560

Merged
merged 28 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
22403a5
remove unneeded provider methods
Tansito Dec 17, 2024
377e9cf
create access policies file
Tansito Dec 18, 2024
2297950
refactor get_functions repository method
Tansito Dec 18, 2024
ddb8d3b
refactor groups and repositories
Tansito Dec 18, 2024
909eab5
repository refactor from files
Tansito Dec 18, 2024
3f0a431
fix some linter problems
Tansito Dec 18, 2024
833068f
fixed a bug when the user retrieves a function
Tansito Dec 18, 2024
2f760ee
fix lint
Tansito Dec 18, 2024
7b89093
refactor of get_function method
Tansito Dec 18, 2024
fd042d0
remove artifact test file
Tansito Dec 18, 2024
512d640
remove programs access policies
Tansito Dec 19, 2024
d7cebc2
Merge branch 'data-folder' into repository-refactor
Tansito Dec 20, 2024
22ef963
Merge branch 'data-folder' into repository-refactor
Tansito Dec 20, 2024
d88c180
refactor programs references to functions
Tansito Dec 20, 2024
99a0fd5
group repository refactor
Tansito Dec 20, 2024
d50a4d2
rename groups repository into user repository
Tansito Dec 20, 2024
1fb6ba9
simplified get_function methods
Tansito Dec 20, 2024
1869aca
fix query
Tansito Dec 20, 2024
82aef0d
adapt get_functions methods
Tansito Dec 20, 2024
da9bf35
updated comments
Tansito Dec 20, 2024
81896a2
create path if doesn't exist
Tansito Dec 26, 2024
305775b
remove some unused code
Tansito Dec 26, 2024
bcf947c
fix files client
Tansito Dec 26, 2024
d426ec4
fix typos
Tansito Dec 26, 2024
f51f345
fixed the creation of the directory
Tansito Dec 26, 2024
d3b48cb
added a test for the provider end-points
Tansito Dec 26, 2024
8a53989
fix some typos from the provider end-points
Tansito Dec 26, 2024
64720ee
fix black on tests
Tansito Dec 26, 2024
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
Empty file.
78 changes: 78 additions & 0 deletions gateway/api/access_policies/programs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Access policies implementation for Program access
"""
import logging

from api.models import Program


logger = logging.getLogger("gateway")


class ProgramAccessPolicy:
Tansito marked this conversation as resolved.
Show resolved Hide resolved
"""
The main objective of this class is to manage the access for the user
to the Program entities.
"""

@staticmethod
def can_view(user, user_view_groups, function: Program) -> bool:
"""
Checks if the user has view access to a Function:
- If it's the author it will always have access
- a view group is in the Program.instances

Args:
user: Django user from the request
user_view_groups: view groups from a user
function: Program instance against to check the access

Returns:
bool: True or False in case the user has access
"""

if function.author.id == user.id:
return True

instances = function.instances.all()
has_access = any(group in instances for group in user_view_groups)
# the message must be different if the function has a provider or not
if not has_access:
logger.warning(
"User [%s] has no access to function [%s/%s].",
user.id,
function.provider.name,
function.title,
)
return has_access

@staticmethod
def can_run(user, user_run_groups, function: Program) -> bool:
"""
Checks if the user has run access to a Function:
- If it's the author it will always have access
- a run group is in the Program.instances

Args:
user: Django user from the request
user_run_groups: run groups from a user
function: Program instance against to check the access

Returns:
bool: True or False in case the user has access
"""

if function.author.id == user.id:
return True

instances = function.instances.all()
has_access = any(group in instances for group in user_run_groups)
# the message must be different if the function has a provider or not
if not has_access:
logger.warning(
"User [%s] has no access to function [%s/%s].",
user.id,
function.provider.name,
function.title,
)
return has_access
38 changes: 38 additions & 0 deletions gateway/api/access_policies/providers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Access policies implementation for Provider access
"""
import logging

from api.models import Provider


logger = logging.getLogger("gateway")


class ProviderAccessPolicy: # pylint: disable=too-few-public-methods
"""
The main objective of this class is to manage the access for the user
to the Provider entities.
"""

@staticmethod
def can_access(user, provider: Provider) -> bool:
"""
Checks if the user has access to a Provider:

Args:
user: Django user from the request
provider: Provider instance against to check the access

Returns:
bool: True or False in case the user has access
"""

user_groups = user.groups.all()
admin_groups = provider.admin_groups.all()
has_access = any(group in admin_groups for group in user_groups)
if not has_access:
logger.warning(
"User [%s] has no access to provider [%s].", user.id, provider.name
)
return has_access
51 changes: 51 additions & 0 deletions gateway/api/repositories/groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Repository implementation for Groups model
"""

from typing import List
from django.contrib.auth.models import Group, Permission
from django.db.models import Q

from api.models import RUN_PROGRAM_PERMISSION, VIEW_PROGRAM_PERMISSION


class GroupRepository:
Tansito marked this conversation as resolved.
Show resolved Hide resolved
"""
The main objective of this class is to manage the access to the model
"""

def get_groups_with_view_permissions_from_user(self, user) -> List[Group]:
"""
Returns all the groups with view permissions available to the user.

Args:
user: Django user from the request

Returns:
List[Group]: all the groups available to the user
"""

view_program_permission = Permission.objects.get(
codename=VIEW_PROGRAM_PERMISSION
)
user_criteria = Q(user=user)
view_permission_criteria = Q(permissions=view_program_permission)

return Group.objects.filter(user_criteria & view_permission_criteria)

def get_groups_with_run_permissions_from_user(self, user) -> List[Group]:
"""
Returns all the groups with run permissions available to the user.

Args:
user: Django user from the request

Returns:
List[Group]: all the groups available to the user
"""

run_program_permission = Permission.objects.get(codename=RUN_PROGRAM_PERMISSION)
user_criteria = Q(user=user)
run_permission_criteria = Q(permissions=run_program_permission)

return Group.objects.filter(user_criteria & run_permission_criteria)
Tansito marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading