hdfs.ko.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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})?(,(default:)?(user|group|mask|other):[[A-Za-z_][A-Za-z0-9._-]]*:([rwx-]{3})?)*$
  18. m = acl.match(/(.*?):(.*?):(.)(.)(.)/);
  19. var acl = ko.mapping.fromJS({
  20. 'type': m[1],
  21. 'name': m[2],
  22. 'r': m[3] != '-',
  23. 'w': m[4] != '-',
  24. 'x': m[5] != '-',
  25. 'status': '',
  26. });
  27. acl.type.subscribe(function(){
  28. acl.status('modified');
  29. });
  30. acl.name.subscribe(function(){
  31. acl.status('modified');
  32. });
  33. acl.r.subscribe(function(){
  34. acl.status('modified');
  35. });
  36. acl.w.subscribe(function(){
  37. acl.status('modified');
  38. });
  39. acl.x.subscribe(function(){
  40. acl.status('modified');
  41. });
  42. return acl;
  43. }
  44. function printAcl(acl) {
  45. return acl.type() + ':' + acl.name() + ':' + (acl.r() ? 'r' : '-') + (acl.w() ? 'w' : '-') + (acl.x() ? 'x' : '-');
  46. }
  47. var Assist = function (vm, assist) {
  48. var self = this;
  49. self.path = ko.observable('');
  50. self.path.subscribe(function () {
  51. self.fetchPath()
  52. });
  53. self.files = ko.observableArray();
  54. self.acls = ko.observableArray();
  55. self.owner = ko.observable('');
  56. self.group = ko.observable('');
  57. self.changed = ko.computed(function() {
  58. return $.grep(self.acls(), function(acl){ return ['new', 'deleted', 'modified'].indexOf(acl.status()) != -1 });
  59. });
  60. self.addAcl = function() {
  61. var newAcl = parseAcl('group::---');
  62. newAcl.status('new');
  63. self.acls.push(newAcl);
  64. };
  65. self.removeAcl = function(acl) {
  66. if (acl.status() == 'new') {
  67. self.acls.remove(acl);
  68. } else {
  69. acl.status('deleted');
  70. }
  71. };
  72. self.fetchPath = function () {
  73. $.getJSON('/filebrowser/view' + self.path() + "?pagesize=15&format=json", function (data) {
  74. if (data['files'] && data['files'][0]['type'] == 'dir') { // Hack for now
  75. self.files.removeAll();
  76. $.each(data.files, function(index, item) {
  77. self.files.push(ko.mapping.fromJS({
  78. 'path': item.path,
  79. 'aclBit': item.rwx.indexOf('+') != -1
  80. })
  81. );
  82. });
  83. }
  84. self.getAcls();
  85. }).fail(function (xhr, textStatus, errorThrown) {
  86. $(document).trigger("error", xhr.responseText);
  87. });
  88. };
  89. self.getAcls = function () {
  90. $(".jHueNotify").hide();
  91. logGA('get_acls');
  92. $.getJSON('/security/api/hdfs/get_acls', {
  93. 'path': self.path()
  94. }, function (data) {
  95. self.acls.removeAll();
  96. $.each(data.entries, function(index, item) {
  97. self.acls.push(parseAcl(item));
  98. });
  99. self.owner(data.owner);
  100. self.group(data.group);
  101. }).fail(function (xhr, textStatus, errorThrown) {
  102. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  103. $(document).trigger("error", xhr.responseText);
  104. }
  105. });
  106. };
  107. self.updateAcls = function () {
  108. $(".jHueNotify").hide();
  109. logGA('updateAcls');
  110. $.post("/security/api/hdfs/update_acls", {
  111. 'path': self.path(),
  112. 'acls': ko.mapping.toJSON(self.acls()),
  113. }, function (data) {
  114. var toDelete = []
  115. $.each(self.acls(), function(index, item) {
  116. if (item.status() == 'deleted') {
  117. toDelete.push(item);
  118. } else {
  119. item.status('');
  120. }
  121. });
  122. $.each(toDelete, function(index, item) {
  123. self.acls.remove(item);
  124. });
  125. $(document).trigger("info", 'Done!');
  126. }
  127. ).fail(function (xhr, textStatus, errorThrown) {
  128. $(document).trigger("error", xhr.responseText);
  129. });
  130. }
  131. }
  132. // Might rename Assist to Acls and create Assist for the tree widget?
  133. var HdfsViewModel = function (context_json) {
  134. var self = this;
  135. self.assist = new Assist(self, context_json.assist);
  136. self.assist.path('/tmp/acl');
  137. };
  138. function logGA(page) {
  139. if (typeof trackOnGA == 'function') {
  140. trackOnGA('security/hdfs' + page);
  141. }
  142. }