ソースを参照

[security] Adding HDFS diff mode logic

Romain Rigaux 11 年 前
コミット
a19fb44

+ 2 - 0
apps/filebrowser/src/filebrowser/views.py

@@ -361,6 +361,8 @@ def listdir_paged(request, path):
     do_as = None
     if request.user.is_superuser:
       do_as = request.GET.get('doas', request.user.username)
+    if request.user.is_superuser and hasattr(request, 'doas'):
+      do_as = request.doas
 
     home_dir_path = request.user.get_home_directory()
     breadcrumbs = parse_breadcrumbs(path)

+ 32 - 0
apps/security/src/security/api/hdfs.py

@@ -19,7 +19,9 @@ import json
 
 from django.http import HttpResponse
 from django.utils.translation import ugettext as _
+
 from desktop.lib.exceptions_renderable import PopupException
+from filebrowser.views import listdir_paged
 
 
 def _get_acl_name(acl):
@@ -28,6 +30,36 @@ def _get_acl_name(acl):
 def _get_acl(acl):
   return _get_acl_name(acl) + ('r' if acl['r']  else '-') + ('w' if acl['w'] else '-') + ('x' if acl['x'] else '-')
 
+def _diff_list_dir(user_listing, hdfs_listing):
+  user_files = [f['stats']['path'] for f in user_listing['files']]
+  hdfs_files = [f['stats']['path'] for f in hdfs_listing['files']]
+  
+  # Files visible by hdfs only  
+  hdfs_only = list(set(hdfs_files) - set(user_files))
+  new_hdfs = filter(lambda f: f['stats']['path'] in hdfs_only, hdfs_listing['files'])
+
+  for f in new_hdfs:
+    f['striked'] = True
+    
+  listing = user_listing['files'] + new_hdfs
+  
+  return sorted(listing, key=lambda f: f['path']) 
+
+
+def list_hdfs(request, path):
+  try:
+    json_response = listdir_paged(request, path)
+  except:
+    json_response = HttpResponse(json.dumps({'files': []}), mimetype="application/json") # AccessControlException: Permission denied: user=test, access=READ_EXECUTE, inode="/tmp/dir":romain:supergroup:drwxr-xr-x:group::r-x,group:bob:---,group:test:---,default:user::rwx,default:group::r--,default:mask::r--,default:other::rwx (error 403)
+  if json.loads(request.GET.get('isDiffMode', 'false')):
+    request.doas = 'hdfs'
+    hdfs_response = listdir_paged(request, path)
+    resp = json.loads(json_response.content)
+    resp['files'] = _diff_list_dir(resp, json.loads(hdfs_response.content))
+    json_response.content = json.dumps(resp)
+      
+  return json_response
+
 
 def get_acls(request):
   path = request.GET.get('path')

+ 5 - 2
apps/security/src/security/templates/hdfs.mako

@@ -227,6 +227,10 @@ ${ layout.menubar(section='hdfs') }
 </%def>
 
 <%def name="aclBitPullRight()">
+  <span data-bind="visible: striked">
+    This is not visible by current user <span data-bind="text: $root.doAs">
+  </span></span>
+  
   <div class="pull-right" style="margin-right: 20px" data-bind="visible: aclBit()">
     <i class="fa fa-lock"></i>
   </div>
@@ -330,5 +334,4 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', itemDblClick='$root.a
 </script>
 
 
-
-${ commonfooter(messages) | n,unicode }
+${ commonfooter(messages) | n,unicode }

+ 1 - 0
apps/security/src/security/urls.py

@@ -26,6 +26,7 @@ urlpatterns = patterns('security.views',
 
 
 urlpatterns += patterns('security.api.hdfs',
+  url(r'^api/hdfs/list(?P<path>/.*)$', 'list_hdfs', name='list_hdfs'),
   url(r'^api/hdfs/get_acls$', 'get_acls', name='get_acls'),
   url(r'^api/hdfs/update_acls', 'update_acls', name='update_acls'),
 )

+ 8 - 4
apps/security/static/js/hdfs.ko.js

@@ -68,6 +68,7 @@ var Assist = function (vm, assist) {
     name: "__HUEROOT__",
     path: "__HUEROOT__",
     aclBit: false,
+    striked: false,
     selected: false,
     nodes: [
       {
@@ -76,6 +77,7 @@ var Assist = function (vm, assist) {
         isDir: true,
         isExpanded: true,
         aclBit: false,
+        striked: false,
         selected: false,
         nodes: []
       }
@@ -165,6 +167,7 @@ var Assist = function (vm, assist) {
         name: _chunks[_chunks.length - 1],
         path: item.path,
         aclBit: item.rwx.indexOf('+') != -1,
+        striked: item.striked != null,
         isExpanded: true,
         isDir: item.type == "dir",
         nodes: []
@@ -215,10 +218,11 @@ var Assist = function (vm, assist) {
   }
 
   self.fetchPath = function () {
-    $.getJSON('/filebrowser/view' + self.path(), {
+	$.getJSON('/security/api/hdfs/list' + self.path(), {
       'pagesize': 15,
       'format': 'json',
-      'doas': vm.doAs()
+      'doas': vm.doAs(),
+      'isDiffMode': self.isDiffMode(),
     }, function (data) {
       self.treeLoadingStatus[self.path()] = true;
       self.loadParents(data.breadcrumbs);
@@ -228,7 +232,8 @@ var Assist = function (vm, assist) {
           self.convertItemToObject(item);
           self.files.push(ko.mapping.fromJS({
               'path': item.path,
-              'aclBit': item.rwx.indexOf('+') != -1
+              'aclBit': item.rwx.indexOf('+') != -1,
+              'striked': item.striked != null,
             })
           );
         });
@@ -320,7 +325,6 @@ var HdfsViewModel = function (initial) {
     self.assist.path(path);
   }
 
-
   self.fetchUsers = function () {
     $.getJSON('/desktop/api/users/autocomplete', function (data) {
       $.each(data.users, function (i, user) {