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

Schema update #92

Merged
merged 9 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
__pycache__
.vercel
tempCodeRunnerFile.py
.pytest_cache
.pytest_cache
60 changes: 26 additions & 34 deletions API/fwapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from API.services.db import *
from API.services.auth import *
from API.services.auth.utils import JWTBearer
from API.utils import scopes
from API.utils import scopes, role_manager
from API.core.ExceptionHandlers import *
from API.core.Exceptions import *
from API.models import (UserAuth, UserOut, UseRefreshToken,
Expand Down Expand Up @@ -83,18 +83,18 @@ def api_get_data(village_name: str, user_credentials: str = Depends(JWTBearer())
}
user_creds = get_current_user_credentials(user_credentials)

@scopes.init_checks(authorized_roles=["admin", "GOVTOff"], village_name=village_name,
@scopes.init_checks(authorized_roles=[role_manager.admin, role_manager.GOVTOff], village_name=village_name,
response_result=response_result)
def scoped_checks(user_creds: UserOut):
if user_creds.role == 'admin':
response_data = fetch_from_db(response_result, user_creds.village_name)
if user_creds.role == role_manager.admin:
village_data = fetch_from_db(response_result, user_creds.village_name)
else:
response_data = fetch_from_db(response_result, village_name)
return response_data['data']
village_data = fetch_from_db(response_result, village_name)
return village_data

response_data = scoped_checks(user_creds)
village_data = scoped_checks(user_creds)

response_result['data'] = response_data
response_result['data'] = village_data
response_result['status'] = 'success'
response_result['message'] = ['authorized']

Expand All @@ -112,7 +112,7 @@ def api_get_familydata(respondents_id: str, user_credentials: str = Depends(JWTB

user_creds = get_current_user_credentials(user_credentials)

@scopes.init_checks(authorized_roles=['admin', 'GOVTOff'], wrong_endpoint_roles=['GOVTOff'],
@scopes.init_checks(authorized_roles=[role_manager.admin, role_manager.GOVTOff], wrong_endpoint_roles=[role_manager.GOVTOff],
village_name=user_creds.village_name, response_result=response_result)
def scoped_checks(user_creds: UserOut):
pass
Expand All @@ -121,7 +121,7 @@ def scoped_checks(user_creds: UserOut):

familydata = fetch_familydata(response_result, user_creds.village_name, respondents_id)

response_result['data'] = familydata["data"]
response_result['data'] = familydata
response_result['status'] = 'success'
response_result['message'] = ['Authenticated']
return response_result
Expand All @@ -138,7 +138,7 @@ def api_get_individual_data(respondents_id: str, user_credentials: str = Depends

user_creds = get_current_user_credentials(user_credentials)

@scopes.init_checks(authorized_roles=['admin', 'GOVTOff'], wrong_endpoint_roles=['GOVTOff'],
@scopes.init_checks(authorized_roles=[role_manager.admin, role_manager.GOVTOff], wrong_endpoint_roles=[role_manager.GOVTOff],
village_name=user_creds.village_name, response_result=response_result)
def scoped_checks(user_creds: UserOut):
pass
Expand All @@ -164,17 +164,17 @@ async def auth_signup(data: Union[UserAuth, BulkSignup], user_credentials: str =

user_creds = get_current_user_credentials(user_credentials)

@scopes.init_checks(authorized_roles=['admin', 'GOVTOff'],
@scopes.init_checks(authorized_roles=[role_manager.admin, role_manager.GOVTOff],
village_name=data.village_name, response_result=response_result)
def scoped_checks(user_creds: UserOut):
if isinstance(data, UserAuth):
if data.role not in ['admin', 'user']:
if data.role not in [role_manager.admin, role_manager.user]:
raise AuthorizationFailedException(response_result, "not authorized")

if data.role == 'admin' and user_creds.role == 'admin':
if data.role == role_manager.admin and user_creds.role == role_manager.admin:
raise AuthorizationFailedException(response_result, "not authorized")

if user_creds.role == "admin" and data.village_name != user_creds.village_name:
if user_creds.role == role_manager.admin and data.village_name != user_creds.village_name:
raise AuthorizationFailedException(response_result, "not authorized")

scoped_checks(user_creds)
Expand Down Expand Up @@ -202,23 +202,15 @@ async def auth_use_refresh_token(existing_tokens: UseRefreshToken):
return handle_refresh_token_access(existing_tokens.refresh_access_token)


@app.get("/ops/get_village_list", summary="Get the list of village names", response_model=FrontendResponseModel,
tags=["Sensitive ops"], dependencies=[Depends(JWTBearer())])
async def get_village_list(user_credentials: str = Depends(JWTBearer())):
@app.get("/api/get_village_list", summary="Get the list of village names", response_model=FrontendResponseModel,
tags=["Resource Server"])
async def get_village_list():
response_result = {
"status": "not_allowed",
"message": ["Not authenticated"],
"data": {},
}

user_creds = get_current_user_credentials(user_credentials)

@scopes.init_checks(authorized_roles=['GOVTOff'], response_result=response_result)
def scoped_checks(user_creds: UserOut):
pass

scoped_checks(user_creds)

village_list = get_available_villages(response_result)
response_result['data']["village_names"] = village_list
return response_result
Expand All @@ -236,7 +228,7 @@ async def ops_delete_database(dbname: str, user_credentials: str = Depends(JWTBe

user_creds = get_current_user_credentials(user_credentials)

@scopes.init_checks(authorized_roles=['GOVTOff'], response_result=response_result, village_name=dbname)
@scopes.init_checks(authorized_roles=[role_manager.GOVTOff], response_result=response_result, village_name=dbname)
def scoped_checks(user_creds):
pass

Expand All @@ -257,7 +249,7 @@ async def ops_update_village_list(dbname: str, user_credentials: str = Depends(J

user_creds = get_current_user_credentials(user_credentials)

@scopes.init_checks(authorized_roles=['GOVTOff'], response_result=response_result)
@scopes.init_checks(authorized_roles=[role_manager.GOVTOff], response_result=response_result)
def scoped_checks(user_creds):
pass

Expand All @@ -266,7 +258,7 @@ def scoped_checks(user_creds):
create_new_village(dbname, user_creds, response_result)
return response_result

@app.get('/api/get_respid_list', summary="Get the list of users", dependencies=[Depends(JWTBearer())], tags=["Resource Server"])
@app.get('/api/get_respdata_list', summary="Get the list of users", dependencies=[Depends(JWTBearer())], tags=["Resource Server"])
async def get_respid_list(date:str, village_name:str=None, user_credentials:str=Depends(JWTBearer())):
response_result={
"status":"not_allowed",
Expand All @@ -276,17 +268,17 @@ async def get_respid_list(date:str, village_name:str=None, user_credentials:str=

user_creds=get_current_user_credentials(user_credentials)

@scopes.init_checks(authorized_roles=['admin','GOVTOff'],response_result=response_result,village_name=village_name)
@scopes.init_checks(authorized_roles=[role_manager.admin,role_manager.GOVTOff],response_result=response_result,village_name=village_name)
def scoped_checks(user_creds:UserOut):
if user_creds.role == 'admin':
response_data = get_resp_id_on_date(user_creds.village_name,'meta',date,response_result)
if user_creds.role == role_manager.admin:
response_data = get_resp_data_on_date(user_creds.village_name, date,response_result)
else:
response_data = get_resp_id_on_date(village_name,'meta',date,response_result)
response_data = get_resp_data_on_date(village_name, date,response_result)
return response_data

response_data = scoped_checks(user_creds)

response_result['data']['user_ids'] = response_data
response_result['data'] = response_data
response_result['status'] = 'success'
response_result['message'] = ['authorized']

Expand Down
5 changes: 3 additions & 2 deletions API/services/auth/AuthServices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from API.core.Exceptions import *
from API.models import UserOut, UserAuth, TokenPayload, BulkSignup
from API.services.db.utils import DBQueries
from API.utils import role_manager

from .utils import Auth

Expand Down Expand Up @@ -48,7 +49,7 @@ def signup(response_result: FrontendResponseModel, data: Union[UserAuth,BulkSign
passwords=[Auth.get_password_hash(passwd) for passwd in data.passwords]
village_name=data.village_name

users=DBQueries.filtered_db_search("Auth","user",["_id","password","village_name"],AADHAR={"$in":AADHAR_NOS})
users=DBQueries.filtered_db_search("Auth", role_manager.user, ["_id","password","village_name"], search_idxs={"AADHAR":{"$in":AADHAR_NOS}})
users=[user["AADHAR"] for user in users]

invalid_users=[]
Expand All @@ -68,7 +69,7 @@ def signup(response_result: FrontendResponseModel, data: Union[UserAuth,BulkSign
valid_users.append(userinfo)

if len(valid_users)!=0:
DBQueries.insert_to_database("Auth", "user", valid_users) # saving user to database
DBQueries.insert_to_database("Auth", role_manager.user, valid_users) # saving user to database
response_result['status'] = f'success'
response_result['message'] = [f'Users created successfully']
else:
Expand Down
Loading
Loading