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

Implement Deactivate user endpoint on Connect #356

Open
wants to merge 4 commits into
base: main
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
11 changes: 11 additions & 0 deletions commcare_connect/connect_id_client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Message,
MessagingBulkResponse,
MessagingResponse,
UserInfo,
)
from commcare_connect.organization.models import Organization

Expand Down Expand Up @@ -67,6 +68,16 @@ def filter_users(country_code: str, credential: list[str]):
return [ConnectIdUser(**user_dict) for user_dict in data["found_users"]]


def get_user_info(token: str) -> UserInfo:
# Returns userinfo for mobile user from ConnectID using access_token.
response = httpx.request(
GET, f"{settings.CONNECTID_URL}/o/userinfo/", headers={"Authorization": f"Bearer {token}"}
)
response.raise_for_status()
data = response.json()
return UserInfo(**data)


def _make_request(method, path, params=None, json=None, timeout=5) -> Response:
if json and not method == "POST":
raise ValueError("json can only be used with POST requests")
Expand Down
11 changes: 11 additions & 0 deletions commcare_connect/connect_id_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ class Credential:

def __str__(self) -> str:
return self.name


@dataclasses.dataclass
class UserInfo:
sub: str
name: str
phone: str
is_active: bool = True

def __str__(self) -> str:
return self.name
2 changes: 2 additions & 0 deletions commcare_connect/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
SMSStatusCallbackView,
accept_invite,
create_user_link_view,
deactivate_user,
demo_user_tokens,
start_learn_app,
user_detail_view,
Expand All @@ -21,4 +22,5 @@
path("accept_invite/<slug:invite_id>/", view=accept_invite, name="accept_invite"),
path("demo_users/", view=demo_user_tokens, name="demo_users"),
path("sms_status_callback/", SMSStatusCallbackView.as_view(), name="sms_status_callback"),
path("deactivate_user/", deactivate_user, name="deactivate_user"),
]
18 changes: 17 additions & 1 deletion commcare_connect/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from commcare_connect.connect_id_client.main import fetch_demo_user_tokens
from commcare_connect.connect_id_client.main import fetch_demo_user_tokens, get_user_info
from commcare_connect.opportunity.models import Opportunity, OpportunityAccess, UserInvite, UserInviteStatus

from .helpers import create_hq_user
Expand Down Expand Up @@ -161,3 +161,19 @@ def post(self, *args, **kwargs):
user_invite.status = UserInviteStatus.sms_not_delivered
user_invite.save()
return Response(status=200)


@csrf_exempt
@api_view(["POST"])
@authentication_classes([OAuth2Authentication])
def deactivate_user(request):
Copy link
Member

Choose a reason for hiding this comment

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

It would be a good practice to add a small note here as to where this is used.

username = request.POST.get("username")
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return Response("User does not exists.", status=400)
user_info = get_user_info(request.auth.token)
if not user_info.is_active:
user.is_active = False
user.save()
return Response(status=200)
Loading