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

Add UserAudit for user activation and deactivation #35134

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions corehq/apps/reports/standard/users/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ def _get_queryset(self):
return UserHistory.objects.none()

changed_by_user_slugs = self.request.GET.getlist(ChangedByUserFilter.slug)
changed_by_user_ids = self._get_user_ids(changed_by_user_slugs)
if changed_by_user_slugs:
changed_by_user_ids = self._get_user_ids(changed_by_user_slugs)
else:
# Show records that might be changed not by user in this domain
changed_by_user_ids = None
# return empty queryset if no matching users were found
if changed_by_user_slugs and not changed_by_user_ids:
return UserHistory.objects.none()
Expand All @@ -141,7 +145,7 @@ def _get_users_es_query(self, slugs):
)

def _build_query(self, user_ids, changed_by_user_ids, user_property, actions, user_upload_record_id):
filters = Q(for_domain__in=self._for_domains())
filters = Q(for_domain__in=self._for_domains()) | Q(for_domain__isnull=True)

if user_ids:
filters = filters & Q(user_id__in=user_ids)
Expand Down
7 changes: 7 additions & 0 deletions corehq/apps/sso/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils.translation import gettext as _

from corehq.apps.registration.models import AsyncSignupRequest
from corehq.apps.users.util import log_user_change
from dimagi.utils.web import get_ip

from corehq.apps.analytics.tasks import (
Expand All @@ -21,6 +22,7 @@
from corehq.apps.sso.utils.user_helpers import get_email_domain_from_username
from corehq.apps.users.models import CouchUser, WebUser
from corehq.const import (
USER_CHANGE_VIA_REACTIVATION,
USER_CHANGE_VIA_SSO_INVITE,
USER_CHANGE_VIA_SSO_NEW_USER,
)
Expand Down Expand Up @@ -84,6 +86,11 @@ def authenticate(self, request, username, idp_slug, is_handshake_successful):
if not is_new_user and not web_user.is_active:
web_user.is_active = True
web_user.save()
log_user_change(by_domain=None, for_domain=None,
by_domain_required_for_log=False, for_domain_required_for_log=False,
couch_user=web_user, changed_by_user=web_user,
fields_changed={'is_active': web_user.is_active},
changed_via=USER_CHANGE_VIA_REACTIVATION)
request.sso_new_user_messages['success'].append(
_("User account for {} has been re-activated.").format(web_user.username)
)
Expand Down
7 changes: 7 additions & 0 deletions corehq/apps/sso/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
from corehq.apps.sso.utils.user_helpers import convert_emails_to_lowercase
from corehq.apps.users.models import WebUser
from corehq.apps.users.models import HQApiKey
from corehq.apps.users.util import SYSTEM_USER_ID, log_user_change
from django.contrib.auth.models import User
from corehq.const import USER_CHANGE_VIA_SSO_DEACTIVATION
from corehq.sql_db.util import paginate_query
from django.db import router
from django.db.models import Q
Expand Down Expand Up @@ -163,6 +165,11 @@ def auto_deactivate_removed_sso_users():
if user and user.is_active:
user.is_active = False
user.save()
log_user_change(by_domain=None, for_domain=None,
by_domain_required_for_log=False, for_domain_required_for_log=False,
couch_user=user, fields_changed={'is_active': user.is_active},
changed_via=USER_CHANGE_VIA_SSO_DEACTIVATION,
changed_by_user=SYSTEM_USER_ID)


def send_deactivation_skipped_email(idp, failure_code, error=None, error_description=None):
Expand Down
3 changes: 3 additions & 0 deletions corehq/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
USER_CHANGE_VIA_SSO_NEW_USER = "sso_new"
USER_CHANGE_VIA_SSO_INVITE = "sso_invitation"
USER_CHANGE_VIA_AUTO_DEACTIVATE = "auto_deactivate"
USER_CHANGE_VIA_REACTIVATION = "reactivation"
USER_CHANGE_VIA_SSO_DEACTIVATION = "sso_deactivation"


LOADTEST_HARD_LIMIT = 500_000 # max cases a loadtest user can sync
Loading