Skip to content

Commit

Permalink
feat: edits
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinan029 committed Jul 24, 2024
1 parent d7cd89d commit 5caccc9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 113 deletions.
119 changes: 34 additions & 85 deletions enterprise/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
get_last_course_run_end_date,
has_course_run_available_for_enrollment,
track_enrollment,
get_pending_enterprise_customer_users,
)
from enterprise.validators import validate_pgp_key

Expand Down Expand Up @@ -854,7 +855,7 @@ def to_representation(self, instance):
Because we are returning whether or not the instance was created from the create method, we must use the
instance for to_representation and ignore the "created" half of the tuple
'''
return super().to_representation(instance[0])
return super().to_representation(instance)

def create(self, attrs): # pylint: disable=arguments-renamed
"""
Expand Down Expand Up @@ -1834,105 +1835,53 @@ class EnterpriseUserSerializer(serializers.Serializer):
"""
Serializer for EnterpriseCustomerUser model with additions.
"""

class Meta:
model = models.EnterpriseCustomerUser
fields = (
'enterprise_customer_user_id',
'user_name',
'user_email',
'is_admin',
'pending_enterprise_customer_user_id',
'is_pending_admin'
'enterprise_customer_user'
'pending_enterprise_customer_user',
'role_assignments'
'is_admin'
)

enterprise_customer_user_id = serializers.SerializerMethodField()
user_name = serializers.SerializerMethodField()
user_email = serializers.SerializerMethodField()
enterprise_customer_user = UserSerializer(source="user", required=False, default=None)
pending_enterprise_customer_user = serializers.SerializerMethodField(required=False, default=None)
role_assignments = serializers.SerializerMethodField()
is_admin = serializers.SerializerMethodField()
pending_enterprise_customer_user_id = serializers.CharField(required=False, default=None)
is_pending_admin = serializers.BooleanField(required=False, default=False)

def get_enterprise_customer_user_id(self, obj):
"""
Get enterprise customer user id
"""
return obj.user_id

def get_user_name(self, obj):
"""
Get enterprise customer user name
"""
return obj.enterprise_customer.name

def get_user_email(self, obj):

def get_pending_enterprise_customer_user(self, obj):
"""
Get enterprise customer user email
Return either the pending user info
"""
return obj.enterprise_customer.contact_email
enterprise_customer_uuid = obj.enterprise_customer.uuid
# if the obj has a user id, this means that it is a realized user, not pending
if hasattr(obj, 'user_id'):
return None
return get_pending_enterprise_customer_users(obj.user_email, enterprise_customer_uuid)

def get_is_admin(self, obj):
"""
Make admin determination based on Enterprise role in SystemWideEnterpriseUserRoleAssignment
"""
enterprise_customer_uuid = obj.enterprise_customer.uuid

if not enterprise_customer_uuid:
return None

user_email = obj.user_email
admin_instance = SystemWideEnterpriseUserRoleAssignment.objects.filter(
role__name=ENTERPRISE_ADMIN_ROLE,
enterprise_customer_id=enterprise_customer_uuid
enterprise_customer_id=enterprise_customer_uuid,
user__email=user_email
)

return admin_instance.exists()


class EnterprisePendingCustomerUserSerializer(serializers.Serializer):
"""
Serializer for PendingEnterpriseCustomerUser model with additions.
"""

class Meta:
model = models.PendingEnterpriseCustomerUser
fields = (
'enterprise_customer_user_id',
'user_name',
'user_email',
'is_admin',
'pending_enterprise_customer_user_id',
'is_pending_admin'
)

enterprise_customer_user_id = serializers.CharField(required=False, default=None)
user_name = serializers.CharField(required=False, default=None)
user_email = serializers.SerializerMethodField()
is_admin = serializers.BooleanField(required=False, default=False)
pending_enterprise_customer_user_id = serializers.SerializerMethodField()
is_pending_admin = serializers.SerializerMethodField()

def get_user_email(self, obj):
"""
Get pending enterprise customer email
"""
return obj.user_email

def get_pending_enterprise_customer_user_id(self, obj):
"""
Get pending enterprise customer user id
"""
return obj.id

def get_is_pending_admin(self, obj):
"""
Make admin determination based on existence in PendingEnterpriseCustomerAdminUser
"""
enterprise_customer_uuid = obj.enterprise_customer.uuid
enterprise_customer_email = obj.user_email

pending_admin_instance = models.PendingEnterpriseCustomerAdminUser.objects.filter(
user_email=enterprise_customer_email,
enterprise_customer_id=enterprise_customer_uuid
)

return pending_admin_instance.exists()
def get_role_assignments(self, obj):
if hasattr(obj, 'user_id'):
user_id = obj.user_id
enterprise_customer_uuid = obj.enterprise_customer.uuid

role_assignments = models.SystemWideEnterpriseUserRoleAssignment.objects.filter(
user_id=user_id
).select_related('role')
role_assignments_by_ecu_id = [
role_assignment.role.name for role_assignment in role_assignments if
role_assignment.user_id == user_id and
role_assignment.enterprise_customer_id == enterprise_customer_uuid
]
return role_assignments_by_ecu_id
2 changes: 1 addition & 1 deletion enterprise/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
name='enterprise-group-remove-learners'
),
re_path(
r'^enterprise-user/(?P<enterprise_uuid>[A-Za-z0-9-]+)$',
r'^enterprise-customer/(?P<enterprise_uuid>[A-Za-z0-9-]+)/users/?$',
enterprise_user.EnterpriseUserViewSet.as_view(
{'get': 'retrieve'}
),
Expand Down
43 changes: 16 additions & 27 deletions enterprise/api/v1/views/enterprise_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,26 @@ def retrieve(self, request, *args, **kwargs):
- Filter down the queryset of groups available to the requesting uuid.
"""
enterprise_uuid = kwargs.get('enterprise_uuid', None)

users = []
try:
enterprise_customer_queryset = models.EnterpriseCustomerUser.objects.filter(
enterprise_customer__uuid=enterprise_uuid
enterprise_customer__uuid=enterprise_uuid,
)

if enterprise_customer_queryset.exists():
serializer = serializers.EnterpriseUserSerializer(enterprise_customer_queryset, many=True)
return response.Response(serializer.data)

except ValidationError:
# did not find UUID match in EnterpriseCustomerUser, try in PendingEnterpriseCustomerUser
pass

try:
users.extend(enterprise_customer_queryset)
pending_enterprise_customer_queryset = models.PendingEnterpriseCustomerUser.objects.filter(
enterprise_customer_id=enterprise_uuid
)

if pending_enterprise_customer_queryset.exists():
serializer = serializers.EnterprisePendingCustomerUserSerializer(
pending_enterprise_customer_queryset,
many=True
)
return response.Response(serializer.data)

enterprise_customer__uuid=enterprise_uuid
).order_by('user_email')
users.extend(pending_enterprise_customer_queryset)
except ValidationError:
# did not find UUID match in either EnterpriseCustomerUser or PendingEnterpriseCustomerUser
pass

return response.Response(
{'detail': 'Could not find enterprise uuid {}'.format(enterprise_uuid)},
status=status.HTTP_404_NOT_FOUND
return response.Response(
{'detail': 'Could not find enterprise uuid {}'.format(enterprise_uuid)},
status=status.HTTP_404_NOT_FOUND
)
serializer = serializers.EnterpriseUserSerializer(
users,
many=True
)
serializer_data = sorted(
serializer.data, key=lambda k: k['is_admin'], reverse=True)
return response.Response(serializer_data)
30 changes: 30 additions & 0 deletions enterprise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,13 @@ def enterprise_customer_invite_key_model():
return apps.get_model('enterprise', 'EnterpriseCustomerInviteKey')


def pending_enterprise_customer_admin_user_model():
"""
Returns the ``PendingEnterpriseCustomerAdminUser`` class.
"""
return apps.get_model('enterprise', 'PendingEnterpriseCustomerAdminUser')


def get_enterprise_customer(uuid):
"""
Get the ``EnterpriseCustomer`` instance associated with ``uuid``.
Expand Down Expand Up @@ -2509,3 +2516,26 @@ def get_active_sso_configurations_for_customer(customer_uuid):
'display_name': sso_configuration.get('display_name'),
})
return active_configurations


def get_pending_enterprise_customer_users(user_email, enterprise_customer_uuid):
"""
Helper method to check if a pending customer user is also a pending admin.
All pending admins are pending users, but not all pending users are pending admins.
"""
pendingEnterpriseCustomerAdminUser = pending_enterprise_customer_admin_user_model()
pending_enterprise_customer_admin_user = pendingEnterpriseCustomerAdminUser.objects.filter(
enterprise_customer__uuid=enterprise_customer_uuid,
user_email=user_email)
if pending_enterprise_customer_admin_user:
return {
'is_pending_admin': True,
'is_pending_learner': True,
'user_email': user_email,
}
return {
'is_pending_admin': False,
'is_pending_learner': True,
'user_email': user_email,
}

0 comments on commit 5caccc9

Please sign in to comment.