// // Copyright (C) 2017 FreeIPA Contributors see COPYING for license // define([ 'dojo/on', './ipa', './jquery', './phases', './menu', './rpc', './reg', './text', './widget'], function(on, IPA, $, phases, menu, rpc, reg, text, widget) { /** * Vault module * @class vault * @alternateClassName IPA.vault * @singleton */ var vault = IPA.vault = { search_facet_group: { name: 'vaults', facets: { vault_search: 'vault_search', user_search: 'vault_user_search', service_search: 'vault_service_search', shared_search: 'vault_shared_search', vaultconfig_details: 'vaultconfig_details' } } }; /** * Create general specification of "* Vaults" details facets. */ var make_vaults_details_page_spec = function() { return { $type: 'details', $factory: vault.custom_details_facet, update_command_name: 'mod_internal', disable_facet_tabs: true, sections: [ { name: 'global_info', layout_ccs_class: 'col-sm-12', fields: [ 'cn', 'description', { name: 'ipavaulttype', read_only: true }, { $type: 'text', name: 'ipavaultsalt', visible: false }, { $type: 'pub_key', name: 'ipavaultpublickey', visible: false } ] }, { $factory: IPA.section, name: 'divider', layout_css_class: 'col-sm-12', fields: [] }, { $factory: IPA.section, name: 'members', label: '@i18n:objects.vault.members', fields: [ { $type: 'association_table', id: 'member_user_cn', name: 'member_user', acl_param: 'member', columns: [ { name: 'member_user', label: '@i18n:objects.vault.user' } ] }, { $type: 'association_table', id: 'member_group_cn', name: 'member_group', other_entity: 'group', acl_param: 'member', columns: [ { name: 'member_group', label: '@i18n:objects.vault.group' } ] }, { $type: 'association_table', id: 'member_service_cn', name: 'member_service', other_entity: 'service', other_option_name: 'services', acl_param: 'member', columns: [ { name: 'member_service', label: '@i18n:objects.vault.service' } ] } ] }, { $factory: IPA.section, name: 'owners', label: '@i18n:objects.vault.owners', fields: [ { $type: 'association_table', id: 'owner_user_cn', name: 'owner_user', add_method: 'add_owner', remove_method: 'remove_owner', other_entity: 'user', acl_param: 'owner', columns: [ { name: 'owner_user', label: '@i18n:objects.vault.user' } ] }, { $type: 'association_table', id: 'owner_group_cn', name: 'owner_group', add_method: 'add_owner', remove_method: 'remove_owner', other_entity: 'group', acl_param: 'owner', columns: [ { name: 'owner_group', label: '@i18n:objects.vault.group' } ] }, { $type: 'association_table', id: 'owner_service_cn', name: 'owner_service', add_method: 'add_owner', remove_method: 'remove_owner', other_entity: 'service', other_option_name: 'services', acl_param: 'owner', columns: [ { name: 'owner_service', label: '@i18n:objects.vault.service' } ] } ] } ] }; }; /** * Create entity spec for whole vaults and also spec for search facet, adder * and deleter dialog. */ var make_my_vault_spec = function() { var entity = { name: 'vault', facets: [ { $type: 'search', tab_label: '@i18n:objects.vault.my_vaults_title', facet_groups: [vault.search_facet_group], facet_group: 'vaults', disable_facet_tabs: false, search_all_entries: true, tabs_in_sidebar: true, custom_actions: [ { $type: 'add', hide_cond: [] }, { $type: 'batch_remove', hide_cond: [] } ], columns: [ 'cn', 'ipavaulttype' ], policies: [ vault.config_sidebar_policy ] } ], adder_dialog: { $factory: vault.custom_adder_dialog, name: 'add', method: 'add_internal', policies: [ { $factory: vault.adder_policy } ] }, deleter_dialog: { // Each parametr is present only in one facet. It could cause errors // in case that table on each facet gather more columns with these names. // I.e. facet with user vaults get column with name 'service', then // the value of 'service' column will be also added to command options. additional_table_attrs: ['username', 'service', 'shared'] } }; /** * This function extends general details facet - so the same declaration * of facet (which would differ only in several lines) * should not be present six times. */ var update_facet_spec = function(facet, facet_type) { facet.sections[0].fields.push(facet_type); facet.refresh_attribute = facet_type; facet.update_attribute = facet_type; var user_members = facet.sections[2].fields[0]; var group_members = facet.sections[2].fields[1]; var service_members = facet.sections[2].fields[2]; var user_owners = facet.sections[3].fields[0]; var group_owners = facet.sections[3].fields[1]; var service_owners = facet.sections[3].fields[2]; var attributes = { refresh_attribute: facet_type, additional_add_del_field: facet_type }; $.extend(user_members, attributes); $.extend(user_owners, attributes); $.extend(group_members, attributes); $.extend(group_owners, attributes); $.extend(service_members, attributes); $.extend(service_owners, attributes); }; // Create details page for my vauls: var details_spec = make_vaults_details_page_spec(); entity.facets.push(details_spec); // Create details page for user vaults and modify it details_spec = make_vaults_details_page_spec(); details_spec.name = 'vault_user'; update_facet_spec(details_spec, 'username'); details_spec.redirect_info = { facet: 'user_search' }; entity.facets.push(details_spec); // Create details page for service vaults and modify it details_spec = make_vaults_details_page_spec(); details_spec.name = 'vault_service'; update_facet_spec(details_spec, 'service'); details_spec.redirect_info = { facet: 'service_search' }; entity.facets.push(details_spec); // Create details page for shared vaults and modify it details_spec = make_vaults_details_page_spec(); details_spec.name = 'vault_shared'; update_facet_spec(details_spec, 'shared'); details_spec.redirect_info = { facet: 'shared_search' }; entity.facets.push(details_spec); return entity; }; vault.custom_details_facet = function(spec) { spec = spec || {}; var that = IPA.details_facet(spec); that.load = function(data) { that.details_facet_load(data); // show fields according to the type of vault var type_f = that.fields.get_field('ipavaulttype'); var type = type_f.value[0]; var salt_w = that.fields.get_field('ipavaultsalt').widget; var pub_key_w = that.fields.get_field('ipavaultpublickey').widget; if (type === 'symmetric') { pub_key_w.set_visible(false); salt_w.set_visible(true); } else if (type === 'asymmetric') { pub_key_w.set_visible(true); salt_w.set_visible(false); } else { pub_key_w.set_visible(false); salt_w.set_visible(false); } }; return that; }; vault.public_key_widget = function(spec) { spec = spec || {}; var that = IPA.sshkey_widget(spec); that.set_user_value = function(value) { var previous = that.key; that.key = value; that.update_link(); if (value !== previous) { that.value_changed.notify([], that); that.emit('value-change', { source: that }); } }; that.update = function(value) { var key = value[0]; if (key) that.key = key; if (that.key && that.key !== '') { that.originally_set = true; that.original_key = that.key; } that.update_link(); that.on_value_changed(value); }; that.get_status = function() { var status = ''; var value = that.key; if (that.original_key) { if (value !== that.original_key) { if (value === '') { status = text.get('@i18n:objects.publickey.status_mod_ns'); } else { status = text.get('@i18n:objects.publickey.status_mod_s'); } } else { // f00c is code of check icon var decimal_check_i = parseInt('f00c', 16); status = String.fromCharCode(decimal_check_i); } } else { if (!value || value === '') { status = text.get('@i18n:objects.publickey.status_new_ns'); } else { status = text.get('@i18n:objects.publickey.status_new_ns'); } } return status; }; that.create_edit_dialog = function() { var writable = that.is_writable(); var dialog = IPA.dialog({ name: 'pubkey-edit-dialog', title: '@i18n:objects.publickey.set_dialog_title', width: 500, height: 380 }); dialog.message = text.get('@i18n:objects.publickey.set_dialog_help'); dialog.create_button({ name: 'update', label: '@i18n:buttons.set', click: function() { var value = dialog.textarea.val(); that.set_user_value(value); dialog.close(); } }); dialog.create_button({ name: 'cancel', label: '@i18n:buttons.cancel', click: function() { dialog.close(); } }); dialog.create_content = function() { dialog.container.append(dialog.message); dialog.textarea = $('