Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement simple account resource CMS-2575
- Loading branch information
1 parent
86ff6d4
commit fc0c65b
Showing
6 changed files
with
682 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,72 @@ | ||
'use strict'; | ||
|
||
import Core, { get } from './Core'; | ||
import ListResource from './resources/ListResource'; | ||
import Resource from './resources/Resource'; | ||
import Core, { get, optionsToQuery } from './Core'; | ||
import AccountList from './resources/AccountList'; | ||
import AccountResource from './resources/AccountResource'; | ||
|
||
const urls = { | ||
live: 'https://accounts.entrecode.de/', | ||
stage: 'https://accounts.cachena.entrecode.de/', | ||
staging: 'https://accounts.buffalo.entrecode.de/', | ||
development: 'http://localhost:7472/', | ||
nightly: 'https://accounts.buffalo.entrecode.de/', | ||
develop: 'http://localhost:7472/', | ||
}; | ||
|
||
/** | ||
* Module for working with Accounts API. | ||
* | ||
* @class | ||
* @module | ||
*/ | ||
export default class Accounts extends Core { | ||
constructor(environment, token) { | ||
super(urls[environment]); | ||
this.resourceName = 'ec:account'; | ||
if (token) { | ||
this.traversal.addRequestOptions({ headers: { Authorization: `Bearer ${token}` } }); | ||
/** | ||
* Creates a new instance of {@link Accounts} module. Can be used to work with Accounts | ||
* API. | ||
* | ||
* @param {?string} environment the environment to connect to. 'live', 'stage', 'nightly', or | ||
* 'develop'. | ||
*/ | ||
constructor(environment) { | ||
if (environment && !{}.hasOwnProperty.call(urls, environment)) { | ||
throw new Error('invalid environment specified'); | ||
} | ||
|
||
super(urls[environment || 'live']); | ||
} | ||
|
||
list(filters) { | ||
/** | ||
* Load a {@link AccountList} of {@link AccountResource} filtered by the values specified | ||
* by the options parameter. | ||
* | ||
* @param {{size: number, page: number, sort: array<string>, filter: filter}} options the | ||
* filter options. | ||
* @returns {Promise.<AccountList>} resolves to account list with applied filters. | ||
*/ | ||
list(options) { | ||
return Promise.resolve() | ||
.then(() => { | ||
const request = this.newRequest() | ||
.follow('ec:accounts/options') | ||
.withTemplateParameters(filters); | ||
.withTemplateParameters(optionsToQuery(options)); | ||
return get(request); | ||
}) | ||
.then(([res, traversal]) => new ListResource(res, this.resourceName, traversal)); | ||
.then(([res, traversal]) => new AccountList(res, traversal)); | ||
} | ||
|
||
/** | ||
* Get a single {@link AccountResource} identified by accountID. | ||
* | ||
* @param {string} accountID id of the Account. | ||
* @returns {Promise.<AccountResource>} resolves to the Account which should be loaded. | ||
*/ | ||
get(accountID) { | ||
if (!accountID) { | ||
throw new Error('accountID must be defined'); | ||
} | ||
return Promise.resolve() | ||
.then(() => { | ||
const request = this.newRequest().follow('ec:account/by-id') | ||
const request = this.newRequest() | ||
.follow('ec:account/by-id') | ||
.withTemplateParameters({ accountID }); | ||
return get(request); | ||
}) | ||
.then(([res, traversal]) => new Resource(res, this.resourceName, traversal)); | ||
.then(([res, traversal]) => new AccountResource(res, traversal)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ListResource from './ListResource'; | ||
import AccountResource from './AccountResource'; | ||
|
||
/** | ||
* Account list resource class. | ||
* | ||
* @class | ||
*/ | ||
export default class AccountList extends ListResource { | ||
/** | ||
* Creates a new {@link AccountList}. | ||
* | ||
* @param {object} resource resource loaded from the API. | ||
* @param {?object} traversal traversal from which traverson can continue. | ||
*/ | ||
constructor(resource, traversal) { | ||
super(resource, 'ec:accounts', traversal); | ||
this.ListClass = AccountList; | ||
this.ItemClass = AccountResource; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
import Resource from './Resource'; | ||
|
||
/** | ||
* Account resource class | ||
* | ||
* @class | ||
*/ | ||
export default class AccountResource extends Resource { | ||
/** | ||
* Will return accountID property. | ||
* | ||
* @returns {string} the accountID. | ||
*/ | ||
getAccountID() { | ||
return this.getProperty('accountID'); | ||
} | ||
|
||
/** | ||
* Will return name property. | ||
* | ||
* @returns {string} the name. | ||
*/ | ||
getName() { | ||
return this.getProperty('name'); | ||
} | ||
|
||
/** | ||
* Will return email property. | ||
* | ||
* @returns {string} the email. | ||
*/ | ||
getEmail() { | ||
return this.getProperty('email'); | ||
} | ||
|
||
/** | ||
* Check if this account has a password. Will be false on openID only and API key accounts. | ||
* | ||
* @returns {boolean} whether or not this account has a password. | ||
*/ | ||
hasPassword() { | ||
return this.getProperty('hasPassword'); | ||
} | ||
|
||
/** | ||
* Check if this account has a pending email. | ||
* | ||
* @returns {array<openID>} whether or not the account has a pending email. | ||
*/ | ||
hasPendingEmail() { | ||
return this.getProperty('hasPendingEmail'); | ||
} | ||
|
||
/** | ||
* Will return groups property. | ||
* | ||
* @returns {array<object>} the groups array. | ||
*/ | ||
getGroups() { | ||
return this.getProperty('groups'); | ||
} | ||
|
||
/** | ||
* Set a new value to language property. | ||
* | ||
* @param {string} value the value to assign. | ||
* @returns {AccountResource} this Resource for chainability | ||
*/ | ||
setLanguage(value) { | ||
if (!value) { | ||
throw new Error('Language must be defined'); | ||
} | ||
|
||
return this.setProperty('language', value); | ||
} | ||
|
||
/** | ||
* Will return language property. | ||
* | ||
* @returns {string} the language. | ||
*/ | ||
getLanguage() { | ||
return this.getProperty('language'); | ||
} | ||
|
||
/** | ||
* Set a new value to state property. | ||
* | ||
* @param {string} value the value to assign. | ||
* @returns {AccountResource} this Resource for chainability | ||
*/ | ||
setState(value) { | ||
if (!value) { | ||
throw new Error('State must be defined'); | ||
} | ||
|
||
return this.setProperty('state', value); | ||
} | ||
|
||
/** | ||
* Will return stage property. | ||
* | ||
* @returns {string} the state. | ||
*/ | ||
getState() { | ||
return this.getProperty('state'); | ||
} | ||
|
||
/** | ||
* Set a new value to openID property. | ||
* | ||
* @param {array<openID>} value the value to assign. | ||
* @returns {AccountResource} this Resource for chainability | ||
*/ | ||
setOpenID(value) { | ||
if (!value) { | ||
throw new Error('openID must be defined'); | ||
} | ||
|
||
return this.setProperty('openID', value); | ||
} | ||
|
||
/** | ||
* Will return openID property. | ||
* | ||
* @returns {array<openID>} the openID array. | ||
*/ | ||
getOpenID() { | ||
return this.getProperty('openID'); | ||
} | ||
|
||
/** | ||
* Set a new value to permissions property. | ||
* | ||
* @param {array<string>} value the value to assign. | ||
* @returns {AccountResource} this Resource for chainability | ||
*/ | ||
setPermissions(value) { | ||
if (!value) { | ||
throw new Error('Permissions must be defined'); | ||
} | ||
|
||
return this.setProperty('permissions', value); | ||
} | ||
|
||
/** | ||
* Adds a new permission to permissions array. | ||
* | ||
* @param {string} value the permission to add. | ||
* @returns {AccountResource} this Resource for chainability | ||
*/ | ||
addPermission(value) { | ||
if (!value) { | ||
throw new Error('permission must be defined'); | ||
} | ||
|
||
const current = this.getPermissions(); | ||
current.push(value); | ||
return this.setPermissions(current); | ||
} | ||
|
||
/** | ||
* Will return permissions property. | ||
* | ||
* @returns {array<string>} the config. | ||
*/ | ||
getPermissions() { | ||
return this.getProperty('permissions'); | ||
} | ||
|
||
/** | ||
* Returns an array of all permissions of this account. The array will contain the account | ||
* permissions and all group permissions. | ||
* | ||
* @returns {array<string>} All permissions. | ||
*/ | ||
getAllPermissions() { | ||
return this.getProperty('groups') | ||
.map(g => g.permissions) | ||
.reduce((all, current) => all.concat(current), this.getPermissions()); | ||
} | ||
|
||
// todo change email | ||
// todo password reset | ||
} | ||
|
||
/** | ||
* Object describing openID connections. | ||
* | ||
* @typedef {{sub: string, iss: string, pending: boolean, email: string, name: string}} openID | ||
*/ |
Oops, something went wrong.