PermissionRequest.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  2. /*
  3. * Copyright 2019 Red Hat, Inc. and/or its affiliates.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import * as React from "../../../../common/keycloak/web_modules/react.js";
  18. import { Button, Modal, Text, Badge, DataListItem, DataList, TextVariants, DataListItemRow, DataListItemCells, DataListCell, Chip, Split, SplitItem } from "../../../../common/keycloak/web_modules/@patternfly/react-core.js";
  19. import { UserCheckIcon } from "../../../../common/keycloak/web_modules/@patternfly/react-icons.js";
  20. import { AccountServiceContext } from "../../account-service/AccountServiceContext.js";
  21. import { Msg } from "../../widgets/Msg.js";
  22. import { ContentAlert } from "../ContentAlert.js";
  23. export class PermissionRequest extends React.Component {
  24. constructor(props, context) {
  25. super(props);
  26. _defineProperty(this, "context", void 0);
  27. _defineProperty(this, "handleApprove", async (shareRequest, index) => {
  28. this.handle(shareRequest.username, shareRequest.scopes, true);
  29. this.props.resource.shareRequests.splice(index, 1);
  30. });
  31. _defineProperty(this, "handleDeny", async (shareRequest, index) => {
  32. this.handle(shareRequest.username, shareRequest.scopes);
  33. this.props.resource.shareRequests.splice(index, 1);
  34. });
  35. _defineProperty(this, "handle", async (username, scopes, approve = false) => {
  36. const id = this.props.resource._id;
  37. this.handleToggleDialog();
  38. const permissionsRequest = await this.context.doGet(`/resources/${id}/permissions`);
  39. const permissions = permissionsRequest.data || [];
  40. const foundPermission = permissions.find(p => p.username === username);
  41. const userScopes = foundPermission ? foundPermission.scopes : [];
  42. if (approve) {
  43. userScopes.push(...scopes);
  44. }
  45. try {
  46. await this.context.doPut(`/resources/${id}/permissions`, [{
  47. username: username,
  48. scopes: userScopes
  49. }]);
  50. ContentAlert.success(Msg.localize('shareSuccess'));
  51. this.props.onClose();
  52. } catch (e) {
  53. console.error('Could not update permissions', e.error);
  54. }
  55. });
  56. _defineProperty(this, "handleToggleDialog", () => {
  57. this.setState({
  58. isOpen: !this.state.isOpen
  59. });
  60. });
  61. this.context = context;
  62. this.state = {
  63. isOpen: false
  64. };
  65. }
  66. render() {
  67. const id = `shareRequest-${this.props.resource.name.replace(/\s/, '-')}`;
  68. return React.createElement(React.Fragment, null, React.createElement(Button, {
  69. id: id,
  70. variant: "link",
  71. onClick: this.handleToggleDialog
  72. }, React.createElement(UserCheckIcon, {
  73. size: "lg"
  74. }), React.createElement(Badge, null, this.props.resource.shareRequests.length)), React.createElement(Modal, {
  75. id: `modal-${id}`,
  76. title: Msg.localize('permissionRequests') + ' - ' + this.props.resource.name,
  77. isLarge: true,
  78. isOpen: this.state.isOpen,
  79. onClose: this.handleToggleDialog,
  80. actions: [React.createElement(Button, {
  81. id: `close-${id}`,
  82. key: "close",
  83. variant: "link",
  84. onClick: this.handleToggleDialog
  85. }, React.createElement(Msg, {
  86. msgKey: "close"
  87. }))]
  88. }, React.createElement(DataList, {
  89. "aria-label": Msg.localize('permissionRequests')
  90. }, React.createElement(DataListItemRow, null, React.createElement(DataListItemCells, {
  91. dataListCells: [React.createElement(DataListCell, {
  92. key: "permissions-name-header",
  93. width: 5
  94. }, React.createElement("strong", null, "Requestor")), React.createElement(DataListCell, {
  95. key: "permissions-requested-header",
  96. width: 5
  97. }, React.createElement("strong", null, React.createElement(Msg, {
  98. msgKey: "permissionRequests"
  99. }))), React.createElement(DataListCell, {
  100. key: "permission-request-header",
  101. width: 5
  102. })]
  103. })), this.props.resource.shareRequests.map((shareRequest, i) => React.createElement(DataListItem, {
  104. key: i,
  105. "aria-labelledby": "requestor"
  106. }, React.createElement(DataListItemRow, null, React.createElement(DataListItemCells, {
  107. dataListCells: [React.createElement(DataListCell, {
  108. id: `requestor${i}`,
  109. key: `requestor${i}`
  110. }, React.createElement("span", null, shareRequest.firstName, " ", shareRequest.lastName, " ", shareRequest.lastName ? '' : shareRequest.username), React.createElement("br", null), React.createElement(Text, {
  111. component: TextVariants.small
  112. }, shareRequest.email)), React.createElement(DataListCell, {
  113. id: `permissions${i}`,
  114. key: `permissions${i}`
  115. }, shareRequest.scopes.map((scope, j) => React.createElement(Chip, {
  116. key: j,
  117. isReadOnly: true
  118. }, scope))), React.createElement(DataListCell, {
  119. key: `actions${i}`
  120. }, React.createElement(Split, {
  121. gutter: "sm"
  122. }, React.createElement(SplitItem, null, React.createElement(Button, {
  123. id: `accept-${i}-${id}`,
  124. onClick: () => this.handleApprove(shareRequest, i)
  125. }, "Accept")), React.createElement(SplitItem, null, React.createElement(Button, {
  126. id: `deny-${i}-${id}`,
  127. variant: "danger",
  128. onClick: () => this.handleDeny(shareRequest, i)
  129. }, "Deny"))))]
  130. })))))));
  131. }
  132. }
  133. _defineProperty(PermissionRequest, "defaultProps", {
  134. permissions: [],
  135. row: 0
  136. });
  137. _defineProperty(PermissionRequest, "contextType", AccountServiceContext);
  138. //# sourceMappingURL=PermissionRequest.js.map