소스 검색

HUE-8758 [connectors] Add permissions for available connector instances

Romain 5 년 전
부모
커밋
829020c9d7

+ 17 - 4
apps/useradmin/src/useradmin/models.py

@@ -52,7 +52,8 @@ from django.utils import timezone as dtz
 from django.utils.translation import ugettext_lazy as _t
 
 from desktop import appmanager
-from desktop.conf import ENABLE_ORGANIZATIONS
+from desktop.conf import ENABLE_ORGANIZATIONS, ENABLE_CONNECTORS
+from desktop.lib.connectors.models import _get_installed_connectors
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.idbroker.conf import is_idbroker_enabled
 from desktop.monkey_patches import monkey_patch_username_validator
@@ -262,14 +263,24 @@ def update_app_permissions(**kwargs):
     uptodate = 0
     added = []
 
-    for app_obj in appmanager.DESKTOP_APPS:
+    if ENABLE_CONNECTORS.get():
+      import collections
+      ConnectorPerm = collections.namedtuple('ConnectorPerm', 'name settings')
+      apps = [
+        ConnectorPerm(name=connector['name'], settings=[]) for connector in _get_installed_connectors()
+      ]
+    else:
+      apps = appmanager.DESKTOP_APPS
+
+    for app_obj in apps:
+      print(app_obj)
       app = app_obj.name
       actions = set([("access", "Launch this application")])
       actions.update(getattr(app_obj.settings, "PERMISSION_ACTIONS", []))
 
       if app not in current:
         current[app] = {}
-
+      print(current)
       for action, description in actions:
         c = current[app].get(action)
         if c:
@@ -283,8 +294,9 @@ def update_app_permissions(**kwargs):
           new_dp = HuePermission(app=app, action=action, description=description)
           new_dp.save()
           added.append(new_dp)
+        print('%s %s %s' % (updated, uptodate, added))
 
-    # Add all hue permissions to default group.
+    # Add all permissions to default group.
     default_group = get_default_user_group()
     if default_group:
       for new_dp in added:
@@ -309,6 +321,7 @@ def update_app_permissions(**kwargs):
         )
       )
 
+
 models.signals.post_migrate.connect(update_app_permissions)
 # models.signals.post_migrate.connect(get_default_user_group)
 

+ 4 - 4
apps/useradmin/src/useradmin/templates/list_permissions.mako

@@ -26,9 +26,9 @@ from useradmin.models import group_permissions, Group
 <%namespace name="layout" file="layout.mako" />
 
 % if not is_embeddable:
-${ commonheader(_('Hue Permissions'), "useradmin", user, request) | n,unicode }
+  ${ commonheader(_('Hue Permissions'), "useradmin", user, request) | n,unicode }
 % endif
-${ layout.menubar(section='permissions') }
+  ${ layout.menubar(section='permissions') }
 
 <div id="permissionsComponents" class="useradmin container-fluid">
   <div class="card card-small">
@@ -56,11 +56,11 @@ ${ layout.menubar(section='permissions') }
               <strong>
                 <a title="${_('Edit permission')}"
                     href="${ url('useradmin.views.edit_permission', app=perm.app, priv=perm.action) }"
-                    data-name="${perm.app}" data-row-selector="true">${perm.app}
+                    data-name="${perm.app}" data-row-selector="true">${ perm.app }
                 </a>
               </strong>
             %else:
-              <strong>${perm.app}</strong>
+              <strong>${ perm.app }</strong>
             %endif
           </td>
             <td>${ perm.description }</td>

+ 7 - 3
apps/useradmin/src/useradmin/views.py

@@ -40,7 +40,7 @@ from django.utils.translation import get_language, ugettext as _
 
 import desktop.conf
 from desktop.auth.backend import is_admin
-from desktop.conf import LDAP, ENABLE_ORGANIZATIONS
+from desktop.conf import LDAP, ENABLE_ORGANIZATIONS, ENABLE_CONNECTORS
 from desktop.lib.django_util import JsonResponse, render
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.models import _get_apps
@@ -93,10 +93,14 @@ def list_groups(request):
 
 
 def list_permissions(request):
-  current_app, other_apps, apps_list = _get_apps(request.user)
+  if ENABLE_CONNECTORS.get():
+    permissions = HuePermission.objects.all()
+  else:
+    current_app, other_apps, apps_list = _get_apps(request.user)
+    permissions = HuePermission.objects.filter(app__in=apps_list)
 
   return render("list_permissions.mako", request, {
-    'permissions': HuePermission.objects.filter(app__in=apps_list),
+    'permissions': permissions,
     'is_embeddable': request.GET.get('is_embeddable', False)
   })
 

+ 1 - 1
desktop/core/src/desktop/lib/apputil.py

@@ -52,7 +52,7 @@ def get_app_for_module(module):
   for app in settings.INSTALLED_APPS:
     if module.__name__.startswith('desktop.lib.metrics.views'):
       return app
-    if module.__name__.startswith('desktop.lib.connectors.views'):
+    if module.__name__.startswith('desktop.lib.connectors'):
       return app
     if module.__name__.startswith(app) and not module.__name__.startswith("desktop.lib"):
       return app

+ 17 - 13
desktop/core/src/desktop/lib/connectors/api.py

@@ -20,9 +20,12 @@ import logging
 
 from django.utils.translation import ugettext as _
 
+from useradmin.models import update_app_permissions
+
 from desktop.lib.django_util import JsonResponse, render
 from desktop.lib.exceptions_renderable import PopupException
-from desktop.lib.connectors.models import AVAILABLE_CONNECTORS, _get_connector_by_id, _get_installed_connectors
+from desktop.lib.connectors import models
+from desktop.lib.connectors.models import AVAILABLE_CONNECTORS, _get_connector_by_id, _get_installed_connectors, _group_category_connectors
 from desktop.lib.connectors.types import CONNECTOR_TYPES, CATEGORIES
 
 
@@ -53,6 +56,8 @@ def new_connector(request, dialect):
   instance['nice_name'] = dialect.title()
   instance['id'] = None
 
+  update_app_permissions()
+
   return JsonResponse({'connector': instance})
 
 
@@ -63,9 +68,7 @@ def get_connector(request, id):
 
 
 def update_connector(request):
-  global CONNECTOR_IDS
-
-  connector = json.loads(request.POST.get('connector'), '{}')
+  connector = json.loads(request.POST.get('connector', '{}'))
   saved_as = False
 
   if connector.get('id'):
@@ -74,11 +77,13 @@ def update_connector(request):
   else:
     saved_as = True
     instance = connector
-    instance['id'] = CONNECTOR_IDS
+    instance['id'] = models.CONNECTOR_IDS
     instance['nice_name'] = instance['nice_name']
-    instance['name'] = '%s-%s' % (instance['dialect'], CONNECTOR_IDS)
-    CONNECTOR_IDS += 1
-    CONNECTOR_INSTANCES.append(instance)
+    instance['name'] = '%s-%s' % (instance['dialect'], models.CONNECTOR_IDS)
+    models.CONNECTOR_IDS += 1
+    models.CONNECTOR_INSTANCES.append(instance)
+
+  update_app_permissions()
 
   return JsonResponse({'connector': instance, 'saved_as': saved_as})
 
@@ -93,15 +98,14 @@ def _get_connector_by_type(dialect):
 
 
 def delete_connector(request):
-  global CONNECTOR_INSTANCES
-
   connector = json.loads(request.POST.get('connector'), '{}')
 
-  size_before = len(CONNECTOR_INSTANCES)
-  CONNECTOR_INSTANCES = [_connector for _connector in CONNECTOR_INSTANCES if _connector['name'] != connector['name']]
-  size_after = len(CONNECTOR_INSTANCES)
+  size_before = len(models.CONNECTOR_INSTANCES)
+  models.CONNECTOR_INSTANCES = [_connector for _connector in models.CONNECTOR_INSTANCES if _connector['name'] != connector['name']]
+  size_after = len(models.CONNECTOR_INSTANCES)
 
   if size_before == size_after + 1:
+    update_app_permissions()
     return JsonResponse({})
   else:
     raise PopupException(_('No connector with the name %(name)s found.') % connector)

+ 2 - 3
desktop/core/src/desktop/middleware.py

@@ -169,7 +169,7 @@ class NotificationMiddleware(object):
 class AppSpecificMiddleware(object):
   @classmethod
   def augment_request_with_app(cls, request, view_func):
-    """ Stuff the app into the request for use in later-stage middleware """
+    """Inject the app name into the request for use in later-stage middleware"""
     if not hasattr(request, "_desktop_app"):
       module = inspect.getmodule(view_func)
       request._desktop_app = apputil.get_app_for_module(module)
@@ -185,7 +185,6 @@ class AppSpecificMiddleware(object):
     return self.middlewares_by_app.get(app, {}).get(type, [])
 
   def process_view(self, request, view_func, view_args, view_kwargs):
-    """View middleware"""
     self.augment_request_with_app(request, view_func)
     if not request._desktop_app:
       return None
@@ -194,7 +193,7 @@ class AppSpecificMiddleware(object):
     ret = None
     for middleware in self._get_middlewares(request._desktop_app, 'view'):
       ret = middleware(request, view_func, view_args, view_kwargs)
-      if ret: return ret # short circuit
+      if ret: return ret  # Short circuit
     return ret
 
   def process_response(self, request, response):

+ 1 - 2
desktop/libs/dashboard/src/dashboard/controller.py

@@ -16,8 +16,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from builtins import str
-from builtins import object
+from builtins import str, object
 import logging
 
 from django.db.models import Q