Skip to content

Commit

Permalink
Refs #253, enable 2FA for users in sites and related user groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
doumdi committed Oct 1, 2024
1 parent 4a6bbc6 commit d26c140
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 14 additions & 2 deletions teraserver/python/modules/DatabaseModule/DBManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def user_group_updated_or_inserted(mapper, connection, target: TeraUserGroup):
if role.id_site and role.service_role_site.site_2fa_required:
# Efficiently load all related users with joinedload
user_ids = set()
for group in target.user_group_users:
user_ids.add(group.id_user)
for user in target.user_group_users:
user_ids.add(user.id_user)

# Perform a bulk update for all users at once
if user_ids:
Expand All @@ -144,6 +144,18 @@ def user_group_updated_or_inserted(mapper, connection, target: TeraUserGroup):
.values(user_2fa_enabled=True)
)

@event.listens_for(TeraUserUserGroup, 'after_update')
@event.listens_for(TeraUserUserGroup, 'after_insert')
def user_user_group_updated_or_inserted(mapper, connection, target: TeraUserUserGroup):
# Check if 2FA is enabled for a related site
for role in target.user_user_group_user_group.user_group_services_roles:
if role.id_site and role.service_role_site.site_2fa_required:
# Perform single update for user
connection.execute(
update(TeraUser)
.where(TeraUser.id_user == target.user_user_group_user.id_user)
.values(user_2fa_enabled=True)
)

def setup_events_for_class(self, cls, event_name):
import json
Expand Down
4 changes: 2 additions & 2 deletions teraserver/python/opentera/db/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
from .TeraUserGroup import TeraUserGroup
from .TeraUserUserGroup import TeraUserUserGroup
from .TeraUserPreference import TeraUserPreference
from .TeraUserUserGroup import TeraUserUserGroup


"""
A map containing the event name and class, useful for event filtering.
A map containing the event name and class, useful for event filtering.
Insert only useful events here.
"""
EventNameClassMap = {
Expand Down

0 comments on commit d26c140

Please sign in to comment.