api.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import logging
  18. from desktop.decorators import check_superuser_permission
  19. from desktop.lib.django_util import JsonResponse
  20. from desktop.lib.i18n import smart_unicode
  21. from useradmin.models import User, Group
  22. LOG = logging.getLogger(__name__)
  23. def error_handler(view_fn):
  24. def decorator(*args, **kwargs):
  25. response = {}
  26. try:
  27. return view_fn(*args, **kwargs)
  28. except Exception as e:
  29. LOG.exception('Error running %s' % view_fn)
  30. response['status'] = -1
  31. response['message'] = smart_unicode(e)
  32. return JsonResponse(response)
  33. return decorator
  34. @error_handler
  35. @check_superuser_permission
  36. def get_users(request):
  37. """
  38. Returns all users with username, ID, groups, active and superuser status by default.
  39. Optional params:
  40. username=<username> - Filter by username
  41. groups=<groupnames> - List of group names to filter on (additive "OR" search)
  42. is_active=true - Only return active users (defaults to all users)
  43. """
  44. response = {
  45. 'users': []
  46. }
  47. username = request.GET.get('username', '').lower()
  48. groups = request.GET.getlist('groups')
  49. is_active = request.GET.get('is_active', '').lower()
  50. users = User.objects
  51. if is_active and is_active == 'true':
  52. users = users.filter(is_active=True)
  53. if username:
  54. users = users.filter(username=username)
  55. if groups:
  56. group_ids = []
  57. for groupname in groups:
  58. groupname = groupname.lower()
  59. try:
  60. group = Group.objects.get(name=groupname)
  61. group_ids.append(group.id)
  62. except Group.DoesNotExist as e:
  63. LOG.exception("Failed to filter by group, group with name %s not found." % groupname)
  64. users = users.filter(groups__in=group_ids)
  65. users = users.order_by('username')
  66. for user in users:
  67. user = {
  68. 'id': user.id,
  69. 'username': user.username,
  70. 'groups': [group.name for group in user.groups.all()],
  71. 'is_active': user.is_active,
  72. 'superuser': user.is_superuser
  73. }
  74. response['users'].append(user)
  75. response['status'] = 0
  76. return JsonResponse(response)