<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def get_user_roles(user):
 """Get a list of a users's roles."""

    if user:
        groups = user.groups.all()   # Important! all() query may be cached on User with prefetch_related.
        roles = (RolesManager.retrieve_role(group.name) for group in groups if group.name in RolesManager.get_roles_names())
        return sorted(roles, key=lambda r: r.get_name() )
    else:
        return []