@@ -13,7 +13,6 @@ import dataSourceToLogoPathLookup from '../config/dataSourceToLogoPathLookup.jso
1313import documentsToSearchResultsMappings from '../config/documentsToSearchResultMappings.json' ;
1414import { RootState } from "../store/store" ;
1515
16-
1716const mapHitToSearchResult = ( hit ) => {
1817 const doc = hit . _source ;
1918 const dataSource = hit . _index ;
@@ -38,20 +37,82 @@ export default function SearchPage() {
3837 //TODO: simplify query state and move it to one place
3938 const [ query , setQuery ] = useState < string > ( "" ) ;
4039
41- const { appName, apiKey, searchEndpoint} = useSelector ( ( state : RootState ) => state . searchApplicationSettings ) ;
40+ const { appName, apiKey : adminApiKey , searchEndpoint, searchPersona } = useSelector ( ( state : RootState ) => state . searchApplicationSettings ) ;
4241 const { sorts} = useSelector ( ( state : RootState ) => state . sort ) ;
4342 const indexFilter = useSelector ( ( state : RootState ) => state . filter ) [ "filters" ] [ "Data sources" ] . values ;
4443
4544
4645 useEffect ( ( ) => {
4746 const fetchData = async ( ) => await handleSearchSubmit ( ) ;
4847 fetchData ( ) ;
49- } , [ indexFilter , sorts , appName , apiKey , searchEndpoint ] ) ;
48+ } , [ indexFilter , sorts , appName , adminApiKey , searchEndpoint ] ) ;
49+
50+ const getOrCreateApiKey = async ( ) => {
51+ if ( searchPersona == "admin" ) {
52+ return adminApiKey
53+ }
54+ else {
55+ const permissions = [ 'foo' ] // todo
56+ const apiKeyRoleDescriptor = {
57+ name : searchPersona ,
58+ role_descriptors : {
59+ "dls-role" : {
60+ "cluster" : [ "all" ] ,
61+ "indices" : [
62+ {
63+ "names" : [ "search-sharepoint" ] ,
64+ "privileges" : [ "read" ] ,
65+ "query" : {
66+ "template" : {
67+ "params" : {
68+ "permissions" : permissions
69+ } ,
70+ 'source' : `
71+ {
72+ "bool": {
73+ "filter": {
74+ "bool": {
75+ "should": [
76+ {
77+ "bool": {
78+ "must_not": {
79+ "exists": {
80+ "field": "_allow_access_control"
81+ }
82+ }
83+ }
84+ },
85+ {
86+ "terms": {
87+ "_allow_access_control": {{#toJson}}permissions{{/toJson}}
88+ }
89+ }
90+ ]
91+ }
92+ }
93+ }
94+ }
95+ `
96+ }
97+ }
98+ }
99+ ]
100+ }
101+ }
102+ }
103+
104+ // TODO Use fetch: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
105+ return "missing"
106+ }
107+ }
50108
51109 const handleSearchSubmit = async ( ) => {
52110 try {
53111 setLoading ( true ) ;
54112
113+ const apiKey = await getOrCreateApiKey ( )
114+ showToast ( "API key is: " + apiKey )
115+
55116 const client = SearchApplicationClient ( appName , searchEndpoint , apiKey , {
56117 "facets" : {
57118 description : {
0 commit comments