Skip to content

Commit

Permalink
wip #1817 commiting to discuss something
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock committed Sep 20, 2023
1 parent 14a344a commit 1cdd22e
Show file tree
Hide file tree
Showing 45 changed files with 686 additions and 576 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
include "com.enonic.xp:lib-io:${xpVersion}"
include "com.enonic.lib:lib-admin-ui:${libAdminUiVersion}"
devResources "com.enonic.lib:lib-admin-ui:${libAdminUiVersion}"
include "com.enonic.lib:lib-graphql:2.0.1"
include "com.enonic.lib:lib-graphql:2.1.0"
include "com.enonic.lib:lib-mustache:2.1.0"
include 'com.enonic.lib:lib-router:3.1.0'
include 'com.enonic.lib:lib-static:1.0.3'
Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
"devDependencies": {
"@enonic-types/global": "^7.13.2",
"@enonic-types/lib-admin": "^7.13.2",
"@enonic-types/lib-auth": "^7.13.2",
"@enonic-types/lib-i18n": "^7.13.2",
"@enonic-types/lib-io": "^7.13.2",
"@enonic-types/lib-node": "^7.13.2",
"@enonic-types/lib-portal": "^7.13.2",
"@enonic/esbuild-plugin-copy-with-hash": "^0.0.1",
"@enonic/eslint-config": "^1.2.0",
Expand Down
24 changes: 12 additions & 12 deletions src/main/resources/lib/auth.js → src/main/resources/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var authLib = require('/lib/xp/auth');
import { hasRole } from '/lib/xp/auth';

var Roles = {
const Roles = {
ADMIN: 'system.admin',
EVERYONE: 'system.everyone',
AUTHENTICATED: 'system.authenticated',
Expand Down Expand Up @@ -31,7 +31,7 @@ function nullOrValue(value) {
* @param {string} params.key IdProvider key.
* @returns {object} the id provider specified, or null if it doesn't exist.
*/
exports.getIdProvider = function (params) {
export function getIdProvider(params) {
var bean = __.newBean('com.enonic.xp.app.users.lib.auth.GetIdProviderHandler');
bean.setIdProviderKey(required(params, 'key'));
return __.toNativeObject(bean.getIdProvider());
Expand All @@ -42,7 +42,7 @@ exports.getIdProvider = function (params) {
*
* @returns {object[]} Array of id providers.
*/
exports.getIdProviders = function () {
export function getIdProviders() {
var bean = __.newBean('com.enonic.xp.app.users.lib.auth.GetIdProvidersHandler');
return __.toNativeObject(bean.getIdProviders());
};
Expand All @@ -54,7 +54,7 @@ exports.getIdProviders = function () {
* @param {string} params.key Application key of the ID Provider application.
* @returns {string} The ID provider mode.
*/
exports.getIdProviderMode = function (params) {
export function getIdProviderMode(params) {
var bean = __.newBean('com.enonic.xp.app.users.lib.auth.GetIdProviderModeHandler');
bean.setApplicationKey(required(params, 'key'));
return bean.getIdProviderMode();
Expand All @@ -67,7 +67,7 @@ exports.getIdProviderMode = function (params) {
* @param {string} params.key Key of the id provider to fetch permissions for.
* @returns {object[]} Returns the list of principals with access level.
*/
exports.getPermissions = function (params) {
export function getPermissions(params) {
var bean = __.newBean('com.enonic.xp.app.users.lib.auth.GetPermissionsHandler');
bean.setIdProviderKey(required(params, 'key'));
return __.toNativeObject(bean.getPermissions());
Expand All @@ -78,7 +78,7 @@ exports.getPermissions = function (params) {
*
* @returns {object[]} Returns the list of principals with access level.
*/
exports.defaultPermissions = function () {
export function defaultPermissions() {
var bean = __.newBean('com.enonic.xp.app.users.lib.auth.DefaultPermissionsHandler');
return __.toNativeObject(bean.defaultPermissions());
};
Expand All @@ -92,7 +92,7 @@ exports.defaultPermissions = function () {
* @param {object} [params.idProviderConfig] ID Provider configuration.
* @param {object} [params.permissions] Id provider permissions.
*/
exports.createIdProvider = function (params) {
export function createIdProvider(params) {
var bean = __.newBean('com.enonic.xp.app.users.lib.auth.CreateIdProviderHandler');

bean.setName(required(params, 'name'));
Expand All @@ -113,7 +113,7 @@ exports.createIdProvider = function (params) {
* @param {object} [params.permissions] Id provider permissions.
* @returns {object} The updated id provider.
*/
exports.modifyIdProvider = function (params) {
export function modifyIdProvider(params) {
var bean = __.newBean('com.enonic.xp.app.users.lib.auth.ModifyIdProviderHandler');

bean.setIdProviderKey(required(params, 'key'));
Expand All @@ -130,12 +130,12 @@ exports.modifyIdProvider = function (params) {
* @param {string} params.keys Array of id provider keys to delete.
* @returns {object} the id providers specified, or null if it doesn't exist.
*/
exports.deleteIdProviders = function (params) {
export function deleteIdProviders(params) {
var bean = __.newBean('com.enonic.xp.app.users.lib.auth.DeleteIdProvidersHandler');
bean.setIdProviderKeys(__.toScriptValue(required(params, 'keys')));
return __.toNativeObject(bean.deleteIdProviders());
};

exports.isAdmin = function() {
return authLib.hasRole(Roles.ADMIN);
export function isAdmin() {
return hasRole(Roles.ADMIN);
};
106 changes: 52 additions & 54 deletions src/main/resources/lib/common.js → src/main/resources/lib/common.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
var nodeLib = require('/lib/xp/node');
var namePrettyfier = Java.type('com.enonic.xp.name.NamePrettyfier');
import { connect } from '/lib/xp/node';

var REPO_NAME = 'system-repo';
var REPO_BRANCH = 'master';
var MAX_COUNT = -1;
var SYSTEM_ADMIN = 'role:system.admin';
const namePrettyfier = Java.type('com.enonic.xp.name.NamePrettyfier');

var UserItemType = exports.UserItemType = {
const REPO_NAME = 'system-repo';
const REPO_BRANCH = 'master';
const MAX_COUNT = -1;
const SYSTEM_ADMIN = 'role:system.admin';

export const UserItemType = {
ROLE: 'ROLE',
USER: 'USER',
GROUP: 'GROUP',
ID_PROVIDER: 'ID_PROVIDER'
};
exports.UserItemType.all = function () {
return [
UserItemType.ROLE,
UserItemType.USER,
UserItemType.GROUP,
UserItemType.ID_PROVIDER
];
ID_PROVIDER: 'ID_PROVIDER',
all: () => {
return [
UserItemType.ROLE,
UserItemType.USER,
UserItemType.GROUP,
UserItemType.ID_PROVIDER
];
}
};

var PrincipalType = exports.PrincipalType = {
export const PrincipalType = {
ROLE: 'ROLE',
USER: 'USER',
GROUP: 'GROUP'
};
exports.PrincipalType.all = function () {
return [PrincipalType.ROLE, PrincipalType.USER, PrincipalType.GROUP];
GROUP: 'GROUP',
all: () => {
return [PrincipalType.ROLE, PrincipalType.USER, PrincipalType.GROUP];
}
};

exports.singleOrArray = function (value) {
export function singleOrArray(value) {
return value && value.length === 1 ? value[0] : value;
};

function isString(str) {
export function isString(str) {
return (typeof str === 'string') || (str instanceof String);
}

exports.isString = isString;

exports.refresh = function (repo) {
export function refresh(repo) {
newConnection(repo).refresh('SEARCH');
};

exports.required = function (params, name, skipTrimming) {
export function required(params, name, skipTrimming?: boolean) {
var value = params[name];
if (value === undefined || value === null) {
throw new Error("Parameter '" + name + "' is required");
Expand All @@ -55,23 +54,24 @@ exports.required = function (params, name, skipTrimming) {
return value;
};

exports.default = function (params, name, defaultValue) {
export default function(params, name, defaultValue) {
var value = params[name];
if (value === undefined || value === null) {
return defaultValue;
}
return value;
};

exports.getByIds = function (ids, repo) {
export function getByIds(ids, repo?: string) {
return newConnection(repo).get(ids);
};

exports.delete = function (ids, repo) {
function _delete(ids, repo) {
return newConnection(repo).delete(ids);
};
}
export { _delete as delete };

exports.keysToPaths = function (keys) {
export function keysToPaths(keys) {
return keys.map(function (key) {
if (isIdProvider(key)) {
return '/identity/' + idProviderFromKey(key);
Expand Down Expand Up @@ -99,27 +99,27 @@ exports.keysToPaths = function (keys) {
});
};

exports.isUser = function isUser(key) {
return exports.typeFromKey(key).toUpperCase() === PrincipalType.USER;
export function isUser(key) {
return typeFromKey(key).toUpperCase() === PrincipalType.USER;
};

exports.isGroup = function isGroup(key) {
return exports.typeFromKey(key).toUpperCase() === PrincipalType.GROUP;
export function isGroup(key) {
return typeFromKey(key).toUpperCase() === PrincipalType.GROUP;
};

exports.isRole = function isRole(key) {
return exports.typeFromKey(key).toUpperCase() === PrincipalType.ROLE;
export function isRole(key) {
return typeFromKey(key).toUpperCase() === PrincipalType.ROLE;
};

exports.isIdProvider = function isIdProvider(key) {
export function isIdProvider(key) {
return splitKey(key).length === 1;
};

exports.isSystemAdmin = function isSystemAdmin(key) {
export function isSystemAdmin(key) {
return key === SYSTEM_ADMIN;
}

exports.createQueryByField = function (field, values) {
export function createQueryByField(field, values) {
if (!values || !field) {
return null;
}
Expand All @@ -137,7 +137,7 @@ function serializeValue(value) {
return typeof value === 'string' ? '"' + value + '"' : value;
}

exports.extensionFromMimeType = function (mimeType) {
export function extensionFromMimeType (mimeType) {
var ext = '';
if (mimeType.indexOf('image/png') > -1) {
ext = '.png';
Expand Down Expand Up @@ -167,7 +167,7 @@ function splitKey(key) {
return parts;
}

exports.idProviderFromKey = function idProviderFromKey(key) {
export function idProviderFromKey(key) {
var parts = splitKey(key);
if (parts[0].toUpperCase() === PrincipalType.ROLE) {
throw new Error(
Expand All @@ -177,27 +177,27 @@ exports.idProviderFromKey = function idProviderFromKey(key) {
return parts.length === 1 ? parts[0] : parts[1];
};

exports.nameFromKey = function nameFromKey(key) {
export function nameFromKey(key) {
var parts = splitKey(key);
if (parts.length === 1) {
throw new Error("Key don't have name [" + key + ']');
}
return parts[0].toUpperCase() !== PrincipalType.ROLE ? parts[2] : parts[1];
};

exports.typeFromKey = function typeFromKey(key) {
export function typeFromKey(key) {
var parts = splitKey(key);
if (parts.length === 1) {
throw new Error("Key don't have type [" + key + ']');
}
return parts[0];
};

exports.prettifyName = function (text) {
export function prettifyName(text) {
return namePrettyfier.create(text);
};

exports.querySingle = function (query, repo) {
export function querySingle(query, repo) {
var results = queryAll({
start: 0,
count: 1,
Expand All @@ -207,15 +207,15 @@ exports.querySingle = function (query, repo) {
return results.total === 1 ? results.hits[0] : null;
};

exports.create = function (params, repo) {
export function create(params, repo) {
return newConnection(repo).create(params);
};

exports.update = function (params, repo) {
export function update(params, repo) {
return newConnection(repo).modify(params);
};

exports.queryAll = function queryAll(params, repo) {
export function queryAll(params, repo?: string) {
var start = params.start || 0;
var count = params.count == null ? MAX_COUNT : params.count;

Expand Down Expand Up @@ -245,11 +245,9 @@ exports.queryAll = function queryAll(params, repo) {
};
};

function newConnection(repo, branch) {
return nodeLib.connect({
export function newConnection(repo: string, branch?: string) {
return connect({
repoId: repo || REPO_NAME,
branch: branch || REPO_BRANCH
});
}

exports.newConnection = newConnection;
File renamed without changes.
Loading

0 comments on commit 1cdd22e

Please sign in to comment.