| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- // Licensed to Cloudera, Inc. under one
- // or more contributor license agreements. See the NOTICE file
- // distributed with this work for additional information
- // regarding copyright ownership. Cloudera, Inc. licenses this file
- // to you under the Apache License, Version 2.0 (the
- // "License"); you may not use this file except in compliance
- // with the License. You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- function parseAcl(acl) {
- // (default:)?(user|group|mask|other):[[A-Za-z_][A-Za-z0-9._-]]*:([rwx-]{3})?
- m = acl.match(/(default:)?(user|group|mask|other):(.*?):(.)(.)(.)/);
- var acl = ko.mapping.fromJS({
- 'isDefault': m[1] != null,
- 'type': m[2],
- 'name': m[3],
- 'r': m[4] != '-',
- 'w': m[5] != '-',
- 'x': m[6] != '-',
- 'status': ''
- });
- acl.type.subscribe(function () {
- acl.status('modified');
- });
- acl.name.subscribe(function () {
- acl.status('modified');
- });
- acl.r.subscribe(function () {
- acl.status('modified');
- });
- acl.w.subscribe(function () {
- acl.status('modified');
- });
- acl.x.subscribe(function () {
- acl.status('modified');
- });
- return acl;
- }
- function printAcl(acl) {
- return (acl.isDefault() ? 'default:' : '') + acl.type() + ':' + acl.name() + ':' + (acl.r() ? 'r' : '-') + (acl.w() ? 'w' : '-') + (acl.x() ? 'x' : '-');
- }
- var Assist = function (vm, assist) {
- var self = this;
- self.compareNames = function (a,b) {
- if (a.name.toLowerCase() < b.name.toLowerCase())
- return -1;
- if (a.name.toLowerCase() > b.name.toLowerCase())
- return 1;
- return 0;
- }
- self.isLoadingAcls = ko.observable(false);
- self.isLoadingTree = ko.observable(false);
- self.showAclsAsText = ko.observable(false);
- self.isDiffMode = ko.observable(false);
- self.isDiffMode.subscribe(function () {
- self.refreshTree();
- });
- self.treeAdditionalData = {};
- self.treeData = ko.observable({nodes: []});
- self.loadData = function (data) {
- self.treeData(new TreeNodeModel(data));
- };
- self.initialGrowingTree = {
- name: "__HUEROOT__",
- path: "__HUEROOT__",
- aclBit: false,
- striked: false,
- selected: false,
- rwx: "",
- page: {
- number: -1,
- num_pages: -1
- },
- nodes: [
- {
- name: "/",
- path: "/",
- isDir: true,
- isExpanded: true,
- aclBit: false,
- striked: false,
- selected: false,
- rwx: "",
- page: {
- number: -1,
- num_pages: -1
- },
- nodes: []
- }
- ]
- };
- self.growingTree = ko.observable(jQuery.extend(true, {}, self.initialGrowingTree));
- self.path = ko.observable('');
- self.path.subscribe(function (path) {
- self.pagenum(1);
- self.fetchPath();
- window.location.hash = path;
- });
- self.pagenum = ko.observable(1);
- self.fromLoadMore = false;
- self.fromRebuildTree = false;
- self.acls = ko.observableArray();
- self.originalAcls = ko.observableArray();
- self.regularAcls = ko.computed(function () {
- return $.grep(self.acls(), function (acl) {
- return ! acl.isDefault();
- });
- });
- self.defaultAcls = ko.computed(function () {
- return $.grep(self.acls(), function (acl) {
- return acl.isDefault();
- });
- });
- self.changedAcls = ko.computed(function () {
- return $.grep(self.acls(), function (acl) {
- return ['new', 'deleted', 'modified'].indexOf(acl.status()) != -1;
- });
- });
- self.owner = ko.observable('');
- self.group = ko.observable('');
- self.afterRender = function() {
- if (! self.fromLoadMore && ! self.fromRebuildTree) {
- $(document).trigger("rendered.tree");
- }
- self.fromLoadMore = false;
- self.fromRebuildTree = false;
- }
- self.addAcl = function () {
- var newAcl = parseAcl('group::---');
- newAcl.status('new');
- self.acls.push(newAcl);
- };
- self.addDefaultAcl = function () {
- var newAcl = parseAcl('default:group::---');
- newAcl.status('new');
- self.acls.push(newAcl);
- };
- self.removeAcl = function (acl) {
- if (acl.status() == 'new') {
- self.acls.remove(acl);
- } else {
- acl.status('deleted');
- }
- };
- self.convertItemToObject = function (item) {
- if (item.path != null && item.name != "." && item.name != "..") {
- var _path = item.path;
- var _parent = _path.substr(0, _path.lastIndexOf("/"));
- if (_parent == "") {
- _parent = "/";
- }
- if (_path != "/") {
- self.growingTree(self.traversePath(self.growingTree(), _parent, item));
- }
- }
- }
- self.traversePath = function (leaf, parent, item) {
- var _mainFound = false;
- leaf.nodes.forEach(function (node) {
- if (node.path == item.path) {
- _mainFound = true;
- }
- if (parent.indexOf(node.path) > -1) {
- self.traversePath(node, parent, item);
- }
- });
- if (!_mainFound && leaf.path == parent) {
- var _chunks = item.path.split("/");
- leaf.nodes.push({
- name: _chunks[_chunks.length - 1],
- path: item.path,
- aclBit: item.rwx.indexOf('+') != -1,
- striked: item.striked != null,
- isExpanded: true,
- rwx: item.rwx,
- isDir: item.type == "dir" || item.isDir == true,
- page: {
- number: -1,
- num_pages: -1
- },
- nodes: []
- });
- leaf.nodes.sort(self.compareNames);
- }
- return leaf;
- }
- self.getTreeAdditionalDataForPath = function (path) {
- if (typeof self.treeAdditionalData[path] == "undefined"){
- var _add = {
- loaded: false
- }
- self.treeAdditionalData[path] = _add;
- }
- return self.treeAdditionalData[path];
- }
- self.updatePathProperty = function (leaf, path, property, value) {
- if (leaf.path == path) {
- leaf[property] = value;
- }
- if (leaf.nodes.length > 0) {
- leaf.nodes.forEach(function (node) {
- self.updatePathProperty(node, path, property, value);
- });
- }
- return leaf;
- }
- self.updateTreeProperty = function (leaf, property, value) {
- leaf[property] = value;
- if (leaf.nodes.length > 0) {
- leaf.nodes.forEach(function (node) {
- self.updateTreeProperty(node, property, value);
- });
- }
- return leaf;
- }
- self.collapseTree = function () {
- self.updateTreeProperty(self.growingTree(), "isExpanded", false);
- self.updatePathProperty(self.growingTree(), "/", "isExpanded", true);
- self.loadData(self.growingTree());
- }
- self.collapseOthers = function () {
- self.updateTreeProperty(self.growingTree(), "isExpanded", false);
- self.updatePathProperty(self.growingTree(), "/", "isExpanded", true);
- var _path = self.path();
- var _crumb = "";
- for (var i = 0; i < _path.length; i++) {
- if ((_path[i] === "/" && _crumb != "")) {
- self.updatePathProperty(self.growingTree(), _crumb, "isExpanded", true);
- }
- _crumb += _path[i];
- }
- self.updatePathProperty(self.growingTree(), _path, "isExpanded", true);
- self.loadData(self.growingTree());
- }
- self.expandTree = function () {
- self.updateTreeProperty(self.growingTree(), "isExpanded", true);
- self.loadData(self.growingTree());
- }
- self.refreshTree = function (force) {
- self.growingTree(jQuery.extend(true, {}, self.initialGrowingTree));
- Object.keys(self.treeAdditionalData).forEach(function (path) {
- if (typeof force == "boolean" && force) {
- self.fetchPath(path);
- } else {
- if (self.treeAdditionalData[path].loaded) {
- self.fetchPath(path);
- }
- }
- });
- }
- self.rebuildTree = function (leaf, paths) {
- paths.push(leaf.path);
- if (leaf.nodes.length > 0) {
- leaf.nodes.forEach(function (node) {
- if (node.isDir){
- self.rebuildTree(node, paths);
- }
- });
- }
- return paths;
- }
- self.setPath = function (obj, toggle) {
- if (self.getTreeAdditionalDataForPath(obj.path()).loaded || (! obj.isExpanded() && ! self.getTreeAdditionalDataForPath(obj.path()).loaded)) {
- if (typeof toggle == "boolean" && toggle){
- obj.isExpanded(!obj.isExpanded());
- } else {
- obj.isExpanded(true);
- }
- self.updatePathProperty(self.growingTree(), obj.path(), "isExpanded", obj.isExpanded());
- }
- self.path(obj.path());
- }
- self.togglePath = function (obj) {
- self.setPath(obj, true);
- }
- self.openPath = function (obj) {
- window.open("/filebrowser/view" + obj.path(), '_blank');
- }
- self.loadParents = function (breadcrumbs) {
- if (typeof breadcrumbs != "undefined" && breadcrumbs != null) {
- breadcrumbs.forEach(function (crumb, idx) {
- if (idx < breadcrumbs.length - 1 && crumb.url != "") {
- var _item = {
- path: crumb.url,
- name: crumb.label,
- rwx: "",
- isDir: true,
- page: {
- number: -1,
- num_pages: -1
- }
- }
- self.convertItemToObject(_item);
- }
- });
- $(document).trigger("loaded.parents");
- }
- }
- self.fetchPath = function (optionalPath, loadCallback) {
- var _path = typeof optionalPath != "undefined" ? optionalPath : self.path();
- $.getJSON('/security/api/hdfs/list' + _path, {
- 'pagesize': 15,
- 'pagenum': self.pagenum(),
- 'format': 'json',
- 'doas': vm.doAs(),
- 'isDiffMode': self.isDiffMode()
- },
- function (data) {
- self.loadParents(data.breadcrumbs);
- if (data['files'] && data['files'][0] && data['files'][0]['type'] == 'dir') { // Hack for now
- $.each(data.files, function (index, item) {
- self.convertItemToObject(item);
- });
- }
- else {
- self.convertItemToObject(data);
- }
- self.getTreeAdditionalDataForPath(_path).loaded = true;
- if (data.page != null && data.page.number != null){
- self.updatePathProperty(self.growingTree(), _path, "page", data.page);
- }
- if (typeof loadCallback != "undefined"){
- loadCallback(data);
- }
- else {
- self.loadData(self.growingTree());
- }
- if (typeof optionalPath == "undefined"){
- self.getAcls();
- }
- }).fail(function (xhr, textStatus, errorThrown) {
- $(document).trigger("error", xhr.responseText);
- });
- };
- self.loadMore = function (what) {
- self.pagenum(what.page().next_page_number());
- self.fetchPath(what.path());
- self.fromLoadMore = true;
- }
- self.getAcls = function () {
- $(".jHueNotify").hide();
- var _isLoading = window.setTimeout(function () {
- self.isLoadingAcls(true);
- }, 1000);
- logGA('get_acls');
- $.getJSON('/security/api/hdfs/get_acls', {
- 'path': self.path()
- }, function (data) {
- window.clearTimeout(_isLoading);
- if (data != null) {
- self.acls.removeAll();
- self.originalAcls.removeAll();
- $.each(data.entries, function (index, item) {
- self.acls.push(parseAcl(item));
- self.originalAcls.push(parseAcl(item));
- });
- self.owner(data.owner);
- self.group(data.group);
- self.isLoadingAcls(false);
- $(document).trigger("loaded.acls");
- }
- }).fail(function (xhr, textStatus, errorThrown) {
- if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
- $(document).trigger("error", xhr.responseText);
- self.isLoadingAcls(false);
- }
- });
- };
- self.updateAcls = function () {
- $(".jHueNotify").hide();
- logGA('updateAcls');
- $.post("/security/api/hdfs/update_acls", {
- 'path': self.path(),
- 'acls': ko.mapping.toJSON(self.acls()),
- 'originalAcls': ko.mapping.toJSON(self.originalAcls())
- }, function (data) {
- var toDelete = []
- $.each(self.acls(), function (index, item) {
- if (item.status() == 'deleted') {
- toDelete.push(item);
- } else {
- item.status('');
- }
- });
- $.each(toDelete, function (index, item) {
- self.acls.remove(item);
- });
- $(document).trigger("info", 'Done!');
- }
- ).fail(function (xhr, textStatus, errorThrown) {
- $(document).trigger("error", JSON.parse(xhr.responseText).message);
- });
- }
- }
- var HdfsViewModel = function (initial) {
- var self = this;
- self.assist = new Assist(self, initial);
- self.doAs = ko.observable(initial.user);
- self.doAs.subscribe(function () {
- self.assist.refreshTree();
- });
- self.availableHadoopUsers = ko.observableArray();
- self.availableHadoopGroups = ko.observableArray();
- self.selectableHadoopUsers = ko.computed(function() {
- var _users = ko.utils.arrayMap(self.availableHadoopUsers(), function(user) {
- return user.username;
- });
- return _users.sort();
- }, self);
- self.selectableHadoopGroups = ko.computed(function() {
- var _users = ko.utils.arrayMap(self.availableHadoopGroups(), function(group) {
- return group.name;
- });
- return _users.sort();
- }, self);
- self.init = function (path) {
- self.fetchUsers();
- self.assist.path(path);
- $(document).one("loaded.parents", function(){
- self.assist.isLoadingTree(true);
- var _paths = self.assist.rebuildTree(self.assist.growingTree().nodes[0], []);
- _paths.forEach(function(path, cnt){
- self.assist.fetchPath(path, function(){
- if (cnt == _paths.length -1){
- self.assist.fromRebuildTree = true;
- self.assist.loadData(self.assist.growingTree());
- self.assist.isLoadingTree(false);
- }
- });
- });
- });
- }
- self.fetchUsers = function () {
- $.getJSON('/desktop/api/users/autocomplete', {
- 'include_myself': true,
- 'extend_user': true
- }, function (data) {
- self.availableHadoopUsers(data.users);
- self.availableHadoopGroups(data.groups);
- $(document).trigger("loaded.users");
- });
- }
- };
- function logGA(page) {
- if (typeof trackOnGA == 'function') {
- trackOnGA('security/hdfs' + page);
- }
- }
|