@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
197 lines (195 loc) • 7.49 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { forkJoin, of } from 'rxjs';
import { del, get, patchJSON, postJSON } from '../utils/ajax';
import { map, pluck, switchMap } from 'rxjs/operators';
import { fetchAll as fetchAllSites } from './sites';
import { toQueryString } from '../utils/object';
import { asArray } from '../utils/array';
import { fetchAuthenticationType } from './auth';
// Check services/auth/login if `me` method is changed.
export function me() {
return forkJoin({
user: get(me.url).pipe(pluck('response', 'authenticatedUser')),
authenticationType: fetchAuthenticationType()
}).pipe(map(({ user, authenticationType }) => Object.assign(Object.assign({}, user), { authenticationType })));
}
me.url = '/studio/api/2/users/me.json';
export function create(user) {
return postJSON(`/studio/api/2/users`, user).pipe(pluck('response', 'user'));
}
export function update(user) {
return patchJSON(`/studio/api/2/users`, user).pipe(pluck('response', 'user'));
}
export function trash(username) {
return del(`/studio/api/2/users?username=${encodeURIComponent(username)}`).pipe(map(() => true));
}
export function fetchAll(options) {
const mergedOptions = Object.assign({ limit: 100, offset: 0 }, options);
return get(`/studio/api/2/users${toQueryString(mergedOptions)}`).pipe(
map(({ response }) =>
Object.assign(response.users, {
limit: response.limit < mergedOptions.limit ? mergedOptions.limit : response.limit,
offset: response.offset,
total: response.total
})
)
);
}
export function enable(usernames) {
return patchJSON('/studio/api/2/users/enable', { usernames: asArray(usernames) }).pipe(
pluck('response', 'users'),
map((users) => (Array.isArray(usernames) ? users : users[0]))
);
}
export function disable(usernames) {
return patchJSON('/studio/api/2/users/disable', { usernames: asArray(usernames) }).pipe(
pluck('response', 'users'),
map((users) => (Array.isArray(usernames) ? users : users[0]))
);
}
/**
* Sets the password for the supplied username. Requires UPDATE_USERS permission.
**/
export function setPassword(username, password) {
return postJSON(`/studio/api/2/users/${encodeURIComponent(username)}/reset_password`, {
username,
new: password
}).pipe(pluck('response'));
}
/**
* Set a new password using a valid password reset token.
**/
export function resetPasswordWithToken(token, password) {
return postJSON('/studio/api/2/users/set_password', {
token,
new: password
}).pipe(pluck('response', 'user'));
}
/**
* Set the password for the current user.
**/
export function setMyPassword(username, currentPassword, newPassword) {
return postJSON('/studio/api/2/users/me/change_password', {
username,
current: currentPassword,
new: newPassword
}).pipe(pluck('response', 'user'));
}
export function fetchByUsername(username) {
return get(`/studio/api/2/users/${encodeURIComponent(username)}`).pipe(pluck('response', 'user'));
}
export function fetchRolesInSite(username, siteId) {
return get(`/studio/api/2/users/${username}/sites/${siteId}/roles`).pipe(pluck('response', 'roles'));
}
export function fetchMyRolesInSite(siteId) {
return get(`/studio/api/2/users/me/sites/${siteId}/roles`).pipe(pluck('response', 'roles'));
}
export function fetchRolesBySite(username, sites) {
return forkJoin({
username: username ? of(username) : me().pipe(map((user) => user.username)),
sites: sites ? of(sites) : fetchAllSites()
}).pipe(
switchMap(({ username, sites }) =>
forkJoin(
sites.reduce((lookup, site) => {
lookup[site.id] = fetchRolesInSite(username, site.id);
return lookup;
}, {})
)
)
);
}
// renamed from fetchRolesBySiteForCurrent
export function fetchMyRolesBySite(sites) {
return (sites ? of(sites) : fetchAllSites()).pipe(
switchMap((sites) =>
forkJoin(
sites.reduce((lookup, site) => {
lookup[site.id] = fetchMyRolesInSite(site.id);
return lookup;
}, {})
)
)
);
}
export function fetchGlobalProperties() {
return get('/studio/api/2/users/me/properties').pipe(pluck('response', 'properties', ''));
}
export function deleteGlobalProperties(...preferenceKeys) {
return del(`/studio/api/2/users/me/properties${toQueryString({ properties: preferenceKeys.join(',') })}`).pipe(
pluck('response', 'properties')
);
}
export function fetchSiteProperties(siteId) {
return get(`/studio/api/2/users/me/properties?siteId=${siteId}`).pipe(pluck('response', 'properties', siteId));
}
export function deleteSiteProperties(site, ...preferenceKeys) {
return del(
`/studio/api/2/users/me/properties${toQueryString({
siteId: site,
properties: JSON.stringify(preferenceKeys)
})}`
).pipe(pluck('response', 'properties'));
}
export function setProperties(preferences, siteId) {
return postJSON('/studio/api/2/users/me/properties', {
siteId,
properties: preferences
}).pipe(pluck('response', 'properties'));
}
export function deleteProperties(properties, siteId) {
return del(`/studio/api/2/users/me/properties${toQueryString({ siteId, properties: properties.join(',') })}`).pipe(
pluck('response', 'properties')
);
}
export function fetchMyPermissions(site) {
return get(`/studio/api/2/users/me/sites/${site}/permissions`).pipe(pluck('response', 'permissions'));
}
export function hasPermissions(site, ...permissions) {
return postJSON(`/studio/api/2/users/me/sites/${site}/has_permissions`, { permissions }).pipe(
pluck('response', 'permissions')
);
}
export function hasPermission(site, permission) {
return hasPermissions(site, permission).pipe(pluck(permission));
}
export function fetchGlobalPermissions() {
return get(`/studio/api/2/users/me/global/permissions`).pipe(pluck('response', 'permissions'));
}
export function hasGlobalPermissions(...permissions) {
return postJSON('/studio/api/2/users/me/global/has_permissions', { permissions }).pipe(
pluck('response', 'permissions')
);
}
export function hasGlobalPermission(permission) {
return hasGlobalPermissions(permission).pipe(pluck(permission));
}