Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nocobase
Commits
a9cfd99b
Commit
a9cfd99b
authored
3 years ago
by
Chareice
Browse files
Options
Download
Email Patches
Plain Diff
feat: setDefaultRole for users
parent
d9593881
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
packages/plugin-users/src/__tests__/role.test.ts
+47
-1
packages/plugin-users/src/__tests__/role.test.ts
packages/plugin-users/src/actions/users.ts
+31
-0
packages/plugin-users/src/actions/users.ts
packages/plugin-users/src/server.ts
+1
-1
packages/plugin-users/src/server.ts
with
79 additions
and
2 deletions
+79
-2
packages/plugin-users/src/__tests__/role.test.ts
+
47
-
1
View file @
a9cfd99b
import
Database
from
'
@nocobase/database
'
;
import
Database
,
{
BelongsToManyRepository
}
from
'
@nocobase/database
'
;
import
PluginACL
from
'
@nocobase/plugin-acl
'
;
import
{
MockServer
,
mockServer
}
from
'
@nocobase/test
'
;
...
...
@@ -69,4 +69,50 @@ describe('role', () => {
expect
(
roles
.
length
).
toEqual
(
1
);
expect
(
roles
[
0
].
get
(
'
name
'
)).
toEqual
(
'
test2
'
);
});
it
(
'
should set users default role
'
,
async
()
=>
{
await
db
.
getRepository
(
'
roles
'
).
create
({
values
:
{
name
:
'
test1
'
,
title
:
'
Admin User
'
,
allowConfigure
:
true
,
default
:
true
,
},
});
await
db
.
getRepository
(
'
roles
'
).
create
({
values
:
{
name
:
'
test2
'
,
title
:
'
test2 user
'
,
allowConfigure
:
true
,
},
});
const
user
=
await
db
.
getRepository
(
'
users
'
).
create
({
values
:
{
token
:
'
123
'
,
},
});
const
userRolesRepo
=
db
.
getRepository
<
BelongsToManyRepository
>
(
'
users.roles
'
,
user
.
get
(
'
id
'
)
as
string
);
await
userRolesRepo
.
add
(
'
test1
'
);
await
userRolesRepo
.
add
(
'
test2
'
);
const
response
=
await
api
.
agent
()
.
post
(
'
/users:setDefaultRole
'
)
.
send
({
defaultRole
:
'
test2
'
,
})
.
set
({
Authorization
:
`Bearer
${
user
.
get
(
'
token
'
)}
`
,
});
expect
(
response
.
statusCode
).
toEqual
(
200
);
const
userRoles
=
await
userRolesRepo
.
find
();
const
defaultRole
=
userRoles
.
find
((
userRole
)
=>
userRole
.
get
(
'
rolesUsers
'
).
default
);
expect
(
defaultRole
[
'
name
'
]).
toEqual
(
'
test2
'
);
});
});
This diff is collapsed.
Click to expand it.
packages/plugin-users/src/actions/users.ts
+
31
-
0
View file @
a9cfd99b
...
...
@@ -148,3 +148,34 @@ export async function changePassword(ctx: Context, next: Next) {
ctx
.
body
=
ctx
.
state
.
currentUser
.
toJSON
();
await
next
();
}
export
async
function
setDefaultRole
(
ctx
:
Context
,
next
:
Next
)
{
const
{
values
:
{
defaultRole
},
}
=
ctx
.
action
.
params
;
const
currentUserId
=
ctx
.
state
.
currentUser
.
id
;
await
ctx
.
db
.
getRepository
(
'
rolesUsers
'
).
update
({
filter
:
{
userId
:
currentUserId
,
},
values
:
{
default
:
false
,
},
});
await
ctx
.
db
.
getRepository
(
'
rolesUsers
'
).
update
({
filter
:
{
userId
:
currentUserId
,
roleName
:
defaultRole
,
},
values
:
{
default
:
true
,
},
});
ctx
.
body
=
'
ok
'
;
await
next
();
}
This diff is collapsed.
Click to expand it.
packages/plugin-users/src/server.ts
+
1
-
1
View file @
a9cfd99b
...
...
@@ -61,7 +61,7 @@ export default class UsersPlugin extends Plugin {
this
.
app
.
resourcer
.
use
(
middlewares
.
parseToken
());
const
publicActions
=
[
'
check
'
,
'
signin
'
,
'
signup
'
,
'
lostpassword
'
,
'
resetpassword
'
,
'
getUserByResetToken
'
];
const
loggedInActions
=
[
'
signout
'
,
'
updateProfile
'
,
'
changePassword
'
];
const
loggedInActions
=
[
'
signout
'
,
'
updateProfile
'
,
'
changePassword
'
,
'
setDefaultRole
'
];
publicActions
.
forEach
((
action
)
=>
this
.
app
.
acl
.
skip
(
'
users
'
,
action
));
loggedInActions
.
forEach
((
action
)
=>
this
.
app
.
acl
.
skip
(
'
users
'
,
action
,
'
logged-in
'
));
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment