-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* temp save * rebase master * fix same error * Add a new user and different permissions * fix eol-last * add Secured decorator * fix list redirect bug (#507) * Mobile menu (#463) * Increase the sliding menu * Add a simple animation * update mobile menu * update * update * update * rebase master * recovery import/first * fix error * Fix some bugs Change "ALL" to "NONE" Remove the "!" Support After landing successfully reload Reset the format * Pump your public logic * add some test * Add documents * use default currentRole in Authorized/AuthorizedRoute * rename props & change some authority setting * A big change 😄 unified router and Secured parameters 😭 loginOut logout also changed to reload * fix siderMeun bugs * Decoupled SiderMenu * Remove the handsome head of information * Add a simple error * rebase master
- Loading branch information
Showing
21 changed files
with
473 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ npm-debug.log* | |
|
||
/coverage | ||
.idea | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import CheckPermissions from './CheckPermissions'; | ||
|
||
class Authorized extends React.Component { | ||
render() { | ||
const { children, authority, noMatch = null } = this.props; | ||
const childrenRender = typeof children === 'undefined' ? null : children; | ||
return CheckPermissions( | ||
authority, | ||
childrenRender, | ||
noMatch | ||
); | ||
} | ||
} | ||
|
||
export default Authorized; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import { Route, Redirect } from 'dva/router'; | ||
import Authorized from './Authorized'; | ||
|
||
class AuthorizedRoute extends React.Component { | ||
render() { | ||
const { component: Component, render, authority, | ||
redirectPath, ...rest } = this.props; | ||
return ( | ||
<Authorized | ||
authority={authority} | ||
noMatch={<Route {...rest} render={() => <Redirect to={{ pathname: redirectPath }} />} />} | ||
> | ||
<Route | ||
{...rest} | ||
render={props => (Component ? <Component {...props} /> : render(props))} | ||
/> | ||
</Authorized> | ||
); | ||
} | ||
} | ||
|
||
export default AuthorizedRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react'; | ||
import PromiseRender from './PromiseRender'; | ||
import { CURRENT } from './index'; | ||
/** | ||
* 通用权限检查方法 | ||
* Common check permissions method | ||
* @param { 权限判定 Permission judgment type string |array | Promise | Function } authority | ||
* @param { 你的权限 Your permission description type:string} currentAuthority | ||
* @param { 通过的组件 Passing components } target | ||
* @param { 未通过的组件 no pass components } Exception | ||
*/ | ||
const checkPermissions = (authority, currentAuthority, target, Exception) => { | ||
// 没有判定权限.默认查看所有 | ||
// Retirement authority, return target; | ||
if (!authority) { | ||
return target; | ||
} | ||
// 数组处理 | ||
if (authority.constructor.name === 'Array') { | ||
if (authority.includes(currentAuthority)) { | ||
return target; | ||
} | ||
return Exception; | ||
} | ||
|
||
// string 处理 | ||
if (authority.constructor.name === 'String') { | ||
if (authority === currentAuthority) { | ||
return target; | ||
} | ||
return Exception; | ||
} | ||
|
||
// Promise 处理 | ||
if (authority.constructor.name === 'Promise') { | ||
return () => ( | ||
<PromiseRender ok={target} error={Exception} promise={authority} /> | ||
); | ||
} | ||
|
||
// Function 处理 | ||
if (authority.constructor.name === 'Function') { | ||
try { | ||
const bool = authority(); | ||
if (bool) { | ||
return target; | ||
} | ||
return Exception; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
throw new Error('unsupported parameters'); | ||
}; | ||
|
||
export { checkPermissions }; | ||
|
||
const check = (authority, target, Exception) => { | ||
return checkPermissions(authority, CURRENT, target, Exception); | ||
}; | ||
|
||
export default check; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { checkPermissions } from './CheckPermissions.js'; | ||
|
||
const target = 'ok'; | ||
const error = 'error'; | ||
|
||
describe('test CheckPermissions', () => { | ||
it('Correct string permission authentication', () => { | ||
expect(checkPermissions('user', 'user', target, error)).toEqual('ok'); | ||
}); | ||
it('Correct string permission authentication', () => { | ||
expect(checkPermissions('user', 'NULL', target, error)).toEqual('error'); | ||
}); | ||
it('authority is undefined , return ok', () => { | ||
expect(checkPermissions(null, 'NULL', target, error)).toEqual('ok'); | ||
}); | ||
it('Wrong string permission authentication', () => { | ||
expect(checkPermissions('admin', 'user', target, error)).toEqual('error'); | ||
}); | ||
it('Correct Array permission authentication', () => { | ||
expect(checkPermissions(['user', 'admin'], 'user', target, error)).toEqual( | ||
'ok' | ||
); | ||
}); | ||
it('Wrong Array permission authentication,currentAuthority error', () => { | ||
expect( | ||
checkPermissions(['user', 'admin'], 'user,admin', target, error) | ||
).toEqual('error'); | ||
}); | ||
it('Wrong Array permission authentication', () => { | ||
expect(checkPermissions(['user', 'admin'], 'guest', target, error)).toEqual( | ||
'error' | ||
); | ||
}); | ||
it('Wrong Function permission authentication', () => { | ||
expect(checkPermissions(() => false, 'guest', target, error)).toEqual( | ||
'error' | ||
); | ||
}); | ||
it('Correct Function permission authentication', () => { | ||
expect(checkPermissions(() => true, 'guest', target, error)).toEqual('ok'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import { Spin } from 'antd'; | ||
|
||
export default class PromiseRender extends React.PureComponent { | ||
state = { | ||
component: false, | ||
}; | ||
async componentDidMount() { | ||
this.props.promise | ||
.then(() => { | ||
this.setState({ | ||
component: this.props.ok, | ||
}); | ||
}) | ||
.catch(() => { | ||
this.setState({ | ||
component: this.props.error, | ||
}); | ||
}); | ||
} | ||
render() { | ||
const C = this.state.component; | ||
return C ? ( | ||
<C {...this.props} /> | ||
) : ( | ||
<div | ||
style={{ | ||
width: '100%', | ||
height: '100%', | ||
margin: 'auto', | ||
paddingTop: 50, | ||
textAlign: 'center', | ||
}} | ||
> | ||
<Spin size="large" /> | ||
</div> | ||
); | ||
} | ||
} |
Oops, something went wrong.