Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ant-design-pro
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
duanledexianxianxian
ant-design-pro
Commits
0f15cfc9
Commit
0f15cfc9
authored
6 years ago
by
afc163
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix authority reading and add test case
parent
5bcf895a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
25 deletions
+31
-25
src/utils/authority.js
src/utils/authority.js
+12
-14
src/utils/authority.test.js
src/utils/authority.test.js
+19
-0
src/utils/utils.js
src/utils/utils.js
+0
-11
No files found.
src/utils/authority.js
View file @
0f15cfc9
import
{
isJsonString
}
from
'
@/utils/utils
'
;
// use localStorage to store the authority info, which might be sent from server in actual project.
export
function
getAuthority
()
{
export
function
getAuthority
(
str
)
{
// return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
const
authorityString
=
localStorage
.
getItem
(
'
antd-pro-authority
'
);
const
authorityString
=
typeof
str
===
'
undefined
'
?
localStorage
.
getItem
(
'
antd-pro-authority
'
)
:
str
;
// authorityString could be admin, "admin", ["admin"]
let
authority
;
if
(
isJsonString
(
authorityString
))
{
try
{
authority
=
JSON
.
parse
(
authorityString
);
}
else
{
authority
=
[
authorityString
];
}
catch
(
e
)
{
authority
=
authorityString
;
}
if
(
typeof
authority
===
'
string
'
)
{
return
[
authority
];
}
return
authority
||
[
'
admin
'
];
}
export
function
setAuthority
(
authority
)
{
let
authorityString
;
if
(
isJsonString
(
authority
))
{
authorityString
=
JSON
.
stringify
(
authority
);
}
else
{
authorityString
=
[
authority
];
}
return
localStorage
.
setItem
(
'
antd-pro-authority
'
,
authorityString
);
const
proAuthority
=
typeof
authority
===
'
string
'
?
[
authority
]
:
authority
;
return
localStorage
.
setItem
(
'
antd-pro-authority
'
,
JSON
.
stringify
(
proAuthority
));
}
This diff is collapsed.
Click to expand it.
src/utils/authority.test.js
0 β 100644
View file @
0f15cfc9
import
{
getAuthority
}
from
'
./authority
'
;
describe
(
'
getAuthority should be strong
'
,
()
=>
{
it
(
'
empty
'
,
()
=>
{
expect
(
getAuthority
(
null
)).
toEqual
([
'
admin
'
]);
// default value
});
it
(
'
string
'
,
()
=>
{
expect
(
getAuthority
(
'
admin
'
)).
toEqual
([
'
admin
'
]);
});
it
(
'
array with double quotes
'
,
()
=>
{
expect
(
getAuthority
(
'
"admin"
'
)).
toEqual
([
'
admin
'
]);
});
it
(
'
array with single item
'
,
()
=>
{
expect
(
getAuthority
(
'
["admin"]
'
)).
toEqual
([
'
admin
'
]);
});
it
(
'
array with multiple items
'
,
()
=>
{
expect
(
getAuthority
(
'
["admin", "guest"]
'
)).
toEqual
([
'
admin
'
,
'
guest
'
]);
});
});
This diff is collapsed.
Click to expand it.
src/utils/utils.js
View file @
0f15cfc9
...
...
@@ -181,14 +181,3 @@ export function formatWan(val) {
export
function
isAntdPro
()
{
return
window
.
location
.
hostname
===
'
preview.pro.ant.design
'
;
}
export
function
isJsonString
(
str
)
{
try
{
if
(
typeof
JSON
.
parse
(
str
)
===
'
object
'
)
{
return
true
;
}
}
catch
(
e
)
{
return
false
;
}
return
false
;
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
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