Navigation Menu

Skip to content

Commit

Permalink
Working on new login tracker.
Browse files Browse the repository at this point in the history
  • Loading branch information
volundmush committed Apr 14, 2019
1 parent b961f28 commit 1827c2e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion evennia/accounts/accounts.py
Expand Up @@ -23,7 +23,7 @@
from evennia.objects.models import ObjectDB
from evennia.comms.models import ChannelDB
from evennia.commands import cmdhandler
from evennia.server.models import ServerConfig
from evennia.server.models import ServerConfig, Host
from evennia.server.throttle import Throttle
from evennia.utils import class_from_module, create, logger
from evennia.utils.utils import (lazy_property, to_str,
Expand Down Expand Up @@ -515,6 +515,10 @@ def authenticate(cls, username, password, ip='', **kwargs):

return None, errors

# Record Host and login.
host, created = Host.objects.get_or_create(ip=ip)
account.login_records.create(source=host, date=time.time())

This comment has been minimized.

Copy link
@strikaco

strikaco Apr 15, 2019

Contributor

You ought to be using django.utils.timezone.now() for capturing timestamps, or set Host.date(auto_now_add=True) to grab them in UTC automatically.

This comment has been minimized.

Copy link
@volundmush

volundmush Apr 15, 2019

Author Contributor

I dislike auto_now_add because it prevents creating manual entries if I wanted to import data, but noted about the first part.

# Account successfully authenticated
logger.log_sec('Authentication Success: %s (IP: %s).' % (account, ip))
return account, errors
Expand Down
9 changes: 9 additions & 0 deletions evennia/accounts/models.py
Expand Up @@ -170,3 +170,12 @@ def __uid_set(self, value):
def __uid_del(self):
raise Exception("User id cannot be deleted!")
uid = property(__uid_get, __uid_set, __uid_del)


class Login(models.Model):
"""
Model for tracking Account logins.
"""
account = models.ForeignKey(AccountDB, related_name='login_records', on_delete=models.CASCADE)
source = models.ForeignKey('server.Host', related_name='account_logins', on_delete=models.CASCADE)
date = models.DateTimeField(null=False, db_index=True)

This comment has been minimized.

Copy link
@strikaco

strikaco Apr 15, 2019

Contributor

Shouldn't the account field be indexed instead of the date field? (Being for occasional lookups, does this table even need an index at all?)

This comment has been minimized.

Copy link
@volundmush

volundmush Apr 15, 2019

Author Contributor

This table design is out of date, see latest commits. All Foreign Keys are automatically indexed however.

14 changes: 14 additions & 0 deletions evennia/commands/default/account.py
Expand Up @@ -897,3 +897,17 @@ def set(self):
return
self.msg('Success! The new value is: %s' % result)


class CmdLogins(COMMAND_DEFAULT_CLASS):
"""
View your recent login records. Useful for spotting potential hijinx.
Usage:
@logins
"""
key = '@logins'
locks = 'cmd:pperm(Player)'
help_category = 'General'

def func(self):
pass
13 changes: 13 additions & 0 deletions evennia/server/models.py
Expand Up @@ -128,3 +128,16 @@ def store(self, key, value):
"""
self.key = key
self.value = value


class Host(models.Model):
"""
Simple table used for keeping track of all IP addresses that have connected to
the game.
"""

# The IP address to be stored.
ip = models.GenericIPAddressField(blank=False, null=False, unique=True)

# If available, the site/reverse DNS lookup attached to the IP.
site = models.TextField(blank=True, null=True)

0 comments on commit 1827c2e

Please sign in to comment.