forked from acquia/reservoir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreservoir.install
138 lines (125 loc) · 4.21 KB
/
reservoir.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the Reservoir install profile.
*/
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\simple_oauth\Entity\Oauth2Client;
use Drupal\taxonomy\Entity\Term;
use Drupal\user\Entity\User;
/**
* Implements hook_install().
*
* Perform actions to set up the site for this profile.
*
* @see system_install()
*/
function reservoir_install() {
// Set front page to "/home".
// @see \Drupal\reservoir_ui\Controller\Home::home()
\Drupal::configFactory()->getEditable('system.site')->set('page.front', '/home')->save(TRUE);
// Allow visitor account creation with administrative approval.
$user_settings = \Drupal::configFactory()->getEditable('user.settings');
$user_settings->set('register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)->save(TRUE);
// Generate initial, temporary keys, for a better onboarding experience.
if (function_exists('openssl_pkey_new')) {
list($private_key, $public_key) = _reservoir_generate_keypair();
}
if (!empty($private_key) && !empty($public_key)) {
$oauth_settings = \Drupal::configFactory()->getEditable('simple_oauth.settings');
foreach (['private' => $private_key, 'public' => $public_key] as $kind => $key) {
$path = sys_get_temp_dir() . '/' . sha1($key) . '.key';
if (!file_exists($path) && !touch($path)) {
throw new \RuntimeException('"%s" key file could not be created', $path);
}
file_put_contents($path, $key);
$oauth_settings->set("{$kind}_key", $path);
}
$oauth_settings->save(TRUE);
drupal_set_message('Generated an initial public/private key pair to use for OAuth2 authentication. Change it before going into production!', 'warning');
}
else {
drupal_set_message('Could not automatically generate an initial public/private key pair to use for OAuth2 authentication. Until configuring this, OAuth2 authentication will not work.', 'warning');
}
// Create demo users & demo client.
User::create([])
->setUsername('demo-user')
->setPassword('demo-user')
->activate()
->save();
User::create([])
->setUsername('demo-writer')
->setPassword('demo-writer')
->set('roles', ['content_administrator'])
->activate()
->save();
User::create([])
->setUsername('demo-developer')
->setPassword('demo-developer')
->set('roles', ['client_developer'])
->activate()
->save();
Oauth2Client::create([
'owner_id' => 1,
'label' => 'Demo app',
'secret' => 'foobar',
'confidential' => TRUE,
'roles' => [
'client__own_articles',
],
])->save();
// Create default content.
file_unmanaged_copy(\Drupal::root() . '/' . drupal_get_path('profile', 'reservoir') . '/reservoir-logo.png', 'public://reservoir-logo.png');
$file = File::create(['uri' => 'public://reservoir-logo.png']);
$file->save();
$term = Term::create([
'name' => 'Camelids',
'vid' => 'tags',
]);
$term->save();
Node::create([
'type' => 'article',
'body' => [
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'basic_html',
],
'field_image' => [
[
'alt' => 'Reservoir logo: a "pancake stack" with layers of water instead of pancakes, to reference Drupal!',
'title' => 'Reservoir logo',
'target_id' => $file->id(),
],
],
'field_tags' => [
[
'target_id' => $term->id(),
],
],
])
->setTitle('Hello world!')
->setOwnerId(1)
->save();
}
// Source: https://stackoverflow.com/a/34962604.
function _reservoir_generate_keypair() {
// generate 2048-bit RSA key
$pk_Generate = openssl_pkey_new(array(
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA
));
if (empty($pk_Generate)) {
return [NULL, NULL];
}
// getting private-key
openssl_pkey_export($pk_Generate, $pk_Generate_Private); // we pass 2nd argument as reference
// getting public-key
$pk_Generate_Details = openssl_pkey_get_details($pk_Generate);
$pk_Generate_Public = isset($pk_Generate_Details['key']) ? $pk_Generate_Details['key'] : NULL;
// free resources
openssl_pkey_free($pk_Generate);
return [
$pk_Generate_Private,
$pk_Generate_Public,
];
}