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

USH-4354: login-as web user #34351

Merged
merged 2 commits into from
Apr 16, 2024
Merged
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
3 changes: 2 additions & 1 deletion corehq/apps/cloudcare/esaccessors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from corehq.apps.es import UserES
from corehq.apps.es.users import web_users, mobile_users
from corehq.apps.locations.models import SQLLocation


Expand Down Expand Up @@ -43,7 +44,7 @@ def login_as_user_query(
if couch_user.has_permission(domain, 'access_default_login_as_user'):
login_as_users.append('default')
user_es = user_es.login_as_user(login_as_users)
return user_es.mobile_users()
return user_es.OR(web_users(), mobile_users())


def _limit_login_as(couch_user, domain):
Expand Down
12 changes: 9 additions & 3 deletions corehq/apps/cloudcare/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
from corehq.apps.reports.formdetails import readable
from corehq.apps.users.decorators import require_can_login_as
from corehq.apps.users.models import CouchUser
from corehq.apps.users.util import format_username
from corehq.apps.users.util import get_complete_username
from corehq.apps.users.views import BaseUserSettingsView
from corehq.apps.integration.util import integration_contexts
from corehq.util.metrics import metrics_histogram
Expand Down Expand Up @@ -111,6 +111,9 @@ def fetch_app(self, domain, app_id):
return _fetch_build(domain, self.request.couch_user.username, app_id)

def get_web_apps_available_to_user(self, domain, user):
if user['doc_type'] == 'WebUser' and not user.can_access_web_apps(domain):
MartinRiese marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this logic belongs better in user_can_access_app

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep them separate - user_can_access_app is about to become legacy functionality (#34421).

return []

app_access = get_application_access_for_domain(domain)
app_ids = get_app_ids_in_domain(domain)

Expand Down Expand Up @@ -142,7 +145,9 @@ def set_cookie(response): # set_coookie is a noop by default
'restoreAs:{}:{}'.format(domain, request.couch_user.username))
username = request.COOKIES.get(cookie_name)
if username:
user = CouchUser.get_by_username(format_username(username, domain))
username = urllib.parse.unquote(username)
username = get_complete_username(username, domain)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 was just gonna point to this

user = CouchUser.get_by_username(username)
if user:
return user, set_cookie
else:
Expand Down Expand Up @@ -379,14 +384,15 @@ def _user_query(self, search_string, page, limit):

def _format_user(self, user_json):
user = CouchUser.wrap_correctly(user_json)
sql_location = user.get_sql_location(self.domain)
formatted_user = {
'username': user.raw_username,
'customFields': user.get_user_data(self.domain).to_dict(),
'first_name': user.first_name,
'last_name': user.last_name,
'phoneNumbers': user.phone_numbers,
'user_id': user.user_id,
'location': user.sql_location.to_json() if user.sql_location else None,
'location': sql_location.to_json() if sql_location else None,
}
return formatted_user

Expand Down
6 changes: 3 additions & 3 deletions corehq/apps/users/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
bulk_auto_deactivate_commcare_users,
cached_user_id_to_user_display,
generate_mobile_username,
get_complete_mobile_username,
get_complete_username,
is_username_available,
user_display_string,
user_id_to_username,
Expand Down Expand Up @@ -261,11 +261,11 @@ def test_returns_false_if_reserved_username(self):
class TestGetCompleteMobileUsername(SimpleTestCase):

def test_returns_unchanged_username_if_already_complete(self):
username = get_complete_mobile_username('test@test-domain.commcarehq.org', 'test-domain')
username = get_complete_username('test@test-domain.commcarehq.org', 'test-domain')
self.assertEqual(username, 'test@test-domain.commcarehq.org')

def test_returns_complete_username_if_incomplete(self):
username = get_complete_mobile_username('test', 'test-domain')
username = get_complete_username('test', 'test-domain')
self.assertEqual(username, 'test@test-domain.commcarehq.org')


Expand Down
4 changes: 2 additions & 2 deletions corehq/apps/users/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def generate_mobile_username(username, domain, is_unique=True):
Example use: generate_mobile_username('username', 'domain') -> 'username@domain.commcarehq.org'
"""
from .validation import validate_mobile_username
username = get_complete_mobile_username(username, domain)
username = get_complete_username(username, domain)
validate_mobile_username(username, domain, is_unique)
return username


def get_complete_mobile_username(username, domain):
def get_complete_username(username, domain):
"""
:param username: accepts both incomplete ('example-user') or complete ('example-user@domain.commcarehq.org')
:param domain: domain associated with the mobile user
Expand Down
Loading