helpdesk-search-base.component.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://www.pwm-project.org
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2021 The PWM Project
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. import SearchResult from '../../models/search-result.model';
  21. import {isArray, isString, IPromise, IQService, IScope, ITimeoutService} from 'angular';
  22. import {IPerson} from '../../models/person.model';
  23. import {IHelpDeskConfigService} from '../../services/helpdesk-config.service';
  24. import LocalStorageService from '../../services/local-storage.service';
  25. import PromiseService from '../../services/promise.service';
  26. import {IHelpDeskService} from '../../services/helpdesk.service';
  27. import IPwmService from '../../services/pwm.service';
  28. import {IAdvancedSearchConfig, IAdvancedSearchQuery, IAttributeMetadata} from '../../services/base-config.service';
  29. import CommonSearchService from '../../services/common-search.service';
  30. let verificationsDialogTemplateUrl = require('./verifications-dialog.template.html');
  31. let recentVerificationsDialogTemplateUrl = require('./recent-verifications-dialog.template.html');
  32. export default abstract class HelpDeskSearchBaseComponent {
  33. advancedSearch = false;
  34. advancedSearchTags = {};
  35. advancedSearchEnabled: boolean;
  36. advancedSearchMaxRows: number;
  37. columnConfiguration: any;
  38. errorMessage: string;
  39. inputDebounce: number;
  40. protected pendingRequests: IPromise<any>[] = [];
  41. photosEnabled: boolean;
  42. query: string;
  43. queries: IAdvancedSearchQuery[];
  44. searchMessage: string;
  45. searchResult: SearchResult;
  46. searchTextLocalStorageKey: string;
  47. searchViewLocalStorageKey: string;
  48. verificationsEnabled: boolean;
  49. constructor(protected $q: IQService,
  50. protected $scope: IScope,
  51. protected $state: angular.ui.IStateService,
  52. protected $stateParams: angular.ui.IStateParamsService,
  53. protected $timeout: ITimeoutService,
  54. protected $translate: angular.translate.ITranslateService,
  55. protected configService: IHelpDeskConfigService,
  56. protected helpDeskService: IHelpDeskService,
  57. protected IasDialogService: any,
  58. protected localStorageService: LocalStorageService,
  59. protected promiseService: PromiseService,
  60. protected pwmService: IPwmService,
  61. protected commonSearchService: CommonSearchService) {
  62. this.searchTextLocalStorageKey = this.localStorageService.keys.HELPDESK_SEARCH_TEXT;
  63. this.searchViewLocalStorageKey = this.localStorageService.keys.HELPDESK_SEARCH_VIEW;
  64. this.inputDebounce = this.pwmService.ajaxTypingWait;
  65. }
  66. protected initialize(): IPromise<void> {
  67. return this.$q.all(
  68. [
  69. this.configService.verificationsEnabled().then((verificationsEnabled: boolean) => {
  70. this.verificationsEnabled = verificationsEnabled;
  71. }),
  72. this.configService.advancedSearchConfig().then((advancedSearchConfig: IAdvancedSearchConfig) => {
  73. this.advancedSearchEnabled = advancedSearchConfig.enabled;
  74. this.advancedSearchMaxRows = advancedSearchConfig.maxRows;
  75. for (let advancedSearchTag of advancedSearchConfig.attributes) {
  76. this.advancedSearchTags[advancedSearchTag.attribute] = advancedSearchTag;
  77. }
  78. })
  79. ]
  80. ).then(result => {
  81. const searchQuery = this.getSearchQuery();
  82. if (searchQuery) {
  83. // A search query has been passed in, disregard the current search state
  84. this.query = searchQuery;
  85. this.advancedSearch = false;
  86. this.storeSearchText();
  87. this.commonSearchService.setHdAdvancedSearchActive(this.advancedSearch);
  88. this.commonSearchService.setHdAdvSearchQueries([]);
  89. } else {
  90. this.query = this.getSearchText();
  91. this.advancedSearch = this.commonSearchService.isHdAdvancedSearchActive();
  92. this.queries = this.commonSearchService.getHdAdvSearchQueries();
  93. if (this.queries.length === 0) {
  94. this.addSearchTag();
  95. }
  96. }
  97. // Once <ias-search-box> from ng-ias allows the autofocus attribute, we can remove this code
  98. this.$timeout(() => {
  99. document.getElementsByTagName('input')[0].focus();
  100. });
  101. this.$scope.$watch('$ctrl.query', (newValue: string, oldValue: string) => {
  102. this.onSearchTextChange(newValue, oldValue);
  103. });
  104. });
  105. }
  106. getMessage(): string {
  107. return this.errorMessage || this.searchMessage;
  108. }
  109. private getSearchQuery(): string {
  110. let param: string = this.$stateParams['query'];
  111. // If multiple query parameters are defined, use the first one
  112. if (isArray(param)) {
  113. param = param[0].trim();
  114. }
  115. else if (isString(param)) {
  116. param = param.trim();
  117. }
  118. return param;
  119. }
  120. private getSearchText(): string {
  121. return this.localStorageService.getItem(this.searchTextLocalStorageKey);
  122. }
  123. abstract fetchData(): void;
  124. protected clearSearch(): void {
  125. this.query = null;
  126. this.queries = [];
  127. this.searchResult = null;
  128. this.clearErrorMessage();
  129. this.clearSearchMessage();
  130. this.abortPendingRequests();
  131. }
  132. protected fetchSearchData(): IPromise<void | SearchResult> {
  133. this.abortPendingRequests();
  134. this.searchResult = null;
  135. let promise;
  136. if (this.advancedSearch) {
  137. if (!this.queries || (this.queries.length === 1 && !this.queries[0].key)) {
  138. this.clearSearch();
  139. return null;
  140. }
  141. const keys = new Set();
  142. for (let searchQuery of this.queries) {
  143. keys.add(searchQuery.key);
  144. }
  145. const duplicateSearchAttrsFound = keys.size < this.queries.length;
  146. if (duplicateSearchAttrsFound) {
  147. this.$translate('Display_SearchAttrsUnique')
  148. .then((translation: string) => {
  149. this.searchMessage = translation;
  150. });
  151. return null;
  152. }
  153. promise = this.helpDeskService.advancedSearch(this.queries);
  154. }
  155. else {
  156. if (!this.query) {
  157. this.clearSearch();
  158. return null;
  159. }
  160. promise = this.helpDeskService.search(this.query);
  161. }
  162. this.pendingRequests.push(promise);
  163. return promise
  164. .then(
  165. function(searchResult: SearchResult) {
  166. this.clearErrorMessage();
  167. this.clearSearchMessage();
  168. // Aborted request
  169. if (!searchResult) {
  170. return;
  171. }
  172. // Too many results returned
  173. if (searchResult.sizeExceeded) {
  174. this.setSearchMessage('Display_SearchResultsExceeded');
  175. }
  176. // No results returned. Not an else if statement so that the more important message is presented
  177. if (!searchResult.people.length) {
  178. this.setSearchMessage('Display_SearchResultsNone');
  179. }
  180. return searchResult;
  181. }.bind(this),
  182. function(error) {
  183. this.setErrorMessage(error);
  184. this.clearSearchMessage();
  185. }.bind(this))
  186. .finally(function() {
  187. this.removePendingRequest(promise);
  188. }.bind(this));
  189. }
  190. private gotoState(state: string): void {
  191. this.$state.go(state);
  192. }
  193. private initiateSearch() {
  194. this.clearSearchMessage();
  195. this.clearErrorMessage();
  196. this.fetchData();
  197. }
  198. private onSearchTextChange(newValue: string, oldValue: string): void {
  199. if (newValue === oldValue) {
  200. return;
  201. }
  202. this.storeSearchText();
  203. this.initiateSearch();
  204. }
  205. protected abortPendingRequests() {
  206. for (let index = 0; index < this.pendingRequests.length; index++) {
  207. let pendingRequest = this.pendingRequests[index];
  208. this.promiseService.abort(pendingRequest);
  209. }
  210. this.pendingRequests = [];
  211. }
  212. protected setErrorMessage(message: string) {
  213. this.errorMessage = message;
  214. }
  215. protected clearErrorMessage() {
  216. this.errorMessage = null;
  217. }
  218. // If message is a string it will be translated. If it is a promise it will assign the string from the resolved
  219. // promise
  220. protected setSearchMessage(translationKey: string) {
  221. if (!translationKey) {
  222. this.clearSearchMessage();
  223. return;
  224. }
  225. const self = this;
  226. this.$translate(translationKey.toString())
  227. .then((translation: string) => {
  228. self.searchMessage = translation;
  229. });
  230. }
  231. protected clearSearchMessage(): void {
  232. this.searchMessage = null;
  233. }
  234. protected removePendingRequest(promise: IPromise<any>) {
  235. let index = this.pendingRequests.indexOf(promise);
  236. if (index > -1) {
  237. this.pendingRequests.splice(index, 1);
  238. }
  239. }
  240. private onAdvancedSearchAttributeChanged(query: IAdvancedSearchQuery) {
  241. // Make sure we set the default value if the type is select
  242. const attributeMetadata: IAttributeMetadata = this.advancedSearchTags[query.key];
  243. if (attributeMetadata.type == 'select') {
  244. query.value = this.commonSearchService.getDefaultValue(attributeMetadata);
  245. }
  246. this.commonSearchService.setHdAdvSearchQueries(this.queries);
  247. this.initiateSearch();
  248. }
  249. private onAdvancedSearchAttributeValueChanged() {
  250. this.commonSearchService.setHdAdvSearchQueries(this.queries);
  251. this.initiateSearch();
  252. }
  253. private onAdvancedSearchValueChanged() {
  254. this.commonSearchService.setHdAdvSearchQueries(this.queries);
  255. this.initiateSearch();
  256. }
  257. removeSearchTag(tagIndex: number): void {
  258. this.queries.splice(tagIndex, 1);
  259. this.commonSearchService.setHdAdvSearchQueries(this.queries);
  260. if (this.queries.length > 0) {
  261. this.initiateSearch();
  262. }
  263. else {
  264. this.clearSearch();
  265. this.advancedSearch = false;
  266. this.commonSearchService.setHdAdvancedSearchActive(this.advancedSearch);
  267. }
  268. }
  269. addSearchTag(): void {
  270. const firstTagName = Object.keys(this.advancedSearchTags)[0];
  271. const attributeMetaData: IAttributeMetadata = this.advancedSearchTags[firstTagName];
  272. const query: IAdvancedSearchQuery = {
  273. key: attributeMetaData.attribute,
  274. value: this.commonSearchService.getDefaultValue(attributeMetaData),
  275. };
  276. this.queries.push(query);
  277. }
  278. protected selectPerson(person: IPerson): void {
  279. this.IasDialogService
  280. .open({
  281. controller: 'VerificationsDialogController as $ctrl',
  282. templateUrl: verificationsDialogTemplateUrl,
  283. locals: {
  284. personUserKey: person.userKey,
  285. showRequiredOnly: true
  286. }
  287. });
  288. }
  289. protected showVerifications(): void {
  290. this.IasDialogService
  291. .open({
  292. controller: 'RecentVerificationsDialogController as $ctrl',
  293. templateUrl: recentVerificationsDialogTemplateUrl
  294. });
  295. }
  296. protected storeSearchText(): void {
  297. this.localStorageService.setItem(this.searchTextLocalStorageKey, this.query || '');
  298. }
  299. enableAdvancedSearch(): void {
  300. this.clearSearch();
  301. this.addSearchTag();
  302. this.advancedSearch = true;
  303. this.commonSearchService.setHdAdvancedSearchActive(this.advancedSearch);
  304. }
  305. protected toggleView(state: string): void {
  306. this.storeSearchView(state);
  307. this.storeSearchText();
  308. this.gotoState(state);
  309. }
  310. private storeSearchView(state: string) {
  311. this.localStorageService.setItem(this.searchViewLocalStorageKey, state);
  312. }
  313. }