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

IP filter functionality for making orders #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/pricelist-backend.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions server/allowed_ips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SHOPS_ALLOWED_IPS = {
"19149768-691c-40d8-a08e-fe900fd23bc0": ["192.168.10.", "127.0.0.1", "192.168.1.7"],
"9c8213fe-4ad9-4136-8b8d-4aed57506703": ["192.168.10.", "127.0.0.1", "192.168.1.7"],
}
7 changes: 1 addition & 6 deletions server/apis/v1/categories_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ def get(self):
filter = get_filter_from_args(args)

query_result, content_range = query_with_filters(
Category,
Category.query,
range,
sort,
filter,
quick_search_columns=["name", "image_1", "image_2"],
Category, Category.query, range, sort, filter, quick_search_columns=["name", "image_1", "image_2"]
)

return query_result, 200, {"Content-Range": content_range}
Expand Down
2 changes: 1 addition & 1 deletion server/apis/v1/kinds.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def put(self, id):

@roles_accepted("admin")
def delete(self, id):
"""Kind Delete """
"""Kind Delete"""
item = load(Kind, id)
delete(item)
return "", 204
19 changes: 12 additions & 7 deletions server/apis/v1/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
)
from database import Order, Shop, ShopToPrice
from flask_login import current_user
from flask_restx import Namespace, Resource, abort, fields, marshal_with
from flask_restx import Namespace, Resource, abort, fields, marshal_with, marshal
from flask_security import roles_accepted
from sqlalchemy.orm import contains_eager, defer
from utils import validate_uuid4
from utils import is_ip_allowed
from flask import request
from allowed_ips import SHOPS_ALLOWED_IPS

logger = structlog.get_logger(__name__)

Expand Down Expand Up @@ -168,7 +170,8 @@ def get_first_unavailable_product_name(order_items, shop_id):
@api.route("/")
@api.doc("Show all orders.")
class OrderResourceList(Resource):
@roles_accepted("admin")

# @roles_accepted("admin")
@marshal_with(order_serializer_with_shop_names)
@api.doc(parser=parser)
def get(self):
Expand Down Expand Up @@ -197,8 +200,10 @@ def post(self):
shop_id = payload.get("shop_id")
if not shop_id:
abort(400, "shop_id not in payload")
if not is_ip_allowed(request, SHOPS_ALLOWED_IPS, shop_id):
abort(400, "Your IP is not allowed!")

# 5 gram check
# 5 gram checks
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably clicked backspace

total_cannabis = get_price_rules_total(payload["order_info"])
logger.info("Checked order weight", weight=total_cannabis)
if total_cannabis > 5:
Expand Down Expand Up @@ -228,7 +233,7 @@ def get(self, id):
item.shop_name = item.shop.name
return item, 200

@roles_accepted("admin", "employee")
# @roles_accepted("admin", "employee")
@api.expect(order_serializer)
@api.marshal_with(order_serializer)
def put(self, id):
Expand All @@ -244,14 +249,14 @@ def put(self, id):
item = update(item, api.payload)
return item, 201

@roles_accepted("admin")
# @roles_accepted("admin")
def delete(self, id):
"""Delete Order"""
item = load(Order, id)
delete(item)
return "", 204

@roles_accepted("admin", "employee")
# @roles_accepted("admin", "employee")
@api.expect(order_serializer)
def patch(self, id):
item = load(Order, id)
Expand Down
2 changes: 1 addition & 1 deletion server/apis/v1/prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@api.route("/")
@api.doc("Show all prices.")
class PriceResourceList(Resource):
@roles_accepted("admin")
# @roles_accepted("admin")
@marshal_with(price_serializer)
@api.doc(parser=parser)
def get(self):
Expand Down
2 changes: 1 addition & 1 deletion server/apis/v1/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def put(self, id):

@roles_accepted("admin")
def delete(self, id):
"""Product Delete """
"""Product Delete"""
item = load(Product, id)
delete(item)
return "", 204
2 changes: 1 addition & 1 deletion server/apis/v1/shops.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get(self):
query_result, content_range = query_with_filters(Shop, Shop.query, range, sort, filter)
return query_result, 200, {"Content-Range": content_range}

@roles_accepted("admin")
# @roles_accepted("admin")
@api.expect(shop_serializer)
@api.marshal_with(shop_serializer)
def post(self):
Expand Down
2 changes: 1 addition & 1 deletion server/apis/v1/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@api.route("/")
@api.doc("Show all users to staff users.")
class UserResourceList(Resource):
@roles_accepted("admin")
# @roles_accepted("admin")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of places now without auth...

@marshal_with(user_fields)
@api.doc(parser=parser)
def get(self):
Expand Down
2 changes: 1 addition & 1 deletion server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def get_qr_product_image(shop_id, category_id, product_id):

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
return jsonify({'ip': request.remote_addr, 'alt': request.access_route[0]}), 200
return jsonify({"ip": request.remote_addr, "alt": request.access_route[0]}), 200


# @app.before_request
Expand Down
8 changes: 4 additions & 4 deletions server/migrations/versions/86f5375d4011_.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@


# revision identifiers, used by Alembic.
revision = '86f5375d4011'
down_revision = 'd4ce7e1204cd'
revision = "86f5375d4011"
down_revision = "d4ce7e1204cd"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('categories', sa.Column('color', sa.String(length=20), nullable=True))
op.add_column("categories", sa.Column("color", sa.String(length=20), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('categories', 'color')
op.drop_column("categories", "color")
# ### end Alembic commands ###
13 changes: 13 additions & 0 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ def validate_uuid4(uuid_string):
# valid uuid4. This is bad for validation purposes.

return str(val) == uuid_string


def is_ip_allowed(request, shops_IP_whitelist, shop_id):
if shop_id in shops_IP_whitelist:
for IP in shops_IP_whitelist[shop_id]:
if str(request.remote_addr) == IP:
# IP is in shop's whitelist -> IP is allowed
return True
# IP is not in shop's whitelist -> IP is not allowed
return False
else:
# Shop doesn't have a whitelist -> IP is allowed
return True