Commit cc65466e authored by Laurent's avatar Laurent

handle Lyon 2 CAS

parent 8d81db99
......@@ -14,7 +14,7 @@ export class KohaCas extends Koha {
public canHandleWebsite($): boolean {
return $('#opac-main a[href*="/cas/login?service="]').length;
return $('#opac-main a[href*="/cas/login?service="]').length || $('#opac-login-page a[href*="/cas/login?service="]').length;
}
......@@ -35,34 +35,52 @@ export class KohaCas extends Koha {
.request(account, { url: cas_link })
.then( (response) => {
let $ = cheerio.load(response.content.toString());
let execution = $('input[name=execution]').attr('value');
let form_inputs = this._extractForm($);
let form_data = {
username: account.credentials['login'],
password: account.credentials['password'],
execution: form_inputs['execution'],
'_eventId': form_inputs['_eventId'],
'lt': form_inputs['lt'] || '',
'submit': form_inputs['submit']
}
return this
.request(account,
{ url: cas_link,
method: "POST",
dontFollowRedirects: true,
content: "username=" + encodeURIComponent(account.credentials['login'])
+ "&password=" + encodeURIComponent(account.credentials['password'])
+ "&execution=" + execution
+ "&_eventId=submit&geolocation="
+ "&submit=SE+CONNECTER",
content: this._encodeForm(form_data),
headers: {"Content-Type": "application/x-www-form-urlencoded"} });
})
})
.then( (response) => {
if (response.statusCode == 302)
return this.request(account, { url: response.headers['Location'] });
return response;
return (response.statusCode == 302)
? this.request(account, { url: response.headers['Location'] })
: response
})
.then( (response) => {
let $ = cheerio.load(response.content.toString());
let cas_link = $('a[href*="/cas/login?service="]').attr('href');
return this
.request(account, { url: cas_link })
.then( (response) => {
return this._handleSignIn(account, response);
});
return cas_link
? this.request(account, { url: cas_link })
: response;
}).then( (response) => {
return this._handleSignIn(account, response);
});
}
protected _extractForm($): any {
let form_data = [];
$('form input')
.filter( (i, elem) => { return $(elem).attr('name') != undefined; } )
.each((i, elem) => {
let input = $(elem);
form_data[input.attr('name')] = input.attr('value') || '';
});
return form_data;
}
}
......@@ -16,13 +16,15 @@ export class Koha extends PortalAdapter {
public canHandleWebsite($): boolean {
return $('form[action^="/cgi-bin/koha/opac-user.pl"]').length
&&
!$('#opac-main a[href*="/cas/login?service="]').length;
&&
!$('#opac-main a[href*="/cas/login?service="]').length
&&
!$('#opac-login-page a[href*="/cas/login?service="]').length;
}
public searchUrl(account: Account, terms: string): Promise<string> {
return Promise.resolve(account.getUrl() + '/cgi-bin/koha/opac-search.pl?q=' + terms);
return Promise.resolve(this._urlFor(account, 'opac-search.pl?q=' + terms))
}
......@@ -31,14 +33,21 @@ export class Koha extends PortalAdapter {
}
protected _urlFor(account: Account, query:string): string {
return new RegExp("(https?://[^/?]+)").exec(account.getUrl())[1]
+ '/cgi-bin/koha/' + query;
}
public updateCard(account: Account): Promise<any> {
return this
._requestProfilePage(account)
.then( ($) => {
let card = account.getCard();
$('label').remove();
$('label').remove();
let card_info = $('fieldset#memberentry_library ol li');
card.setNumber(card_info.eq(0).text().trim());
......@@ -54,7 +63,7 @@ export class Koha extends PortalAdapter {
return this
.request(account,
{ url: account.getUrl() + '/cgi-bin/koha/opac-user.pl',
{ url: this._urlFor(account, 'opac-user.pl'),
method: "POST",
content: encodeURI("koha_login_context=opac&userid=" + account.credentials['login'] + "&password=" + account.credentials['password']),
headers: {"Content-Type": "application/x-www-form-urlencoded"} })
......@@ -75,7 +84,7 @@ export class Koha extends PortalAdapter {
protected _requestProfilePage(account: Account): Promise<any> {
return this
.request(account, { url: account.getUrl() + '/cgi-bin/koha/opac-memberentry.pl' })
.request(account, { url: this._urlFor(account, 'opac-memberentry.pl') })
.then( (response) => {
return cheerio.load(response.content.toString());
});
......@@ -97,7 +106,7 @@ export class Koha extends PortalAdapter {
protected _fetchLoans(account: Account): Promise<Array<Loan>> {
return this
.request(account,
{ url: account.getUrl() + '/cgi-bin/koha/opac-user.pl' })
{ url: this._urlFor(account, 'opac-user.pl') })
.then( (response) => { return this._parseLoansPage(account, response)});
}
......@@ -119,7 +128,7 @@ export class Koha extends PortalAdapter {
.setAuthor(author)
.setDateDue(date_due)
.setRecordId(record_id)
.setRecordUrl(account.getUrl() + '/cgi-bin/koha/opac-detail.pl?biblionumber=' + record_id)
.setRecordUrl(this._urlFor(account, 'opac-detail.pl?biblionumber=' + record_id))
);
});
return loans;
......@@ -129,7 +138,7 @@ export class Koha extends PortalAdapter {
protected _fetchHolds(account: Account): Promise<Array<Hold>> {
return this
.request(account,
{ url: account.getUrl() + '/cgi-bin/koha/opac-user.pl' })
{ url: this._urlFor(account, 'opac-user.pl') })
.then( (response) => { return this._parseHoldsPage(account, response)});
}
......@@ -156,7 +165,7 @@ export class Koha extends PortalAdapter {
.setLibrary(hold_node.find('td.branch').text().trim())
.setStatus(hold_node.find('td.status').text().trim())
.setRecordId(record_id)
.setRecordUrl(account.getUrl() + '/cgi-bin/koha/opac-detail.pl?biblionumber=' + record_id)
.setRecordUrl(this._urlFor(account, 'opac-detail.pl?biblionumber=' + record_id))
);
});
return holds;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment