hdfs.ko.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function parseAcl(acl) {
  17. // (default:)?(user|group|mask|other):[[A-Za-z_][A-Za-z0-9._-]]*:([rwx-]{3})?
  18. m = acl.match(/(default:)?(user|group|mask|other):(.*?):(.)(.)(.)/);
  19. var acl = ko.mapping.fromJS({
  20. 'isDefault': m[1] != null,
  21. 'type': m[2],
  22. 'name': m[3],
  23. 'r': m[4] != '-',
  24. 'w': m[5] != '-',
  25. 'x': m[6] != '-',
  26. 'status': '',
  27. });
  28. acl.type.subscribe(function() {
  29. acl.status('modified');
  30. });
  31. acl.name.subscribe(function() { // TODO duplicates
  32. acl.status('modified');
  33. });
  34. acl.r.subscribe(function() {
  35. acl.status('modified');
  36. });
  37. acl.w.subscribe(function() {
  38. acl.status('modified');
  39. });
  40. acl.x.subscribe(function() {
  41. acl.status('modified');
  42. });
  43. return acl;
  44. }
  45. function printAcl(acl) {
  46. return acl.type() + ':' + acl.name() + ':' + (acl.r() ? 'r' : '-') + (acl.w() ? 'w' : '-') + (acl.x() ? 'x' : '-');
  47. }
  48. var Assist = function(vm, assist) {
  49. var self = this;
  50. self.path = ko.observable('');
  51. self.path.subscribe(function () {
  52. self.fetchPath()
  53. });
  54. self.files = ko.observableArray();
  55. self.acls = ko.observableArray();
  56. self.regularAcls = ko.computed(function() {
  57. return $.grep(self.acls(), function(acl){ return ! acl.isDefault(); });
  58. });
  59. self.defaultAcls = ko.computed(function() {
  60. return $.grep(self.acls(), function(acl){ return acl.isDefault(); });
  61. });
  62. self.changedAcls = ko.computed(function() {
  63. return $.grep(self.acls(), function(acl){ return ['new', 'deleted', 'modified'].indexOf(acl.status()) != -1 });
  64. });
  65. self.owner = ko.observable('');
  66. self.group = ko.observable('');
  67. self.addAcl = function() {
  68. var newAcl = parseAcl('group::---');
  69. newAcl.status('new');
  70. self.acls.push(newAcl);
  71. };
  72. self.addDefaultAcl = function() {
  73. var newAcl = parseAcl('default:group::---');
  74. newAcl.status('new');
  75. self.acls.push(newAcl);
  76. };
  77. self.removeAcl = function(acl) {
  78. if (acl.status() == 'new') {
  79. self.acls.remove(acl);
  80. } else {
  81. acl.status('deleted');
  82. }
  83. };
  84. self.fetchPath = function() {
  85. $.getJSON('/filebrowser/view' + self.path() + "?pagesize=15&format=json", function (data) {
  86. if (data['files'] && data['files'][0]['type'] == 'dir') { // Hack for now
  87. self.files.removeAll();
  88. $.each(data.files, function(index, item) {
  89. self.files.push(ko.mapping.fromJS({
  90. 'path': item.path,
  91. 'aclBit': item.rwx.indexOf('+') != -1
  92. })
  93. );
  94. });
  95. }
  96. self.getAcls();
  97. }).fail(function (xhr, textStatus, errorThrown) {
  98. $(document).trigger("error", xhr.responseText);
  99. });
  100. };
  101. self.getAcls = function() {
  102. $(".jHueNotify").hide();
  103. logGA('get_acls');
  104. $.getJSON('/security/api/hdfs/get_acls', {
  105. 'path': self.path()
  106. }, function (data) {
  107. self.acls.removeAll();
  108. $.each(data.entries, function(index, item) {
  109. self.acls.push(parseAcl(item));
  110. });
  111. self.owner(data.owner);
  112. self.group(data.group);
  113. }).fail(function (xhr, textStatus, errorThrown) {
  114. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  115. $(document).trigger("error", xhr.responseText);
  116. }
  117. });
  118. };
  119. self.updateAcls = function() {
  120. $(".jHueNotify").hide();
  121. logGA('updateAcls');
  122. $.post("/security/api/hdfs/update_acls", {
  123. 'path': self.path(),
  124. 'acls': ko.mapping.toJSON(self.acls()),
  125. }, function (data) {
  126. var toDelete = []
  127. $.each(self.acls(), function(index, item) {
  128. if (item.status() == 'deleted') {
  129. toDelete.push(item);
  130. } else {
  131. item.status('');
  132. }
  133. });
  134. $.each(toDelete, function(index, item) {
  135. self.acls.remove(item);
  136. });
  137. $(document).trigger("info", 'Done!');
  138. }
  139. ).fail(function (xhr, textStatus, errorThrown) {
  140. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  141. });
  142. }
  143. }
  144. // Might rename Assist to Acls and create Assist for the tree widget?
  145. var HdfsViewModel = function(context_json) {
  146. var self = this;
  147. self.assist = new Assist(self, context_json.assist);
  148. self.assist.path('/tmp/dir');
  149. };
  150. function logGA(page) {
  151. if (typeof trackOnGA == 'function') {
  152. trackOnGA('security/hdfs' + page);
  153. }
  154. }