Skip to content

Commit

Permalink
Fix connectid sms
Browse files Browse the repository at this point in the history
  • Loading branch information
sravfeyn committed Oct 25, 2024
1 parent 1f3e480 commit cf7b51f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
8 changes: 5 additions & 3 deletions corehq/apps/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,10 @@ def __init__(self, project, request_user, *args, **kwargs):
provision_by_sms = TWO_STAGE_USER_PROVISIONING_BY_SMS.enabled(self.domain)

if provision_by_sms or provision_by_cid:
varname = 'account_invite_by_cid' if provision_by_cid else 'force_account_confirmation_by_sms'
confirm_account_by_sms_field = crispy.Field(
'force_account_confirmation_by_sms' if provision_by_sms else 'account_invite_by_cid',
data_bind='checked: force_account_confirmation_by_sms || account_invite_by_cid',
varname,
data_bind=f'checked: {varname}',
)
phone_number_field = crispy.Div(
crispy.Field(
Expand Down Expand Up @@ -945,7 +946,8 @@ def __init__(self, project, request_user, *args, **kwargs):
<i class="fa fa-warning"></i> {disabled_email}
<!-- /ko -->
<!-- ko if: !($root.stagedUser().force_account_confirmation())
&& $root.stagedUser().force_account_confirmation_by_sms() -->
&& ($root.stagedUser().force_account_confirmation_by_sms()
|| $root.stagedUser().account_invite_by_cid) -->
<i class="fa fa-warning"></i> {disabled_phone}
<!-- /ko -->
<!-- ko if: !($root.stagedUser().force_account_confirmation())
Expand Down
4 changes: 1 addition & 3 deletions corehq/apps/users/static/users/js/mobile_workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ hqDefine("users/js/mobile_workers",[
self.force_account_confirmation_by_sms() || self.force_account_confirmation() || self.account_invite_by_cid())
);

self.passwordEnabled = ko.observable(!(self.force_account_confirmation_by_sms() || self.force_account_confirmation()));

self.action_error = ko.observable(''); // error when activating/deactivating a user

self.edit_url = ko.computed(function () {
Expand Down Expand Up @@ -331,7 +329,7 @@ hqDefine("users/js/mobile_workers",[
return self.STATUS.DISABLED;
}

if (self.stagedUser().force_account_confirmation_by_sms()) {
if (self.stagedUser().force_account_confirmation_by_sms() || self.stagedUser().account_invite_by_cid()) {
return self.STATUS.DISABLED;
}

Expand Down
50 changes: 40 additions & 10 deletions corehq/apps/users/views/mobile/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,10 +802,15 @@ def create_mobile_worker(self, in_data):

if self.new_mobile_worker_form.cleaned_data['send_account_confirmation_email']:
send_account_confirmation_if_necessary(couch_user)

if self.new_mobile_worker_form.cleaned_data['force_account_confirmation_by_sms']:
phone_number = self.new_mobile_worker_form.cleaned_data['phone_number']
couch_user.set_default_phone_number(phone_number)
send_account_confirmation_sms_if_necessary(couch_user)
if self.new_mobile_worker_form.cleaned_data['account_invite_by_cid']:
phone_number = self.new_mobile_worker_form.cleaned_data['phone_number']
couch_user.set_default_phone_number(phone_number)
send_connectid_invite_sms(couch_user)

plan_limit, user_count = Subscription.get_plan_and_user_count_by_domain(self.domain)
check_and_send_limit_email(self.domain, plan_limit, user_count, user_count - 1)
Expand All @@ -827,14 +832,19 @@ def create_attendee_for_user(self, commcare_user):

def _build_commcare_user(self):
username = self.new_mobile_worker_form.cleaned_data['username']
password = self.new_mobile_worker_form.cleaned_data['new_password']
if self.new_mobile_worker_form.cleaned_data['account_invite_by_cid']:
# Passwordless login using ConnectID
password = ''
else:
password = self.new_mobile_worker_form.cleaned_data['new_password']
first_name = self.new_mobile_worker_form.cleaned_data['first_name']
email = self.new_mobile_worker_form.cleaned_data['email']
last_name = self.new_mobile_worker_form.cleaned_data['last_name']
location_id = self.new_mobile_worker_form.cleaned_data['location_id']
is_account_confirmed = not (
self.new_mobile_worker_form.cleaned_data['force_account_confirmation']
or self.new_mobile_worker_form.cleaned_data['force_account_confirmation_by_sms'])
or self.new_mobile_worker_form.cleaned_data['force_account_confirmation_by_sms']
or self.new_mobile_worker_form.cleaned_data['account_invite_by_cid'])

role_id = UserRole.commcare_user_default(self.domain).get_id
commcare_user = CommCareUser.create(
Expand Down Expand Up @@ -884,6 +894,7 @@ def _construct_form_data(self, in_data):
'force_account_confirmation': user_data.get('force_account_confirmation'),
'send_account_confirmation_email': user_data.get('send_account_confirmation_email'),
'force_account_confirmation_by_sms': user_data.get('force_account_confirmation_by_sms'),
'account_invite_by_cid': user_data.get('account_invite_by_cid'),
'phone_number': user_data.get('phone_number'),
'deactivate_after_date': user_data.get('deactivate_after_date'),
'domain': self.domain,
Expand Down Expand Up @@ -1737,21 +1748,40 @@ def link_connectid_user(request, domain):
def send_connectid_invite(request, domain, user_id):
# Currently same as what send_confirmation_sms does
user = CommCareUser.get_by_user_id(user_id, domain)
if user.is_account_confirmed or not user.is_commcare_user() or user.domain != domain:
messages.error(request, "The user is already confirmed or is not a mobile user")
if user.domain != domain:
return HttpResponse(status=400)
is_sent = send_connectid_invite_sms(user)
if not is_sent:
return HttpResponse(status=400)
return HttpResponse(status=200)


def send_connectid_invite_sms(user):
if user.is_account_confirmed or not user.is_commcare_user():
messages.error(request, "The user is already confirmed or is not a mobile user")

Check failure on line 1761 in corehq/apps/users/views/mobile/users.py

View workflow job for this annotation

GitHub Actions / Flake8

corehq/apps/users/views/mobile/users.py#L1761

Undefined name 'request' (F821)
return False

invite_code = encrypt_account_confirmation_info(user)
deeplink = f"connect://hq_invite/{get_site_domain()}/a/{quote_plus(domain)}/{quote_plus(invite_code)}/{quote_plus(user.username)}/"
deeplink = f"connect://hq_invite/{get_site_domain()}/a/{quote_plus(user.domain)}/{quote_plus(invite_code)}/{quote_plus(user.raw_username)}/"

Check failure on line 1765 in corehq/apps/users/views/mobile/users.py

View workflow job for this annotation

GitHub Actions / Flake8

corehq/apps/users/views/mobile/users.py#L1765

Line too long (144 > 115 characters) (E501)
text_content = f"""
You are invited to join a CommCare project (domain)
Please click on {deeplink} to accept.
You are invited to join a CommCare project ({user.domain})
Please click on {deeplink} to join using your ConnectID
account.
Once you confirm, you will be able to login using your
ConnectID account. Your username is {(user.raw_username)}
Thanks.
-The CommCare HQ team.
"""
return send_sms(
domain=domain,

send_sms(
domain=user.domain,
contact=None,
phone_number=user.default_phone_number,
text=text_content)
text=text_content
)
return True


@csrf_exempt
Expand Down

0 comments on commit cf7b51f

Please sign in to comment.