Skip to content

Commit

Permalink
Move the auth_event table out of AuthS3
Browse files Browse the repository at this point in the history
Fixes sahana#1525

This prevents the `auth_event` table from being loaded on every request.
The `table_event` getter in web2py tries looking for the table in `db`,
so I also had to override that getter on the AuthS3 class to look in
`s3db` instead.
  • Loading branch information
hallamoore committed Dec 5, 2019
1 parent c8e7483 commit 969a017
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
43 changes: 4 additions & 39 deletions modules/s3/s3aaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ def __init__(self):
# CLI can override with auth.override=True
self.PROTECTED = ("admin",)

# -------------------------------------------------------------------------
def table_event(self):
return current.s3db[self.settings.table_event_name]

# -------------------------------------------------------------------------
def define_tables(self, migrate=True, fake_migrate=False):
"""
Expand Down Expand Up @@ -423,45 +427,6 @@ def define_tables(self, migrate=True, fake_migrate=False):
# migrate = migrate,
# fake_migrate=fake_migrate)

# Event table (auth_event)
# Records Logins & ?
# @ToDo: Move to s3db.auth to prevent it from being defined every request
# (lazy tables means no big issue for Production but helps Devs)
# Deprecate?
# - date of most recent login is the most useful thing recorded, which we already record in the main auth_user table
if not settings.table_event:
request = current.request
define_table(
settings.table_event_name,
Field("time_stamp", "datetime",
default = request.utcnow,
#label = messages.label_time_stamp
),
Field("client_ip",
default = request.client,
#label=messages.label_client_ip
),
Field("user_id", utable,
default = None,
requires = IS_IN_DB(db, "%s.id" % uname,
"%(id)s: %(first_name)s %(last_name)s"),
#label=messages.label_user_id
),
Field("origin", length=512,
default = "auth",
#label = messages.label_origin,
requires = IS_NOT_EMPTY(),
),
Field("description", "text",
default = "",
#label = messages.label_description,
requires = IS_NOT_EMPTY(),
),
migrate = migrate,
fake_migrate=fake_migrate,
*S3MetaFields.sync_meta_fields())
settings.table_event = db[settings.table_event_name]

# -------------------------------------------------------------------------
def login_bare(self, username, password):
"""
Expand Down
53 changes: 53 additions & 0 deletions modules/s3db/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"auth_Consent",
"auth_user_options_get_osm",
"AuthUserTempModel",
"AuthEventModel",
)

import datetime
Expand Down Expand Up @@ -1180,4 +1181,56 @@ def model(self):
#
return {}

# =============================================================================
class AuthEventModel(S3Model):
"""
Model to store auth events
"""

names = (current.auth.settings.table_event_name, )

def model(self):

utable = current.auth.settings.table_user

# Event table (auth_event)
# Records Logins & ?
# Deprecate?
# - date of most recent login is the most useful thing recorded, which we already record in the main auth_user table
request = current.request
settings = current.auth.settings
self.define_table(
settings.table_event_name,
Field("time_stamp", "datetime",
default = request.utcnow,
#label = messages.label_time_stamp
),
Field("client_ip",
default = request.client,
#label=messages.label_client_ip
),
Field("user_id", utable,
default = None,
requires = IS_IN_DB(current.db, "%s.id" % settings.table_user_name,
"%(id)s: %(first_name)s %(last_name)s"),
#label=messages.label_user_id
),
Field("origin", length=512,
default = "auth",
#label = messages.label_origin,
requires = IS_NOT_EMPTY(),
),
Field("description", "text",
default = "",
#label = messages.label_description,
requires = IS_NOT_EMPTY(),
),
*S3MetaFields.sync_meta_fields())
settings.table_event = current.s3db[settings.table_event_name]

# ---------------------------------------------------------------------
# Pass names back to global scope (s3.*)
#
return {}

# END =========================================================================

0 comments on commit 969a017

Please sign in to comment.