Skip to content

Commit 3df5d92

Browse files
authored
Support entire query for security:searchProfiles (#667)
## What does this PR do ? Since [Kuzzle 2.14.1](https://github.com/kuzzleio/kuzzle/releases/tag/2.14.1), the `security:searchProfiles` action can take a search query. This PR update the SDK and doc to support this. ### Other changes - allows custom properties into profile object
1 parent af1a1aa commit 3df5d92

File tree

11 files changed

+54
-20
lines changed

11 files changed

+54
-20
lines changed

.eslintignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ src/core/searchResult/Specifications.js
3232
src/core/searchResult/User.js
3333
src/utils/Deprecation.js
3434
src/utils/interfaces.js
35+
src/controllers/Server.js
36+
src/core/Observer.js
37+
src/core/RealtimeDocument.js
38+
src/core/searchResult/RealtimeDocument.js

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ src/core/searchResult/Specifications.js
6161
src/core/searchResult/User.js
6262
src/utils/Deprecation.js
6363
src/utils/interfaces.js
64+
src/controllers/Server.js
65+
src/core/Observer.js
66+
src/core/RealtimeDocument.js
67+
src/core/searchResult/RealtimeDocument.js

doc/7/controllers/security/m-get-profiles/snippets/m-get-profiles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ try {
55
'profile3'
66
]);
77

8-
console.log(response);
8+
console.log(`Successfully retrieved ${response.length} profiles`);
99
/*
1010
[ Profile {
1111
_id: 'profile1',

doc/7/controllers/security/m-get-profiles/snippets/m-get-profiles.test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ hooks:
1212
curl -XDELETE kuzzle:7512/profiles/profile${i}
1313
done
1414
template: default
15-
expected: '.*Profile.*_id.*rateLimit.*policies.*'
15+
expected: Successfully retrieved 3 profiles

doc/7/controllers/security/search-profiles/index.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ description: Searches security profiles, optionally returning only those linked
77

88
# searchProfiles
99

10-
Searches security profiles, optionally returning only those linked to the provided list of security roles.
10+
Searches security profiles.
11+
12+
<SinceBadge version="auto-version" />
13+
<SinceBadge version="Kuzzle 2.14.1" />
14+
15+
Support for search using a search query with the `query` property.
16+
17+
This method also supports the [Koncorde Filters DSL](/core/2/api/koncorde-filters-syntax) to match documents by passing the `lang` argument with the value `koncorde`.
18+
Koncorde filters will be translated into an Elasticsearch query.
1119

1220
<br />
1321

@@ -19,21 +27,24 @@ searchProfiles([body], [options]);
1927

2028
| Property | Type | Description |
2129
|--- |--- |--- |
22-
| `body` | <pre>object</pre> | Query including role identifiers to search for |
30+
| `body` | <pre>object</pre> | Search query |
2331
| `options` | <pre>object</pre> | Query options |
2432

2533
### body
26-
2734
| Property | Type | Description |
2835
| --- | --- | --- |
29-
| `roles` | <pre>array&lt;string&gt;</pre> | Role identifiers |
36+
| `roles` | <pre>array&lt;string&gt;</pre> | Role identifiers <DeprecatedBadge version="auto-version"/>|
37+
| `query` | <pre>object</pre> | Search query using the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl.html) or the [Koncorde Filters DSL](/core/2/api/koncorde-filters-syntax) syntax. <SinceBadge version="auto-version"/>|
38+
39+
If the body is left empty, the result will return all available profiles.
3040

3141
### options
3242

3343
| Property | Type<br/>(default) | Description |
3444
| ---------- | ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
3545
| `queuable` | <pre>boolean</pre><br/>(`true`) | If true, queues the request during downtime, until connected to Kuzzle again |
3646
| `from` | <pre>number</pre><br/>(`0`) | Offset of the first document to fetch |
47+
| `lang` | <pre>string</pre> | Specify the query language to use. By default, it's `elasticsearch` but `koncorde` can also be used. |
3748
| `size` | <pre>number</pre><br/>(`10`) | Maximum number of documents to retrieve per page |
3849
| `scroll` | <pre>string</pre><br/>(`""`) | When set, gets a forward-only cursor having its ttl set to the given value (ie `30s`; cf [elasticsearch time limits](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/common-options.html#time-units)) |
3950

doc/7/controllers/security/search-profiles/snippets/search-profiles.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
try {
22
const results = await kuzzle.security.searchProfiles({
3-
roles: [ 'default' ]
3+
query: {
4+
term: { 'policies.roleId': 'default' }
5+
}
46
});
57

68
console.log(results);

doc/7/controllers/security/search-profiles/snippets/search-profiles.test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ hooks:
1212
curl -XDELETE kuzzle:7512/profiles/profile${i}
1313
done
1414
template: default
15-
expected: ^Successfully retrieved 4 profiles$
15+
expected: ^Successfully retrieved \d+ profiles$

src/controllers/Security.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ export class SecurityController extends BaseController {
441441
body,
442442
action: 'searchProfiles'
443443
};
444-
for (const opt of ['from', 'size', 'scroll']) {
445-
request[opt] = options[opt];
444+
for (const [key, value] of Object.entries(options)) {
445+
request[key] = value;
446446
}
447447

448448
return this.query(request, options)
@@ -454,8 +454,8 @@ export class SecurityController extends BaseController {
454454
body,
455455
action: 'searchRoles'
456456
};
457-
for (const opt of ['from', 'size']) {
458-
request[opt] = options[opt];
457+
for (const [key, value] of Object.entries(options)) {
458+
request[key] = value;
459459
}
460460

461461
return this.query(request, options)

src/core/Observer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { Kuzzle } from '../Kuzzle';
22
import { RealtimeDocument } from './RealtimeDocument';
33
import { Document, DocumentNotification, JSONObject } from '../types';
44
import { RealtimeDocumentSearchResult } from './searchResult/RealtimeDocument';
5-
import { ArgsDocumentControllerGet, ArgsDocumentControllerMGet, ArgsDocumentControllerSearch } from '../controllers/Document';
5+
import {
6+
ArgsDocumentControllerGet,
7+
ArgsDocumentControllerMGet,
8+
ArgsDocumentControllerSearch,
9+
} from '../controllers/Document';
610

711
/**
812
* Class based on a Set<string> that holds the observed documents IDs of

src/core/security/Profile.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { Role } from './Role';
22
import { JSONObject, ProfilePolicy } from '../../types';
3+
import { Kuzzle } from '../../Kuzzle';
34

45
export class Profile {
6+
private _kuzzle: Kuzzle;
7+
58
/**
69
* Profile unique ID
710
*/
@@ -17,21 +20,30 @@ export class Profile {
1720
*/
1821
policies: Array<ProfilePolicy>;
1922

20-
private _kuzzle: any;
23+
// Other properties
24+
[property: string]: any;
2125

2226
/**
2327
*
2428
* @param {Kuzzle} kuzzle
2529
* @param {Object} data
2630
*/
27-
constructor (kuzzle, _id = null, content = null) {
31+
constructor (kuzzle: Kuzzle, _id: string = null, content: JSONObject = {}) {
2832
Reflect.defineProperty(this, '_kuzzle', {
2933
value: kuzzle
3034
});
3135

3236
this._id = _id;
3337
this.rateLimit = content && content.rateLimit ? content.rateLimit : 0;
3438
this.policies = content && content.policies ? content.policies : [];
39+
40+
for (const [key, value] of Object.entries(content)) {
41+
if (this[key]) {
42+
continue;
43+
}
44+
45+
this[key] = value;
46+
}
3547
}
3648

3749
protected get kuzzle () {

0 commit comments

Comments
 (0)