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

feat/login log #288

Closed
wants to merge 4 commits into from
Closed
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 model/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def login(username, password):
- 403 Login Failed
'''
try:
user = User.login(username, password)
user = User.login(request, username, password)
except DoesNotExist:
return HTTPError('Login Failed', 403)
if not user.active:
Expand Down
9 changes: 8 additions & 1 deletion mongo/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def escape(self):
self.input,
self.output,
self.hint,
))
))
_io = zip(self.sample_input, self.sample_output)
for i, (ip, op) in enumerate(_io):
self.sample_input[i] = ip or html.escape(ip)
Expand Down Expand Up @@ -460,3 +460,10 @@ class SubmissionConfig(Config):
],
db_field='sandboxInstances',
)


class LoginRecords(Document):
user_id = StringField(db_field='userId', max_length=24, required=True)
success = BooleanField(default=False)
ip_addr = StringField(default=None, null=True)
last_login = DateTimeField(default=datetime.min)
22 changes: 22 additions & 0 deletions mongo/login_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from datetime import datetime
from . import engine
from .base import *

class LoginRecord(MongoBase, engine=engine.LoginRecords):
@classmethod
def record_login(cls, user_id, ip_addr, success)->None:
cls.engine(
user_id=user_id,
ip_addr=ip_addr,
success=success,
last_login=datetime.now()
).save(force_insert=True)

@classmethod
def get_records_by_ip(cls, ip_addr):
return cls.engine.objects.get(ip_addr=ip_addr)


@classmethod
def get_records_by_user_id(cls, user_id):
return cls.engine.objects.get(user_id=user_id)
6 changes: 4 additions & 2 deletions mongo/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from hmac import compare_digest
from typing import Any, Dict, List, TYPE_CHECKING, Optional

from . import engine, course
from . import engine, course, login_record
from .utils import *
from .base import *

Expand Down Expand Up @@ -121,15 +121,17 @@ def force_update(self, new_user: Dict[str, Any], course: Optional[Course]):
self.reload()

@classmethod
def login(cls, username, password):
def login(cls, request, username, password):
try:
user = cls.get_by_username(username)
except engine.DoesNotExist:
user = cls.get_by_email(username)
user_id = hash_id(user.username, password)
if (compare_digest(user.user_id, user_id)
or compare_digest(user.user_id2, user_id)):
login_record.LoginRecord.record_login(user_id, request.remote_addr, True)
return user
login_record.LoginRecord.record_login(user_id, request.remote_addr, False)
raise engine.DoesNotExist

@classmethod
Expand Down