-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add user secrets endpoints * Edit secret deletion error message Co-authored-by: Sujen Shah <[email protected]> * Restrict login_required decorator to specific roles; Improved permissions error handling; minor refactoring --------- Co-authored-by: bsatoriu <[email protected]> Co-authored-by: Sujen Shah <[email protected]>
- Loading branch information
1 parent
ce6a216
commit 1d0e59c
Showing
13 changed files
with
972 additions
and
646 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
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,97 @@ | ||
import logging | ||
from flask_restx import Resource | ||
from flask import request | ||
from flask_api import status | ||
from api.models.role import Role | ||
from api.restplus import api | ||
from api.auth.security import login_required | ||
from api.maap_database import db | ||
from api.models.pre_approved import PreApproved | ||
from api.schemas.pre_approved_schema import PreApprovedSchema | ||
from datetime import datetime | ||
import json | ||
|
||
from api.utils.http_util import err_response | ||
|
||
log = logging.getLogger(__name__) | ||
ns = api.namespace('admin', description='Operations related to the MAAP admin') | ||
|
||
@ns.route('/pre-approved') | ||
class PreApprovedEmails(Resource): | ||
|
||
@api.doc(security='ApiKeyAuth') | ||
@login_required(role=Role.ROLE_ADMIN) | ||
def get(self): | ||
pre_approved = db.session.query( | ||
PreApproved.email, | ||
PreApproved.creation_date | ||
).order_by(PreApproved.email).all() | ||
|
||
pre_approved_schema = PreApprovedSchema() | ||
result = [json.loads(pre_approved_schema.dumps(p)) for p in pre_approved] | ||
return result | ||
|
||
@api.doc(security='ApiKeyAuth') | ||
@login_required(role=Role.ROLE_ADMIN) | ||
def post(self): | ||
|
||
""" | ||
Create new pre-approved email. Wildcards are supported for starting email characters. | ||
Format of JSON to post: | ||
{ | ||
"email": "" | ||
} | ||
Sample 1. Any email ending in "@maap-project.org" is pre-approved | ||
{ | ||
"email": "*@maap-project.org" | ||
} | ||
Sample 2. Any email matching "[email protected]" is pre-approved | ||
{ | ||
"email": "[email protected]" | ||
} | ||
""" | ||
|
||
req_data = request.get_json() | ||
if not isinstance(req_data, dict): | ||
return err_response("Valid JSON body object required.") | ||
|
||
email = req_data.get("email", "") | ||
if not isinstance(email, str) or not email: | ||
return err_response("Valid email is required.") | ||
|
||
pre_approved_email = db.session.query(PreApproved).filter_by(email=email).first() | ||
|
||
if pre_approved_email is not None: | ||
return err_response(msg="Email already exists") | ||
|
||
new_email = PreApproved(email=email, creation_date=datetime.utcnow()) | ||
|
||
db.session.add(new_email) | ||
db.session.commit() | ||
|
||
pre_approved_schema = PreApprovedSchema() | ||
return json.loads(pre_approved_schema.dumps(new_email)) | ||
|
||
|
||
@ns.route('/pre-approved/<string:email>') | ||
class PreApprovedEmails(Resource): | ||
|
||
@api.doc(security='ApiKeyAuth') | ||
@login_required(role=Role.ROLE_ADMIN) | ||
def delete(self, email): | ||
""" | ||
Delete pre-approved email | ||
""" | ||
|
||
pre_approved_email = db.session.query(PreApproved).filter_by(email=email).first() | ||
|
||
if pre_approved_email is None: | ||
return err_response(msg="Email does not exist") | ||
|
||
db.session.query(PreApproved).filter_by(email=email).delete() | ||
db.session.commit() | ||
|
||
return {"code": status.HTTP_200_OK, "message": "Successfully deleted {}.".format(email)} |
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
Oops, something went wrong.