forked from contentacms/contenta_jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contenta_jsonapi.profile
155 lines (141 loc) · 5.61 KB
/
contenta_jsonapi.profile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @file
* Enables modules and site configuration for a api first site installation.
*/
use Drupal\contenta_jsonapi\Plugin\Contenta\OptionalModule\AbstractOptionalModule;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Serialization\Yaml;
/**
* Implements hook_form_FORM_ID_alter() for install_configure_form().
*
* Allows the profile to alter the site configuration form.
*/
function contenta_jsonapi_form_install_configure_form_alter(&$form, FormStateInterface $form_state) {
// Add a value as example that one can choose an arbitrary site name.
$form['site_information']['site_name']['#placeholder'] = t('Contenta JSON API');
}
/**
* Implements hook_install_tasks().
*/
function contenta_jsonapi_install_tasks(&$install_state) {
$tasks = [
'_contenta_jsonapi_generate_keys' => [
'display_name' => t('Generate OAuth 2 keys'),
],
'_contenta_jsonapi_enable_cors' => [
'display_name' => t('Enable CORS by default'),
],
'contenta_jsonapi_module_configure_form' => [
'display_name' => t('Configure additional modules'),
'type' => 'form',
'function' => 'Drupal\contenta_jsonapi\Installer\Form\ModuleConfigureForm',
],
'contenta_jsonapi_module_install' => [
'display_name' => t('Install additional modules'),
],
];
return $tasks;
}
/**
* Generates the OAuth Keys.
*/
function _contenta_jsonapi_generate_keys() {
// Build all the dependencies manually to avoid having to rely on the
// container to be ready.
$dir_name = 'keys';
/** @var \Drupal\simple_oauth\Service\KeyGeneratorService $key_gen */
$key_gen = \Drupal::service('simple_oauth.key.generator');
/** @var \Drupal\simple_oauth\Service\Filesystem\FileSystemChecker $file_system_checker */
$file_system_checker = \Drupal::service('simple_oauth.filesystem_checker');
/** @var \Drupal\Core\File\FileSystem $file_system */
$file_system = \Drupal::service('file_system');
/** @var \Drupal\Core\Logger\LoggerChannelInterface $logger */
$logger = \Drupal::service('logger.channel.contentacms');
$relative_path = DRUPAL_ROOT . '/../' . $dir_name;
if (!$file_system_checker->isDirectory($relative_path)) {
$file_system->mkdir($relative_path);
}
$keys_path = $file_system->realpath($relative_path);
$pub_filename = sprintf('%s/public.key', $keys_path);
$pri_filename = sprintf('%s/private.key', $keys_path);
if ($file_system_checker->fileExist($pub_filename) && $file_system_checker->fileExist($pri_filename)) {
// 1. If the file already exists, then just set the correct permissions.
$file_system->chmod($pub_filename, 0600);
$file_system->chmod($pri_filename, 0600);
$logger->info('Key pair for OAuth 2 token signing already exists.');
}
else {
// 2. Generate the pair in the selected directory.
try {
$key_gen->generateKeys($keys_path);
} catch (\Exception $e) {
// Unable to generate files after all.
$logger->error($e->getMessage());
return;
}
}
}
/**
* Installs the contenta_jsonapi modules.
*
* @param array $install_state
* The install state.
*/
function contenta_jsonapi_module_install(array &$install_state) {
set_time_limit(0);
$extensions = $install_state['forms']['contenta_jsonapi_additional_modules'];
$form_values = $install_state['forms']['form_state_values'];
$optional_modules_manager = \Drupal::service('plugin.manager.contenta_jsonapi.optional_modules');
$definitions = array_map(function ($extension_name) use ($optional_modules_manager) {
return $optional_modules_manager->getDefinition($extension_name);
}, $extensions);
$modules = array_filter($definitions, function (array $definition) {
return $definition['type'] == 'module';
});
$themes = array_filter($definitions, function (array $definition) {
return $definition['type'] == 'theme';
});
if (!empty($modules)) {
/** @var \Drupal\Core\Extension\ModuleInstallerInterface $installer */
$installer = \Drupal::service('module_installer');
$installer->install(array_map(function (array $module) {
return $module['id'];
}, $modules));
}
if (!empty($themes)) {
/** @var \Drupal\Core\Extension\ModuleInstallerInterface $installer */
$installer = \Drupal::service('theme_installer');
$installer->install(array_map(function (array $theme) {
return $theme['id'];
}, $themes));
}
$instances = array_map(function ($extension_name) use ($optional_modules_manager) {
return $optional_modules_manager->createInstance($extension_name);
}, $extensions);
array_walk($instances, function (AbstractOptionalModule $instance) use ($form_values) {
$instance->submitForm($form_values);
});
}
/**
* Alters the services.yml to enable CORS by default.
*/
function _contenta_jsonapi_enable_cors() {
// Enable CORS for localhost.
/** @var \Drupal\Core\DrupalKernelInterface $drupal_kernel */
$drupal_kernel = \Drupal::service('kernel');
$file_path = $drupal_kernel->getAppRoot() . '/' . $drupal_kernel->getSitePath();
$filename = $file_path . '/services.yml';
if (file_exists($filename)) {
$services_yml = file_get_contents($filename);
$yml_data = Yaml::decode($services_yml);
if (empty($yml_data['parameters']['cors.config']['enabled'])) {
$yml_data['parameters']['cors.config']['enabled'] = TRUE;
$yml_data['parameters']['cors.config']['allowedHeaders'] = ['*'];
$yml_data['parameters']['cors.config']['allowedMethods'] = ['*'];
$yml_data['parameters']['cors.config']['allowedOrigins'] = ['localhost'];
$yml_data['parameters']['cors.config']['allowedOriginsPatterns'] = ['/localhost:\d+/'];
file_put_contents($filename, Yaml::encode($yml_data));
}
}
}