Commit d395a6f5 authored by Aurel's avatar Aurel

allow to defined login/password for authentication

parent 23991ccf
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
/*jslint nomen: true, unparam: true */ /*jslint nomen: true, unparam: true */
/*global jIO, UriTemplate, FormData, RSVP, URI, Blob, /*global jIO, UriTemplate, FormData, RSVP, URI, Blob,
SimpleQuery, ComplexQuery*/ SimpleQuery, ComplexQuery, btoa*/
(function (jIO, UriTemplate, FormData, RSVP, URI, Blob, (function (jIO, UriTemplate, FormData, RSVP, URI, Blob,
SimpleQuery, ComplexQuery) { SimpleQuery, ComplexQuery) {
...@@ -24,8 +24,9 @@ ...@@ -24,8 +24,9 @@
"type": "GET", "type": "GET",
"url": storage._url, "url": storage._url,
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: storage._thisCredentials
} },
"headers": storage._headers
}); });
}) })
.push(function (event) { .push(function (event) {
...@@ -50,8 +51,9 @@ ...@@ -50,8 +51,9 @@
view: options._view view: options._view
}), }),
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: storage._thisCredentials
} },
"headers": storage._headers
}); });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
...@@ -128,8 +130,8 @@ ...@@ -128,8 +130,8 @@
}); });
} }
function extractPropertyFromForm(context, id) { function extractPropertyFromForm(storage, id) {
return context.getAttachment(id, "view") return storage.getAttachment(id, "view")
.push(function (blob) { .push(function (blob) {
return jIO.util.readBlobAsText(blob); return jIO.util.readBlobAsText(blob);
}) })
...@@ -149,6 +151,13 @@ ...@@ -149,6 +151,13 @@
} }
this._url = spec.url; this._url = spec.url;
this._default_view_reference = spec.default_view_reference; this._default_view_reference = spec.default_view_reference;
this._headers = null;
this._thisCredentials = true;
if (spec.login !== undefined && spec.password !== undefined) {
this._headers = {"Authorization": "Basic "
+ btoa(spec.login + ":" + spec.password)};
this._thisCredentials = false;
}
} }
function convertJSONToGet(json) { function convertJSONToGet(json) {
...@@ -190,8 +199,9 @@ ...@@ -190,8 +199,9 @@
// "Content-Type": "application/json" // "Content-Type": "application/json"
// }, // },
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: storage._thisCredentials
} },
"headers": storage._headers
}); });
}) })
.push(function (response) { .push(function (response) {
...@@ -213,7 +223,7 @@ ...@@ -213,7 +223,7 @@
}; };
ERP5Storage.prototype.post = function (data) { ERP5Storage.prototype.post = function (data) {
var context = this, var storage = this,
new_id; new_id;
return getSiteDocument(this) return getSiteDocument(this)
...@@ -226,15 +236,16 @@ ...@@ -226,15 +236,16 @@
url: site_hal._actions.add.href, url: site_hal._actions.add.href,
data: form_data, data: form_data,
xhrFields: { xhrFields: {
withCredentials: true withCredentials: storage._thisCredentials
} },
"headers": storage._headers
}); });
}) })
.push(function (evt) { .push(function (evt) {
var location = evt.target.getResponseHeader("X-Location"), var location = evt.target.getResponseHeader("X-Location"),
uri = new URI(location); uri = new URI(location);
new_id = uri.segment(2); new_id = uri.segment(2);
return context.put(new_id, data); return storage.put(new_id, data);
}) })
.push(function () { .push(function () {
return new_id; return new_id;
...@@ -242,9 +253,9 @@ ...@@ -242,9 +253,9 @@
}; };
ERP5Storage.prototype.put = function (id, data) { ERP5Storage.prototype.put = function (id, data) {
var context = this; var storage = this;
return extractPropertyFromForm(context, id) return extractPropertyFromForm(storage, id)
.push(function (result) { .push(function (result) {
var key, var key,
json = result.form_data, json = result.form_data,
...@@ -277,7 +288,7 @@ ...@@ -277,7 +288,7 @@
403 403
); );
} }
return context.putAttachment( return storage.putAttachment(
id, id,
result.action_href, result.action_href,
new Blob([JSON.stringify(form_data)], {type: "application/json"}) new Blob([JSON.stringify(form_data)], {type: "application/json"})
...@@ -286,10 +297,10 @@ ...@@ -286,10 +297,10 @@
}; };
ERP5Storage.prototype.allAttachments = function (id) { ERP5Storage.prototype.allAttachments = function (id) {
var context = this; var storage = this;
return getDocumentAndHateoas(this, id) return getDocumentAndHateoas(this, id)
.push(function () { .push(function () {
if (context._default_view_reference === undefined) { if (storage._default_view_reference === undefined) {
return { return {
links: {} links: {}
}; };
...@@ -302,6 +313,7 @@ ...@@ -302,6 +313,7 @@
}; };
ERP5Storage.prototype.getAttachment = function (id, action, options) { ERP5Storage.prototype.getAttachment = function (id, action, options) {
var storage = this;
if (options === undefined) { if (options === undefined) {
options = {}; options = {};
} }
...@@ -348,8 +360,9 @@ ...@@ -348,8 +360,9 @@
"dataType": "blob", "dataType": "blob",
"url": action, "url": action,
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: storage._thisCredentials
} },
"headers": storage._headers
}; };
if (options.start !== undefined || options.end !== undefined) { if (options.start !== undefined || options.end !== undefined) {
start = options.start || 0; start = options.start || 0;
...@@ -369,7 +382,11 @@ ...@@ -369,7 +382,11 @@
} }
range = "bytes=" + start + "-" + end; range = "bytes=" + start + "-" + end;
} }
request_options.headers = {Range: range}; if (storage._headers === undefined) {
request_options.headers = {Range: range};
} else {
request_options.headers.Range = range;
}
} }
return jIO.util.ajax(request_options); return jIO.util.ajax(request_options);
}) })
...@@ -388,6 +405,7 @@ ...@@ -388,6 +405,7 @@
}; };
ERP5Storage.prototype.putAttachment = function (id, name, blob) { ERP5Storage.prototype.putAttachment = function (id, name, blob) {
var storage = this;
// Assert we use a callable on a document from the ERP5 site // Assert we use a callable on a document from the ERP5 site
if (name.indexOf(this._url) !== 0) { if (name.indexOf(this._url) !== 0) {
throw new jIO.util.jIOError("Can not store outside ERP5: " + throw new jIO.util.jIOError("Can not store outside ERP5: " +
...@@ -428,8 +446,9 @@ ...@@ -428,8 +446,9 @@
"url": name, "url": name,
"data": data, "data": data,
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: storage._thisCredentials
} },
"headers": storage._headers
}); });
}); });
}; };
...@@ -478,6 +497,7 @@ ...@@ -478,6 +497,7 @@
// jIO.Query.objectToSearchText(options.query) : // jIO.Query.objectToSearchText(options.query) :
// undefined); // undefined);
// } // }
var storage = this;
return getSiteDocument(this) return getSiteDocument(this)
.push(function (site_hal) { .push(function (site_hal) {
var query = options.query, var query = options.query,
...@@ -546,8 +566,9 @@ ...@@ -546,8 +566,9 @@
local_roles: local_roles local_roles: local_roles
}), }),
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: storage._thisCredentials
} },
"headers": storage._headers
}); });
}) })
.push(function (response) { .push(function (response) {
......
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