fuxingfan 4 anni fa
parent
commit
64188f5ddf

BIN
estateagents-admin-manager/src/assets/head2.png Vedi File


+ 32
- 0
estateagents-admin-manager/src/components/AuthButton/index.jsx Vedi File

@@ -0,0 +1,32 @@
1
+import React from 'react';
2
+
3
+let allBtns = [];
4
+let current = [];
5
+
6
+const AuthButton = ({ children, name, noRight }) => {
7
+  const btn = allBtns.filter(x => x.code === name)[0]
8
+
9
+  // 没维护的按钮, 或者不需要权限的按钮直接通过
10
+  // if (!btn || !btn.roles || !btn.roles.length) {
11
+  //   return <>{children}</>
12
+  // }
13
+
14
+  // const hasRight = btn.roles.some(x => current.some(y => x === y))
15
+
16
+  if (!btn) {
17
+    return <>{children}</>
18
+  }
19
+
20
+  const hasRight = current.filter(x => x.code === name)[0]
21
+  
22
+  return hasRight ? <>{children}</> : <>{noRight}</>
23
+}
24
+
25
+const setAllBtnAuth = x => allBtns = x;
26
+const setUserBtnAuth = x => current = x;
27
+
28
+export default AuthButton;
29
+export {
30
+  setAllBtnAuth,
31
+  setUserBtnAuth,
32
+};

+ 10
- 0
estateagents-admin-manager/src/components/Authorized/Authorized.jsx Vedi File

@@ -0,0 +1,10 @@
1
+import React from 'react';
2
+import check from './CheckPermissions';
3
+
4
+const Authorized = ({ children, authority, noMatch = null }) => {
5
+  const childrenRender = typeof children === 'undefined' ? null : children;
6
+  const dom = check(authority, childrenRender, noMatch);
7
+  return <>{dom}</>;
8
+};
9
+
10
+export default Authorized;

+ 25
- 0
estateagents-admin-manager/src/components/Authorized/AuthorizedRoute.jsx Vedi File

@@ -0,0 +1,25 @@
1
+import { Redirect, Route } from 'umi';
2
+import React from 'react';
3
+import Authorized from './Authorized';
4
+
5
+const AuthorizedRoute = ({ component: Component, render, authority, redirectPath, ...rest }) => (
6
+  <Authorized
7
+    authority={authority}
8
+    noMatch={
9
+      <Route
10
+        {...rest}
11
+        render={() => (
12
+          <Redirect
13
+            to={{
14
+              pathname: redirectPath,
15
+            }}
16
+          />
17
+        )}
18
+      />
19
+    }
20
+  >
21
+    <Route {...rest} render={props => (Component ? <Component {...props} /> : render(props))} />
22
+  </Authorized>
23
+);
24
+
25
+export default AuthorizedRoute;

+ 76
- 0
estateagents-admin-manager/src/components/Authorized/CheckPermissions.jsx Vedi File

@@ -0,0 +1,76 @@
1
+import React from 'react';
2
+import { CURRENT } from './renderAuthorize'; // eslint-disable-next-line import/no-cycle
3
+
4
+import PromiseRender from './PromiseRender';
5
+
6
+/**
7
+ * 通用权限检查方法
8
+ * Common check permissions method
9
+ * @param { 权限判定 | Permission judgment } authority
10
+ * @param { 你的权限 | Your permission description } currentAuthority
11
+ * @param { 通过的组件 | Passing components } target
12
+ * @param { 未通过的组件 | no pass components } Exception
13
+ */
14
+const checkPermissions = (authority, currentAuthority, target, Exception) => {
15
+  // 没有判定权限.默认查看所有
16
+  // Retirement authority, return target;
17
+  if (!authority) {
18
+    return target;
19
+  } // 数组处理
20
+
21
+  if (Array.isArray(authority)) {
22
+    if (Array.isArray(currentAuthority)) {
23
+      if (currentAuthority.some(item => authority.includes(item))) {
24
+        return target;
25
+      }
26
+    } else if (authority.includes(currentAuthority)) {
27
+      return target;
28
+    }
29
+
30
+    return Exception;
31
+  } // string 处理
32
+
33
+  if (typeof authority === 'string') {
34
+    if (Array.isArray(currentAuthority)) {
35
+      if (currentAuthority.some(item => authority === item)) {
36
+        return target;
37
+      }
38
+    } else if (authority === currentAuthority) {
39
+      return target;
40
+    }
41
+
42
+    return Exception;
43
+  } // Promise 处理
44
+
45
+  if (authority instanceof Promise) {
46
+    return <PromiseRender ok={target} error={Exception} promise={authority} />;
47
+  } // Function 处理
48
+
49
+  if (typeof authority === 'function') {
50
+    try {
51
+      const bool = authority(currentAuthority); // 函数执行后返回值是 Promise
52
+
53
+      if (bool instanceof Promise) {
54
+        return <PromiseRender ok={target} error={Exception} promise={bool} />;
55
+      }
56
+
57
+      if (bool) {
58
+        return target;
59
+      }
60
+
61
+      return Exception;
62
+    } catch (error) {
63
+      throw error;
64
+    }
65
+  }
66
+
67
+  throw new Error('unsupported parameters');
68
+};
69
+
70
+export { checkPermissions };
71
+
72
+function check(authority, target, Exception) {
73
+  return checkPermissions(authority, CURRENT, target, Exception);
74
+}
75
+
76
+export default check;

+ 78
- 0
estateagents-admin-manager/src/components/Authorized/PromiseRender.jsx Vedi File

@@ -0,0 +1,78 @@
1
+import React from 'react';
2
+import { Spin } from 'antd';
3
+import isEqual from 'lodash/isEqual';
4
+import { isComponentClass } from './Secured'; // eslint-disable-next-line import/no-cycle
5
+
6
+export default class PromiseRender extends React.Component {
7
+  state = {
8
+    component: () => null,
9
+  };
10
+
11
+  componentDidMount() {
12
+    this.setRenderComponent(this.props);
13
+  }
14
+
15
+  shouldComponentUpdate = (nextProps, nextState) => {
16
+    const { component } = this.state;
17
+
18
+    if (!isEqual(nextProps, this.props)) {
19
+      this.setRenderComponent(nextProps);
20
+    }
21
+
22
+    if (nextState.component !== component) return true;
23
+    return false;
24
+  }; // set render Component : ok or error
25
+
26
+  setRenderComponent(props) {
27
+    const ok = this.checkIsInstantiation(props.ok);
28
+    const error = this.checkIsInstantiation(props.error);
29
+    props.promise
30
+      .then(() => {
31
+        this.setState({
32
+          component: ok,
33
+        });
34
+        return true;
35
+      })
36
+      .catch(() => {
37
+        this.setState({
38
+          component: error,
39
+        });
40
+      });
41
+  } // Determine whether the incoming component has been instantiated
42
+  // AuthorizedRoute is already instantiated
43
+  // Authorized  render is already instantiated, children is no instantiated
44
+  // Secured is not instantiated
45
+
46
+  checkIsInstantiation = target => {
47
+    if (isComponentClass(target)) {
48
+      const Target = target;
49
+      return props => <Target {...props} />;
50
+    }
51
+
52
+    if (React.isValidElement(target)) {
53
+      return props => React.cloneElement(target, props);
54
+    }
55
+
56
+    return () => target;
57
+  };
58
+
59
+  render() {
60
+    const { component: Component } = this.state;
61
+    const { ok, error, promise, ...rest } = this.props;
62
+    return Component ? (
63
+      <Component {...rest} />
64
+    ) : (
65
+      <div
66
+        style={{
67
+          width: '100%',
68
+          height: '100%',
69
+          margin: 'auto',
70
+          paddingTop: 50,
71
+          textAlign: 'center',
72
+        }}
73
+      >
74
+        <Spin size="large" />
75
+      </div>
76
+    );
77
+  }
78
+}

+ 10
- 0
estateagents-admin-manager/src/components/Authorized/index.jsx Vedi File

@@ -0,0 +1,10 @@
1
+import Authorized from './Authorized';
2
+import AuthorizedRoute from './AuthorizedRoute';
3
+import Secured from './Secured';
4
+import check from './CheckPermissions';
5
+import renderAuthorize from './renderAuthorize';
6
+Authorized.Secured = Secured;
7
+Authorized.AuthorizedRoute = AuthorizedRoute;
8
+Authorized.check = check;
9
+const RenderAuthorize = renderAuthorize(Authorized);
10
+export default RenderAuthorize;

+ 30
- 0
estateagents-admin-manager/src/components/Authorized/renderAuthorize.js Vedi File

@@ -0,0 +1,30 @@
1
+/* eslint-disable eslint-comments/disable-enable-pair */
2
+
3
+/* eslint-disable import/no-mutable-exports */
4
+let CURRENT = 'NULL';
5
+
6
+/**
7
+ * use  authority or getAuthority
8
+ * @param {string|()=>String} currentAuthority
9
+ */
10
+const renderAuthorize = Authorized => currentAuthority => {
11
+  if (currentAuthority) {
12
+    if (typeof currentAuthority === 'function') {
13
+      CURRENT = currentAuthority();
14
+    }
15
+
16
+    if (
17
+      Object.prototype.toString.call(currentAuthority) === '[object String]' ||
18
+      Array.isArray(currentAuthority)
19
+    ) {
20
+      CURRENT = currentAuthority;
21
+    }
22
+  } else {
23
+    CURRENT = 'NULL';
24
+  }
25
+
26
+  return Authorized;
27
+};
28
+
29
+export { CURRENT };
30
+export default Authorized => renderAuthorize(Authorized);