tycho-components 0.17.2 → 0.18.0

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.
@@ -33,10 +33,14 @@ export default function HeaderApps({ navigateLogout, navigateNotLogged, hideKeyb
33
33
  return null;
34
34
  if (item.admin && !isAdminOfAnyCorpus())
35
35
  return null;
36
- if (item.lexicon && !hasLexiconAccess())
36
+ if (item.lexicon &&
37
+ (!hasCorpus() || !hasLexiconAccess(getCorpus().uid))) {
37
38
  return null;
38
- if (item.parser && !hasParserAccess())
39
+ }
40
+ if (item.parser &&
41
+ (!hasCorpus() || !hasParserAccess(getCorpus().uid))) {
39
42
  return null;
43
+ }
40
44
  return (_jsxs("div", { className: "item", title: t(`${item.code}.desc`), onClick: () => goto(item, item.external ? true : false), children: [item.icon && _jsx(Icon, { name: item.icon, size: "medium" }), item.image && _jsx("img", { src: item.image }), _jsx("span", { className: "title-app", children: t(`${item.code}.name`) }), _jsxs("div", { className: "options", children: [item.visibility && (_jsx(Tag, { text: t(`common:label.${item.visibility}`), size: "small", color: "green", className: "d-none" })), _jsx(IconButton, { name: "open_in_new", size: "x-small", className: "icon-open", mode: "ghost", onClick: (e) => {
41
45
  e.stopPropagation();
42
46
  goto(item, true);
@@ -5,6 +5,10 @@ export declare const useLoggedUtils: () => {
5
5
  isLogged: () => boolean;
6
6
  setLogged: (user: User | undefined) => void;
7
7
  isAdminOfAnyCorpus: () => boolean;
8
- hasLexiconAccess: () => boolean;
9
- hasParserAccess: () => boolean;
8
+ hasLexiconAccess: (lexiconUid: string, roles?: string[]) => boolean;
9
+ hasParserAccess: (parserUid: string, roles?: string[]) => boolean;
10
+ hasCorpusAccess: (corpusUid: string, roles?: string[]) => boolean;
11
+ hasAnyParserAccess: () => boolean;
12
+ hasAnyLexiconAccess: () => boolean;
13
+ hasAnyCorpusAccess: () => boolean;
10
14
  };
@@ -2,6 +2,15 @@ import { useContext } from 'react';
2
2
  import CommonContext from './CommonContext';
3
3
  import { logged } from './store/actions';
4
4
  import { UserStatus } from './types/User';
5
+ function escapeRegex(s) {
6
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
7
+ }
8
+ function roleSegmentForRegex(roles) {
9
+ if (roles && roles.length > 0) {
10
+ return `(${roles.map(escapeRegex).join('|')})`;
11
+ }
12
+ return '(.*)';
13
+ }
5
14
  export const useLoggedUtils = () => {
6
15
  const { state, dispatch } = useContext(CommonContext);
7
16
  const getLogged = () => {
@@ -24,7 +33,37 @@ export const useLoggedUtils = () => {
24
33
  return false;
25
34
  return user.permissions.some((permission) => /^ROLE_ADMIN_CORPUS_[\w-]+$/.test(permission));
26
35
  };
27
- const hasParserAccess = () => {
36
+ const hasParserAccess = (parserUid, roles) => {
37
+ if (isSuper())
38
+ return true;
39
+ const user = getLogged();
40
+ if (!user || !user.permissions)
41
+ return false;
42
+ const id = escapeRegex(parserUid);
43
+ const re = new RegExp(`^ROLE_${roleSegmentForRegex(roles)}_PARSER_${id}$`);
44
+ return user.permissions.some((permission) => re.test(permission));
45
+ };
46
+ const hasLexiconAccess = (lexiconUid, roles) => {
47
+ if (isSuper())
48
+ return true;
49
+ const user = getLogged();
50
+ if (!user || !user.permissions)
51
+ return false;
52
+ const id = escapeRegex(lexiconUid);
53
+ const re = new RegExp(`^ROLE_${roleSegmentForRegex(roles)}_LEXICON_${id}$`);
54
+ return user.permissions.some((permission) => re.test(permission));
55
+ };
56
+ const hasCorpusAccess = (corpusUid, roles) => {
57
+ if (isSuper())
58
+ return true;
59
+ const user = getLogged();
60
+ if (!user || !user.permissions)
61
+ return false;
62
+ const id = escapeRegex(corpusUid);
63
+ const re = new RegExp(`^ROLE_${roleSegmentForRegex(roles)}_CORPUS_${id}$`);
64
+ return user.permissions.some((permission) => re.test(permission));
65
+ };
66
+ const hasAnyParserAccess = () => {
28
67
  if (isSuper())
29
68
  return true;
30
69
  const user = getLogged();
@@ -32,7 +71,7 @@ export const useLoggedUtils = () => {
32
71
  return false;
33
72
  return user.permissions.some((permission) => /^ROLE_(.*)_PARSER_[\w-]+$/.test(permission));
34
73
  };
35
- const hasLexiconAccess = () => {
74
+ const hasAnyLexiconAccess = () => {
36
75
  if (isSuper())
37
76
  return true;
38
77
  const user = getLogged();
@@ -40,6 +79,14 @@ export const useLoggedUtils = () => {
40
79
  return false;
41
80
  return user.permissions.some((permission) => /^ROLE_(.*)_LEXICON_[\w-]+$/.test(permission));
42
81
  };
82
+ const hasAnyCorpusAccess = () => {
83
+ if (isSuper())
84
+ return true;
85
+ const user = getLogged();
86
+ if (!user || !user.permissions)
87
+ return false;
88
+ return user.permissions.some((permission) => /^ROLE_(.*)_CORPUS_[\w-]+$/.test(permission));
89
+ };
43
90
  return {
44
91
  getLogged,
45
92
  isSuper,
@@ -48,5 +95,9 @@ export const useLoggedUtils = () => {
48
95
  isAdminOfAnyCorpus,
49
96
  hasLexiconAccess,
50
97
  hasParserAccess,
98
+ hasCorpusAccess,
99
+ hasAnyParserAccess,
100
+ hasAnyLexiconAccess,
101
+ hasAnyCorpusAccess,
51
102
  };
52
103
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tycho-components",
3
3
  "private": false,
4
- "version": "0.17.2",
4
+ "version": "0.18.0",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {