|
Line 0
Link Here
|
|
|
1 |
<template> |
| 2 |
<div v-if="!initialized">{{ $__("Loading") }}</div> |
| 3 |
<BaseResource v-else :routeAction="routeAction" :instancedResource="this" /> |
| 4 |
</template> |
| 5 |
|
| 6 |
<script> |
| 7 |
import { ref, onMounted, reactive, watch } from "vue"; |
| 8 |
import BaseResource from "./../BaseResource.vue"; |
| 9 |
import { useBaseResource } from "../../composables/base-resource.js"; |
| 10 |
import { APIClient } from "../../fetch/api-client.js"; |
| 11 |
import { $__ } from "@koha-vue/i18n"; |
| 12 |
|
| 13 |
const PROTOCOL_CONFIG_FIELDS = { |
| 14 |
OAuth: [ |
| 15 |
{ |
| 16 |
name: "key", |
| 17 |
label: __("Client ID"), |
| 18 |
required: true, |
| 19 |
type: "text", |
| 20 |
group: "OAuth settings", |
| 21 |
}, |
| 22 |
{ |
| 23 |
name: "secret", |
| 24 |
label: __("Client secret"), |
| 25 |
required: true, |
| 26 |
type: "text", |
| 27 |
group: "OAuth settings", |
| 28 |
}, |
| 29 |
{ |
| 30 |
name: "authorize_url", |
| 31 |
label: __("Authorization URL"), |
| 32 |
required: true, |
| 33 |
type: "text", |
| 34 |
group: "OAuth settings", |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "token_url", |
| 38 |
label: __("Token URL"), |
| 39 |
required: true, |
| 40 |
type: "text", |
| 41 |
group: "OAuth settings", |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "userinfo_url", |
| 45 |
label: __("User info URL"), |
| 46 |
required: false, |
| 47 |
type: "text", |
| 48 |
group: "OAuth settings", |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "scope", |
| 52 |
label: __("Scope"), |
| 53 |
required: false, |
| 54 |
type: "text", |
| 55 |
group: "OAuth settings", |
| 56 |
toolTip: __("Space-separated list of scopes, e.g. 'email profile'"), |
| 57 |
}, |
| 58 |
], |
| 59 |
OIDC: [ |
| 60 |
{ |
| 61 |
name: "key", |
| 62 |
label: __("Client ID"), |
| 63 |
required: true, |
| 64 |
type: "text", |
| 65 |
group: "OIDC settings", |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "secret", |
| 69 |
label: __("Client secret"), |
| 70 |
required: true, |
| 71 |
type: "text", |
| 72 |
group: "OIDC settings", |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "well_known_url", |
| 76 |
label: __("Well-known URL"), |
| 77 |
required: true, |
| 78 |
type: "text", |
| 79 |
group: "OIDC settings", |
| 80 |
toolTip: __( |
| 81 |
"OpenID Connect discovery endpoint, e.g. https://login.example.com/.well-known/openid-configuration" |
| 82 |
), |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "scope", |
| 86 |
label: __("Scope"), |
| 87 |
required: false, |
| 88 |
type: "text", |
| 89 |
group: "OIDC settings", |
| 90 |
toolTip: __( |
| 91 |
"Space-separated list of scopes, e.g. 'openid email profile'" |
| 92 |
), |
| 93 |
}, |
| 94 |
], |
| 95 |
SAML2: [ |
| 96 |
{ |
| 97 |
name: "saml2_sp_note", |
| 98 |
type: "group_placeholder", |
| 99 |
group: "SAML2 settings", |
| 100 |
description: __( |
| 101 |
"SAML2/Shibboleth connection is handled by the native service provider software (e.g. mod_shib) configured on this server. No Koha-side connection settings are required." |
| 102 |
), |
| 103 |
}, |
| 104 |
], |
| 105 |
}; |
| 106 |
|
| 107 |
export default { |
| 108 |
name: "ProviderResource", |
| 109 |
components: { |
| 110 |
BaseResource, |
| 111 |
}, |
| 112 |
props: { |
| 113 |
routeAction: String, |
| 114 |
}, |
| 115 |
emits: ["select-resource"], |
| 116 |
setup(props) { |
| 117 |
const initialized = ref(false); |
| 118 |
|
| 119 |
// Tracks the currently selected/loaded protocol so config field groups |
| 120 |
// can be shown or hidden reactively via hideIn closures. |
| 121 |
const selectedProtocol = ref(null); |
| 122 |
|
| 123 |
// Tracks the IDs of sub-resources that existed when the provider was |
| 124 |
// loaded, so we can delete any that the user removed during editing. |
| 125 |
const originalMappingIds = ref([]); |
| 126 |
const originalHostnameIds = ref([]); |
| 127 |
const originalDomainIds = ref([]); |
| 128 |
|
| 129 |
const staticResourceAttrs = [ |
| 130 |
{ |
| 131 |
name: "code", |
| 132 |
required: true, |
| 133 |
type: "text", |
| 134 |
label: __("Code"), |
| 135 |
group: "Basic configuration", |
| 136 |
toolTip: __( |
| 137 |
"Unique identifier for this provider. Alphanumeric and underscore only." |
| 138 |
), |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "description", |
| 142 |
required: true, |
| 143 |
type: "text", |
| 144 |
label: __("Description"), |
| 145 |
group: "Basic configuration", |
| 146 |
toolTip: __("User-friendly name displayed on login pages"), |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "protocol", |
| 150 |
required: true, |
| 151 |
type: "select", |
| 152 |
label: __("Protocol"), |
| 153 |
group: "Basic configuration", |
| 154 |
options: [ |
| 155 |
{ value: "OAuth", label: "OAuth" }, |
| 156 |
{ value: "OIDC", label: "OIDC" }, |
| 157 |
{ value: "SAML2", label: "SAML2 / Shibboleth" }, |
| 158 |
], |
| 159 |
requiredKey: "value", |
| 160 |
selectLabel: "label", |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: "icon_url", |
| 164 |
type: "text", |
| 165 |
label: __("Icon URL"), |
| 166 |
group: "Basic configuration", |
| 167 |
toolTip: __( |
| 168 |
"URL to an icon image shown on the OPAC login button" |
| 169 |
), |
| 170 |
}, |
| 171 |
{ |
| 172 |
name: "enabled", |
| 173 |
type: "boolean", |
| 174 |
label: __("Enabled"), |
| 175 |
group: "Basic configuration", |
| 176 |
toolTip: __( |
| 177 |
"When disabled, this provider will not be shown on login pages" |
| 178 |
), |
| 179 |
}, |
| 180 |
]; |
| 181 |
|
| 182 |
const protocolPlaceholderAttr = { |
| 183 |
name: "_protocol_placeholder", |
| 184 |
type: "group_placeholder", |
| 185 |
group: "Protocol settings", |
| 186 |
description: __( |
| 187 |
"Select a protocol above to expose protocol-specific settings" |
| 188 |
), |
| 189 |
hideIn: () => |
| 190 |
selectedProtocol.value |
| 191 |
? ["Form", "Show", "List"] |
| 192 |
: ["Show", "List"], |
| 193 |
}; |
| 194 |
|
| 195 |
// Build config attrs for ALL protocols. Each gets a hideIn closure that |
| 196 |
// hides it when a different protocol is selected, or when no protocol |
| 197 |
// has been selected yet (add mode). |
| 198 |
const configResourceAttrs = Object.entries( |
| 199 |
PROTOCOL_CONFIG_FIELDS |
| 200 |
).flatMap(([protocol, fields]) => |
| 201 |
fields.map(f => ({ |
| 202 |
...f, |
| 203 |
name: `_config_${f.name}`, |
| 204 |
hideIn: () => |
| 205 |
!selectedProtocol.value || |
| 206 |
selectedProtocol.value !== protocol |
| 207 |
? ["Form", "Show", "List"] |
| 208 |
: ["List"], |
| 209 |
})) |
| 210 |
); |
| 211 |
|
| 212 |
const borrowerColumnsArray = (window.borrower_columns || []).map( |
| 213 |
col => ({ value: col.value, label: col.label }) |
| 214 |
); |
| 215 |
|
| 216 |
const matchpointOptions = [ |
| 217 |
{ value: "cardnumber", label: __("Card number") }, |
| 218 |
{ value: "userid", label: __("Username") }, |
| 219 |
{ value: "email", label: __("Email address") }, |
| 220 |
...(window.unique_patron_attributes || []), |
| 221 |
]; |
| 222 |
const librariesArray = (window.libraries_map || []).map(lib => ({ |
| 223 |
value: lib.value, |
| 224 |
label: lib.label, |
| 225 |
})); |
| 226 |
const categoriesArray = (window.categories_map || []).map(cat => ({ |
| 227 |
value: cat.value, |
| 228 |
label: cat.label, |
| 229 |
})); |
| 230 |
|
| 231 |
const hostnameAttr = { |
| 232 |
name: "hostnames", |
| 233 |
type: "relationshipWidget", |
| 234 |
group: "Network & Entry Settings", |
| 235 |
hideIn: ["List"], |
| 236 |
showElement: { |
| 237 |
type: "table", |
| 238 |
columnData: "hostnames", |
| 239 |
hidden: provider => !!provider.hostnames?.length, |
| 240 |
columns: [ |
| 241 |
{ name: __("Hostname"), value: "hostname" }, |
| 242 |
{ name: __("Force SSO"), value: "force_sso" }, |
| 243 |
{ name: __("Matchpoint"), value: "matchpoint" }, |
| 244 |
], |
| 245 |
}, |
| 246 |
componentProps: { |
| 247 |
resourceRelationships: { resourceProperty: "hostnames" }, |
| 248 |
relationshipI18n: { |
| 249 |
nameUpperCase: __("Hostname"), |
| 250 |
removeThisMessage: __("Remove this hostname"), |
| 251 |
addNewMessage: __("Add hostname"), |
| 252 |
noneCreatedYetMessage: __( |
| 253 |
"No hostnames configured. Add a hostname to surface this provider on its login page." |
| 254 |
), |
| 255 |
}, |
| 256 |
newRelationshipDefaultAttrs: { |
| 257 |
type: "object", |
| 258 |
value: { |
| 259 |
hostname: "", |
| 260 |
force_sso: false, |
| 261 |
matchpoint: null, |
| 262 |
}, |
| 263 |
}, |
| 264 |
}, |
| 265 |
relationshipFields: [ |
| 266 |
{ |
| 267 |
name: "hostname", |
| 268 |
required: true, |
| 269 |
indexRequired: true, |
| 270 |
type: "select", |
| 271 |
label: __("Hostname"), |
| 272 |
placeholder: __("Select or type to add a new hostname..."), |
| 273 |
options: [], |
| 274 |
selectLabel: "hostname", |
| 275 |
requiredKey: "hostname", |
| 276 |
taggable: true, |
| 277 |
createOption: h => ({ hostname: h }), |
| 278 |
toolTip: __( |
| 279 |
"Base URL used to access this Koha inferface, protocol not required" |
| 280 |
), |
| 281 |
}, |
| 282 |
{ |
| 283 |
name: "force_sso", |
| 284 |
type: "boolean", |
| 285 |
indexRequired: true, |
| 286 |
label: __("Force SSO"), |
| 287 |
toolTip: __( |
| 288 |
"Automatically redirect users on this hostname to this provider" |
| 289 |
), |
| 290 |
badgeTrueLabel: __("Force SSO"), |
| 291 |
badgeTrueClass: "bg-primary", |
| 292 |
}, |
| 293 |
{ |
| 294 |
name: "matchpoint", |
| 295 |
required: true, |
| 296 |
indexRequired: true, |
| 297 |
type: "select", |
| 298 |
label: __("Matchpoint"), |
| 299 |
options: matchpointOptions, |
| 300 |
requiredKey: "value", |
| 301 |
selectLabel: "label", |
| 302 |
toolTip: __( |
| 303 |
"Koha field used to identify existing patrons logging in from this hostname" |
| 304 |
), |
| 305 |
}, |
| 306 |
], |
| 307 |
}; |
| 308 |
|
| 309 |
const mappingsAttr = { |
| 310 |
name: "mappings", |
| 311 |
type: "relationshipWidget", |
| 312 |
label: __("Attribute Mappings"), |
| 313 |
group: "Attribute Mappings", |
| 314 |
hideIn: ["List"], |
| 315 |
showElement: { |
| 316 |
type: "table", |
| 317 |
columnData: "mappings", |
| 318 |
hidden: provider => !!provider.mappings?.length, |
| 319 |
columns: [ |
| 320 |
{ name: __("IdP field"), value: "provider_field" }, |
| 321 |
{ name: __("Koha field"), value: "koha_field" }, |
| 322 |
{ name: __("Default value"), value: "default_content" }, |
| 323 |
], |
| 324 |
}, |
| 325 |
componentProps: { |
| 326 |
resourceRelationships: { resourceProperty: "mappings" }, |
| 327 |
relationshipI18n: { |
| 328 |
nameUpperCase: __("Mapping"), |
| 329 |
noneCreatedYetMessage: __("No attribute mappings defined."), |
| 330 |
addNewMessage: __("Add mapping"), |
| 331 |
removeThisMessage: __("Remove"), |
| 332 |
}, |
| 333 |
newRelationshipDefaultAttrs: { |
| 334 |
type: "object", |
| 335 |
value: { |
| 336 |
provider_field: "", |
| 337 |
koha_field: borrowerColumnsArray[0]?.value || "", |
| 338 |
default_content: "", |
| 339 |
}, |
| 340 |
}, |
| 341 |
}, |
| 342 |
relationshipFields: [ |
| 343 |
{ |
| 344 |
name: "provider_field", |
| 345 |
required: true, |
| 346 |
indexRequired: true, |
| 347 |
type: "text", |
| 348 |
label: __("IdP field"), |
| 349 |
placeholder: __("e.g. given_name"), |
| 350 |
}, |
| 351 |
{ |
| 352 |
name: "koha_field", |
| 353 |
required: true, |
| 354 |
indexRequired: true, |
| 355 |
type: "select", |
| 356 |
label: __("Koha field"), |
| 357 |
options: borrowerColumnsArray, |
| 358 |
requiredKey: "value", |
| 359 |
selectLabel: "label", |
| 360 |
}, |
| 361 |
{ |
| 362 |
name: "default_content", |
| 363 |
indexRequired: true, |
| 364 |
type: "text", |
| 365 |
label: __("Default value"), |
| 366 |
}, |
| 367 |
], |
| 368 |
}; |
| 369 |
|
| 370 |
const domainsAttr = { |
| 371 |
name: "domains", |
| 372 |
type: "relationshipWidget", |
| 373 |
label: __("Email Domain Rules"), |
| 374 |
group: "Email Domain Rules", |
| 375 |
hideIn: ["List"], |
| 376 |
showElement: { |
| 377 |
type: "table", |
| 378 |
columnData: "domains", |
| 379 |
hidden: provider => !!provider.domains?.length, |
| 380 |
columns: [ |
| 381 |
{ name: __("Domain"), value: "domain" }, |
| 382 |
{ name: __("Allow OPAC"), value: "allow_opac" }, |
| 383 |
{ name: __("Allow staff"), value: "allow_staff" }, |
| 384 |
{ |
| 385 |
name: __("Auto-register (OPAC)"), |
| 386 |
value: "auto_register_opac", |
| 387 |
}, |
| 388 |
{ |
| 389 |
name: __("Auto-register (staff)"), |
| 390 |
value: "auto_register_staff", |
| 391 |
}, |
| 392 |
{ name: __("Update on auth"), value: "update_on_auth" }, |
| 393 |
{ |
| 394 |
name: __("Send welcome email"), |
| 395 |
value: "send_welcome_email", |
| 396 |
}, |
| 397 |
{ |
| 398 |
name: __("Default library"), |
| 399 |
value: "default_library_id", |
| 400 |
}, |
| 401 |
{ |
| 402 |
name: __("Default category"), |
| 403 |
value: "default_category_id", |
| 404 |
}, |
| 405 |
], |
| 406 |
}, |
| 407 |
componentProps: { |
| 408 |
resourceRelationships: { resourceProperty: "domains" }, |
| 409 |
relationshipI18n: { |
| 410 |
nameUpperCase: __("Domain"), |
| 411 |
removeThisMessage: __("Remove this domain"), |
| 412 |
addNewMessage: __("Add domain rule"), |
| 413 |
noneCreatedYetMessage: __("No domain rules defined."), |
| 414 |
}, |
| 415 |
newRelationshipDefaultAttrs: { |
| 416 |
type: "object", |
| 417 |
value: { |
| 418 |
domain: "", |
| 419 |
allow_opac: false, |
| 420 |
allow_staff: false, |
| 421 |
auto_register_opac: false, |
| 422 |
auto_register_staff: false, |
| 423 |
update_on_auth: false, |
| 424 |
send_welcome_email: false, |
| 425 |
default_library_id: librariesArray[0]?.value || "", |
| 426 |
default_category_id: categoriesArray[0]?.value || "", |
| 427 |
}, |
| 428 |
}, |
| 429 |
}, |
| 430 |
relationshipFields: [ |
| 431 |
{ |
| 432 |
name: "domain", |
| 433 |
type: "text", |
| 434 |
indexRequired: true, |
| 435 |
label: __("Domain"), |
| 436 |
placeholder: __("e.g. library.org or *"), |
| 437 |
toolTip: __( |
| 438 |
"Email domain to match. Use '*' or leave empty for any domain." |
| 439 |
), |
| 440 |
}, |
| 441 |
{ |
| 442 |
name: "allow_opac", |
| 443 |
type: "boolean", |
| 444 |
indexRequired: true, |
| 445 |
label: __("Allow OPAC login"), |
| 446 |
badgeTrueLabel: __("OPAC"), |
| 447 |
badgeTrueClass: "bg-success", |
| 448 |
}, |
| 449 |
{ |
| 450 |
name: "allow_staff", |
| 451 |
type: "boolean", |
| 452 |
indexRequired: true, |
| 453 |
label: __("Allow staff login"), |
| 454 |
badgeTrueLabel: __("Staff"), |
| 455 |
badgeTrueClass: "bg-primary", |
| 456 |
}, |
| 457 |
{ |
| 458 |
name: "auto_register_opac", |
| 459 |
type: "boolean", |
| 460 |
indexRequired: true, |
| 461 |
label: __("Auto-register (OPAC)"), |
| 462 |
}, |
| 463 |
{ |
| 464 |
name: "auto_register_staff", |
| 465 |
type: "boolean", |
| 466 |
indexRequired: true, |
| 467 |
label: __("Auto-register (staff)"), |
| 468 |
}, |
| 469 |
{ |
| 470 |
name: "update_on_auth", |
| 471 |
type: "boolean", |
| 472 |
indexRequired: true, |
| 473 |
label: __("Update patron data on login"), |
| 474 |
}, |
| 475 |
{ |
| 476 |
name: "send_welcome_email", |
| 477 |
type: "boolean", |
| 478 |
indexRequired: true, |
| 479 |
label: __("Send welcome email"), |
| 480 |
toolTip: __( |
| 481 |
"Send a welcome email to the patron on their first login via this provider" |
| 482 |
), |
| 483 |
}, |
| 484 |
{ |
| 485 |
name: "default_library_id", |
| 486 |
type: "select", |
| 487 |
indexRequired: true, |
| 488 |
label: __("Default library"), |
| 489 |
options: librariesArray, |
| 490 |
requiredKey: "value", |
| 491 |
selectLabel: "label", |
| 492 |
}, |
| 493 |
{ |
| 494 |
name: "default_category_id", |
| 495 |
type: "select", |
| 496 |
indexRequired: true, |
| 497 |
label: __("Default category"), |
| 498 |
options: categoriesArray, |
| 499 |
requiredKey: "value", |
| 500 |
selectLabel: "label", |
| 501 |
}, |
| 502 |
], |
| 503 |
}; |
| 504 |
|
| 505 |
const resourceAttrs = [ |
| 506 |
...staticResourceAttrs, |
| 507 |
protocolPlaceholderAttr, |
| 508 |
...configResourceAttrs, |
| 509 |
hostnameAttr, |
| 510 |
mappingsAttr, |
| 511 |
domainsAttr, |
| 512 |
]; |
| 513 |
|
| 514 |
// Unpack the JSON config blob into flat _config_* fields so the form |
| 515 |
// and show views can bind to them individually. |
| 516 |
const afterResourceFetch = (componentData, resource) => { |
| 517 |
selectedProtocol.value = resource.protocol || null; |
| 518 |
const config = resource.config || {}; |
| 519 |
const fields = PROTOCOL_CONFIG_FIELDS[resource.protocol] || []; |
| 520 |
fields.forEach(field => { |
| 521 |
if (field.type === "group_placeholder") return; |
| 522 |
resource[`_config_${field.name}`] = |
| 523 |
field.type === "boolean" |
| 524 |
? (config[field.name] ?? false) |
| 525 |
: (config[field.name] ?? ""); |
| 526 |
}); |
| 527 |
if (!resource.domains) resource.domains = []; |
| 528 |
if (!resource.mappings) resource.mappings = []; |
| 529 |
resource.hostnames = resource.hostnames || []; |
| 530 |
|
| 531 |
// Store the IDs of existing sub-resources so we can delete any |
| 532 |
// that the user removes during an edit. |
| 533 |
originalMappingIds.value = resource.mappings |
| 534 |
.map(m => m.mapping_id) |
| 535 |
.filter(Boolean); |
| 536 |
originalHostnameIds.value = resource.hostnames |
| 537 |
.map(h => h.identity_provider_hostname_id) |
| 538 |
.filter(Boolean); |
| 539 |
originalDomainIds.value = resource.domains |
| 540 |
.map(d => d.identity_provider_domain_id) |
| 541 |
.filter(Boolean); |
| 542 |
}; |
| 543 |
|
| 544 |
const baseResource = useBaseResource({ |
| 545 |
resourceName: "provider", |
| 546 |
nameAttr: "code", |
| 547 |
idAttr: "identity_provider_id", |
| 548 |
components: { |
| 549 |
show: "ProviderShow", |
| 550 |
list: "ProvidersList", |
| 551 |
add: "ProviderFormAdd", |
| 552 |
edit: "ProviderFormEdit", |
| 553 |
}, |
| 554 |
apiClient: APIClient.identity_providers.providers, |
| 555 |
i18n: { |
| 556 |
deleteConfirmationMessage: $__( |
| 557 |
"Are you sure you want to delete this identity provider? All related domain and mapping configuration will also be deleted." |
| 558 |
), |
| 559 |
deleteSuccessMessage: $__("Identity provider deleted"), |
| 560 |
displayName: $__("Identity provider"), |
| 561 |
editLabel: $__("Edit identity provider"), |
| 562 |
emptyListMessage: $__( |
| 563 |
"There are no identity providers configured" |
| 564 |
), |
| 565 |
newLabel: $__("New identity provider"), |
| 566 |
}, |
| 567 |
table: { |
| 568 |
resourceTableUrl: "/api/v1/auth/identity_providers", |
| 569 |
options: {}, |
| 570 |
}, |
| 571 |
stickyToolbar: ["Form"], |
| 572 |
embedded: props.embedded, |
| 573 |
formGroupsDisplayMode: "accordion", |
| 574 |
resourceAttrs, |
| 575 |
afterResourceFetch, |
| 576 |
props, |
| 577 |
moduleStore: "IdentityProvidersStore", |
| 578 |
}); |
| 579 |
|
| 580 |
const tableOptions = { |
| 581 |
url: baseResource.getResourceTableUrl(), |
| 582 |
actions: { |
| 583 |
0: ["show"], |
| 584 |
"-1": ["edit", "delete"], |
| 585 |
}, |
| 586 |
}; |
| 587 |
|
| 588 |
// In add mode the form uses reactive(newResource) as its data object. |
| 589 |
// reactive() uses a WeakMap so calling it with the same plain object |
| 590 |
// returns the same proxy. Watching here therefore sees the same |
| 591 |
// mutations that ResourceFormSave makes when the user changes the |
| 592 |
// protocol dropdown. |
| 593 |
const formStateObj = reactive(baseResource.newResource.value); |
| 594 |
watch( |
| 595 |
() => formStateObj.protocol, |
| 596 |
newProtocol => { |
| 597 |
selectedProtocol.value = newProtocol || null; |
| 598 |
} |
| 599 |
); |
| 600 |
|
| 601 |
onMounted(async () => { |
| 602 |
try { |
| 603 |
const data = |
| 604 |
await APIClient.identity_providers.allHostnames.getAll(); |
| 605 |
hostnameAttr.relationshipFields[0].options = data || []; |
| 606 |
} catch (_) { |
| 607 |
// proceed with empty options; user can still type a hostname |
| 608 |
} |
| 609 |
initialized.value = true; |
| 610 |
}); |
| 611 |
|
| 612 |
const onFormSave = async (e, providerToSave) => { |
| 613 |
e.preventDefault(); |
| 614 |
const provider = JSON.parse(JSON.stringify(providerToSave)); |
| 615 |
const providerId = provider.identity_provider_id; |
| 616 |
const protocol = provider.protocol; |
| 617 |
|
| 618 |
// Collect the _config_* fields for the selected protocol into a |
| 619 |
// config object, then strip all _config_* keys from the payload. |
| 620 |
const configFieldDefs = PROTOCOL_CONFIG_FIELDS[protocol] || []; |
| 621 |
const config = {}; |
| 622 |
configFieldDefs.forEach(field => { |
| 623 |
if (field.type === "group_placeholder") return; |
| 624 |
const flatKey = `_config_${field.name}`; |
| 625 |
if (flatKey in provider) { |
| 626 |
config[field.name] = provider[flatKey]; |
| 627 |
} |
| 628 |
}); |
| 629 |
// Strip all _-prefixed keys (UI-only fields: _config_*, _protocol_*, etc.) |
| 630 |
Object.keys(provider) |
| 631 |
.filter(k => k.startsWith("_")) |
| 632 |
.forEach(k => delete provider[k]); |
| 633 |
// matchpoint moved to hostname level; remove from provider payload |
| 634 |
delete provider.matchpoint; |
| 635 |
provider.config = config; |
| 636 |
|
| 637 |
// Hostnames are managed separately via the hostnames API. |
| 638 |
const hostnamesFromForm = (provider.hostnames || []).filter( |
| 639 |
h => h.hostname |
| 640 |
); |
| 641 |
delete provider.hostnames; |
| 642 |
|
| 643 |
// Mappings are managed separately via the mappings API. |
| 644 |
// Only items with a koha_field are saved. |
| 645 |
const mappingsFromForm = (provider.mappings || []).filter( |
| 646 |
m => m.koha_field |
| 647 |
); |
| 648 |
delete provider.mappings; |
| 649 |
|
| 650 |
// Validate that each hostname's matchpoint (if set) has a corresponding mapping. |
| 651 |
for (const h of hostnamesFromForm) { |
| 652 |
const matchpoint = h.matchpoint || null; |
| 653 |
if (matchpoint) { |
| 654 |
const hasMapping = mappingsFromForm.some( |
| 655 |
m => m.koha_field === matchpoint |
| 656 |
); |
| 657 |
if (!hasMapping) { |
| 658 |
baseResource.setError( |
| 659 |
__( |
| 660 |
"The selected matchpoint must have a corresponding attribute mapping" |
| 661 |
) |
| 662 |
); |
| 663 |
return; |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
// Domains are managed separately via the domains API. |
| 669 |
const domainsFromForm = provider.domains || []; |
| 670 |
delete provider.domains; |
| 671 |
|
| 672 |
delete provider.identity_provider_id; |
| 673 |
|
| 674 |
try { |
| 675 |
if (providerId) { |
| 676 |
const updatedProvider = await baseResource.apiClient.update( |
| 677 |
provider, |
| 678 |
providerId |
| 679 |
); |
| 680 |
|
| 681 |
// Sync hostnames: delete removed, update existing, create new. |
| 682 |
const keptHostnameIds = hostnamesFromForm |
| 683 |
.map(h => h.identity_provider_hostname_id) |
| 684 |
.filter(Boolean); |
| 685 |
for (const id of originalHostnameIds.value.filter( |
| 686 |
id => !keptHostnameIds.includes(id) |
| 687 |
)) { |
| 688 |
await APIClient.identity_providers.hostnames.delete( |
| 689 |
providerId, |
| 690 |
id |
| 691 |
); |
| 692 |
} |
| 693 |
for (const h of hostnamesFromForm) { |
| 694 |
const body = { |
| 695 |
hostname: h.hostname, |
| 696 |
is_enabled: h.is_enabled ?? true, |
| 697 |
force_sso: h.force_sso ?? false, |
| 698 |
matchpoint: h.matchpoint || null, |
| 699 |
}; |
| 700 |
if (h.identity_provider_hostname_id) { |
| 701 |
await APIClient.identity_providers.hostnames.update( |
| 702 |
providerId, |
| 703 |
body, |
| 704 |
h.identity_provider_hostname_id |
| 705 |
); |
| 706 |
} else { |
| 707 |
await APIClient.identity_providers.hostnames.create( |
| 708 |
providerId, |
| 709 |
body |
| 710 |
); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
// Sync mappings: delete removed, update existing, create new. |
| 715 |
const keptMappingIds = mappingsFromForm |
| 716 |
.map(m => m.mapping_id) |
| 717 |
.filter(Boolean); |
| 718 |
for (const id of originalMappingIds.value.filter( |
| 719 |
id => !keptMappingIds.includes(id) |
| 720 |
)) { |
| 721 |
await APIClient.identity_providers.mappings.delete( |
| 722 |
providerId, |
| 723 |
id |
| 724 |
); |
| 725 |
} |
| 726 |
for (const m of mappingsFromForm) { |
| 727 |
const body = { |
| 728 |
provider_field: m.provider_field || null, |
| 729 |
koha_field: m.koha_field, |
| 730 |
default_content: m.default_content || null, |
| 731 |
}; |
| 732 |
if (m.mapping_id) { |
| 733 |
await APIClient.identity_providers.mappings.update( |
| 734 |
providerId, |
| 735 |
body, |
| 736 |
m.mapping_id |
| 737 |
); |
| 738 |
} else { |
| 739 |
await APIClient.identity_providers.mappings.create( |
| 740 |
providerId, |
| 741 |
body |
| 742 |
); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
// Sync domains: delete removed, update existing, create new. |
| 747 |
const keptDomainIds = domainsFromForm |
| 748 |
.map(d => d.identity_provider_domain_id) |
| 749 |
.filter(Boolean); |
| 750 |
for (const id of originalDomainIds.value.filter( |
| 751 |
id => !keptDomainIds.includes(id) |
| 752 |
)) { |
| 753 |
await APIClient.identity_providers.domains.delete( |
| 754 |
providerId, |
| 755 |
id |
| 756 |
); |
| 757 |
} |
| 758 |
for (const d of domainsFromForm) { |
| 759 |
const body = { |
| 760 |
domain: d.domain || null, |
| 761 |
allow_opac: d.allow_opac || false, |
| 762 |
allow_staff: d.allow_staff || false, |
| 763 |
auto_register_opac: d.auto_register_opac || false, |
| 764 |
auto_register_staff: d.auto_register_staff || false, |
| 765 |
update_on_auth: d.update_on_auth || false, |
| 766 |
send_welcome_email: d.send_welcome_email || false, |
| 767 |
default_library_id: d.default_library_id || null, |
| 768 |
default_category_id: d.default_category_id || null, |
| 769 |
}; |
| 770 |
if (d.identity_provider_domain_id) { |
| 771 |
await APIClient.identity_providers.domains.update( |
| 772 |
providerId, |
| 773 |
body, |
| 774 |
d.identity_provider_domain_id |
| 775 |
); |
| 776 |
} else { |
| 777 |
await APIClient.identity_providers.domains.create( |
| 778 |
providerId, |
| 779 |
body |
| 780 |
); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
baseResource.setMessage($__("Identity provider updated")); |
| 785 |
return updatedProvider; |
| 786 |
} else { |
| 787 |
const newProvider = |
| 788 |
await baseResource.apiClient.create(provider); |
| 789 |
const newId = newProvider.identity_provider_id; |
| 790 |
|
| 791 |
// Create a bridge record for each linked hostname |
| 792 |
for (const h of hostnamesFromForm) { |
| 793 |
await APIClient.identity_providers.hostnames.create( |
| 794 |
newId, |
| 795 |
{ |
| 796 |
hostname: h.hostname, |
| 797 |
is_enabled: true, |
| 798 |
force_sso: h.force_sso ?? false, |
| 799 |
matchpoint: h.matchpoint || null, |
| 800 |
} |
| 801 |
); |
| 802 |
} |
| 803 |
|
| 804 |
// Create each attribute mapping |
| 805 |
for (const m of mappingsFromForm) { |
| 806 |
await APIClient.identity_providers.mappings.create( |
| 807 |
newId, |
| 808 |
{ |
| 809 |
provider_field: m.provider_field || null, |
| 810 |
koha_field: m.koha_field, |
| 811 |
default_content: m.default_content || null, |
| 812 |
} |
| 813 |
); |
| 814 |
} |
| 815 |
|
| 816 |
// Create each domain rule |
| 817 |
for (const d of domainsFromForm) { |
| 818 |
await APIClient.identity_providers.domains.create( |
| 819 |
newId, |
| 820 |
{ |
| 821 |
domain: d.domain || null, |
| 822 |
allow_opac: d.allow_opac || false, |
| 823 |
allow_staff: d.allow_staff || false, |
| 824 |
auto_register_opac: |
| 825 |
d.auto_register_opac || false, |
| 826 |
auto_register_staff: |
| 827 |
d.auto_register_staff || false, |
| 828 |
update_on_auth: d.update_on_auth || false, |
| 829 |
send_welcome_email: |
| 830 |
d.send_welcome_email || false, |
| 831 |
default_library_id: |
| 832 |
d.default_library_id || null, |
| 833 |
default_category_id: |
| 834 |
d.default_category_id || null, |
| 835 |
} |
| 836 |
); |
| 837 |
} |
| 838 |
|
| 839 |
baseResource.setMessage($__("Identity provider created")); |
| 840 |
return newProvider; |
| 841 |
} |
| 842 |
} catch (error) { |
| 843 |
// errors surfaced by the httpClient |
| 844 |
} |
| 845 |
}; |
| 846 |
|
| 847 |
return { |
| 848 |
...baseResource, |
| 849 |
initialized, |
| 850 |
PROTOCOL_CONFIG_FIELDS, |
| 851 |
tableOptions, |
| 852 |
onFormSave, |
| 853 |
}; |
| 854 |
}, |
| 855 |
}; |
| 856 |
</script> |