Skip to content

Commit

Permalink
Merge pull request #695 from shimilgithub/api-refactor
Browse files Browse the repository at this point in the history
Merging API-refactor to version-2
  • Loading branch information
kavitharaju authored Sep 4, 2023
2 parents b05ce81 + 326a9f5 commit cdeb139
Show file tree
Hide file tree
Showing 59 changed files with 7,150 additions and 5,618 deletions.
365 changes: 185 additions & 180 deletions app/auth/api-permissions.csv

Large diffs are not rendered by default.

54 changes: 29 additions & 25 deletions app/auth/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# from auth.api_permission_map import api_permission_map
from auth import utils
from schema import schema_auth
from dependencies import log, get_db
from dependencies import log
from custom_exceptions import GenericException ,\
AlreadyExistsException,NotAvailableException,UnAuthorizedException,\
UnprocessableException, PermissionException, AuthException
Expand Down Expand Up @@ -90,26 +90,31 @@ def get_user_or_none_graphql(info):

def api_resourcetype_map(endpoint, path_params=None):
'''Default correlation between API endpoints and resource they act upon'''
if endpoint.split('/')[2] in ["contents", "languages", "licenses", 'versions']:
if endpoint.endswith(("resources/types", "languages", "licenses", "versions")):
resource_type = schema_auth.ResourceType.METACONTENT.value
elif endpoint.startswith('/v2/translation/project'):
elif endpoint.startswith('/v2/text/translate/token-based/project'):
resource_type = schema_auth.ResourceType.PROJECT.value
elif endpoint.startswith('/v2/user'):
resource_type = schema_auth.ResourceType.USER.value
elif endpoint.startswith("/v2/translation") or endpoint.startswith("/v2/nlp"):
resource_type = schema_auth.ResourceType.TRANSLATION.value
elif endpoint.startswith("/v2/lookup"):

elif endpoint.startswith("/v2/resources/lookups/bible/books") or \
endpoint.startswith("/v2/nlp/stopwords"):
resource_type = schema_auth.ResourceType.LOOKUP.value

elif endpoint.startswith("/v2/text/translate/token-based") or \
endpoint.startswith("/v2/nlp"):
resource_type = schema_auth.ResourceType.TRANSLATION.value

elif endpoint.startswith("/v2/jobs"):
resource_type = schema_auth.ResourceType.JOBS.value
elif endpoint.startswith("/v2/media"):
resource_type = schema_auth.ResourceType.MEDIA.value
elif endpoint.startswith("/v2/files"):
resource_type = schema_auth.ResourceType.FILE.value
elif endpoint.startswith("/v2/sources") or (
path_params is not None and "source_name" in path_params):
elif endpoint.startswith("/v2/resources") or (
path_params is not None and "resource_name" in path_params):
resource_type = schema_auth.ResourceType.CONTENT.value
elif endpoint.split('/')[2] in ["restore","deleted-items"]:
elif endpoint.split('/')[3] in ["restore","cleanup"]:
resource_type = schema_auth.ResourceType.DATAMANIPULATION.value
else:
raise GenericException("Resource Type of API not defined")
Expand Down Expand Up @@ -160,22 +165,21 @@ def get_access_tag(db_, resource_type, path_params=None, kw_args = None, resourc
}
if resource_type in resource_tag_map:
return resource_tag_map[resource_type]
if path_params is not None and "source_name" in path_params:
db_entry = db_.query(db_models.Source.metaData['accessPermissions']).filter(
db_models.Source.sourceName == path_params['source_name']).first()
if path_params is not None and "resource_name" in path_params:
db_entry = db_.query(db_models.Resource.metaData['accessPermissions']).filter(
db_models.Resource.resourceName == path_params['resource_name']).first()
if db_entry is not None:
return db_entry[0]
if kw_args is not None and "source_name" in kw_args:
db_entry = db_.query(db_models.Source.metaData['accessPermissions']).filter(
db_models.Source.sourceName == kw_args['source_name']).first()
if kw_args is not None and "resource_name" in kw_args:
db_entry = db_.query(db_models.Resource.metaData['accessPermissions']).filter(
db_models.Resource.resourceName == kw_args['resource_name']).first()
if db_entry is not None:
return db_entry[0]
if resource:
return resource.metaData['accessPermissions']
if resource_type == schema_auth.ResourceType.CONTENT:
return ['content']
return []

def is_project_owner(db_:Session, db_resource, user_id):
'''checks if the user is the owner of the given project'''
if hasattr(db_resource, 'projectId'):
Expand Down Expand Up @@ -284,17 +288,17 @@ async def wrapper(*args, **kwargs):#pylint: disable=too-many-branches,too-many-s
#########################################
obj = None
if isinstance(response, dict):
# separating out intended response and (source/project)object passed for auth check
# separating out intended response and (resource/project)object passed for auth check
if "db_content" in response:
if "source_content" in response:
obj = response['source_content']
if "resource_content" in response:
obj = response['resource_content']
if "project_content" in response:
obj = response['project_content']
response = response['db_content']
elif "data" in response:
if isinstance(response['data'], dict) and "db_content" in response['data']:
if "source_content" in response['data']:
obj = response['data']['source_content']
if "resource_content" in response['data']:
obj = response['data']['resource_content']
if "project_content" in response['data']:
obj = response['data']['project_content']
response['data'] = response['data']['db_content']
Expand All @@ -306,19 +310,19 @@ async def wrapper(*args, **kwargs):#pylint: disable=too-many-branches,too-many-s
# All no-auth and role based cases checked and appoved if applicable
if db_:
db_.commit()
# if (method == "DELETE" and "source" in endpoint) or "restore" in endpoint:
# if (method == "DELETE" and "resource" in endpoint) or "restore" in endpoint:
# db_models.map_all_dynamic_tables(db_= next(get_db()))

elif obj is not None:
# Resource(item) specific checks
if check_right(user_details, required_rights, obj, db_):
if db_:
db_.commit()
if (method == "DELETE" and "source" in endpoint):
if (method == "DELETE" and "resource" in endpoint):
db_models.dynamicTables = {}
db_models.map_all_dynamic_tables(db_= next(get_db()))
db_models.map_all_dynamic_tables(db_= db_)
if "restore" in endpoint:
db_models.map_all_dynamic_tables(db_= next(get_db()))
db_models.map_all_dynamic_tables(db_= db_)
else:
if user_details['user_id'] is None:
raise UnAuthorizedException("Access token not provided or user not recognized.")
Expand Down
Loading

0 comments on commit cdeb139

Please sign in to comment.