Skip to content

Commit

Permalink
Merge pull request #687 from DiamondLightSource/pre-release/2023-R4.3
Browse files Browse the repository at this point in the history
Pre release/2023 r4.3
  • Loading branch information
John-Holt-Tessella authored Nov 2, 2023
2 parents 7d6f57f + f03a8d5 commit aa9dfcd
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 45 deletions.
7 changes: 5 additions & 2 deletions api/config_sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# CAS CA Cert (for SSO)
$cacert = '/etc/certs/ca-bundle.crt';

# ldap server, used for lookup and authentication (if using)
# ldap server, used for lookup and authentication (if using, set to null if not)
# Update the ldap(s) prefix, hostname and search settings as required
$ldap_server = 'ldaps://ldap.example.com';
$ldap_search = 'ou=people,dc=example,dc=com';
Expand Down Expand Up @@ -248,7 +248,10 @@
# Shipping service details
$use_shipping_service = null;
$use_shipping_service_incoming_shipments = null;
$shipping_service_url = null;
$shipping_service_api_url = null;
$shipping_service_api_user = null;
$shipping_service_api_password = null;
$shipping_service_app_url = null;
$shipping_service_links_in_emails = null;


Expand Down
8 changes: 7 additions & 1 deletion api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use SynchWeb\Database\DatabaseParent;
use SynchWeb\ImagingShared;
use SynchWeb\Dispatch;
use SynchWeb\Options;

require 'vendor/autoload.php';

Expand Down Expand Up @@ -71,8 +72,9 @@ function setupApplication($mode): Slim
$dhl_enable, $dhl_link, $scale_grid, $scale_grid_end_date, $preset_proposal, $timezone,
$valid_components, $enabled_container_types, $ifsummary;
$app->contentType('application/json');
$options = $app->container['options'];
$app->response()->body(json_encode(array(
'motd' => $motd,
'motd' => $options->get('motd', $motd),
'authentication_type' => $authentication_type,
'cas_url' => $cas_url,
'cas_sso' => $cas_sso,
Expand Down Expand Up @@ -145,4 +147,8 @@ function setupDependencyInjectionContainer($app)
$app->container->singleton('dispatch', function () use ($app) {
return new Dispatch($app, $app->container['db'], $app->container['user']);
});

$app->container->singleton('options', function () use ($app) {
return new Options($app->container['db']);
});
}
10 changes: 9 additions & 1 deletion api/src/Authentication/Type/OIDC.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ function __construct() {
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errno = curl_errno($ch);

if ($errno || $http_code != 200) {
error_log("Failed to connect to OIDC discovery endpoint. HTTP code: " . $http_code . ". CURL err. no.: " . $errno);
return;
}

curl_close($ch);
$newProviderConfig = json_decode($response);

if(!$newProviderConfig
|| !isset($newProviderConfig->userinfo_endpoint)
|| !isset($newProviderConfig->authorization_endpoint)
|| !isset($newProviderConfig->token_endpoint)) {
error_log("OIDC Authentication provider replied with invalid JSON body");
error_log("OIDC Authentication provider replied with invalid JSON discovery body");
return;
}
$newProviderConfig->b64ClientCreds = base64_encode(
Expand Down
2 changes: 1 addition & 1 deletion api/src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private function _retrieve() {

$this->options = array();
foreach ($options as $o) {
$this->options[$o['NAME']] = unserialize($o['VALUE']);
$this->options[$o['NAME']] = $o['VALUE'];
if ($o['UI'] == 1) $this->public[$o['NAME']] = $this->options[$o['NAME']];

}
Expand Down
4 changes: 4 additions & 0 deletions api/src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,10 @@ function _ldap_search($search, $email = False)
global $ldap_server, $ldap_search;

$ret = array();
if (is_null($ldap_server)) {
error_log("Ldap server is not configured, not looking up user.");
return $ret;
}
$ds = ldap_connect($ldap_server);
if ($ds)
{
Expand Down
2 changes: 1 addition & 1 deletion api/src/Page/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function _add_contact() {
foreach ($lfields as $i => $f) {
if ($this->has_arg($f)) {
$c = $f == 'LABNAME' ? 'NAME' : $f;
$this->db->pq('UPDATE laboratory SET '.$c.'=:1 WHERE laboratoryid=:2', array($this->arg($f), $lab['LABORATORYID']));
$this->db->pq('UPDATE laboratory SET '.$c.'=:1 WHERE laboratoryid=:2', array($this->arg($f), $lab[0]['LABORATORYID']));
}
}
}
Expand Down
61 changes: 36 additions & 25 deletions api/src/Page/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,10 +927,10 @@ function _dispatch_dewar_in_shipping_service($dispatch_info, $dewar)
global $facility_country;
global $facility_phone;
global $facility_contact;
global $shipping_service_url;
global $shipping_service_api_url;
global $facility_email;
if (!isset($shipping_service_url)) {
throw new Exception("Could not send request to shipping service: shipping_service_url not set");
if (!isset($shipping_service_api_url)) {
throw new Exception("Could not send request to shipping service: shipping_service_api_url not set");
}

# Create shipment
Expand Down Expand Up @@ -2613,46 +2613,57 @@ function _add_shipment()

function _get_default_dewar()
{
if (!$this->has_arg('visit'))
$this->_error('No visit specified');

$sids = $this->db->pq("SELECT s.sessionid FROM blsession s INNER JOIN proposal p ON p.proposalid = s.proposalid WHERE CONCAT(p.proposalcode, p.proposalnumber, '-', s.visit_number) LIKE :1 AND p.proposalid=:2", array($this->arg('visit'), $this->proposalid));

if (!sizeof($sids))
$this->_error('No such visit');
else
$sid = $sids[0]['SESSIONID'];
if ($this->has_arg('visit')) {
$shipmentName = $this->arg('visit') . '_Shipment1';
$dewarName = $this->arg('visit') . '_Dewar1';
$sids = $this->db->pq("SELECT s.sessionid FROM blsession s INNER JOIN proposal p ON p.proposalid = s.proposalid WHERE CONCAT(p.proposalcode, p.proposalnumber, '-', s.visit_number) LIKE :1 AND p.proposalid=:2", array($this->arg('visit'), $this->proposalid));
if (!sizeof($sids))
$this->_error('No such visit');
else
$sid = $sids[0]['SESSIONID'];

} elseif ($this->has_arg('prop')) {
$shipmentName = $this->arg('prop') . '_Shipment1';
$dewarName = $this->arg('prop') . '_Dewar1';
$sid = null;
} else {
$this->_error('No visit or proposal specified');
}

$shids = $this->db->pq("SELECT shippingid FROM shipping WHERE proposalid LIKE :1 AND shippingname LIKE :2", array($this->proposalid, $this->arg('visit') . '_Shipment1'));
$shids = $this->db->pq("SELECT shippingid FROM shipping WHERE proposalid LIKE :1 AND shippingname LIKE :2", array($this->proposalid, $shipmentName));

if (sizeof($shids) > 0) {
$shid = $shids[0]['SHIPPINGID'];
} else {
$this->db->pq("INSERT INTO shipping (shippingid,proposalid,shippingname,bltimestamp,creationdate,shippingstatus) VALUES (s_shipping.nextval,:1,:2,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,'processing') RETURNING shippingid INTO :id", array($this->proposalid, $this->arg('visit') . '_Shipment1'));
$this->db->pq("INSERT INTO shipping (shippingid,proposalid,shippingname,bltimestamp,creationdate,shippingstatus) VALUES (s_shipping.nextval,:1,:2,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,'processing') RETURNING shippingid INTO :id", array($this->proposalid, $shipmentName));

$shid = $this->db->id();

$vals = $this->db->pq("INSERT INTO shippinghassession (shippingid,sessionid) VALUES (:1,:2)", array($shid, $sid));
if ($sid)
$vals = $this->db->pq("INSERT INTO shippinghassession (shippingid,sessionid) VALUES (:1,:2)", array($shid, $sid));
}

$did = -1;
if ($sid) {
$dids = $this->db->pq("SELECT dewarid from dewar WHERE shippingid LIKE :1 AND code LIKE :2", array($shid, $this->arg('visit') . '_Dewar1'));

if (sizeof($dids) > 0) {
$did = $dids[0]['DEWARID'];
} else {
$this->db->pq("INSERT INTO dewar (dewarid,code,shippingid,bltimestamp,dewarstatus,firstexperimentid) VALUES (s_dewar.nextval,:1,:2,CURRENT_TIMESTAMP,'processing',:3) RETURNING dewarid INTO :id", array($this->arg('visit') . '_Dewar1', $shid, $sid));
$dids = $this->db->pq("SELECT dewarid from dewar WHERE shippingid LIKE :1 AND code LIKE :2", array($shid, $dewarName));

if (sizeof($dids) > 0) {
$did = $dids[0]['DEWARID'];
} else {
$this->db->pq("INSERT INTO dewar (dewarid,code,shippingid,bltimestamp,dewarstatus,firstexperimentid) VALUES (s_dewar.nextval,:1,:2,CURRENT_TIMESTAMP,'processing',:3) RETURNING dewarid INTO :id", array($dewarName, $shid, $sid));

$did = $this->db->id();
$did = $this->db->id();

# Need to generate barcode
# Need to generate barcode
if ($this->has_arg('visit')) {
$bl = $this->db->pq("SELECT s.beamlinename as bl FROM blsession s WHERE s.sessionid=:1", array($sid));
$this->db->pq("UPDATE dewar set barcode=:1 WHERE dewarid=:2", array($this->arg('visit') . '-' . $bl[0]['BL'] . '-' . str_pad($did, 7, '0', STR_PAD_LEFT), $did));
$barcode = $this->arg('visit') . '-' . $bl[0]['BL'] . '-' . str_pad($did, 7, '0', STR_PAD_LEFT);
} else {
$barcode = $this->arg('prop') . '-' . str_pad($did, 7, '0', STR_PAD_LEFT);
}
$this->db->pq("UPDATE dewar set barcode=:1 WHERE dewarid=:2", array($barcode, $did));
}


if ($did == -1)
$this->_error('Couldn\'t create default dewar');
$this->_output($did);
Expand Down
40 changes: 32 additions & 8 deletions api/src/Shipment/ShippingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,53 @@
class ShippingService
{
private $shipping_api_url;
private $headers;
private $shipping_app_url;
public const JOURNEY_TO_FACILITY = "TO_FACILITY";
public const JOURNEY_FROM_FACILITY = "FROM_FACILITY";

function __construct()
function _build_headers()
{
global $shipping_service_url;
$this->shipping_api_url = $shipping_service_url;
$this->headers = array(
global $cookie_key;
global $shipping_service_api_user;
global $shipping_service_api_password;

$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);

if (isset($_COOKIE[$cookie_key])) {
array_push($headers, "Authorization: Bearer {$_COOKIE[$cookie_key]}");
return $headers;
}

if (isset($shipping_service_api_user) && isset($shipping_service_api_password)) {
$basic_auth = base64_encode($shipping_service_api_user . ":" . $shipping_service_api_password);
array_push($headers, "Authorization: Basic {$basic_auth}");
return $headers;
}

throw new \Exception("Shipping service auth error: no cookie found and basic auth credentials unset");
}

function __construct()
{
global $shipping_service_api_url;
global $shipping_service_app_url;
$this->shipping_api_url = $shipping_service_api_url;
$this->shipping_app_url = $shipping_service_app_url;
}


function _send_request($url, $type, $data, $expected_status_code)
{
$ch = curl_init($url);
$base_headers = $this->_build_headers();
curl_setopt_array(
$ch,
array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $this->headers,
CURLOPT_HTTPHEADER => $base_headers,
CURLOPT_TIMEOUT => 5
)
);
Expand All @@ -52,7 +76,7 @@ function _send_request($url, $type, $data, $expected_status_code)
$ch,
array(
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array_merge($this->headers, array('Content-Length: ' . strlen(json_encode($data)))),
CURLOPT_HTTPHEADER => array_merge($base_headers, array('Content-Length: ' . strlen(json_encode($data)))),
CURLOPT_POSTFIELDS => json_encode($data),
)
);
Expand Down Expand Up @@ -125,6 +149,6 @@ function dispatch_shipment($shipment_id, $pickup_requested)

function get_awb_pdf_url($shipment_id)
{
return $this->shipping_api_url . '/shipments/' . $shipment_id . '/awb/pdf';
return $this->shipping_app_url . '/shipments/' . $shipment_id . '/awb';
}
}
3 changes: 2 additions & 1 deletion client/src/js/modules/fault/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const routes = [
{
path: 'edit',
name: 'faults-view-edit',
component: MarionetteView,
props: {
mview: FaultTypeEditor,
breadcrumbs: [bc, { title: 'Edit Fault Types' }],
Expand All @@ -102,4 +103,4 @@ const routes = [
},
]

export default routes
export default routes
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,8 @@
methods: {
async setDewarInformation() {
// Try to retrieve the default dewar for this proposal/visit
// Uses the special session-0 because at this point we are not necessarily on a session
this.defaultDewarId = await this.$store.dispatch('fetchDataFromApi', {
url: '/shipment/dewars/default',
data: { visit: `${this.$store.getters['proposal/currentProposal']}-0`},
requestType: 'fetching default dewar for this proposal/visit'
})
},
Expand Down Expand Up @@ -542,7 +540,7 @@
})
let container = new Container({
NAME: app.prop + '-' + app.visit + '_samples',
NAME: app.visit ? app.prop + '-' + app.visit + '_samples' : app.prop + '_samples',
CAPACITY: self.capacity,
CONTAINERTYPE: 'Box',
COMMENTS: item.comments
Expand Down Expand Up @@ -620,7 +618,7 @@
})
let container = new Container({
NAME: app.prop + '-' + app.visit + '_samples',
NAME: app.visit ? app.prop + '-' + app.visit + '_samples' : app.prop + '_samples',
CAPACITY: this.capacity,
CONTAINERTYPE: 'Box',
COMMENTS: this.comments
Expand Down Expand Up @@ -840,4 +838,4 @@
}
}
}
</script>
</script>

0 comments on commit aa9dfcd

Please sign in to comment.