Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
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
090e1840
Commit
090e1840
authored
Mar 22, 2019
by
何乐
Committed by
陈帅
Mar 22, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Authorized] fix props cannot be delivered if target is a react element (#3813)
parent
899950fb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
48 deletions
+51
-48
src/components/Authorized/CheckPermissions.js
src/components/Authorized/CheckPermissions.js
+14
-33
src/components/Authorized/PromiseRender.js
src/components/Authorized/PromiseRender.js
+18
-8
src/components/Authorized/Secured.js
src/components/Authorized/Secured.js
+19
-7
No files found.
src/components/Authorized/CheckPermissions.js
View file @
090e1840
...
@@ -2,21 +2,13 @@ import React from 'react';
...
@@ -2,21 +2,13 @@ import React from 'react';
import
PromiseRender
from
'
./PromiseRender
'
;
import
PromiseRender
from
'
./PromiseRender
'
;
import
{
CURRENT
}
from
'
./renderAuthorize
'
;
import
{
CURRENT
}
from
'
./renderAuthorize
'
;
function
isPromise
(
obj
)
{
return
(
!!
obj
&&
(
typeof
obj
===
'
object
'
||
typeof
obj
===
'
function
'
)
&&
typeof
obj
.
then
===
'
function
'
);
}
/**
/**
* 通用权限检查方法
* 通用权限检查方法
* Common check permissions method
* Common check permissions method
* @param { 权限判定
Permission judgment type string |array | Promise | Function
} authority
* @param { 权限判定
| Permission judgment
} authority
* @param { 你的权限
Your permission description type:string
} currentAuthority
* @param { 你的权限
| Your permission description
} currentAuthority
* @param { 通过的组件 Passing components } target
* @param { 通过的组件
|
Passing components } target
* @param { 未通过的组件 no pass components } Exception
* @param { 未通过的组件
|
no pass components } Exception
*/
*/
const
checkPermissions
=
(
authority
,
currentAuthority
,
target
,
Exception
)
=>
{
const
checkPermissions
=
(
authority
,
currentAuthority
,
target
,
Exception
)
=>
{
// 没有判定权限.默认查看所有
// 没有判定权限.默认查看所有
...
@@ -26,47 +18,36 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => {
...
@@ -26,47 +18,36 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => {
}
}
// 数组处理
// 数组处理
if
(
Array
.
isArray
(
authority
))
{
if
(
Array
.
isArray
(
authority
))
{
if
(
authority
.
indexOf
(
currentAuthority
)
>=
0
)
{
return
target
;
}
if
(
Array
.
isArray
(
currentAuthority
))
{
if
(
Array
.
isArray
(
currentAuthority
))
{
for
(
let
i
=
0
;
i
<
currentAuthority
.
length
;
i
+=
1
)
{
if
(
currentAuthority
.
some
(
item
=>
authority
.
includes
(
item
)))
{
const
element
=
currentAuthority
[
i
];
return
target
;
if
(
authority
.
indexOf
(
element
)
>=
0
)
{
return
target
;
}
}
}
}
else
if
(
authority
.
includes
(
currentAuthority
))
{
return
target
;
}
}
return
Exception
;
return
Exception
;
}
}
// string 处理
// string 处理
if
(
typeof
authority
===
'
string
'
)
{
if
(
typeof
authority
===
'
string
'
)
{
if
(
authority
===
currentAuthority
)
{
return
target
;
}
if
(
Array
.
isArray
(
currentAuthority
))
{
if
(
Array
.
isArray
(
currentAuthority
))
{
for
(
let
i
=
0
;
i
<
currentAuthority
.
length
;
i
+=
1
)
{
if
(
currentAuthority
.
some
(
item
=>
authority
===
item
))
{
const
element
=
currentAuthority
[
i
];
return
target
;
if
(
authority
===
element
)
{
return
target
;
}
}
}
}
else
if
(
authority
===
currentAuthority
)
{
return
target
;
}
}
return
Exception
;
return
Exception
;
}
}
// Promise 处理
// Promise 处理
if
(
isPromise
(
authority
)
)
{
if
(
authority
instanceof
Promise
)
{
return
<
PromiseRender
ok
=
{
target
}
error
=
{
Exception
}
promise
=
{
authority
}
/>
;
return
<
PromiseRender
ok
=
{
target
}
error
=
{
Exception
}
promise
=
{
authority
}
/>
;
}
}
// Function 处理
// Function 处理
if
(
typeof
authority
===
'
function
'
)
{
if
(
typeof
authority
===
'
function
'
)
{
try
{
try
{
const
bool
=
authority
(
currentAuthority
);
const
bool
=
authority
(
currentAuthority
);
// 函数执行后返回值是 Promise
// 函数执行后返回值是 Promise
if
(
isPromise
(
bool
)
)
{
if
(
bool
instanceof
Promise
)
{
return
<
PromiseRender
ok
=
{
target
}
error
=
{
Exception
}
promise
=
{
bool
}
/>
;
return
<
PromiseRender
ok
=
{
target
}
error
=
{
Exception
}
promise
=
{
bool
}
/>
;
}
}
if
(
bool
)
{
if
(
bool
)
{
...
...
src/components/Authorized/PromiseRender.js
View file @
090e1840
import
React
from
'
react
'
;
import
{
Spin
}
from
'
antd
'
;
import
{
Spin
}
from
'
antd
'
;
import
isEqual
from
'
lodash/isEqual
'
;
import
React
from
'
react
'
;
import
{
isComponentClass
}
from
'
./Secured
'
;
export
default
class
PromiseRender
extends
React
.
Pure
Component
{
export
default
class
PromiseRender
extends
React
.
Component
{
state
=
{
state
=
{
component
:
null
,
component
:
null
,
};
};
...
@@ -10,10 +12,14 @@ export default class PromiseRender extends React.PureComponent {
...
@@ -10,10 +12,14 @@ export default class PromiseRender extends React.PureComponent {
this
.
setRenderComponent
(
this
.
props
);
this
.
setRenderComponent
(
this
.
props
);
}
}
componentDidUpdate
(
nextProps
)
{
shouldComponentUpdate
=
(
nextProps
,
nextState
)
=>
{
// new Props enter
const
{
component
}
=
this
.
state
;
this
.
setRenderComponent
(
nextProps
);
if
(
!
isEqual
(
nextProps
,
this
.
props
))
{
}
this
.
setRenderComponent
(
nextProps
);
}
if
(
nextState
.
component
!==
component
)
return
true
;
return
false
;
};
// set render Component : ok or error
// set render Component : ok or error
setRenderComponent
(
props
)
{
setRenderComponent
(
props
)
{
...
@@ -37,8 +43,12 @@ export default class PromiseRender extends React.PureComponent {
...
@@ -37,8 +43,12 @@ export default class PromiseRender extends React.PureComponent {
// Authorized render is already instantiated, children is no instantiated
// Authorized render is already instantiated, children is no instantiated
// Secured is not instantiated
// Secured is not instantiated
checkIsInstantiation
=
target
=>
{
checkIsInstantiation
=
target
=>
{
if
(
!
React
.
isValidElement
(
target
))
{
if
(
isComponentClass
(
target
))
{
return
target
;
const
Target
=
target
;
return
props
=>
<
Target
{...
props
}
/>
;
}
if
(
React
.
isValidElement
(
target
))
{
return
props
=>
React
.
cloneElement
(
target
,
props
);
}
}
return
()
=>
target
;
return
()
=>
target
;
};
};
...
...
src/components/Authorized/Secured.js
View file @
090e1840
import
React
from
'
react
'
;
import
React
from
'
react
'
;
import
Exception
from
'
../Exception
'
;
import
Exception
from
'
../Exception
'
;
import
CheckPermissions
from
'
./CheckPermissions
'
;
import
CheckPermissions
from
'
./CheckPermissions
'
;
/**
/**
* 默认不能访问任何页面
* 默认不能访问任何页面
* default is "NULL"
* default is "NULL"
*/
*/
const
Exception403
=
()
=>
<
Exception
type
=
"
403
"
/>
;
const
Exception403
=
()
=>
<
Exception
type
=
"
403
"
/>
;
export
const
isComponentClass
=
component
=>
{
if
(
!
component
)
return
false
;
const
proto
=
Object
.
getPrototypeOf
(
component
);
if
(
proto
===
React
.
Component
||
proto
===
Function
.
prototype
)
return
true
;
return
isComponentClass
(
proto
);
};
// Determine whether the incoming component has been instantiated
// Determine whether the incoming component has been instantiated
// AuthorizedRoute is already instantiated
// AuthorizedRoute is already instantiated
// Authorized render is already instantiated, children is no instantiated
// Authorized render is already instantiated, children is no instantiated
// Secured is not instantiated
// Secured is not instantiated
const
checkIsInstantiation
=
target
=>
{
const
checkIsInstantiation
=
target
=>
{
if
(
!
React
.
isValidElement
(
target
))
{
if
(
isComponentClass
(
target
))
{
return
target
;
const
Target
=
target
;
return
props
=>
<
Target
{...
props
}
/>
;
}
if
(
React
.
isValidElement
(
target
))
{
return
props
=>
React
.
cloneElement
(
target
,
props
);
}
}
return
()
=>
target
;
return
()
=>
target
;
};
};
/**
/**
* 用于判断是否拥有权限访问此
view
权限
* 用于判断是否拥有权限访问此
view
权限
* authority 支持传入 string,
function:()=>boolean|
Promise
* authority 支持传入 string,
() => boolean |
Promise
* e.g. 'user' 只有
user
用户能访问
* e.g. 'user' 只有
user
用户能访问
* e.g. 'user,admin' user和 admin 都能访问
* e.g. 'user,admin' user
和 admin 都能访问
* e.g. ()=>boolean 返回true能访问,返回false不能访问
* e.g. ()=>boolean 返回true能访问,返回false不能访问
* e.g. Promise then 能访问 catch不能访问
* e.g. Promise then 能访问 catch不能访问
* e.g. authority support incoming string,
function:
() => boolean | Promise
* e.g. authority support incoming string, () => boolean | Promise
* e.g. 'user' only user user can access
* e.g. 'user' only user user can access
* e.g. 'user, admin' user and admin can access
* e.g. 'user, admin' user and admin can access
* e.g. () => boolean true to be able to visit, return false can not be accessed
* e.g. () => boolean true to be able to visit, return false can not be accessed
...
...
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