Skip to content

Commit 04c5ca9

Browse files
authored
Implement security:getUserStrategies (#612)
## What does this PR do? As the title suggests, this **PR** implements the recently added [security:getUserStrategies](https://docs.kuzzle.io/core/2/api/controllers/security/get-user-strategies/) _(@ Kuzzle Backend)_ So it can be used directly, instead of having to use **query** to access it. Closes #611
1 parent 44f022d commit 04c5ca9

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
code: true
3+
type: page
4+
title: getUserStrategies
5+
description: Gets all the available authentication strategies of a user
6+
---
7+
8+
# getUserStrategies
9+
10+
<SinceBadge version="Kuzzle 2.9.0"/>
11+
<SinceBadge version="auto-version"/>
12+
13+
Gets all the available authentication strategies of a user.
14+
15+
<br />
16+
17+
```js
18+
getUserStrategies(kuid, [options]);
19+
```
20+
21+
<br />
22+
23+
| Property | Type | Description |
24+
|--- |--- |--- |
25+
| `kuid` | <pre>string</pre> | User [kuid](/core/2/guides/main-concepts/authentication#kuzzle-user-identifier-kuid) |
26+
| `options` | <pre>object</pre> | Query options |
27+
28+
### options
29+
30+
| Property | Type<br />(default) | Description |
31+
| --- | --- | --- |
32+
| `queuable` | <pre>boolean</pre><br />(`true`) | If true, queues the request during downtime, until connected to Kuzzle again |
33+
34+
## Resolves
35+
36+
Resolves to an array of strings containing all the authentication strategies available for the requested user.
37+
38+
## Usage
39+
40+
<<< ./snippets/get-user-strategies.js
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const strategies = await kuzzle.security.getUserStrategies('john.doe');
2+
3+
console.log(strategies);
4+
// [ 'local' ]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: security#getUserStrategies
2+
description: get user strategies
3+
hooks:
4+
before: >
5+
curl --fail -H "Content-type: application/json" -d '{
6+
"content": {
7+
"profileIds": [ "default" ],
8+
"fullName": "John Doe"
9+
},
10+
"credentials": {
11+
"local": {
12+
"username": "jdoe",
13+
"password": "password"
14+
}
15+
}
16+
}' kuzzle:7512/users/john.doe/_create
17+
after: curl -XDELETE kuzzle:7512/users/john.doe
18+
template: catch
19+
expected:
20+
- "[ 'local' ]"

src/controllers/Security.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ class SecurityController extends BaseController {
330330
.then(response => response.result.hits);
331331
}
332332

333+
getUserStrategies (_id, options = {}) {
334+
return this.query({
335+
_id,
336+
action: 'getUserStrategies'
337+
}, options)
338+
.then(response => response.result.strategies);
339+
}
340+
333341
hasCredentials (strategy, _id, options = {}) {
334342
return this.query({
335343
strategy,

src/protocols/routes.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@
250250
"url": "/users/:_id/_rights",
251251
"verb": "GET"
252252
},
253+
"getUserStrategies": {
254+
"url": "/users/:_id/_strategies",
255+
"verb": "GET"
256+
},
253257
"searchUsers": {
254258
"url": "/users/_search",
255259
"verb": "POST"

test/controllers/security.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,28 @@ describe('Security Controller', () => {
760760
});
761761
});
762762

763+
describe('getUserStrategies', () => {
764+
it('should call security/getUserStrategies query with the user id return a Promise which resolves the list of strategies', () => {
765+
const result = {
766+
strategies: ['local']
767+
};
768+
kuzzle.query.resolves({result});
769+
770+
return kuzzle.security.getUserStrategies('kuid', options)
771+
.then(res => {
772+
should(kuzzle.query)
773+
.be.calledOnce()
774+
.be.calledWith({
775+
_id: 'kuid',
776+
controller: 'security',
777+
action: 'getUserStrategies'
778+
}, options);
779+
780+
should(res).be.eql(result.strategies);
781+
});
782+
});
783+
});
784+
763785
describe('hasCredentials', () => {
764786
it('should call security/hasCredentials query and return a Promise which resolves a boolean', () => {
765787
kuzzle.query.resolves({result: true});

0 commit comments

Comments
 (0)