@teipublisher/pb-components 2.16.0 → 2.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.
package/src/pb-login.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { LitElement, html, css } from 'lit-element';
2
2
  import { pbMixin, waitOnce } from './pb-mixin.js';
3
3
  import { translate } from "./pb-i18n.js";
4
+ import { registry } from "./urls.js";
4
5
  import '@polymer/iron-ajax';
5
6
  import '@polymer/paper-dialog';
6
7
  import '@polymer/paper-dialog-scrollable';
@@ -250,6 +251,15 @@ export class PbLogin extends pbMixin(LitElement) {
250
251
  }
251
252
  }
252
253
  this.emitTo('pb-login', resp);
254
+
255
+ if (this.loggedIn) {
256
+ registry.currentUser = {
257
+ user: this.user,
258
+ groups: this.groups
259
+ }
260
+ } else {
261
+ registry.currentUser = null;
262
+ }
253
263
  }
254
264
 
255
265
  _handleError() {
@@ -266,6 +276,7 @@ export class PbLogin extends pbMixin(LitElement) {
266
276
  this._loginDialog.open();
267
277
  }
268
278
 
279
+ registry.currentUser = null;
269
280
  this.emitTo('pb-login', resp);
270
281
  }
271
282
 
@@ -1,6 +1,41 @@
1
1
  import { LitElement, html, css } from 'lit-element';
2
2
  import { pbMixin } from './pb-mixin.js';
3
+ import { registry } from './urls.js';
3
4
 
5
+ /**
6
+ *
7
+ * @param {Array<String>} arr array containg string values (name of groups)
8
+ * @param {String} val value to check if it's in the array
9
+ * @returns true if the checked values is in the array
10
+ */
11
+ function _isItemInArray(arr, val) {
12
+ return arr.some((arrVal) => val === arrVal);
13
+ }
14
+
15
+ /**
16
+ *
17
+ * @param {object} user user name returned by login function;
18
+ * @param {object} groups contains groups (an array) the logged user is a member of
19
+ * @param {string} targetGroups string containing (optionally) space separated list of target groups
20
+ * @returns true if user is member of one of the target groups
21
+ */
22
+ export function isLoggedIn(user, groups, targetGroups) {
23
+ if (user == null) {
24
+ return false;
25
+ }
26
+ if (targetGroups) {
27
+ if (!groups) {
28
+ return false;
29
+ }
30
+ const groupArray = targetGroups.split(/[\s+,]+/);
31
+ let exists = false;
32
+ groupArray.forEach(async (oneItem) => {
33
+ exists = _isItemInArray(groups, oneItem) || exists;
34
+ });
35
+ return exists;
36
+ }
37
+ return true;
38
+ }
4
39
 
5
40
  /**
6
41
  * Show content if the user is logged in. Optionally requires the user
@@ -18,7 +53,9 @@ export class PbRestricted extends pbMixin(LitElement) {
18
53
  static get properties() {
19
54
  return {
20
55
  ...super.properties,
21
- /** Id of the pb-login element to connect to */
56
+ /** Id of the pb-login element to connect to
57
+ * @deprecated no longer used
58
+ */
22
59
  login: {
23
60
  type: String
24
61
  },
@@ -50,15 +87,10 @@ export class PbRestricted extends pbMixin(LitElement) {
50
87
  this.classList.add('fallback');
51
88
  }
52
89
 
53
- const login = document.getElementById(this.login);
54
- if (!login) {
55
- console.error('<pb-restricted> connected pb-login element not found!');
56
- return;
57
- }
58
90
  this.subscribeTo('pb-login', (ev) => {
59
91
  this.show = this._loggedIn(ev.detail.user, ev.detail.groups);
60
92
  }, []);
61
- this.show = login.loggedIn && this._loggedIn(login.user, login.groups);
93
+ this.show = registry.currentUser && this._loggedIn(registry.currentUser.user, registry.currentUser.groups);
62
94
  }
63
95
 
64
96
  render() {
@@ -74,43 +106,13 @@ export class PbRestricted extends pbMixin(LitElement) {
74
106
  }
75
107
 
76
108
  :host(.fallback), :host([show]) {
77
- display: block;
109
+ display: inherit;
78
110
  }
79
111
  `;
80
112
  }
81
113
 
82
- /**
83
- *
84
- * @param {Array<String>} arr array containg string values (name of groups)
85
- * @param {String} val value to check if it's in the array
86
- * @returns true if the checked values is in the array
87
- */
88
- _isItemInArray(arr, val) {
89
- return arr.some((arrVal) => val === arrVal);
90
- }
91
-
92
- /**
93
- *
94
- * @param {object} user user name returned by login function;
95
- * @param {object} groups contains groups (an array) the logged user is a member of
96
- * @returns true if user is member of one of defined groups
97
- */
98
114
  _loggedIn(user, groups) {
99
- if (user == null) {
100
- return false;
101
- }
102
- if (this.group) {
103
- if (!groups) {
104
- return false;
105
- }
106
- let groupArray = this.group.split(/[\s+,]+/);
107
- let exists = false;
108
- groupArray.forEach(async (oneItem) => {
109
- exists = this._isItemInArray(groups, oneItem) || exists;
110
- });
111
- return exists;
112
- }
113
- return true;
115
+ return isLoggedIn(user, groups, this.group);
114
116
  }
115
117
  }
116
118
  customElements.define('pb-restricted', PbRestricted);
package/src/urls.js CHANGED
@@ -69,6 +69,12 @@ class Registry {
69
69
 
70
70
  this.urlIgnore = new Set();
71
71
  this.pathParams = new Set();
72
+
73
+ /**
74
+ * Information about the user currently logged in or null. If a user is logged in
75
+ * this will be an object with two properties: `user` and `groups`.
76
+ */
77
+ this.currentUser = null;
72
78
  }
73
79
 
74
80
  configure(usePath = true, idHash = false, rootPath = '', urlPattern, ignoredParams) {