add trigger user create

This commit is contained in:
Frédéric FRANCE 2020-10-29 20:53:28 +01:00
parent 317ffc65c9
commit e049eb960d
No known key found for this signature in database
GPG Key ID: 06809324E4B2ABC1
9 changed files with 102 additions and 98 deletions

View File

@ -1,6 +1,6 @@
/*jshint esversion: 6 */ /*jshint esversion: 6 */
const testAuth = (z , bundle) => { const test = (z , bundle) => {
const url = bundle.authData.url+'/api/index.php/login'; const url = bundle.authData.url+'/api/index.php/status';
// Normally you want to make a request to an endpoint that is either specifically designed to test auth, or one that // Normally you want to make a request to an endpoint that is either specifically designed to test auth, or one that
// every user will have access to, such as an account or profile endpoint like /me. // every user will have access to, such as an account or profile endpoint like /me.
// In this example, we'll hit httpbin, which validates the Authorization Header against the arguments passed in the URL path // In this example, we'll hit httpbin, which validates the Authorization Header against the arguments passed in the URL path
@ -11,67 +11,92 @@ const testAuth = (z , bundle) => {
// This method can return any truthy value to indicate the credentials are valid. // This method can return any truthy value to indicate the credentials are valid.
// Raise an error to show // Raise an error to show
return promise.then((response) => { return promise.then((response) => {
if (response.status === 401) { if (response.status === 400) {
throw new Error('The Session Key you supplied is invalid'); throw new Error('400 -The Session Key you supplied is invalid');
}
if (response.status === 403) {
throw new Error('403 -The Session Key you supplied is invalid');
} }
return response; return response;
}); });
}; };
const getSessionKey = (z, bundle) => { // To include the session key header on all outbound requests, simply define a function here.
// It runs runs before each request is sent out, allowing you to make tweaks to the request in a centralized spot
const includeSessionKeyHeader = (request, z, bundle) => {
if (bundle.authData.sessionKey) {
request.headers = request.headers || {};
request.headers['DOLAPIKEY'] = bundle.authData.sessionKey;
}
return request;
};
// If we get a response and it is a 401, we can raise a special error telling Zapier to retry this after another exchange.
const sessionRefreshIf401 = (response, z, bundle) => {
if (bundle.authData.sessionKey) {
if (response.status === 401) {
throw new z.errors.RefreshAuthError('Session apikey needs refreshing.');
}
}
return response;
};
const getSessionKey = async (z, bundle) => {
const url = bundle.authData.url + '/api/index.php/login'; const url = bundle.authData.url + '/api/index.php/login';
const promise = z.request({ const response = await z.request({
method: 'POST',
url: url, url: url,
method: 'POST',
body: { body: {
login: bundle.authData.login, login: bundle.authData.login,
password: bundle.authData.password, password: bundle.authData.password,
} },
}); });
return promise.then((response) => { // if (response.status === 401) {
if (response.status === 401) { // throw new Error('The login/password you supplied is invalid');
throw new Error('The login/password you supplied is invalid'); // }
} const json = JSON.parse(response.content);
const json = JSON.parse(response.content); return {
return { sessionKey: json.success.token || '',
sessionKey: json.success.token || 'secret' };
};
});
}; };
module.exports = { module.exports = {
type: 'session', config: {
// Define any auth fields your app requires here. The user will be prompted to enter this info when type: 'session',
// they connect their account. sessionConfig: {
fields: [ perform: getSessionKey
{
key: 'url',
label: 'Url of service without ending-slash',
required: true,
type: 'string'
}, },
{ // Define any auth fields your app requires here. The user will be prompted to enter this info when
key: 'login', // they connect their account.
label: 'Login', fields: [
required: true, {
type: 'string' key: 'url',
}, label: 'Url of service without ending-slash',
{ required: true,
key: 'password', type: 'string'
label: 'Password', },
required: true, {
type: 'password' key: 'login',
} label: 'Login',
], required: true,
// The test method allows Zapier to verify that the credentials a user provides are valid. We'll execute this type: 'string'
// method whenever a user connects their account for the first time. },
test: testAuth, {
// The method that will exchange the fields provided by the user for session credentials. key: 'password',
sessionConfig: { label: 'Password',
perform: getSessionKey required: true,
type: 'password'
}
],
// The test method allows Zapier to verify that the credentials a user provides are valid. We'll execute this
// method whenever a user connects their account for the first time.
test,
// The method that will exchange the fields provided by the user for session credentials.
// assuming "login" is a key returned from the test
connectionLabel: '{{login}}'
}, },
// assuming "login" is a key returned from the test befores: [includeSessionKeyHeader],
connectionLabel: '{{login}}' afters: [sessionRefreshIf401],
}; };

View File

@ -8,27 +8,31 @@ const searchThirdparty = require('./searches/thirdparty');
const createThirdparty = require('./creates/thirdparty'); const createThirdparty = require('./creates/thirdparty');
const authentication = require('./authentication'); const {
config: authentication,
befores = [],
afters = [],
} = require('./authentication');
// To include the session key header on all outbound requests, simply define a function here. // To include the session key header on all outbound requests, simply define a function here.
// It runs runs before each request is sent out, allowing you to make tweaks to the request in a centralized spot // It runs runs before each request is sent out, allowing you to make tweaks to the request in a centralized spot
const includeSessionKeyHeader = (request, z, bundle) => { // const includeSessionKeyHeader = (request, z, bundle) => {
if (bundle.authData.sessionKey) { // if (bundle.authData.sessionKey) {
request.headers = request.headers || {}; // request.headers = request.headers || {};
request.headers['DOLAPIKEY'] = bundle.authData.sessionKey; // request.headers['DOLAPIKEY'] = bundle.authData.sessionKey;
} // }
return request; // return request;
}; // };
// If we get a response and it is a 401, we can raise a special error telling Zapier to retry this after another exchange. // If we get a response and it is a 401, we can raise a special error telling Zapier to retry this after another exchange.
const sessionRefreshIf401 = (response, z, bundle) => { // const sessionRefreshIf401 = (response, z, bundle) => {
if (bundle.authData.sessionKey) { // if (bundle.authData.sessionKey) {
if (response.status === 401) { // if (response.status === 401) {
throw new z.errors.RefreshAuthError('Session apikey needs refreshing.'); // throw new z.errors.RefreshAuthError('Session apikey needs refreshing.');
} // }
} // }
return response; // return response;
}; // };
// We can roll up all our behaviors in an App. // We can roll up all our behaviors in an App.
const App = { const App = {
@ -41,11 +45,12 @@ const App = {
// beforeRequest & afterResponse are optional hooks into the provided HTTP client // beforeRequest & afterResponse are optional hooks into the provided HTTP client
beforeRequest: [ beforeRequest: [
includeSessionKeyHeader ...befores
], ],
afterResponse: [ afterResponse: [
sessionRefreshIf401 ...afters
//sessionRefreshIf401
], ],
// If you want to define optional resources to simplify creation of triggers, searches, creates - do that here! // If you want to define optional resources to simplify creation of triggers, searches, creates - do that here!

View File

@ -17,7 +17,7 @@ const subscribeHook = (z, bundle) => {
const options = { const options = {
url: url, url: url,
method: 'POST', method: 'POST',
body: JSON.stringify(data) body: data,
}; };
// You may return a promise or a normal data structure from any perform method. // You may return a promise or a normal data structure from any perform method.

View File

@ -17,7 +17,7 @@ const subscribeHook = (z, bundle) => {
const options = { const options = {
url: url, url: url,
method: 'POST', method: 'POST',
body: JSON.stringify(data) body: data,
}; };
// You may return a promise or a normal data structure from any perform method. // You may return a promise or a normal data structure from any perform method.

View File

@ -17,7 +17,7 @@ const subscribeHook = (z, bundle) => {
const options = { const options = {
url: url, url: url,
method: 'POST', method: 'POST',
body: JSON.stringify(data) body: data,
}; };
// You may return a promise or a normal data structure from any perform method. // You may return a promise or a normal data structure from any perform method.

View File

@ -17,7 +17,7 @@ const subscribeHook = (z, bundle) => {
const options = { const options = {
url: url, url: url,
method: 'POST', method: 'POST',
body: JSON.stringify(data) body: data,
}; };
// You may return a promise or a normal data structure from any perform method. // You may return a promise or a normal data structure from any perform method.

View File

@ -242,7 +242,7 @@ abstract class DoliDB implements Database
$fields=explode(',', $sortfield); $fields=explode(',', $sortfield);
$orders=explode(',', $sortorder); $orders=explode(',', $sortorder);
$i=0; $i=0;
foreach($fields as $val) foreach ($fields as $val)
{ {
if (!$return) $return .= ' ORDER BY '; if (!$return) $return .= ' ORDER BY ';
else $return .= ', '; else $return .= ', ';

View File

@ -1125,21 +1125,7 @@ class User extends CommonObject
global $mysoc; global $mysoc;
// Clean parameters // Clean parameters
<<<<<<< HEAD
if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) {
$this->lastname = dol_ucwords(dol_strtolower($this->lastname));
}
if (!empty($conf->global->MAIN_ALL_TO_UPPER)) {
$this->lastname = dol_strtoupper($this->lastname);
}
if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) {
$this->firstname = dol_ucwords(dol_strtolower($this->firstname));
}
=======
$this->setUpperOrLowerCase(); $this->setUpperOrLowerCase();
>>>>>>> upstream/develop
$this->login = trim($this->login); $this->login = trim($this->login);
if (!isset($this->entity)) { if (!isset($this->entity)) {
$this->entity = $conf->entity; // If not defined, we use default value $this->entity = $conf->entity; // If not defined, we use default value
@ -1476,20 +1462,6 @@ class User extends CommonObject
dol_syslog(get_class($this)."::update notrigger=".$notrigger.", nosyncmember=".$nosyncmember.", nosyncmemberpass=".$nosyncmemberpass); dol_syslog(get_class($this)."::update notrigger=".$notrigger.", nosyncmember=".$nosyncmember.", nosyncmemberpass=".$nosyncmemberpass);
// Clean parameters // Clean parameters
<<<<<<< HEAD
if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) {
$this->lastname = dol_ucwords(dol_strtolower($this->lastname));
}
if (!empty($conf->global->MAIN_ALL_TO_UPPER)) {
$this->lastname = dol_strtoupper($this->lastname);
}
if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) {
$this->firstname = dol_ucwords(dol_strtolower($this->firstname));
}
=======
>>>>>>> upstream/develop
$this->lastname = trim($this->lastname); $this->lastname = trim($this->lastname);
$this->firstname = trim($this->firstname); $this->firstname = trim($this->firstname);
$this->employee = $this->employee ? $this->employee : 0; $this->employee = $this->employee ? $this->employee : 0;

View File

@ -110,6 +110,7 @@ class ZapierApi extends DolibarrApi
'orders' => 'Orders', 'orders' => 'Orders',
'thirdparties' => 'Thirparties', 'thirdparties' => 'Thirparties',
'contacts' => 'Contacts', 'contacts' => 'Contacts',
'users' => 'Users',
); );
// $result = $this->hook->fetch($id); // $result = $this->hook->fetch($id);
// if (! $result ) { // if (! $result ) {
@ -244,6 +245,7 @@ class ZapierApi extends DolibarrApi
$fields = array( $fields = array(
'url', 'url',
); );
dol_syslog("API Zapier create hook receive : " . print_r($request_data, true), LOG_NOTICE);
$result = $this->validate($request_data, $fields); $result = $this->validate($request_data, $fields);
foreach ($request_data as $field => $value) { foreach ($request_data as $field => $value) {