Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
88 / 88 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
AuthorizationProfileForm | |
100.00% |
88 / 88 |
|
100.00% |
6 / 6 |
16 | |
100.00% |
1 / 1 |
getProviderPluginManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getConsumerPluginManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
buildEntityForm | |
100.00% |
66 / 66 |
|
100.00% |
1 / 1 |
7 | |||
getProviderOptions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getConsumerOptions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
save | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\authorization\Form; |
6 | |
7 | use Drupal\Component\Utility\Html; |
8 | use Drupal\Core\Entity\EntityForm; |
9 | use Drupal\Core\Form\FormStateInterface; |
10 | use Drupal\authorization\Consumer\ConsumerPluginManager; |
11 | use Drupal\authorization\Provider\ProviderPluginManager; |
12 | |
13 | /** |
14 | * Authorization profile form. |
15 | * |
16 | * @package Drupal\authorization\Form |
17 | */ |
18 | abstract class AuthorizationProfileForm extends EntityForm { |
19 | |
20 | const SAVED_NEW = 1; |
21 | |
22 | /** |
23 | * The provider plugin manager. |
24 | * |
25 | * @var \Drupal\authorization\Provider\ProviderPluginManager |
26 | */ |
27 | protected $providerPluginManager; |
28 | |
29 | /** |
30 | * The consumer plugin manager. |
31 | * |
32 | * @var \Drupal\authorization\Consumer\ConsumerPluginManager |
33 | */ |
34 | protected $consumerPluginManager; |
35 | |
36 | /** |
37 | * The provider in use. |
38 | * |
39 | * @var string |
40 | */ |
41 | protected $provider; |
42 | |
43 | /** |
44 | * The consumer in use. |
45 | * |
46 | * @var string |
47 | */ |
48 | protected $consumer; |
49 | |
50 | /** |
51 | * Storage. |
52 | * |
53 | * @var \Drupal\Core\Entity\EntityStorageInterface |
54 | */ |
55 | protected $storage; |
56 | |
57 | /** |
58 | * Retrieves the Provider plugin manager. |
59 | * |
60 | * @return \Drupal\authorization\Provider\ProviderPluginManager |
61 | * The Provider plugin manager. |
62 | */ |
63 | protected function getProviderPluginManager(): ProviderPluginManager { |
64 | return $this->providerPluginManager; |
65 | } |
66 | |
67 | /** |
68 | * Retrieves the Consumer plugin manager. |
69 | * |
70 | * @return \Drupal\authorization\Consumer\ConsumerPluginManager |
71 | * The Consumer plugin manager. |
72 | */ |
73 | protected function getConsumerPluginManager(): ConsumerPluginManager { |
74 | return $this->consumerPluginManager; |
75 | } |
76 | |
77 | /** |
78 | * Builds the form for the basic server properties. |
79 | * |
80 | * @param array $form |
81 | * An associative array containing the structure of the form. |
82 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
83 | * Form state. |
84 | * @param bool $is_new |
85 | * Whether the form is for a new entity. |
86 | */ |
87 | protected function buildEntityForm(array &$form, FormStateInterface $form_state, $is_new): void { |
88 | /** @var \Drupal\authorization\AuthorizationProfileInterface $authorization_profile */ |
89 | $authorization_profile = $this->getEntity(); |
90 | $create = $this->t('Authorization profile cannot be created.'); |
91 | $edit = $this->t('Authorization profile cannot be edited.'); |
92 | $form['label'] = [ |
93 | '#type' => 'textfield', |
94 | '#title' => $this->t('Profile name'), |
95 | '#maxlength' => 255, |
96 | '#default_value' => $authorization_profile->label(), |
97 | '#required' => TRUE, |
98 | '#weight' => -50, |
99 | ]; |
100 | |
101 | $form['id'] = [ |
102 | '#type' => 'machine_name', |
103 | '#default_value' => $authorization_profile->id(), |
104 | '#machine_name' => [ |
105 | 'exists' => '\Drupal\authorization\Entity\AuthorizationProfile::load', |
106 | ], |
107 | '#disabled' => !$authorization_profile->isNew(), |
108 | '#weight' => -40, |
109 | ]; |
110 | |
111 | /* You will need additional form elements for your custom properties. */ |
112 | $form['status'] = [ |
113 | '#type' => 'checkbox', |
114 | '#title' => $this->t('Enabled'), |
115 | '#default_value' => $authorization_profile->get('status'), |
116 | '#weight' => -30, |
117 | ]; |
118 | |
119 | $provider_options = $this->getProviderOptions(); |
120 | if ($provider_options) { |
121 | if (count($provider_options) === 1) { |
122 | $authorization_profile->set('provider', key($provider_options)); |
123 | } |
124 | |
125 | $form['provider'] = [ |
126 | '#type' => 'radios', |
127 | '#title' => $this->t('Provider'), |
128 | '#options' => $provider_options, |
129 | '#default_value' => $authorization_profile->getProviderId(), |
130 | '#required' => TRUE, |
131 | '#weight' => -20, |
132 | ]; |
133 | } |
134 | else { |
135 | $this->messenger()->addError($this->t('There are no provider plugins available. You will need to install and enable something like the LDAP Authorization Provider module that ships with LDAP.')); |
136 | $form['#access'] = FALSE; |
137 | $form['#markup'] = $is_new ? $create : $edit; |
138 | $form['#cache'] = [ |
139 | 'tags' => [], |
140 | 'contexts' => [], |
141 | 'max-age' => 0, |
142 | ]; |
143 | } |
144 | |
145 | $consumer_options = $this->getConsumerOptions(); |
146 | if ($consumer_options) { |
147 | if (count($consumer_options) == 1) { |
148 | $authorization_profile->set('consumer', key($consumer_options)); |
149 | } |
150 | |
151 | $form['consumer'] = [ |
152 | '#type' => 'radios', |
153 | '#title' => $this->t('Consumer'), |
154 | '#options' => $consumer_options, |
155 | '#default_value' => $authorization_profile->getConsumerId(), |
156 | '#required' => TRUE, |
157 | '#weight' => -10, |
158 | ]; |
159 | } |
160 | else { |
161 | $this->messenger()->addError($this->t('There are no consumer plugins available. You can enable the Authorization Drupal Roles submodule to provide integration with core user roles or write your own using that as a template.')); |
162 | $form['#access'] = FALSE; |
163 | $form['#markup'] = $is_new ? $create : $edit; |
164 | $form['#cache'] = [ |
165 | 'tags' => [], |
166 | 'contexts' => [], |
167 | 'max-age' => 0, |
168 | ]; |
169 | } |
170 | } |
171 | |
172 | /** |
173 | * Returns all available Provider plugins, as an options list. |
174 | * |
175 | * @return string[] |
176 | * An associative array mapping Provider plugin IDs to their (HTML-escaped) |
177 | * labels. |
178 | */ |
179 | protected function getProviderOptions(): array { |
180 | $options = []; |
181 | foreach ($this->getProviderPluginManager()->getDefinitions() as $plugin_id => $plugin_definition) { |
182 | $label = $plugin_definition['label']->render(); |
183 | $options[$plugin_id] = Html::escape($label); |
184 | } |
185 | return $options; |
186 | } |
187 | |
188 | /** |
189 | * Returns all available Consumer plugins, as an options list. |
190 | * |
191 | * @return string[] |
192 | * An associative array mapping Consumer plugin IDs to their (HTML-escaped) |
193 | * labels. |
194 | */ |
195 | protected function getConsumerOptions(): array { |
196 | $options = []; |
197 | foreach ($this->getConsumerPluginManager()->getDefinitions() as $plugin_id => $plugin_definition) { |
198 | $label = $plugin_definition['label']->render(); |
199 | $options[$plugin_id] = Html::escape($label); |
200 | } |
201 | return $options; |
202 | } |
203 | |
204 | /** |
205 | * {@inheritdoc} |
206 | */ |
207 | public function save(array $form, FormStateInterface $form_state): void { |
208 | $authorization_profile = $this->entity; |
209 | $status = $authorization_profile->save(); |
210 | |
211 | switch ($status) { |
212 | case self::SAVED_NEW: |
213 | $this->messenger()->addStatus($this->t('Created the %label Authorization profile.', [ |
214 | '%label' => $authorization_profile->label(), |
215 | ])); |
216 | $form_state->setRedirectUrl($authorization_profile->toUrl('edit-form')); |
217 | break; |
218 | |
219 | default: |
220 | $this->messenger()->addStatus($this->t('Saved the %label Authorization profile.', [ |
221 | '%label' => $authorization_profile->label(), |
222 | ])); |
223 | $form_state->setRedirectUrl($authorization_profile->toUrl('collection')); |
224 | break; |
225 | } |
226 | |
227 | } |
228 | |
229 | } |