@nocobase/plugin-acl 0.8.1-alpha.3 → 0.9.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -201
- package/lib/actions/role-check.js +2 -0
- package/lib/actions/role-collections.js +39 -12
- package/lib/collections/roles.js +4 -0
- package/lib/collections/rolesResourcesActions.js +2 -1
- package/lib/collections/users.js +1 -0
- package/lib/migrations/20221214072638-set-role-snippets.d.ts +5 -0
- package/lib/migrations/20221214072638-set-role-snippets.js +49 -0
- package/lib/model/RoleModel.js +1 -0
- package/lib/model/RoleResourceActionModel.js +9 -3
- package/lib/server.js +393 -28
- package/package.json +7 -13
- package/src/__tests__/acl.test.ts +0 -548
- package/src/__tests__/association-field.test.ts +0 -308
- package/src/__tests__/configuration.test.ts +0 -74
- package/src/__tests__/middleware.test.ts +0 -228
- package/src/__tests__/own.test.ts +0 -133
- package/src/__tests__/prepare.ts +0 -20
- package/src/__tests__/role-check.test.ts +0 -41
- package/src/__tests__/role-resource.test.ts +0 -189
- package/src/__tests__/role-user.test.ts +0 -123
- package/src/__tests__/role.test.ts +0 -99
- package/src/__tests__/scope.test.ts +0 -59
- package/src/__tests__/setCurrentRole.test.ts +0 -83
- package/src/__tests__/users.test.ts +0 -52
- package/src/actions/available-actions.ts +0 -18
- package/src/actions/role-check.ts +0 -41
- package/src/actions/role-collections.ts +0 -65
- package/src/actions/user-setDefaultRole.ts +0 -45
- package/src/collections/roles-users.ts +0 -6
- package/src/collections/roles.ts +0 -79
- package/src/collections/rolesResources.ts +0 -31
- package/src/collections/rolesResourcesActions.ts +0 -28
- package/src/collections/rolesResourcesScopes.ts +0 -23
- package/src/collections/users.ts +0 -30
- package/src/index.ts +0 -2
- package/src/middlewares/setCurrentRole.ts +0 -32
- package/src/model/RoleModel.ts +0 -21
- package/src/model/RoleResourceActionModel.ts +0 -88
- package/src/model/RoleResourceModel.ts +0 -74
- package/src/server.ts +0 -463
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { ACL } from '@nocobase/acl';
|
|
2
|
-
import { Database } from '@nocobase/database';
|
|
3
|
-
import PluginUser from '@nocobase/plugin-users';
|
|
4
|
-
import { MockServer } from '@nocobase/test';
|
|
5
|
-
|
|
6
|
-
import { prepareApp } from './prepare';
|
|
7
|
-
|
|
8
|
-
describe('own test', () => {
|
|
9
|
-
let app: MockServer;
|
|
10
|
-
let db: Database;
|
|
11
|
-
let acl: ACL;
|
|
12
|
-
|
|
13
|
-
let pluginUser: PluginUser;
|
|
14
|
-
let adminToken: string;
|
|
15
|
-
let userToken: string;
|
|
16
|
-
|
|
17
|
-
let admin;
|
|
18
|
-
let user;
|
|
19
|
-
|
|
20
|
-
let role;
|
|
21
|
-
let agent;
|
|
22
|
-
let adminAgent;
|
|
23
|
-
let userAgent;
|
|
24
|
-
|
|
25
|
-
afterEach(async () => {
|
|
26
|
-
await app.destroy();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
beforeEach(async () => {
|
|
30
|
-
app = await prepareApp();
|
|
31
|
-
db = app.db;
|
|
32
|
-
|
|
33
|
-
const PostCollection = db.collection({
|
|
34
|
-
name: 'posts',
|
|
35
|
-
fields: [
|
|
36
|
-
{ type: 'string', name: 'title' },
|
|
37
|
-
{ type: 'belongsToMany', name: 'tags' },
|
|
38
|
-
],
|
|
39
|
-
createdBy: true,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
const TagCollection = db.collection({
|
|
43
|
-
name: 'tags',
|
|
44
|
-
fields: [
|
|
45
|
-
{ type: 'string', name: 'name' },
|
|
46
|
-
{ type: 'belongsToMany', name: 'posts' },
|
|
47
|
-
],
|
|
48
|
-
createdBy: true,
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const TestCollection = db.collection({
|
|
52
|
-
name: 'tests',
|
|
53
|
-
fields: [{ type: 'string', name: 'name' }],
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
await db.sync();
|
|
57
|
-
|
|
58
|
-
agent = app.agent();
|
|
59
|
-
|
|
60
|
-
acl = app.acl;
|
|
61
|
-
|
|
62
|
-
role = await db.getRepository('roles').findOne({
|
|
63
|
-
filter: {
|
|
64
|
-
name: 'admin',
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
admin = await db.getRepository('users').findOne();
|
|
69
|
-
|
|
70
|
-
pluginUser = app.getPlugin('users');
|
|
71
|
-
|
|
72
|
-
adminToken = pluginUser.jwtService.sign({ userId: admin.get('id') });
|
|
73
|
-
|
|
74
|
-
adminAgent = app.agent().auth(adminToken, { type: 'bearer' });
|
|
75
|
-
|
|
76
|
-
user = await db.getRepository('users').create({
|
|
77
|
-
values: {
|
|
78
|
-
nickname: 'test',
|
|
79
|
-
roles: ['admin'],
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
userToken = pluginUser.jwtService.sign({ userId: user.get('id') });
|
|
84
|
-
|
|
85
|
-
userAgent = app.agent().auth(userToken, { type: 'bearer' });
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('should list without createBy', async () => {
|
|
89
|
-
await adminAgent
|
|
90
|
-
.patch('/roles/admin')
|
|
91
|
-
.send({
|
|
92
|
-
strategy: {
|
|
93
|
-
actions: ['view:own'],
|
|
94
|
-
},
|
|
95
|
-
})
|
|
96
|
-
.set({ Authorization: 'Bearer ' + adminToken });
|
|
97
|
-
|
|
98
|
-
const response = await userAgent.get('/tests:list');
|
|
99
|
-
expect(response.statusCode).toEqual(200);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('should delete with createdBy', async () => {
|
|
103
|
-
await adminAgent
|
|
104
|
-
.resource('roles')
|
|
105
|
-
.update({
|
|
106
|
-
filterByTk: 'admin',
|
|
107
|
-
values: {
|
|
108
|
-
strategy: {
|
|
109
|
-
actions: ['view:own', 'create', 'destroy:own'],
|
|
110
|
-
},
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
let response = await userAgent
|
|
115
|
-
.resource('posts')
|
|
116
|
-
.create({
|
|
117
|
-
values: {
|
|
118
|
-
title: 't1',
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
expect(response.statusCode).toEqual(200);
|
|
123
|
-
|
|
124
|
-
const data = response.body;
|
|
125
|
-
const id = data.data['id'];
|
|
126
|
-
|
|
127
|
-
response = await userAgent.resource('posts').destroy({
|
|
128
|
-
filterByTk: id
|
|
129
|
-
});
|
|
130
|
-
expect(response.statusCode).toEqual(200);
|
|
131
|
-
expect(await db.getRepository('posts').count()).toEqual(0);
|
|
132
|
-
});
|
|
133
|
-
});
|
package/src/__tests__/prepare.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { mockServer } from '@nocobase/test';
|
|
2
|
-
import PluginACL from '../server';
|
|
3
|
-
|
|
4
|
-
export async function prepareApp() {
|
|
5
|
-
const app = mockServer({
|
|
6
|
-
registerActions: true,
|
|
7
|
-
acl: true,
|
|
8
|
-
plugins: ['error-handler', 'users', 'ui-schema-storage', 'collection-manager'],
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
app.plugin(PluginACL, {
|
|
12
|
-
name: 'acl',
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
await app.loadAndInstall({ clean: true });
|
|
16
|
-
|
|
17
|
-
await app.db.sync();
|
|
18
|
-
|
|
19
|
-
return app;
|
|
20
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { Database } from '@nocobase/database';
|
|
2
|
-
import UsersPlugin from '@nocobase/plugin-users';
|
|
3
|
-
import { MockServer } from '@nocobase/test';
|
|
4
|
-
|
|
5
|
-
import { prepareApp } from './prepare';
|
|
6
|
-
|
|
7
|
-
describe('role check action', () => {
|
|
8
|
-
let app: MockServer;
|
|
9
|
-
let db: Database;
|
|
10
|
-
|
|
11
|
-
beforeEach(async () => {
|
|
12
|
-
app = await prepareApp();
|
|
13
|
-
db = app.db;
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
afterEach(async () => {
|
|
17
|
-
await app.destroy();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should return role info', async () => {
|
|
21
|
-
const role = await db.getRepository('roles').create({
|
|
22
|
-
values: {
|
|
23
|
-
name: 'test',
|
|
24
|
-
},
|
|
25
|
-
});
|
|
26
|
-
const user = await db.getRepository('users').create({
|
|
27
|
-
values: {
|
|
28
|
-
roles: ['test']
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
const userPlugin = app.getPlugin('users') as UsersPlugin;
|
|
32
|
-
const agent = app.agent().auth(userPlugin.jwtService.sign({
|
|
33
|
-
userId: user.get('id'),
|
|
34
|
-
}), { type: 'bearer' });
|
|
35
|
-
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
const response = await agent.resource('roles').check();
|
|
38
|
-
|
|
39
|
-
expect(response.statusCode).toEqual(200);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import { Database, Model } from '@nocobase/database';
|
|
2
|
-
import { CollectionRepository } from '@nocobase/plugin-collection-manager';
|
|
3
|
-
import UsersPlugin from '@nocobase/plugin-users';
|
|
4
|
-
import { MockServer } from '@nocobase/test';
|
|
5
|
-
import { prepareApp } from './prepare';
|
|
6
|
-
|
|
7
|
-
describe('role resource api', () => {
|
|
8
|
-
let app: MockServer;
|
|
9
|
-
let db: Database;
|
|
10
|
-
let role: Model;
|
|
11
|
-
let admin;
|
|
12
|
-
let adminAgent;
|
|
13
|
-
|
|
14
|
-
afterEach(async () => {
|
|
15
|
-
await app.destroy();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
beforeEach(async () => {
|
|
19
|
-
app = await prepareApp();
|
|
20
|
-
db = app.db;
|
|
21
|
-
|
|
22
|
-
role = await db.getRepository('roles').findOne({
|
|
23
|
-
filter: {
|
|
24
|
-
name: 'admin',
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
const UserRepo = db.getCollection('users').repository;
|
|
29
|
-
admin = await UserRepo.create({
|
|
30
|
-
values: {
|
|
31
|
-
roles: ['admin']
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
const userPlugin = app.getPlugin('users') as UsersPlugin;
|
|
36
|
-
adminAgent = app.agent().auth(userPlugin.jwtService.sign({
|
|
37
|
-
userId: admin.get('id'),
|
|
38
|
-
}), { type: 'bearer' });
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should grant resource by createRepository', async () => {
|
|
42
|
-
const collectionManager = db.getRepository('collections') as CollectionRepository;
|
|
43
|
-
await collectionManager.create({
|
|
44
|
-
values: {
|
|
45
|
-
name: 'c1',
|
|
46
|
-
title: 'table1',
|
|
47
|
-
},
|
|
48
|
-
context: {},
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
await collectionManager.create({
|
|
52
|
-
values: {
|
|
53
|
-
name: 'c2',
|
|
54
|
-
title: 'table2',
|
|
55
|
-
},
|
|
56
|
-
context: {},
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
await db.getRepository('roles').create({
|
|
60
|
-
values: {
|
|
61
|
-
name: 'testRole',
|
|
62
|
-
resources: [
|
|
63
|
-
{
|
|
64
|
-
name: 'c1',
|
|
65
|
-
actions: [
|
|
66
|
-
{
|
|
67
|
-
name: 'create',
|
|
68
|
-
},
|
|
69
|
-
],
|
|
70
|
-
},
|
|
71
|
-
],
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
const acl = app.acl;
|
|
76
|
-
const testRole = acl.getRole('testRole');
|
|
77
|
-
const resource = testRole.getResource('c1');
|
|
78
|
-
expect(resource).toBeDefined();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should grant resource action', async () => {
|
|
82
|
-
const collectionManager = db.getRepository('collections') as CollectionRepository;
|
|
83
|
-
|
|
84
|
-
await collectionManager.create({
|
|
85
|
-
values: {
|
|
86
|
-
name: 'c1',
|
|
87
|
-
title: 'table1',
|
|
88
|
-
},
|
|
89
|
-
context: {},
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
await collectionManager.create({
|
|
93
|
-
values: {
|
|
94
|
-
name: 'c2',
|
|
95
|
-
title: 'table2',
|
|
96
|
-
},
|
|
97
|
-
context: {},
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
// get collections list
|
|
101
|
-
let response = await adminAgent
|
|
102
|
-
.resource('roles.collections', 'admin')
|
|
103
|
-
.list({
|
|
104
|
-
filter: {
|
|
105
|
-
$or: [{ name: 'c1' }, { name: 'c2' }],
|
|
106
|
-
},
|
|
107
|
-
sort: ['sort'],
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
expect(response.statusCode).toEqual(200);
|
|
111
|
-
|
|
112
|
-
expect(response.body.data).toMatchObject([
|
|
113
|
-
{
|
|
114
|
-
name: 'c1',
|
|
115
|
-
title: 'table1',
|
|
116
|
-
usingConfig: 'strategy',
|
|
117
|
-
exists: false,
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
name: 'c2',
|
|
121
|
-
title: 'table2',
|
|
122
|
-
usingConfig: 'strategy',
|
|
123
|
-
exists: false,
|
|
124
|
-
},
|
|
125
|
-
]);
|
|
126
|
-
|
|
127
|
-
// set resource actions
|
|
128
|
-
response = await adminAgent
|
|
129
|
-
.resource('roles.resources', 'admin')
|
|
130
|
-
.create({
|
|
131
|
-
values: {
|
|
132
|
-
name: 'c1',
|
|
133
|
-
usingActionsConfig: true,
|
|
134
|
-
actions: [
|
|
135
|
-
{
|
|
136
|
-
name: 'create',
|
|
137
|
-
},
|
|
138
|
-
],
|
|
139
|
-
},
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
expect(response.statusCode).toEqual(200);
|
|
143
|
-
|
|
144
|
-
// get collections list
|
|
145
|
-
response = await adminAgent
|
|
146
|
-
.resource('roles.collections')
|
|
147
|
-
.list({
|
|
148
|
-
associatedIndex: role.get('name') as string,
|
|
149
|
-
filter: {
|
|
150
|
-
name: 'c1',
|
|
151
|
-
},
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
expect(response.body.data[0]['usingConfig']).toEqual('resourceAction');
|
|
155
|
-
|
|
156
|
-
response = await adminAgent
|
|
157
|
-
.resource('roles.resources')
|
|
158
|
-
.list({
|
|
159
|
-
associatedIndex: role.get('name') as string,
|
|
160
|
-
appends: 'actions',
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
expect(response.statusCode).toEqual(200);
|
|
164
|
-
const resources = response.body.data;
|
|
165
|
-
const resourceAction = resources[0]['actions'][0];
|
|
166
|
-
|
|
167
|
-
expect(resourceAction['name']).toEqual('create');
|
|
168
|
-
|
|
169
|
-
// update resource actions
|
|
170
|
-
response = await adminAgent
|
|
171
|
-
.resource('roles.resources')
|
|
172
|
-
.update({
|
|
173
|
-
associatedIndex: role.get('name') as string,
|
|
174
|
-
values: {
|
|
175
|
-
name: 'c1',
|
|
176
|
-
usingActionsConfig: true,
|
|
177
|
-
actions: [
|
|
178
|
-
{
|
|
179
|
-
name: 'view',
|
|
180
|
-
},
|
|
181
|
-
],
|
|
182
|
-
},
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
expect(response.statusCode).toEqual(200);
|
|
186
|
-
expect(response.body.data[0]['actions'].length).toEqual(1);
|
|
187
|
-
expect(response.body.data[0]['actions'][0]['name']).toEqual('view');
|
|
188
|
-
});
|
|
189
|
-
});
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import Database, { BelongsToManyRepository } from '@nocobase/database';
|
|
2
|
-
import PluginACL from '@nocobase/plugin-acl';
|
|
3
|
-
import UsersPlugin from '@nocobase/plugin-users';
|
|
4
|
-
import { MockServer, mockServer } from '@nocobase/test';
|
|
5
|
-
|
|
6
|
-
describe('role', () => {
|
|
7
|
-
let api: MockServer;
|
|
8
|
-
let db: Database;
|
|
9
|
-
|
|
10
|
-
let usersPlugin: UsersPlugin;
|
|
11
|
-
|
|
12
|
-
beforeEach(async () => {
|
|
13
|
-
api = mockServer();
|
|
14
|
-
await api.cleanDb();
|
|
15
|
-
api.plugin(UsersPlugin, { name: 'users' });
|
|
16
|
-
api.plugin(PluginACL, { name: 'acl' });
|
|
17
|
-
await api.loadAndInstall();
|
|
18
|
-
|
|
19
|
-
db = api.db;
|
|
20
|
-
usersPlugin = api.getPlugin('users');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
afterEach(async () => {
|
|
24
|
-
await api.destroy();
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('should set default role', async () => {
|
|
28
|
-
await db.getRepository('roles').create({
|
|
29
|
-
values: {
|
|
30
|
-
name: 'test1',
|
|
31
|
-
title: 'Admin User',
|
|
32
|
-
allowConfigure: true,
|
|
33
|
-
default: true,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
const user = await db.getRepository('users').create({});
|
|
38
|
-
|
|
39
|
-
// @ts-ignore
|
|
40
|
-
const roles = await user.getRoles();
|
|
41
|
-
|
|
42
|
-
expect(roles.length).toEqual(1);
|
|
43
|
-
expect(roles[0].get('name')).toEqual('test1');
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should not add role when user has role', async () => {
|
|
47
|
-
await db.getRepository('roles').create({
|
|
48
|
-
values: {
|
|
49
|
-
name: 'test1',
|
|
50
|
-
default: true,
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
await db.getRepository('roles').create({
|
|
55
|
-
values: {
|
|
56
|
-
name: 'test2',
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const user = await db.getRepository('users').create({
|
|
61
|
-
values: {
|
|
62
|
-
roles: [
|
|
63
|
-
{
|
|
64
|
-
name: 'test2',
|
|
65
|
-
},
|
|
66
|
-
],
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// @ts-ignore
|
|
71
|
-
const roles = await user.getRoles();
|
|
72
|
-
|
|
73
|
-
expect(roles.length).toEqual(1);
|
|
74
|
-
expect(roles[0].get('name')).toEqual('test2');
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should set users default role', async () => {
|
|
78
|
-
await db.getRepository('roles').create({
|
|
79
|
-
values: {
|
|
80
|
-
name: 'test1',
|
|
81
|
-
title: 'Admin User',
|
|
82
|
-
allowConfigure: true,
|
|
83
|
-
default: true,
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
await db.getRepository('roles').create({
|
|
88
|
-
values: {
|
|
89
|
-
name: 'test2',
|
|
90
|
-
title: 'test2 user',
|
|
91
|
-
allowConfigure: true,
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
const user = await db.getRepository('users').create({
|
|
96
|
-
values: {
|
|
97
|
-
token: '123',
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
const userRolesRepo = db.getRepository<BelongsToManyRepository>('users.roles', user.get('id') as string);
|
|
102
|
-
await userRolesRepo.add('test1');
|
|
103
|
-
await userRolesRepo.add('test2');
|
|
104
|
-
|
|
105
|
-
const userToken = usersPlugin.jwtService.sign({ userId: user.get('id') });
|
|
106
|
-
const response = await api
|
|
107
|
-
.agent()
|
|
108
|
-
.post('/users:setDefaultRole')
|
|
109
|
-
.send({
|
|
110
|
-
roleName: 'test2',
|
|
111
|
-
})
|
|
112
|
-
.set({
|
|
113
|
-
Authorization: `Bearer ${userToken}`,
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
expect(response.statusCode).toEqual(200);
|
|
117
|
-
|
|
118
|
-
const userRoles = await userRolesRepo.find();
|
|
119
|
-
const defaultRole = userRoles.find((userRole) => userRole.get('rolesUsers').default);
|
|
120
|
-
|
|
121
|
-
expect(defaultRole['name']).toEqual('test2');
|
|
122
|
-
});
|
|
123
|
-
});
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { Database, Model } from '@nocobase/database';
|
|
2
|
-
import UsersPlugin from '@nocobase/plugin-users';
|
|
3
|
-
import { MockServer } from '@nocobase/test';
|
|
4
|
-
|
|
5
|
-
import { prepareApp } from './prepare';
|
|
6
|
-
|
|
7
|
-
describe('role api', () => {
|
|
8
|
-
let app: MockServer;
|
|
9
|
-
let db: Database;
|
|
10
|
-
|
|
11
|
-
afterEach(async () => {
|
|
12
|
-
await app.destroy();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
beforeEach(async () => {
|
|
16
|
-
app = await prepareApp();
|
|
17
|
-
db = app.db;
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('grant', () => {
|
|
21
|
-
let role: Model;
|
|
22
|
-
let admin: Model;
|
|
23
|
-
let adminAgent;
|
|
24
|
-
|
|
25
|
-
beforeEach(async () => {
|
|
26
|
-
role = await db.getRepository('roles').findOne({
|
|
27
|
-
filter: {
|
|
28
|
-
name: 'admin',
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
const UserRepo = db.getCollection('users').repository;
|
|
33
|
-
admin = await UserRepo.create({
|
|
34
|
-
values: {
|
|
35
|
-
roles: ['admin'],
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const userPlugin = app.getPlugin('users') as UsersPlugin;
|
|
40
|
-
adminAgent = app.agent().auth(
|
|
41
|
-
userPlugin.jwtService.sign({
|
|
42
|
-
userId: admin.get('id'),
|
|
43
|
-
}),
|
|
44
|
-
{ type: 'bearer' },
|
|
45
|
-
);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should list actions', async () => {
|
|
49
|
-
const response = await adminAgent.resource('availableActions').list();
|
|
50
|
-
expect(response.statusCode).toEqual(200);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should grant universal role actions', async () => {
|
|
54
|
-
// grant role actions
|
|
55
|
-
const response = await adminAgent.resource('roles').update({
|
|
56
|
-
forceUpdate: true,
|
|
57
|
-
values: {
|
|
58
|
-
strategy: {
|
|
59
|
-
actions: ['create:all', 'view:own'],
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
expect(response.statusCode).toEqual(200);
|
|
65
|
-
|
|
66
|
-
await role.reload();
|
|
67
|
-
|
|
68
|
-
expect(role.get('strategy')).toMatchObject({
|
|
69
|
-
actions: ['create:all', 'view:own'],
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('should works with default option', async () => {
|
|
75
|
-
await db.getRepository('roles').create({
|
|
76
|
-
values: {
|
|
77
|
-
name: 'role1',
|
|
78
|
-
title: 'admin 1',
|
|
79
|
-
default: true,
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
await db.getRepository('roles').create({
|
|
84
|
-
values: {
|
|
85
|
-
name: 'role2',
|
|
86
|
-
default: true,
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
const defaultRole = await db.getRepository('roles').find({
|
|
91
|
-
filter: {
|
|
92
|
-
default: true,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
expect(defaultRole.length).toEqual(1);
|
|
97
|
-
expect(defaultRole[0].get('name')).toEqual('role2');
|
|
98
|
-
});
|
|
99
|
-
});
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { Database } from '@nocobase/database';
|
|
2
|
-
import UsersPlugin from '@nocobase/plugin-users';
|
|
3
|
-
import { MockServer } from '@nocobase/test';
|
|
4
|
-
import { prepareApp } from './prepare';
|
|
5
|
-
|
|
6
|
-
describe('scope api', () => {
|
|
7
|
-
let app: MockServer;
|
|
8
|
-
let db: Database;
|
|
9
|
-
|
|
10
|
-
let admin;
|
|
11
|
-
let adminAgent;
|
|
12
|
-
|
|
13
|
-
afterEach(async () => {
|
|
14
|
-
await app.destroy();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
beforeEach(async () => {
|
|
18
|
-
app = await prepareApp();
|
|
19
|
-
db = app.db;
|
|
20
|
-
|
|
21
|
-
const UserRepo = db.getCollection('users').repository;
|
|
22
|
-
admin = await UserRepo.create({
|
|
23
|
-
values: {
|
|
24
|
-
roles: ['admin']
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
const userPlugin = app.getPlugin('users') as UsersPlugin;
|
|
29
|
-
adminAgent = app.agent().auth(userPlugin.jwtService.sign({
|
|
30
|
-
userId: admin.get('id'),
|
|
31
|
-
}), { type: 'bearer' });
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('should create scope of resource', async () => {
|
|
35
|
-
const response = await adminAgent
|
|
36
|
-
.resource('rolesResourcesScopes')
|
|
37
|
-
.create({
|
|
38
|
-
values: {
|
|
39
|
-
resourceName: 'posts',
|
|
40
|
-
name: 'published posts',
|
|
41
|
-
scope: {
|
|
42
|
-
published: true,
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
expect(response.statusCode).toEqual(200);
|
|
48
|
-
|
|
49
|
-
const scope = await db.getRepository('rolesResourcesScopes').findOne({
|
|
50
|
-
filter: {
|
|
51
|
-
name: 'published posts',
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
expect(scope.get('scope')).toMatchObject({
|
|
56
|
-
published: true,
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
});
|