tapis_job-1.4.1-alpha1/tapis_job.install
tapis_job.install
<?php
/**
* @file
* Handles module installation, uninstallation, and version updates.
*/
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Update the permissions.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function tapis_job_update_10101() {
$updatedPermissions = [
// Update permissions for tapis_app_wevform module
"edit app webform" => "edit own app webform",
"restrict webform elements" => "restrict webform elements",
// Update permissions for tapis_system module
"view system credential" => "view own system credential",
"edit system credential" => "edit own system credential",
"delete system credential" => "delete own system credential",
"use auto generated keypair" => "use auto generated ssh key pair",
"use custom keypair" => "allow uploading own ssh key pair",
// Update permissions for tapis_job module
"cancel job" => "cancel own job",
"view job" => "view own job",
"edit job" => "edit own job",
"delete job" => "delete own job",
];
// Load the role storage.
$role_storage = \Drupal::entityTypeManager()->getStorage('user_role');
// Load all roles.
$roles = $role_storage->loadMultiple();
// Iterate over each role.
foreach ($roles as $roleId => $role) {
// Check if the role has the old permission.
/** @var \Drupal\user\Entity\Role $role */
\Drupal::logger("tapis_job")->notice("role: $roleId");
// First, revoke all old permissions.
$removedPermissions = [];
foreach ($updatedPermissions as $oldPerm => $newPerm) {
if ($role->hasPermission($oldPerm)) {
$role->revokePermission($oldPerm);
$removedPermissions[] = $newPerm;
\Drupal::logger("tapis_job")->notice("revoked old perm: $oldPerm");
}
}
// Then, grant all new permissions.
if (!empty($removedPermissions)) {
// Save the role after revoking all old permissions.
$role->save();
foreach ($removedPermissions as $newPerm) {
$role->grantPermission($newPerm);
\Drupal::logger("tapis_job")->notice("granted new perm: $newPerm");
}
// Save the role after granting all new permissions.
$role->save();
}
}
// Flush all caches.
drupal_flush_all_caches();
return 'Update complete.';
}
/**
* This hook is used to update the tapis_job entity type (add tapisCondition field).
*
*/
function tapis_job_update_10102(): void {
// Add the "tapisCondition" fields to the tapis_job entity type.
$tapisCondition = BaseFieldDefinition::create('string')
->setLabel(t('Condition'))
->setRequired(FALSE)
->setSetting('max_length', 255)
->setDisplayConfigurable('form', FALSE)
->setDisplayOptions('view',
[
'label' => 'inline',
'type' => 'string',
]
)
->setDisplayConfigurable('view', TRUE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition(
'tapisCondition',
'tapis_job',
'tapis_job',
$tapisCondition
);
}
/**
* This hook is used to update the tapis_job entity type (add jobUsage field).
*
*/
function tapis_job_update_10103(): void {
// Add the "jobUsage" field to the tapis_job entity type.
$jobUsage = BaseFieldDefinition::create('string_long')
->setLabel(t('Job Usage'))
->setDescription(t('Provide a summary of the job usage.'))
->setDefaultValue('')
->setRequired(FALSE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'basic_string',
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', FALSE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition(
'jobUsage',
'tapis_job',
'tapis_job',
$jobUsage
);
// Add the "remoteSubmitted" field to the tapis_job entity type.
$remoteSubmitted = BaseFieldDefinition::create('datetime')
->setLabel(t('Job submit timestamp'))
->setRequired(FALSE)
->setDisplayConfigurable('form', FALSE)
->setDisplayOptions('view',
[
'label' => 'inline',
'type' => 'string',
]
)
->setDisplayConfigurable('view', TRUE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition(
'remoteSubmitted',
'tapis_job',
'tapis_job',
$remoteSubmitted
);
}
/**
* Update configuration to add new configuration for displaying files.
*/
function tapis_job_update_10104() {
// Load the existing configuration.
$config = \Drupal::configFactory()->getEditable('tapis_job.config');
// Add new configuration items if they don't already exist.
if (empty($config->get('supported_extensions'))) {
$config->set('supported_extensions', 'htm html xml svg mathml css clike js abap abnf as ada conf htaccess apl applescript arduino arff asciidoc adoc asm6502 aspnet autohotkey autoit sh csh basic batch bison bnf rbnf brainfuck bro c cs csv hpp cpp cil coffeescript cmake clojure crystal csp d dart diff django jinja2 docker dockerfile ebnf eiffel ejs elixir elm erb erlang fsharp flow fortran gcode gedcom gherkin git glsl gml gamemakerlanguage go graphql groovy haml handlebars haskell hs haxe hcl http hpkp hsts ichigojam icon inform7 ini io j java javadoc javadoclike javastacktrace jolie jsdoc json jsonp json5 julia keyman kotlin tex less liquid lisp emacs elisp emacs-lisp livescript lolcode lua makefile markdown md markup-templating mat m mel mizar monkey n1ql n4js n4jsd nand2tetris-hdl nasm nim nix nsis objectivec ocaml opencl oz parigp parser pascal objectpascal perl php pl plsql ps1 bat processing prolog properties protobuf pug puppet pure py q qore r jsx tsx renpy reason regex rest rip roboconf rb rust sas sass scss scala scheme smalltalk smarty sql soy stylus swift tap tcl txt textile toml tsv tt2 twig typescript ts vala vbnet velocity verilog vhdl vim vb wasm wiki xeora xeoracube xojo xquery yaml yml log env out par');
}
if (empty($config->get('supported_mimes'))) {
$config->set('supported_mimes', 'text/plain text/css text/csv text/javascript text/html');
}
if (empty($config->get('supported_image_extensions'))) {
$config->set('supported_image_extensions', 'png jpeg jpg svg pdf');
}
if (empty($config->get('supported_image_mimes'))) {
$config->set('supported_image_mimes', 'image/png image/jpeg image/svg+xml application/pdf');
}
// Ensure prism_css is an array, defaulting to an empty array if not.
$prism_css = $config->get('prism_css') ?? [];
if (empty(array_filter($prism_css, function($value) {
return trim($value) !== '';
}))) {
$prism_css = "https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/prism.min.css\n" .
"https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.css";
$config->set('prism_css', explode("\n", $prism_css));
}
// Ensure prism_js is an array, defaulting to an empty array if not.
$prism_js = $config->get('prism_js') ?? [];
if (empty(array_filter($prism_js, function($value) {
return trim($value) !== '';
}))) {
$prism_js = "https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js\n" .
"https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js\n" .
"https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.js";
$config->set('prism_js', explode("\n", $prism_js));
}
// Save the updated configuration.
$config->save();
}
