@webiny/app-tenancy 0.0.0-mt-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @webiny/app-security-tenancy
2
+
3
+ [![](https://img.shields.io/npm/dw/@webiny/app-security-tenancy.svg)](https://www.npmjs.com/package/@webiny/app-security-tenancy)
4
+ [![](https://img.shields.io/npm/v/@webiny/app-security-tenancy.svg)](https://www.npmjs.com/package/@webiny/app-security-tenancy)
5
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
6
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
7
+
8
+ Adds a Security module in the Admin area. Contains the following
9
+ submodules:
10
+
11
+ - Users
12
+ - Groups
13
+ - Roles
14
+
15
+ Use together with [@webiny/api-security-user-management](../api-security-user-management) package.
@@ -0,0 +1,12 @@
1
+ import React from "react";
2
+ export declare const TenancyContext: React.Context<any>;
3
+ export interface Tenant {
4
+ id: string;
5
+ name: string;
6
+ }
7
+ export declare type TenancyContextValue = {
8
+ tenant: string | null;
9
+ setTenant(tenant: string): void;
10
+ isMultiTenant: boolean;
11
+ };
12
+ export declare const TenancyProvider: (props: any) => JSX.Element;
@@ -0,0 +1,55 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import React, { useMemo, useCallback, Fragment, useState } from "react";
3
+ import { default as localStorage } from "store";
4
+ import { plugins } from "@webiny/plugins";
5
+ import { TenantHeaderLinkPlugin } from "@webiny/app/plugins/TenantHeaderLinkPlugin";
6
+ export var TenancyContext = /*#__PURE__*/React.createContext(null);
7
+ var LOCAL_STORAGE_KEY = "webiny_tenant";
8
+
9
+ function loadState() {
10
+ return localStorage.get(LOCAL_STORAGE_KEY) || null;
11
+ }
12
+
13
+ function storeState(state) {
14
+ localStorage.set(LOCAL_STORAGE_KEY, state);
15
+ }
16
+
17
+ var getInitialTenant = function getInitialTenant() {
18
+ var currentTenant = loadState();
19
+ plugins.register(new TenantHeaderLinkPlugin(currentTenant || "root"));
20
+ return currentTenant;
21
+ };
22
+
23
+ export var TenancyProvider = function TenancyProvider(props) {
24
+ var _useState = useState(getInitialTenant),
25
+ _useState2 = _slicedToArray(_useState, 2),
26
+ currentTenant = _useState2[0],
27
+ setTenant = _useState2[1];
28
+
29
+ var changeTenant = useCallback(function (tenant) {
30
+ if (!tenant) {
31
+ localStorage.remove(LOCAL_STORAGE_KEY);
32
+ window.location.pathname = "/";
33
+ }
34
+
35
+ if (!currentTenant) {
36
+ plugins.register(new TenantHeaderLinkPlugin(tenant));
37
+ setTenant(tenant);
38
+ storeState(tenant);
39
+ return;
40
+ }
41
+
42
+ storeState(tenant);
43
+ window.location.pathname = "/";
44
+ }, [currentTenant]);
45
+ var value = useMemo(function () {
46
+ return {
47
+ tenant: currentTenant,
48
+ setTenant: changeTenant,
49
+ isMultiTenant: process.env.REACT_APP_WEBINY_MULTI_TENANCY === "true"
50
+ };
51
+ }, [currentTenant]);
52
+ return /*#__PURE__*/React.createElement(TenancyContext.Provider, {
53
+ value: value
54
+ }, /*#__PURE__*/React.createElement(Fragment, null, props.children));
55
+ };
@@ -0,0 +1,2 @@
1
+ import { TenancyContextValue } from "../contexts/Tenancy";
2
+ export declare function useTenancy(): TenancyContextValue;
@@ -0,0 +1,5 @@
1
+ import { useContext } from "react";
2
+ import { TenancyContext } from "../contexts/Tenancy";
3
+ export function useTenancy() {
4
+ return useContext(TenancyContext);
5
+ }
package/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./contexts/Tenancy";
2
+ export * from "./hooks/useTenancy";
3
+ export * from "./withTenant";
4
+ export declare const plugins: () => import("@webiny/app-admin/types").AdminInstallationPlugin[];
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ import installation from "./plugins/installation";
2
+ export * from "./contexts/Tenancy";
3
+ export * from "./hooks/useTenancy";
4
+ export * from "./withTenant";
5
+ export var plugins = function plugins() {
6
+ return [installation];
7
+ };
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@webiny/app-tenancy",
3
+ "version": "0.0.0-mt-1",
4
+ "main": "index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/webiny/webiny-js.git"
8
+ },
9
+ "contributors": [
10
+ "Pavel Denisjuk <pavel@webiny.com>",
11
+ "Sven Al Hamad <sven@webiny.com>",
12
+ "Adrian Smijulj <adrian@webiny.com>"
13
+ ],
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "@apollo/react-hooks": "3.1.5",
17
+ "@babel/runtime": "7.15.4",
18
+ "@emotion/styled": "10.0.27",
19
+ "@types/react": "16.14.2",
20
+ "@webiny/app": "0.0.0-mt-1",
21
+ "@webiny/app-admin": "0.0.0-mt-1",
22
+ "@webiny/plugins": "0.0.0-mt-1",
23
+ "@webiny/ui": "0.0.0-mt-1",
24
+ "graphql-tag": "2.12.5",
25
+ "react": "16.14.0",
26
+ "react-dom": "16.14.0",
27
+ "store": "2.0.12"
28
+ },
29
+ "devDependencies": {
30
+ "@babel/cli": "^7.5.5",
31
+ "@babel/core": "^7.5.5",
32
+ "@babel/plugin-proposal-class-properties": "^7.5.5",
33
+ "@babel/preset-env": "^7.5.5",
34
+ "@babel/preset-react": "^7.0.0",
35
+ "@babel/preset-typescript": "^7.8.3",
36
+ "@webiny/cli": "^0.0.0-mt-1",
37
+ "@webiny/project-utils": "^0.0.0-mt-1",
38
+ "babel-plugin-emotion": "^9.2.8",
39
+ "babel-plugin-lodash": "^3.3.4",
40
+ "babel-plugin-named-asset-import": "^1.0.0-next.3e165448",
41
+ "rimraf": "^3.0.2",
42
+ "ttypescript": "^1.3.2",
43
+ "typescript": "^4.1.3"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public",
47
+ "directory": "dist"
48
+ },
49
+ "scripts": {
50
+ "build": "yarn webiny run build",
51
+ "watch": "yarn webiny run watch"
52
+ },
53
+ "svgo": {
54
+ "plugins": {
55
+ "removeViewBox": false
56
+ }
57
+ },
58
+ "gitHead": "37736d8456a6ecb342a6c3645060bd9a3f2d4bb0"
59
+ }
@@ -0,0 +1,3 @@
1
+ import { AdminInstallationPlugin } from "@webiny/app-admin/types";
2
+ declare const plugin: AdminInstallationPlugin;
3
+ export default plugin;
@@ -0,0 +1,99 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
+ import _taggedTemplateLiteral from "@babel/runtime/helpers/taggedTemplateLiteral";
4
+
5
+ var _templateObject, _templateObject2;
6
+
7
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
8
+ import React, { useState, useEffect } from "react";
9
+ import gql from "graphql-tag";
10
+ import { useApolloClient } from "@apollo/react-hooks";
11
+ import { Alert } from "@webiny/ui/Alert";
12
+ import { CircularProgress } from "@webiny/ui/Progress";
13
+ import { SimpleForm, SimpleFormContent } from "@webiny/app-admin/components/SimpleForm";
14
+ import styled from "@emotion/styled";
15
+ var SimpleFormPlaceholder = /*#__PURE__*/styled("div", {
16
+ target: "ewob5260",
17
+ label: "SimpleFormPlaceholder"
18
+ })({
19
+ minHeight: 300,
20
+ minWidth: 400
21
+ });
22
+ var IS_INSTALLED = gql(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n query IsTenancyInstalled {\n tenancy {\n version\n }\n }\n"])));
23
+ var INSTALL = gql(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n mutation InstallTenancy {\n tenancy {\n install {\n data\n error {\n code\n message\n }\n }\n }\n }\n"])));
24
+
25
+ var TenancyInstaller = function TenancyInstaller(_ref) {
26
+ var onInstalled = _ref.onInstalled;
27
+ var client = useApolloClient();
28
+
29
+ var _useState = useState(null),
30
+ _useState2 = _slicedToArray(_useState, 2),
31
+ error = _useState2[0],
32
+ setError = _useState2[1];
33
+
34
+ useEffect(function () {
35
+ client.mutate({
36
+ mutation: INSTALL
37
+ }).then(function (_ref2) {
38
+ var data = _ref2.data;
39
+ var error = data.tenancy.install.error;
40
+
41
+ if (error) {
42
+ setError(error.message);
43
+ return;
44
+ } // Just so the user sees the actual message.
45
+
46
+
47
+ setTimeout(onInstalled, 3000);
48
+ });
49
+ }, []);
50
+ var label = error ? /*#__PURE__*/React.createElement(Alert, {
51
+ title: "Something went wrong",
52
+ type: "danger"
53
+ }, error) : "Installing Tenancy...";
54
+ return /*#__PURE__*/React.createElement(SimpleForm, null, /*#__PURE__*/React.createElement(CircularProgress, {
55
+ label: label
56
+ }), /*#__PURE__*/React.createElement(SimpleFormContent, null, /*#__PURE__*/React.createElement(SimpleFormPlaceholder, null)));
57
+ };
58
+
59
+ var plugin = {
60
+ name: "admin-installation-tenancy",
61
+ type: "admin-installation",
62
+ title: "Tenancy",
63
+ dependencies: [],
64
+ secure: false,
65
+ getInstalledVersion: function getInstalledVersion(_ref3) {
66
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
67
+ var client, _yield$client$query, data;
68
+
69
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
70
+ while (1) {
71
+ switch (_context.prev = _context.next) {
72
+ case 0:
73
+ client = _ref3.client;
74
+ _context.next = 3;
75
+ return client.query({
76
+ query: IS_INSTALLED
77
+ });
78
+
79
+ case 3:
80
+ _yield$client$query = _context.sent;
81
+ data = _yield$client$query.data;
82
+ return _context.abrupt("return", data.tenancy.version);
83
+
84
+ case 6:
85
+ case "end":
86
+ return _context.stop();
87
+ }
88
+ }
89
+ }, _callee);
90
+ }))();
91
+ },
92
+ render: function render(_ref4) {
93
+ var onInstalled = _ref4.onInstalled;
94
+ return /*#__PURE__*/React.createElement(TenancyInstaller, {
95
+ onInstalled: onInstalled
96
+ });
97
+ }
98
+ };
99
+ export default plugin;
@@ -0,0 +1,6 @@
1
+ /// <reference types="react" />
2
+ export declare const GET_DEFAULT_TENANT: import("graphql").DocumentNode;
3
+ export declare const withTenant: (Component: any) => ({ getIdentityData, children }: {
4
+ getIdentityData: any;
5
+ children: any;
6
+ }) => JSX.Element;
package/withTenant.js ADDED
@@ -0,0 +1,71 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _taggedTemplateLiteral from "@babel/runtime/helpers/taggedTemplateLiteral";
3
+
4
+ var _templateObject;
5
+
6
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
7
+ import React from "react";
8
+ import { gql } from "graphql-tag";
9
+ import { useTenancy } from "./hooks/useTenancy";
10
+ export var GET_DEFAULT_TENANT = gql(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n query GetDefaultTenant {\n tenancy {\n getDefaultTenant {\n data {\n id\n name\n description\n }\n error {\n code\n message\n }\n }\n }\n }\n"])));
11
+ export var withTenant = function withTenant(Component) {
12
+ var WithTenant = function WithTenant(_ref) {
13
+ var getIdentityData = _ref.getIdentityData,
14
+ children = _ref.children;
15
+
16
+ var _useTenancy = useTenancy(),
17
+ tenant = _useTenancy.tenant,
18
+ setTenant = _useTenancy.setTenant,
19
+ isMultiTenant = _useTenancy.isMultiTenant;
20
+
21
+ var getIdentityWithTenant = /*#__PURE__*/function () {
22
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
23
+ var response, defaultTenantId;
24
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
25
+ while (1) {
26
+ switch (_context.prev = _context.next) {
27
+ case 0:
28
+ if (!(tenant || !isMultiTenant)) {
29
+ _context.next = 2;
30
+ break;
31
+ }
32
+
33
+ return _context.abrupt("return", getIdentityData(params));
34
+
35
+ case 2:
36
+ _context.next = 4;
37
+ return params.client.query({
38
+ query: GET_DEFAULT_TENANT,
39
+ context: {
40
+ headers: {
41
+ "x-tenant": tenant || "root"
42
+ }
43
+ }
44
+ });
45
+
46
+ case 4:
47
+ response = _context.sent;
48
+ defaultTenantId = response.data.tenancy.getDefaultTenant.data.id;
49
+ setTenant(defaultTenantId);
50
+ return _context.abrupt("return", getIdentityData(params));
51
+
52
+ case 8:
53
+ case "end":
54
+ return _context.stop();
55
+ }
56
+ }
57
+ }, _callee);
58
+ }));
59
+
60
+ return function getIdentityWithTenant(_x) {
61
+ return _ref2.apply(this, arguments);
62
+ };
63
+ }();
64
+
65
+ return /*#__PURE__*/React.createElement(Component, {
66
+ getIdentityData: getIdentityWithTenant
67
+ }, children);
68
+ };
69
+
70
+ return WithTenant;
71
+ };